Skip to content

Commit

Permalink
Remove jQuery functions and add them to the cmb JS object instead.
Browse files Browse the repository at this point in the history
  • Loading branch information
jtsternberg committed Mar 16, 2015
1 parent 833e2f2 commit b85edba
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 15 deletions.
30 changes: 16 additions & 14 deletions js/cmb2.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ window.CMB2 = (function(window, document, $, undefined){
repeatEls : 'input:not([type="button"]),select,textarea,.cmb2-media-status',
styleBreakPoint : 450,
mediaHandlers : {},
neweditor_id : [],
defaults : {
time_picker : l10n.defaults.time_picker,
date_picker : l10n.defaults.date_picker,
Expand Down Expand Up @@ -306,21 +307,21 @@ window.CMB2 = (function(window, document, $, undefined){
return false;
};

$.fn.cleanRow = function( prevNum, group ) {
var $self = $(this);
var $inputs = $self.find( 'input:not([type="button"]), select, textarea, label' );
var $other = $self.find('[id]').not( 'input:not([type="button"]), select, textarea, label' );
cmb.cleanRow = function( $row, prevNum, group ) {

var $inputs = $row.find( 'input:not([type="button"]), select, textarea, label' );
var $other = $row.find('[id]').not( 'input:not([type="button"]), select, textarea, label' );
if ( group ) {
// Remove extra ajaxed rows
$self.find('.cmb-repeat-table .cmb-repeat-row:not(:first-child)').remove();
$row.find('.cmb-repeat-table .cmb-repeat-row:not(:first-child)').remove();

// Update all elements w/ an ID
if ( $other.length ) {
$other.each( function() {
var $_this = $( this );
var oldID = $_this.attr( 'id' );
var newID = oldID.replace( '_'+ prevNum, '_'+ cmb.idNumber );
var $buttons = $self.find('[data-selector="'+ oldID +'"]');
var $buttons = $row.find('[data-selector="'+ oldID +'"]');
$_this.attr( 'id', newID );

// Replace data-selector vars
Expand All @@ -335,8 +336,8 @@ window.CMB2 = (function(window, document, $, undefined){
$inputs.filter(':checked').prop( 'checked', false );
$inputs.filter(':selected').prop( 'selected', false );

if ( $self.find('h3.cmb-group-title').length ) {
$self.find( 'h3.cmb-group-title' ).text( $self.data( 'title' ).replace( '{#}', ( cmb.idNumber + 1 ) ) );
if ( $row.find('h3.cmb-group-title').length ) {
$row.find( 'h3.cmb-group-title' ).text( $row.data( 'title' ).replace( '{#}', ( cmb.idNumber + 1 ) ) );
}

$inputs.each( function(){
Expand Down Expand Up @@ -385,11 +386,11 @@ window.CMB2 = (function(window, document, $, undefined){
}
});

return this;
return cmb;
};

$.fn.newRowHousekeeping = function() {
var $row = $(this);
cmb.newRowHousekeeping = function( $row ) {

var $colorPicker = $row.find( '.wp-picker-container' );
var $list = $row.find( '.cmb2-media-status' );

Expand All @@ -406,7 +407,7 @@ window.CMB2 = (function(window, document, $, undefined){
$list.empty();
}

return this;
return cmb;
};

cmb.afterRowInsert = function( $row ) {
Expand Down Expand Up @@ -488,7 +489,8 @@ window.CMB2 = (function(window, document, $, undefined){
cmb.idNumber = prevNum + 1;
var $row = $oldRow.clone();

$row.data( 'title', $self.data( 'grouptitle' ) ).newRowHousekeeping().cleanRow( prevNum, true ).find( '.cmb-add-row-button' ).prop( 'disabled', false );
cmb.newRowHousekeeping( $row.data( 'title', $self.data( 'grouptitle' ) ) ).cleanRow( $row, prevNum, true );
$row.find( '.cmb-add-row-button' ).prop( 'disabled', false );

var $newRow = $( '<div class="postbox cmb-row cmb-repeatable-grouping" data-iterator="'+ cmb.idNumber +'">'+ $row.html() +'</div>' );
$oldRow.after( $newRow );
Expand All @@ -514,7 +516,7 @@ window.CMB2 = (function(window, document, $, undefined){
cmb.idNumber = prevNum + 1;
var $row = $emptyrow.clone();

$row.newRowHousekeeping().cleanRow( prevNum );
cmb.newRowHousekeeping( $row ).cleanRow( $row, prevNum );

$emptyrow.removeClass('empty-row hidden').addClass('cmb-repeat-row');
$emptyrow.after( $row );
Expand Down
Loading

0 comments on commit b85edba

Please sign in to comment.