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

[new] Make groups collapsable #56

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 4 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
15 changes: 15 additions & 0 deletions assets/css/main.css
Original file line number Diff line number Diff line change
Expand Up @@ -525,3 +525,18 @@ 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;
}

.wpsf-group-field-header span {
color: #fff;
}

tr.wpsf-group-field-header.wpsf-group-field-header--close td {
border-bottom: 1px solid #F0F0F1;
}
61 changes: 60 additions & 1 deletion assets/js/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -191,16 +191,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 );
}
} );

} );
},

/**
Expand Down Expand Up @@ -229,6 +286,8 @@
$groups.each( function( index, group ) {
wpsf.reindex_group( jQuery( group ) );
} );

wpsf.update_group_header_placeholder();
},

/**
Expand Down
22 changes: 17 additions & 5 deletions settings/example-settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand All @@ -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(
Expand Down
25 changes: 21 additions & 4 deletions wp-settings-framework.php
Original file line number Diff line number Diff line change
Expand Up @@ -476,12 +476,29 @@ 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(
'<tr class="wpsf-group-field-header wpsf-group-field-header--%s" data-collapse="%s" data-title="%s">
<td colspan="3" class="">
<span class="wpsf-group-field-header__icon dashicons dashicons-arrow-down"></span>
<span class="wpsf-group-field-header__title">title</span>
</td>
</tr>',
esc_attr( $collapse_default ),
esc_attr( $collapsable ),
esc_attr( $collapse_heading ),
);
}

$row_template .= sprintf( '<tr class="wpsf-group__row %s">', $row_class );

Expand Down
2 changes: 1 addition & 1 deletion wpsf-test.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public function add_settings_page() {
$this->wpsf->add_settings_page( array(
'parent_slug' => 'woocommerce',
'page_title' => __( 'Page Title', 'text-domain' ),
'menu_title' => __( 'menu Title', 'text-domain' ),
'menu_title' => __( 'Menu Title', 'text-domain' ),
'capability' => 'manage_woocommerce',
) );
}
Expand Down