Mike Gerwitz

Activist for User Freedom

aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorGoldsmith, Mark <mark.goldsmith@ryansg.com>2020-02-19 09:33:24 -0500
committerGoldsmith, Mark <mark.goldsmith@ryansg.com>2020-02-19 09:33:24 -0500
commit29fae72aea31733fbd5f4cb7c02b138641b7b441 (patch)
tree5d91854aa95bab66e4dc5c09caee8ae8452f3d8b /src
parent200ae99ce504909e19c1a7173c01cbfe02150742 (diff)
parent1e76291ada7040a0fdd9f96979e26f782b82bf2d (diff)
downloadliza-master.tar.gz
liza-master.tar.bz2
liza-master.zip
[DEV-7060] Remove key sizzle calls from ElementStylerHEADmaster
See merge request floss/liza!90
Diffstat (limited to 'src')
-rw-r--r--src/ui/ElementStyler.js116
1 files changed, 75 insertions, 41 deletions
diff --git a/src/ui/ElementStyler.js b/src/ui/ElementStyler.js
index d4ad572..3101a05 100644
--- a/src/ui/ElementStyler.js
+++ b/src/ui/ElementStyler.js
@@ -20,7 +20,7 @@
*
* @needsLove
* - Everything! This class exists from when the framework was barely
- * more than a few prototypes and has rotted ever since with little else
+ * more than a few prototypes and has rotted ever since with little else
* but workarounds.
* @end needsLove
*/
@@ -82,6 +82,18 @@ module.exports = Class( 'ElementStyler',
*/
'private _$context': null,
+ /**
+ * jQuery Object
+ * @type {jQuery}
+ */
+ 'private _jquery': null,
+
+ /**
+ * HTML Document
+ * @type {HTMLElement}
+ */
+ 'private _document': null,
+
_answerStyles: {
'deductible': function( value, _, default_val )
@@ -186,6 +198,8 @@ module.exports = Class( 'ElementStyler',
__construct: function( jquery )
{
this._$context = jquery;
+ this._jquery = jquery;
+ this._document = jquery( 'body' ).context;
},
@@ -802,28 +816,34 @@ module.exports = Class( 'ElementStyler',
*
* This allows for a simple mapping from bucket to UI.
*
- * @param {string} name element name (question name)
- * @param {number=} index index of element to retrieve (bucket index)
- * @param {string=} filter filter to apply to widgets
- * @param {jQuery=} $context filtering context
+ * @param {string} name element name (question name)
+ * @param {number=} index index of element to retrieve (bucket index)
+ * @param {string=} filter filter to apply to widgets
+ * @param {HTMLElement} $context filtering context
*
* @return {jQuery} matches
*/
- 'public getWidgetByName': function( name, index, filter, $context )
+ 'public getWidgetByName': function( name, index, filter, context )
{
- $context = $context || this._$context;
+ context = context || this._$context;
+
+ // Todo: Transitional step to remove jQuery
+ if ( context instanceof jQuery )
+ {
+ context = context[ 0 ];
+ }
// find the field; note that we *skip* the index selection if we have
// been notified---via a property on the context---that the content
// should contain only the index we are looking for
- var $results = this._getWidgetByNameQuick( name, index, $context );
+ var results = this._getWidgetByNameQuick( name, index, context );
if ( filter )
{
- return $results.filter( filter );
+ throw new Error( 'Filter deprecated' );
}
- return $results;
+ return this._jquery( results );
},
@@ -835,33 +855,31 @@ module.exports = Class( 'ElementStyler',
* performed the check to begin with, so the idea is to find the id for as
* many as possible.
*/
- 'private _getWidgetByNameQuick': function( name, index, $context )
+ 'private _getWidgetByNameQuick': function( name, index, context )
{
- var hasindex = ( ( index !== undefined ) && !$context.singleIndex );
+ var hasindex = ( ( index !== undefined ) && !context.singleIndex );
if ( hasindex )
{
- var id = this._getElementId( name, index, $context );
+ var id = this._getElementId( name, index );
if ( id )
{
- $element = $context.find( '#' + id );
+ element = this._document.getElementById( id );
// let's hope for the best
- if ( $element.length )
+ if ( element !== null )
{
- return $element;
+ return element;
}
}
+
+ // Fallback to the painfully slow method.
+ return context.querySelectorAll( '[data-field-name="' + name + '"]' )[ index ];
}
- // damnit. Fallback to the painfully slow method.
- return $context.find( '[data-field-name="' + name + '"]' +
- ( ( hasindex )
- ? ':nth(' + +index + ')'
- : ''
- )
- );
+ // If no index, return first element
+ return context.querySelector( '[data-field-name="' + name + '"]' );
},
@@ -922,28 +940,32 @@ module.exports = Class( 'ElementStyler',
* using NAME as an element id; otherwise, this acts just as
* getElementByName.
*
- * @param {string} name element name (question name)
- * @param {number=} index index of element to retrieve (bucket index)
- * @param {string=} filter filter to apply to widgets
- * @param {jQuery=} $context filtering context
+ * @param {string} name element name (question name)
+ * @param {number=} index index of element to retrieve (bucket index)
+ * @param {string=} filter filter to apply to widgets
+ * @param {HTMLElement} context filtering context
*
* @return {jQuery} matches
*/
'public getElementByNameLax': function(
- name, index, filter, $context
+ name, index, filter, context
)
{
- $context = $context || this._$context;
+ context = context || this._$context;
+
+ // Todo: Transitional step to remove jQuery
+ if ( context instanceof jQuery )
+ {
+ context = context[ 0 ];
+ }
if ( !( this.isAField( name ) ) )
{
- return $context.find(
- '#' + name + ':nth(' + index + ')'
- );
+ return this._jquery( context.querySelectorAll( '#' + name )[ index ] );
}
return this.getElementByName(
- name, index, filter, $context
+ name, index, filter, context
);
},
@@ -1030,11 +1052,25 @@ module.exports = Class( 'ElementStyler',
},
- 'private _getElementId': function( name, index, $context )
+ /**
+ * Determines the id of an element based on the type
+ *
+ * @param {string} name element name
+ * @param {number} index index of element to retrieve (bucket index)
+ *
+ * @return {string} element id
+ */
+ 'private _getElementId': function( name, index )
{
switch ( this._getElementType( name ) )
{
- case 'radio': return '';
+ case 'radio':
+ return '';
+ case 'answer':
+ return name;
+ case 'checkbox':
+ name += '_n';
+ break;
case 'noyes':
// append yes/no depending on whether or not the given index is
// even/odd
@@ -1042,13 +1078,11 @@ module.exports = Class( 'ElementStyler',
? '_y'
: '_n';
- index = index / 2;
-
- /* fallthrough */
-
- default:
- return 'q_' + name + '_' + index;
+ index = Math.floor( index / 2 );
+ break;
}
+
+ return 'q_' + name + '_' + index;
},