Skip to content

Commit

Permalink
Merge branch 'DevinWalker-master' into trunk. Props @DevinWalker. Closes
Browse files Browse the repository at this point in the history
  • Loading branch information
jtsternberg committed May 3, 2014
2 parents 69be8c9 + f815f73 commit b642f31
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 13 deletions.
2 changes: 1 addition & 1 deletion helpers/cmb_Meta_Box_field.php
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ public function get_data( $field_id = '', $args = array() ) {
? cmb_Meta_Box::get_option( $id, $field_id )
: get_metadata( $type, $id, $field_id, ( $single || $repeat ) /* If multicheck this can be multiple values */ );

if ( $this->group ) {
if ( $this->group && $data ) {
$data = isset( $data[ $this->group->args( 'count' ) ][ $this->args( '_id' ) ] )
? $data[ $this->group->args( 'count' ) ][ $this->args( '_id' ) ]
: false;
Expand Down
2 changes: 1 addition & 1 deletion init.php
Original file line number Diff line number Diff line change
Expand Up @@ -617,7 +617,7 @@ public static function save_group( $args ) {
}

// Get old value
$old_val = isset( $old[ $field_group->index ][ $sub_id ] )
$old_val = is_array( $old ) && isset( $old[ $field_group->index ][ $sub_id ] )
? $old[ $field_group->index ][ $sub_id ]
: false;

Expand Down
44 changes: 34 additions & 10 deletions js/cmb.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,10 @@ window.CMB = (function(window, document, $, undefined){

// CMB functionality object
var cmb = {
formfield : '',
idNumber : false,
file_frames: {},
formfield : '',
idNumber : false,
file_frames : {},
repeatEls : 'input:not([type="button"]),select,textarea,.cmb_media_status'
};

cmb.metabox = function() {
Expand Down Expand Up @@ -401,6 +402,31 @@ window.CMB = (function(window, document, $, undefined){
cmb.initPickers( $row.find('input:text.cmb_timepicker'), $row.find('input:text.cmb_datepicker'), $row.find('input:text.cmb_colorpicker') );
};

cmb.updateNameAttr = function () {

var $this = $(this);
var name = $this.attr( 'name' ); // get current name

// No name? bail
if ( typeof name === 'undefined' ) {
return false;
}

var prevNum = parseInt( $this.parents( '.repeatable-grouping' ).data( 'iterator' ) );
var newNum = prevNum - 1; // Subtract 1 to get new iterator number

// Update field name attributes so data is not orphaned when a row is removed and post is saved
var $newName = name.replace( '[' + prevNum + ']', '[' + newNum + ']' );

// New name with replaced iterator
$this.attr( 'name', $newName );

};

cmb.emptyValue = function( event, row ) {
$('input:not([type="button"]), textarea', row).val('');
};

cmb.addGroupRow = function( event ) {

event.preventDefault();
Expand Down Expand Up @@ -430,10 +456,6 @@ window.CMB = (function(window, document, $, undefined){
$table.trigger( 'cmb_add_row', $newRow );
};

cmb.emptyValue = function( event, row ) {
$('input:not([type="button"]), textarea', row).val('');
};

cmb.addAjaxRow = function( event ) {

event.preventDefault();
Expand Down Expand Up @@ -462,6 +484,9 @@ window.CMB = (function(window, document, $, undefined){
var $parent = $self.parents('.repeatable-grouping');
var noRows = $table.find('.repeatable-grouping').length;

// when a group is removed loop through all next groups and update fields names
$parent.nextAll( '.repeatable-grouping' ).find( cmb.repeatEls ).each( cmb.updateNameAttr );

if ( noRows > 1 ) {
$parent.remove();
if ( noRows < 3 ) {
Expand Down Expand Up @@ -496,7 +521,6 @@ window.CMB = (function(window, document, $, undefined){

var $self = $(this);
var $parent = $self.parents( '.repeatable-grouping' );
var toReplace = 'input:not([type="button"]),select,textarea,.cmb_media_status';
var $goto = $self.hasClass( 'move-up' ) ? $parent.prev( '.repeatable-grouping' ) : $parent.next( '.repeatable-grouping' );

if ( ! $goto.length ) {
Expand All @@ -505,7 +529,7 @@ window.CMB = (function(window, document, $, undefined){

var inputVals = [];
// Loop this items fields
$parent.find( toReplace ).each( function() {
$parent.find( cmb.repeatEls ).each( function() {
var $element = $(this);
var val;
if ( $element.hasClass('cmb_media_status') ) {
Expand All @@ -524,7 +548,7 @@ window.CMB = (function(window, document, $, undefined){
inputVals.push( { val: val, $: $element } );
});
// And swap them all
$goto.find( toReplace ).each( function( index ) {
$goto.find( cmb.repeatEls ).each( function( index ) {
var $element = $(this);
var val;

Expand Down
2 changes: 1 addition & 1 deletion js/cmb.min.js

Large diffs are not rendered by default.

0 comments on commit b642f31

Please sign in to comment.