From 2fa20c9864d5c998e07e27e934fa94a2c29d2e87 Mon Sep 17 00:00:00 2001 From: Mike Gerwitz Date: Wed, 30 Mar 2016 13:57:18 -0400 Subject: [PATCH] Group support for showing/hiding sub-fields * src/ui/GroupUi.js (getFieldElements): New method (showField, hideField): Use GroupUi#getFieldElements * src/ui/group/TabbedGroupUi (showField, hideField): Use GroupUi#getFieldElements --- src/ui/group/GroupUi.js | 40 +++++++++++++++++++++++------------ src/ui/group/TabbedGroupUi.js | 13 +++--------- 2 files changed, 30 insertions(+), 23 deletions(-) diff --git a/src/ui/group/GroupUi.js b/src/ui/group/GroupUi.js index 7ccadbd..986a860 100644 --- a/src/ui/group/GroupUi.js +++ b/src/ui/group/GroupUi.js @@ -747,6 +747,29 @@ module.exports = Class( 'GroupUi' ) }, + /** + * Retrieve field elements for show/hide operations + * + * If the field is a child of another field, then only the element + * associated with it will be selected; otherwise, the parent container + * of the field (which may be multiple elements) will be returned. + * + * @param {string} field field name + * @param {number} index field index + * + * @return {jQuery} field elements + */ + 'virtual protected getFieldElements': function( field, index ) + { + var $element = this.getElementByName( field, index ), + is_sub = $element.parent().hasClass( 'widget' ); + + return ( !is_sub && $element.parents( 'dd' ).length ) + ? $element.parents( 'dd' ).prev( 'dt' ).andSelf() + : $element; + }, + + 'public hideField': function( field, index ) { if ( this.isFieldVisible( field, index ) === false ) @@ -764,17 +787,13 @@ module.exports = Class( 'GroupUi' ) 'virtual protected doHideField': function( field, index ) { - var $element = this.getElementByName( field, index ); + var $elements = this.getFieldElements( field, index ); - // fall back to self if we cannot match on $p - var $p = $element.parents( 'dd' ).prev( 'dt' ).andSelf(), - $e = ( $p.length ) ? $p : $element; - - $e.stop( true, true ).slideUp( 500, function() + $elements.stop( true, true ).slideUp( 500, function() { // be sure to remove the display:none added by jQuery so that we can // perform our own handling of what it means to be "hidden" - $e.addClass( 'hidden' ).attr( 'style', '' ); + $elements.addClass( 'hidden' ).attr( 'style', '' ); } ); }, @@ -796,12 +815,7 @@ module.exports = Class( 'GroupUi' ) 'virtual protected doShowField': function( field, index ) { - var $element = this.getElementByName( field, index ); - - // static element or a question - var $elements = ( $element.is( 'dt.static' ) ) - ? $element - : $element.parents( 'dd' ).prev( 'dt' ).andSelf(); + var $elements = this.getFieldElements( field, index ); $elements.find( '.hidden' ).andSelf() .stop( true, true ) diff --git a/src/ui/group/TabbedGroupUi.js b/src/ui/group/TabbedGroupUi.js index 45bc61f..4e80a96 100644 --- a/src/ui/group/TabbedGroupUi.js +++ b/src/ui/group/TabbedGroupUi.js @@ -375,13 +375,10 @@ module.exports = Class( 'TabbedGroupUi' ) }, 25 ); } - var $element = this.getElementByName( field, index ); - - var $elements = ( $element.parents( 'dd' ).length ) - ? $element.parents( 'dd' ).prev( 'dt' ).andSelf() - : $element; + var $elements = this.getFieldElements( field, index ); $elements.stop( true, true ); + if ( this.isOnVisibleTab( field, index ) ) { $elements.slideUp( 500, function() @@ -410,11 +407,7 @@ module.exports = Class( 'TabbedGroupUi' ) }, 25 ); } - var $element = this.getElementByName( field, index ); - - var $elements = ( $element.parents( 'dd' ).length ) - ? $element.parents( 'dd' ).prev( 'dt' ).andSelf() - : $element; + var $elements = this.getFieldElements( field, index ); // it's important to stop animations *before* removing the hidden class, // since forcing its completion may add it