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 confirm dialog to group Remove button #1208

Merged
merged 10 commits into from
Dec 31, 2018
5 changes: 3 additions & 2 deletions includes/CMB2.php
Original file line number Diff line number Diff line change
Expand Up @@ -594,7 +594,7 @@ public function render_group_row( $field_group ) {
<div class="postbox cmb-row cmb-repeatable-grouping', $closed_class, '" data-iterator="', $field_group->index, '">';

if ( $field_group->args( 'repeatable' ) ) {
echo '<button type="button" data-selector="', $field_group->id(), '_repeat" class="dashicons-before dashicons-no-alt cmb-remove-group-row" title="', esc_attr( $field_group->options( 'remove_button' ) ), '"></button>';
echo '<button type="button" data-selector="', $field_group->id(), '_repeat" data-confirm="', (bool) $field_group->options( 'remove_confirm' ) ? 'yes' : 'no', '" class="dashicons-before dashicons-no-alt cmb-remove-group-row" title="', esc_attr( $field_group->options( 'remove_button' ) ), '"></button>';
}

echo '
Expand All @@ -617,11 +617,12 @@ public function render_group_row( $field_group ) {
$this->get_field( $field_args, $field_group )->render_field();
}
}

if ( $field_group->args( 'repeatable' ) ) {
echo '
<div class="cmb-row cmb-remove-field-row">
<div class="cmb-remove-row">
<button type="button" data-selector="', $field_group->id(), '_repeat" class="cmb-remove-group-row cmb-remove-group-row-button alignright button-secondary">', $field_group->options( 'remove_button' ), '</button>
<button type="button" data-selector="', $field_group->id(), '_repeat" data-confirm="', (bool) $field_group->options( 'remove_confirm' ) ? 'yes' : 'no', '" class="cmb-remove-group-row cmb-remove-group-row-button alignright button-secondary">', $field_group->options( 'remove_button' ), '</button>
</div>
</div>
';
Expand Down
5 changes: 3 additions & 2 deletions includes/CMB2_Field.php
Original file line number Diff line number Diff line change
Expand Up @@ -1345,8 +1345,9 @@ public function _set_field_defaults( $args ) {
*/
protected function set_field_defaults_group( $args ) {
$args['options'] = wp_parse_args( $args['options'], array(
'add_button' => esc_html__( 'Add Group', 'cmb2' ),
'remove_button' => esc_html__( 'Remove Group', 'cmb2' ),
'add_button' => esc_html__( 'Add Group', 'cmb2' ),
'remove_button' => esc_html__( 'Remove Group', 'cmb2' ),
'remove_confirm' => false
) );

return $args;
Expand Down
15 changes: 8 additions & 7 deletions includes/CMB2_JS.php
Original file line number Diff line number Diff line change
Expand Up @@ -223,13 +223,14 @@ protected static function localize( $debug ) {
),
),
'strings' => array(
'upload_file' => esc_html__( 'Use this file', 'cmb2' ),
'upload_files' => esc_html__( 'Use these files', 'cmb2' ),
'remove_image' => esc_html__( 'Remove Image', 'cmb2' ),
'remove_file' => esc_html__( 'Remove', 'cmb2' ),
'file' => esc_html__( 'File:', 'cmb2' ),
'download' => esc_html__( 'Download', 'cmb2' ),
'check_toggle' => esc_html__( 'Select / Deselect All', 'cmb2' ),
'upload_file' => esc_html__( 'Use this file', 'cmb2' ),
'upload_files' => esc_html__( 'Use these files', 'cmb2' ),
'remove_image' => esc_html__( 'Remove Image', 'cmb2' ),
'remove_file' => esc_html__( 'Remove', 'cmb2' ),
'file' => esc_html__( 'File:', 'cmb2' ),
'download' => esc_html__( 'Download', 'cmb2' ),
'check_toggle' => esc_html__( 'Select / Deselect All', 'cmb2' ),
'group_remove_confirm' => esc_html__( 'Are you sure you want to remove it?', 'cmb2' ),
),
);

Expand Down
18 changes: 12 additions & 6 deletions js/cmb2.js
Original file line number Diff line number Diff line change
Expand Up @@ -695,24 +695,30 @@ window.CMB2 = window.CMB2 || {};
cmb.removeGroupRow = function( evt ) {
evt.preventDefault();

var $this = $( this );
var $table = $id( $this.data('selector') );
var $parent = $this.parents('.cmb-repeatable-grouping');
var number = $table.find('.cmb-repeatable-grouping').length;
var $this = $( this ),
isConfirmable = $this.data('confirm') === 'yes';
slaFFik marked this conversation as resolved.
Show resolved Hide resolved

// Process further only if deletion confirmation enabled and user agreed.
if ( isConfirmable && ! window.confirm( l10n.strings.group_remove_confirm ) ) {
return;
}

var $table = $id( $this.data('selector') ),
$parent = $this.parents('.cmb-repeatable-grouping'),
number = $table.find('.cmb-repeatable-grouping').length;

if ( number < 2 ) {
return cmb.resetRow( $parent.parents('.cmb-repeatable-group').find( '.cmb-add-group-row' ), $this );
}

cmb.triggerElement( $table, 'cmb2_remove_group_row_start', $this );

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

$parent.remove();

cmb.triggerElement( $table, { type: 'cmb2_remove_row', group: true } );

};

cmb.removeAjaxRow = function( evt ) {
Expand Down
2 changes: 1 addition & 1 deletion js/cmb2.min.js

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions tests/test-cmb-core.php
Original file line number Diff line number Diff line change
Expand Up @@ -506,7 +506,7 @@ public function test_group_field_param_callbacks() {
</div>
before_group_row output
<div class="postbox cmb-row cmb-repeatable-grouping closed" data-iterator="0">
<button type="button" data-selector="group_field_repeat" class="dashicons-before dashicons-no-alt cmb-remove-group-row" title="Remove Group"></button>
<button type="button" data-selector="group_field_repeat" data-confirm="no" class="dashicons-before dashicons-no-alt cmb-remove-group-row" title="Remove Group"></button>
<div class="cmbhandle" title="Click to toggle"><br></div>
<h3 class="cmb-group-title cmbhandle-title"><span></span></h3>
<div class="inside cmb-td cmb-nested cmb-field-list">
Expand Down Expand Up @@ -538,7 +538,7 @@ public function test_group_field_param_callbacks() {
</div>
<div class="cmb-row cmb-remove-field-row">
<div class="cmb-remove-row">
<button type="button" data-selector="group_field_repeat" class="cmb-remove-group-row cmb-remove-group-row-button alignright button-secondary">Remove Group</button>
<button type="button" data-selector="group_field_repeat" data-confirm="no" class="cmb-remove-group-row cmb-remove-group-row-button alignright button-secondary">Remove Group</button>
</div>
</div>
</div>
Expand Down