diff --git a/assets/css/main.css b/assets/css/main.css index c7ee97d..23ffcfd 100644 --- a/assets/css/main.css +++ b/assets/css/main.css @@ -580,3 +580,37 @@ p.submit { .wpsf-list li:last-child { margin-bottom: 0; } + +/* Styling for group collapse */ +tr.wpsf-group-field-header { + cursor: pointer; + background: #000; + user-select: none; + position: relative; +} + +.wpsf-group-field-header span { + color: #fff; +} + +tr.wpsf-group-field-header.wpsf-group-field-header--close td { + border-bottom: 1px solid #F0F0F1; +} + +.wpsf-group-field-header__add span { + color: #2271b1; +} + +a.wpsf-group-field-header__add { + position: absolute; + bottom: -10px; + background: #fff; + z-index: 99; + border-radius: 100%; + right: 13px; + display: none; +} + +tr.wpsf-group-field-header.wpsf-group-field-header--close .wpsf-group-field-header__add { + display: block; +} \ No newline at end of file diff --git a/assets/js/main.js b/assets/js/main.js index 9debb7b..cffbe99 100644 --- a/assets/js/main.js +++ b/assets/js/main.js @@ -167,13 +167,17 @@ // add row - $( document ).on( 'click', '.wpsf-group__row-add', function() { + $( document ).on( 'click', '.wpsf-group__row-add, .wpsf-group-field-header__add', function() { var $group = $( this ).closest( '.wpsf-group' ), $row = $( this ).closest( '.wpsf-group__row' ), template_name = $( this ).data( 'template' ), $template = $( $( '#' + template_name ).html() ); + if ( !$row.length && $( this ).parent().parent().next().is( '.wpsf-group__row' ) ) { + $row = $( this ).parent().parent().next(); + } + $template.find( '.wpsf-group__row-id' ).val( wpsf.generate_random_id() ); $row.after( $template ); @@ -182,6 +186,11 @@ wpsf.trigger_dynamic_fields(); + // hide .wpsf-group__row if header is closed. + if ( $template.hasClass( 'wpsf-group-field-header--close' ) ) { + $template.next( '.wpsf-group__row' ).hide(); + } + return false; } ); @@ -191,16 +200,73 @@ $( document ).on( 'click', '.wpsf-group__row-remove', function() { var $group = jQuery( this ).closest( '.wpsf-group' ), - $row = jQuery( this ).closest( '.wpsf-group__row' ); + $row = jQuery( this ).closest( '.wpsf-group__row' ), + $header = $row.prev(); $row.remove(); + if ( $header.is( '.wpsf-group-field-header' ) ) { + $header.remove(); + } + wpsf.reindex_group( $group ); return false; } ); + + // Handle collapse. + $( document ).on( 'click', '.wpsf-group-field-header', function() { + if ( $(this).hasClass( 'wpsf-group-field-header--close' ) ) { + $(this).removeClass( 'wpsf-group-field-header--close' ); + $(this).next().show(); + $(this).find( '.wpsf-group-field-header__icon' ).removeClass( 'dashicons-arrow-right' ).addClass( 'dashicons-arrow-down' ); + } else { + $(this).addClass( 'wpsf-group-field-header--close' ); + $(this).next().hide(); + $(this).find( '.wpsf-group-field-header__icon' ).removeClass( 'dashicons-arrow-down' ).addClass( 'dashicons-arrow-right' ); + } + } ); + + $( '.wpsf-group' ).on( 'change', 'input, select, textarea', wpsf.update_group_header_placeholder ); + + // Collapse on page load. + $( '.wpsf-group-field-header--close' ).each( function () { + $( this ).find( '.wpsf-group-field-header__icon' ).removeClass( 'dashicons-arrow-down' ).addClass( 'dashicons-arrow-right' ); + $( this ).next().hide(); + } ) + + wpsf.update_group_header_placeholder(); + }, + + /** + * Update placeholder value for the group header title. + * + * Ex: replace [fname] with the actual value of fname subfield. + */ + update_group_header_placeholder: function () { + $( '.wpsf-group__row' ).each( function () { + if ( ! $( this ).prev().is( '.wpsf-group-field-header' ) ) { + return; + } + + var $row = $( this ), + $header = $( this ).prev(), + $header_span = $header.find( '.wpsf-group-field-header__title' ), + header_text = $header.data( 'title' ); + + $row.find( 'input, select, textarea' ).each( function () { + var name = $( this ).attr( 'name' ); + // Example: Use regex to retrieve "sub-text" from "my_example_settings_settings[general_group][0][sub-text]". + var matches = name.match( /.*\[(.*)\]/ ); + if ( matches[ 1 ] ) { + header_text = header_text.replace( '[' + matches[ 1 ] + ']', $( this ).val() ); + $header_span.text( header_text ); + } + } ); + + } ); }, /** @@ -229,6 +295,8 @@ $groups.each( function( index, group ) { wpsf.reindex_group( jQuery( group ) ); } ); + + wpsf.update_group_header_placeholder(); }, /** diff --git a/settings/example-settings.php b/settings/example-settings.php index f005be9..69c2b6f 100644 --- a/settings/example-settings.php +++ b/settings/example-settings.php @@ -63,12 +63,16 @@ function wpsf_tabless_settings( $wpsf_settings ) { 'datepicker' => array(), // Array of datepicker options (http://api.jqueryui.com/datepicker/) ), array( - 'id' => 'group', - 'title' => 'Group', - 'desc' => 'This is a description.', - 'type' => 'group', + 'id' => 'group', + 'title' => 'Group', + 'desc' => 'This is a description.', + 'type' => 'group', + 'collapse' => array( // Default value: false. + 'default' => 'close', // Possible values: 'open', 'close'. + 'heading' => '[sub-text] - [sub-text2]', // Can use id of any subfields in square-brackets. Can add multiple subfields too ex: [sub-field] + ), 'subfields' => array( - // accepts most types of fields + // accepts most types of fields. array( 'id' => 'sub-text', 'title' => 'Sub Text', @@ -77,6 +81,14 @@ function wpsf_tabless_settings( $wpsf_settings ) { 'type' => 'text', 'default' => 'Sub text', ), + array( + 'id' => 'sub-text2', + 'title' => 'Sub Text2', + 'desc' => 'This is a description.', + 'placeholder' => 'This is a placeholder.', + 'type' => 'text', + 'default' => 'Sub text', + ), ) ), array( diff --git a/wp-settings-framework.php b/wp-settings-framework.php index 22eb6ad..c134f28 100644 --- a/wp-settings-framework.php +++ b/wp-settings-framework.php @@ -517,12 +517,31 @@ public function generate_group_field( $args ) { * @return string|bool */ public function generate_group_row_template( $args, $blank = false, $row = 0 ) { - $row_template = false; - $row_id = ! empty( $args['value'][ $row ]['row_id'] ) ? $args['value'][ $row ]['row_id'] : $row; - $row_id_value = $blank ? '' : $row_id; + $row_template = false; + $row_id = ! empty( $args['value'][ $row ]['row_id'] ) ? $args['value'][ $row ]['row_id'] : $row; + $row_id_value = $blank ? '' : $row_id; + $collapsable = ! empty( $args['collapse'] ) ? true : false; + $collapse_default = ! empty( $args['collapse']['default'] ) ? $args['collapse']['default'] : 'open'; + $collapse_heading = ! empty( $args['collapse']['heading'] ) ? $args['collapse']['heading'] : ''; if ( $args['subfields'] ) { - $row_class = $row % 2 == 0 ? 'alternate' : ''; + $row_class = $row % 2 === 0 ? 'alternate' : ''; + + if ( $collapsable ) { + $row_template .= sprintf( + '