Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add All and Remove All buttons for the select_advanced dropdowns #1331

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
.svn
img/drag_icon.png
10 changes: 10 additions & 0 deletions css/select-advanced.css
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,14 @@
}
body > .select2-container {
z-index: 999999;
}
a.rwmb-button.button-primary.add-clone, a.rwmb-button.button-primary.add-all, a.rwmb-button.button-primary.remove-all {
float: left;
}
a.rwmb-button.button-primary.add-all, a.rwmb-button.button-primary.remove-all {
margin-top: 10px;
}
a.rwmb-button.button-primary.add-all {
clear: left;
margin-right: 10px;
}
Binary file modified img/jqueryui/ui-bg_flat_0_aaaaaa_40x100.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified img/jqueryui/ui-bg_flat_75_ffffff_40x100.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified img/jqueryui/ui-bg_glass_65_ffffff_1x400.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified img/jqueryui/ui-bg_highlight-soft_75_cccccc_1x100.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified img/jqueryui/ui-icons_222222_256x240.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified img/jqueryui/ui-icons_2e83ff_256x240.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified img/jqueryui/ui-icons_454545_256x240.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified img/jqueryui/ui-icons_888888_256x240.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified img/jqueryui/ui-icons_cd0a0a_256x240.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
31 changes: 31 additions & 0 deletions inc/clone.php
Original file line number Diff line number Diff line change
Expand Up @@ -127,4 +127,35 @@ public static function remove_clone_button( $field ) {
$text = RWMB_Field::filter( 'remove_clone_button_text', '<span class="dashicons dashicons-dismiss"></span>', $field );
return '<a href="#" class="rwmb-button remove-clone">' . $text . '</a>';
}

/**
* Add All button.
*
* @param array $field Field parameters.
* @return string $html
*/
public static function add_all_button( $field ) {
// Only add this button if bulk cloning is allowed and it is a select advanced field.
if ( ! $field['clone'] || ! $field['bulk_clone'] || ( 'select_advanced' !== $field['field_type'] ) ) {
return '';
}
$text = RWMB_Field::filter( 'add_all_button_text', $field['add_all_button'], $field );
return '<a href="#" class="rwmb-button button-primary add-all">' . esc_html( $text ) . '</a>';
}

/**
* Remove All button.
*
* @param array $field Field parameters.
* @return string $html
*/
public static function remove_all_button( $field ) {
// Only add this button if bulk cloning is allowed and it is a select advanced field.
if ( ! $field['clone'] || ! $field['bulk_clone'] || ( 'select_advanced' !== $field['field_type'] ) ) {
return '';
}
$text = RWMB_Field::filter( 'remove_all_button_text', $field['remove_all_button'], $field );
return '<a href="#" class="rwmb-button button-primary remove-all">' . esc_html( $text ) . '</a>';
}

}
9 changes: 8 additions & 1 deletion inc/field.php
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,11 @@ public static function begin_html( $meta, $field ) {
* @return string
*/
public static function end_html( $meta, $field ) {
return RWMB_Clone::add_clone_button( $field ) . self::call( 'input_description', $field ) . '</div>';
return RWMB_Clone::add_clone_button( $field ) .
RWMB_Clone::add_all_button( $field ) .
RWMB_Clone::remove_all_button( $field ) .
self::call( 'input_description', $field ) .
'</div>';
}

/**
Expand Down Expand Up @@ -354,6 +358,9 @@ public static function normalize( $field ) {
'add_button' => __( '+ Add more', 'meta-box' ),
'clone_default' => false,
'clone_as_multiple' => false,
'bulk_clone' => false,
'add_all_button' => __( '+ Add All', 'meta-box' ),
'remove_all_button' => __( '- Remove All', 'meta-box' ),

'class' => '',
'disabled' => false,
Expand Down
63 changes: 61 additions & 2 deletions js/clone.js
Original file line number Diff line number Diff line change
Expand Up @@ -192,8 +192,41 @@
function addClone( e ) {
e.preventDefault();

var $container = $( this ).closest( '.rwmb-input' );
var $this = $( this ),
$container = $this.closest( '.rwmb-input' );

clone( $container );
$container.children( '.rwmb-clone' ).last().val('8349').change();
toggleRemoveButtons( $container );
toggleAddButton( $container );
sortClones.apply( $container[0] );
}

function addAll( e ) {
e.preventDefault();

var $this = $( this ),
$container = $this.closest( '.rwmb-input' ),
$clones = $container.children( '.rwmb-clone' ),
options = [],
i = 0;

$clones.find('select[class*="rwmb"]').first().find('option').each( function() {
options.push( $(this).val() );
});


$clones.find('select[class*="rwmb"]').each( function () {
var index = options.indexOf($( this ).val());
if (index > -1) {
options.splice(index, 1);
}
} );

while( (i = options.shift()) !== undefined ) {
if ( '' !== $container.children( '.rwmb-clone' ).last().find('select[class*="rwmb"]').last().val() ) clone( $container );
$container.children( '.rwmb-clone' ).last().find('select[class*="rwmb"]').last().val(i).change()
}

toggleRemoveButtons( $container );
toggleAddButton( $container );
Expand All @@ -219,6 +252,30 @@
$container.find( rwmb.inputSelectors ).first().trigger( 'mb_change' );
}

function removeAll( e ) {
e.preventDefault();

var $this = $( this ),
$container = $this.closest( '.rwmb-input' ),
$clones = $container.children( '.rwmb-clone' ),
$firstClone = $clones.slice(0);

// Iterate all and remove. Skip the first one so we can always clone.
$.each( $clones.slice(1), function () {
$( this ).trigger( 'remove' ).remove();
} );


// Trigger the clearing of the first value too, but leave the field there for future cloning.
$firstClone.find(".select2-selection__clear").first().mousedown();

// Close the dropdown that openned upon the clear
$firstClone.mousedown()

toggleRemoveButtons( $container );
toggleAddButton( $container );
}

/**
* Sort clones.
* Expect this = .rwmb-input element.
Expand Down Expand Up @@ -273,5 +330,7 @@
rwmb.$document
.on( 'mb_ready', init )
.on( 'click', '.add-clone', addClone )
.on( 'click', '.remove-clone', removeClone );
.on( 'click', '.remove-clone', removeClone )
.on( 'click', '.add-all', addAll )
.on( 'click', '.remove-all', removeAll );
} )( jQuery, rwmb );