From 40505c332860e9373dc23834ea29553b81b22126 Mon Sep 17 00:00:00 2001 From: Mike Gerwitz Date: Mon, 4 Apr 2016 15:25:52 -0400 Subject: [PATCH] FieldStyler#isApplied added Field stylers now determine whether they've been applied to the target field on their own, which solves the problem of the system getting out of sync. * src/ui/styler/ErrorFieldStyler.js (isApplied): Added * src/ui/styler/FieldStyler.js (isApplied): Added * src/ui/styler/NaFieldStyler.js (isApplied): Added * test/ui/styler/NaFieldStylerTest.js: Test for #isApplied --- src/ui/styler/ErrorFieldStyler.js | 18 ++++++++++++++++++ src/ui/styler/FieldStyler.js | 14 ++++++++++++++ src/ui/styler/NaFieldStyler.js | 18 ++++++++++++++++++ test/ui/styler/NaFieldStylerTest.js | 24 ++++++++++++++++++++++++ 4 files changed, 74 insertions(+) diff --git a/src/ui/styler/ErrorFieldStyler.js b/src/ui/styler/ErrorFieldStyler.js index a065047..9f44016 100644 --- a/src/ui/styler/ErrorFieldStyler.js +++ b/src/ui/styler/ErrorFieldStyler.js @@ -35,6 +35,24 @@ module.exports = Class( 'ErrorFieldStyler' ) }, + /** + * Determines whether the field has been styled + * + * Having this predicate on the styler rather than the field ensures + * that, even if the two somehow get out of sync (or styles are applied + * elsewhere), application/revocation will function sanely. + * + * @param {DomField} field field to style + * @param {HTMLElement} element DOM element to style + * + * @return {boolean} whether FIELD has been styled by this styler + */ + 'public isApplied': function( field, element ) + { + return /\binvalid_field\b/.test( element.className ); + }, + + 'public applyStyle': function( field, element, row, msg ) { var _self = this; diff --git a/src/ui/styler/FieldStyler.js b/src/ui/styler/FieldStyler.js index 7ed5cfe..3d62cd9 100644 --- a/src/ui/styler/FieldStyler.js +++ b/src/ui/styler/FieldStyler.js @@ -36,6 +36,20 @@ module.exports = AbstractClass( 'FieldStyler', */ 'abstract public getId': [], + /** + * Determines whether the field has been styled + * + * Having this predicate on the styler rather than the field ensures + * that, even if the two somehow get out of sync (or styles are applied + * elsewhere), application/revocation will function sanely. + * + * @param {DomField} field field to style + * @param {HTMLElement} element DOM element to style + * + * @return {boolean} whether FIELD has been styled by this styler + */ + 'abstract public isApplied': [ 'field', 'element' ], + /** * Apply style to field * diff --git a/src/ui/styler/NaFieldStyler.js b/src/ui/styler/NaFieldStyler.js index 9889558..1f03d42 100644 --- a/src/ui/styler/NaFieldStyler.js +++ b/src/ui/styler/NaFieldStyler.js @@ -43,6 +43,24 @@ module.exports = Class( 'NaFieldStyler' ) }, + /** + * Determines whether the field has been styled + * + * Having this predicate on the styler rather than the field ensures + * that, even if the two somehow get out of sync (or styles are applied + * elsewhere), application/revocation will function sanely. + * + * @param {DomField} field field to style + * @param {HTMLElement} element DOM element to style + * + * @return {boolean} whether FIELD has been styled by this styler + */ + 'public isApplied': function( field, element ) + { + return /\bhidden\b/.test( element.className ); + }, + + /** * Apply style to field * diff --git a/test/ui/styler/NaFieldStylerTest.js b/test/ui/styler/NaFieldStylerTest.js index e1ab337..26c7431 100644 --- a/test/ui/styler/NaFieldStylerTest.js +++ b/test/ui/styler/NaFieldStylerTest.js @@ -221,6 +221,30 @@ describe( 'ui.styler.NaFieldStyler', function() } ); + describe( '#isApplied', function() + { + it( 'recognizes when applied', function() + { + var element = { + className: '', + }; + + var sut = Sut(), + field = getStubField( element ); + + sut.applyStyle( field, element, [] ); + + expect( sut.isApplied( field, element ) ) + .to.be.true; + + sut.revokeStyle( field, element, [] ); + + expect( sut.isApplied( field, element ) ) + .to.be.false; + } ); + } ); + + describe( 'protected API', function() { describe( '#isSubField', function()