Skip to content

Commit

Permalink
Allow removing last repeat item row by resetting it to default. Related
Browse files Browse the repository at this point in the history
  • Loading branch information
jtsternberg committed Aug 7, 2017
1 parent c2b381e commit 1132586
Show file tree
Hide file tree
Showing 6 changed files with 66 additions and 55 deletions.
12 changes: 5 additions & 7 deletions includes/CMB2_Types.php
Expand Up @@ -377,7 +377,7 @@ public function repeatable_rows() {
$count = count( $meta_value );
foreach ( (array) $meta_value as $val ) {
$this->field->escaped_value = $val;
$this->repeat_row( $count < 2 );
$this->repeat_row();
$this->iterator++;
}
} else {
Expand All @@ -386,32 +386,30 @@ public function repeatable_rows() {
$this->field->escaped_value = $this->field->value = null;

// Otherwise add one row
$this->repeat_row( true );
$this->repeat_row();
}

// Then add an empty row
$this->field->escaped_value = '';
$this->iterator = $this->iterator ? $this->iterator : 1;
$this->repeat_row( false, 'empty-row hidden' );
$this->repeat_row( 'empty-row hidden' );
}

/**
* Generates a repeatable row's markup
*
* @since 1.1.0
* @param bool $disable_remover Whether remove button should be disabled
* @param string $class Repeatable table row's class
*/
protected function repeat_row( $disable_remover = false, $class = 'cmb-repeat-row' ) {
$disabled = $disable_remover ? ' button-disabled' : '';
protected function repeat_row( $class = 'cmb-repeat-row' ) {
?>

<div class="cmb-row <?php echo $class; ?>">
<div class="cmb-td">
<?php $this->_render(); ?>
</div>
<div class="cmb-td cmb-remove-row">
<button type="button" class="button-secondary cmb-remove-row-button<?php echo $disabled; ?>" title="<?php echo esc_attr( $this->_text( 'remove_row_button_title', esc_html__( 'Remove Row', 'cmb2' ) ) ); ?>"><?php echo esc_html( $this->_text( 'remove_row_text', esc_html__( 'Remove', 'cmb2' ) ) ); ?></button>
<button type="button" class="button-secondary cmb-remove-row-button" title="<?php echo esc_attr( $this->_text( 'remove_row_button_title', esc_html__( 'Remove Row', 'cmb2' ) ) ); ?>"><?php echo esc_html( $this->_text( 'remove_row_text', esc_html__( 'Remove', 'cmb2' ) ) ); ?></button>
</div>
</div>

Expand Down
37 changes: 18 additions & 19 deletions js/cmb2.js
Expand Up @@ -587,8 +587,6 @@ window.CMB2 = window.CMB2 || {};

cmb.triggerElement( $table, { type: 'cmb2_add_row', group: false }, $row );

$table.find( '.cmb-remove-row-button' ).removeClass( 'button-disabled' );

};

cmb.removeGroupRow = function( evt ) {
Expand All @@ -600,11 +598,7 @@ window.CMB2 = window.CMB2 || {};
var number = $table.find('.cmb-repeatable-grouping').length;

if ( number < 2 ) {
// Needs to always be at least one group, but we will click the "add new" button followed by the "remove this" button
// in order to reset the group to empty values.
$parent.parents('.cmb-repeatable-group').find( '.cmb-add-group-row' ).trigger( 'click' );
$this.trigger( 'click' );
return;
return cmb.resetRow( $parent.parents('.cmb-repeatable-group').find( '.cmb-add-group-row' ), $this );
}

cmb.triggerElement( $table, 'cmb2_remove_group_row_start', $this );
Expand Down Expand Up @@ -632,20 +626,25 @@ window.CMB2 = window.CMB2 || {};
var $table = $this.parents('.cmb-repeat-table');
var number = $table.find('.cmb-row').length;

if ( number > 2 ) {
if ( $parent.hasClass('empty-row') ) {
$parent.prev().addClass( 'empty-row' ).removeClass('cmb-repeat-row');
}
$this.parents('.cmb-repeat-table .cmb-row').remove();
if ( number === 3 ) {
$table.find( '.cmb-remove-row-button' ).addClass( 'button-disabled' );
}

cmb.triggerElement( $table, { type: 'cmb2_remove_row', group: false } );
if ( number <= 2 ) {
return cmb.resetRow( $parent.find( '.cmb-add-row-button' ), $this );
}

} else {
$this.addClass( 'button-disabled' );
if ( $parent.hasClass('empty-row') ) {
$parent.prev().addClass( 'empty-row' ).removeClass('cmb-repeat-row');
}

$this.parents('.cmb-repeat-table .cmb-row').remove();


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

cmb.resetRow = function( $addNewBtn, $removeBtn ) {
// Click the "add new" button followed by the "remove this" button
// in order to reset the repeat row to empty values.
$addNewBtn.trigger( 'click' );
$removeBtn.trigger( 'click' );
};

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

Large diffs are not rendered by default.

66 changes: 40 additions & 26 deletions languages/cmb2.pot
Expand Up @@ -4,11 +4,11 @@ msgid ""
msgstr ""
"Project-Id-Version: CMB2 2.2.4\n"
"Report-Msgid-Bugs-To: http://wordpress.org/support/plugin/cmb2\n"
"POT-Creation-Date: 2017-04-19 03:43:15+00:00\n"
"POT-Creation-Date: 2017-08-07 15:33:29+00:00\n"
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=utf-8\n"
"Content-Transfer-Encoding: 8bit\n"
"PO-Revision-Date: 2017-4-18 3:43+240\n"
"PO-Revision-Date: 2017-8-7 15:33+240\n"
"Last-Translator: CMB2 Team hello@cmb2.io\n"
"Language-Team: CMB2 Team hello@cmb2.io\n"
"X-Generator: grunt-wp-i18n 0.4.9\n"
Expand Down Expand Up @@ -50,7 +50,7 @@ msgstr ""
#: example-functions.php:577 example-functions.php:584
#: example-functions.php:591 example-functions.php:618
#: example-functions.php:626 example-functions.php:633
#: example-functions.php:670 tests/test-cmb-field.php:275
#: example-functions.php:677 tests/test-cmb-field.php:275
msgid "field description (optional)"
msgstr ""

Expand Down Expand Up @@ -306,39 +306,39 @@ msgstr ""
msgid "Arbitrary Term Field"
msgstr ""

#: example-functions.php:654
msgid "Theme Options Metabox"
#: example-functions.php:651
msgid "Theme Options"
msgstr ""

#: example-functions.php:669
#: example-functions.php:676
msgid "Site Background Color"
msgstr ""

#: example-functions.php:705
#: example-functions.php:712
msgid "REST Test Box"
msgstr ""

#: example-functions.php:714
#: example-functions.php:721
msgid "REST Test Text"
msgstr ""

#: example-functions.php:715
#: example-functions.php:722
msgid "Will show in the REST API for this box and for pages."
msgstr ""

#: example-functions.php:721
#: example-functions.php:728
msgid "REST Editable Test Text"
msgstr ""

#: example-functions.php:722
#: example-functions.php:729
msgid "Will show in REST API \"editable\" contexts only (`POST` requests)."
msgstr ""

#: includes/CMB2.php:150
#: includes/CMB2.php:174
msgid "Metabox configuration is required to have an ID parameter."
msgstr ""

#: includes/CMB2.php:509
#: includes/CMB2.php:548
msgid "Click to toggle"
msgstr ""

Expand Down Expand Up @@ -380,32 +380,33 @@ msgid ""
"version %2$s with no alternative available."
msgstr ""

#: includes/CMB2_Base.php:475 includes/types/CMB2_Type_Base.php:149
#: includes/CMB2_Base.php:475 includes/CMB2_Hookup_Base.php:87
#: includes/types/CMB2_Type_Base.php:149
msgid "Invalid %1$s property: %2$s"
msgstr ""

#: includes/CMB2_Base.php:490 includes/types/CMB2_Type_Base.php:133
msgid "Invalid %1$s method: %2$s"
msgstr ""

#: includes/CMB2_Field.php:1162
#: includes/CMB2_Field.php:1174
msgid "Add Group"
msgstr ""

#: includes/CMB2_Field.php:1163
#: includes/CMB2_Field.php:1175
msgid "Remove Group"
msgstr ""

#: includes/CMB2_Field.php:1185 includes/CMB2_Field.php:1189
#: includes/CMB2_Field.php:1197 includes/CMB2_Field.php:1201
#: tests/test-cmb-field.php:237
msgid "None"
msgstr ""

#: includes/CMB2_Field.php:1250
#: includes/CMB2_Field.php:1262
msgid "Sorry, this field does not have a cmb_id specified."
msgstr ""

#: includes/CMB2_Field_Display.php:430 includes/CMB2_JS.php:167
#: includes/CMB2_Field_Display.php:432 includes/CMB2_JS.php:167
#: includes/types/CMB2_Type_File_Base.php:78 tests/test-cmb-types-base.php:149
#: tests/test-cmb-types.php:727
msgid "File:"
Expand Down Expand Up @@ -501,7 +502,7 @@ msgstr ""
msgid "Remove Image"
msgstr ""

#: includes/CMB2_JS.php:166 includes/CMB2_Types.php:409
#: includes/CMB2_JS.php:166 includes/CMB2_Types.php:412
#: includes/types/CMB2_Type_File_Base.php:83 tests/test-cmb-types-base.php:149
#: tests/test-cmb-types.php:47 tests/test-cmb-types.php:55
#: tests/test-cmb-types.php:727
Expand All @@ -517,25 +518,38 @@ msgstr ""
msgid "Select / Deselect All"
msgstr ""

#: includes/CMB2_Types.php:228
#: includes/CMB2_Options_Hookup.php:111
msgid "Settings updated."
msgstr ""

#: includes/CMB2_Options_Hookup.php:113
msgid "Nothing to update."
msgstr ""

#: includes/CMB2_Types.php:231
msgid "Custom CMB2 field type classes must extend CMB2_Type_Base."
msgstr ""

#: includes/CMB2_Types.php:344
#: includes/CMB2_Types.php:349
msgid "Add Row"
msgstr ""

#: includes/CMB2_hookup.php:153
#: includes/CMB2_Types.php:412 tests/test-cmb-types.php:47
#: tests/test-cmb-types.php:55
msgid "Remove Row"
msgstr ""

#: includes/CMB2_hookup.php:163
msgid ""
"Term Metadata is a WordPress 4.4+ feature. Please upgrade your WordPress "
"install."
msgstr ""

#: includes/CMB2_hookup.php:157
#: includes/CMB2_hookup.php:167
msgid "Term metaboxes configuration requires a \"taxonomies\" parameter."
msgstr ""

#: includes/CMB2_hookup.php:404
#: includes/CMB2_hookup.php:437
msgid "Toggle panel: %s"
msgstr ""

Expand Down Expand Up @@ -661,7 +675,7 @@ msgid ""
"response."
msgstr ""

#: includes/types/CMB2_Type_File.php:40 tests/test-cmb-types.php:703
#: includes/types/CMB2_Type_File.php:76 tests/test-cmb-types.php:703
#: tests/test-cmb-types.php:727
msgid "Add or Upload File"
msgstr ""
Expand Down
2 changes: 1 addition & 1 deletion tests/test-cmb-field.php
Expand Up @@ -410,7 +410,7 @@ public function test_string() {
</select>
</div>
<div class="cmb-td cmb-remove-row">
<button type="button" class="button-secondary cmb-remove-row-button button-disabled" title="Remove Row">Remove</button>
<button type="button" class="button-secondary cmb-remove-row-button" title="Remove Row">Remove</button>
</div>
</div>
<div class="cmb-row empty-row hidden">
Expand Down
2 changes: 1 addition & 1 deletion tests/test-cmb-types.php
Expand Up @@ -44,7 +44,7 @@ public function test_repeatable_field() {
<input type="text" class="regular-text" name="field_test_field[0]" id="field_test_field_0" data-iterator="0" value=""/>
</div>
<div class="cmb-td cmb-remove-row">
<button type="button" class="button-secondary cmb-remove-row-button button-disabled" title="' . esc_attr__( 'Remove Row', 'cmb2' ) . '">' . esc_html__( 'Remove', 'cmb2' ) . '</button>
<button type="button" class="button-secondary cmb-remove-row-button" title="' . esc_attr__( 'Remove Row', 'cmb2' ) . '">' . esc_html__( 'Remove', 'cmb2' ) . '</button>
</div>
</div>
<div class="cmb-row empty-row hidden">
Expand Down

0 comments on commit 1132586

Please sign in to comment.