From c204a12c190b6c5caa004346097fdeb48b2b2692 Mon Sep 17 00:00:00 2001 From: Mike Gerwitz Date: Fri, 1 Apr 2016 22:13:21 -0400 Subject: [PATCH] Add NaFieldStyler * src/ui/styler/NaFieldStyler.js: Added * test/ui/styler/NaFieldStylerTest.js: Added --- src/ui/styler/NaFieldStyler.js | 84 ++++++++++++++++++++++ test/ui/styler/NaFieldStylerTest.js | 108 ++++++++++++++++++++++++++++ 2 files changed, 192 insertions(+) create mode 100644 src/ui/styler/NaFieldStyler.js create mode 100644 test/ui/styler/NaFieldStylerTest.js diff --git a/src/ui/styler/NaFieldStyler.js b/src/ui/styler/NaFieldStyler.js new file mode 100644 index 0000000..0a66723 --- /dev/null +++ b/src/ui/styler/NaFieldStyler.js @@ -0,0 +1,84 @@ +/** + * N/A field styler + * + * Copyright (C) 2016 LoVullo Associates, Inc. + * + * This file is part of liza. + * + * liza is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +var Class = require( 'easejs' ).Class, + FieldStyler = require( './FieldStyler' ); + + +/** + * Style fields that are not applicable (and so do not need to collect data + * from the user) + */ +module.exports = Class( 'NaFieldStyler' ) + .extend( FieldStyler, +{ + /** + * Retrieve unique identifier + * + * @return {string} unique identifier + */ + 'public getId': function() + { + return 'na'; + }, + + + /** + * Apply style to field + * + * @param {DomField} field field to style + * @param {HTMLElement} element DOM element to style + * @param {Array.} row DOM elements of containing row + * + * @return {FieldStyler} self + */ + 'public applyStyle': function( field, element, row ) + { + this.addClass( element, 'hidden' ); + + // this is a workaround from the old days where jQuery would add + // styles to hide elements, which we wanted to override; this can be + // removed once jQuery is eradicated from the framework + element.style = ''; + + for ( var i in row ) + { + this.addClass( row[ i ], 'hidden' ); + row[ i ].style = ''; + } + }, + + + /** + * Remove style from field + * + * @param {DomField} field field to unstyle + * @param {HTMLElement} element DOM element to unstyle + * @param {Array.} row DOM elements of containing row + * + * @return {FieldStyler} self + */ + 'public revokeStyle': function( field, element, row ) + { + this.removeClass( element, 'hidden' ); + this.removeClass( row, 'hidden' ); + } +} ); diff --git a/test/ui/styler/NaFieldStylerTest.js b/test/ui/styler/NaFieldStylerTest.js new file mode 100644 index 0000000..f06767a --- /dev/null +++ b/test/ui/styler/NaFieldStylerTest.js @@ -0,0 +1,108 @@ +/** + * Test case for NaFieldStyler + * + * Copyright (C) 2016 LoVullo Associates, Inc. + * + * This file is part of the Liza Data Collection Framework + * + * Liza is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +var styler = require( '../../../' ).ui.styler, + expect = require( 'chai' ).expect, + Class = require( 'easejs' ).Class, + Sut = styler.NaFieldStyler; + + +describe( 'ui.styler.NaFieldStyler', function() +{ + describe( '#getId', function() + { + it( 'returns unique identifier', function() + { + expect( Sut().getId() ).to.equal( 'na' ); + } ); + } ); + + + describe( '#applyStyle', function() + { + it( 'sets hidden class on all elements', function() + { + var element = { className: '' }, + r1 = { className: '' }, + r2 = { className: '' }, + row = [ r1, r2 ]; + + Sut().applyStyle( {}, element, row ); + + [ element, r1, r2 ].forEach( function( ele ) + { + expect( ele.className ).to.match( /\bhidden\b/ ); + } ); + } ); + + + it( 'clears style on all elements', function() + { + var element = { style: 'foo' }, + r1 = { style: 'foo' }, + r2 = { style: 'foo' }, + row = [ r1, r2 ]; + + Sut().applyStyle( {}, element, row ); + + [ element, r1, r2 ].forEach( function( ele ) + { + expect( ele.style ).to.equal( '' ); + } ); + } ); + } ); + + + describe( '#revokeStyle', function() + { + it( 'removes hidden class on all elements', function() + { + var element = { className: 'foo hidden' }, + r1 = { className: 'foo hidden' }, + r2 = { className: 'foo hidden' }, + row = [ r1, r2 ]; + + Sut().revokeStyle( {}, element, row ); + + [ element, r1, r2 ].forEach( function( ele ) + { + expect( ele.className ).to.not.match( /\bhidden\b/ ); + expect( ele.className ).to.match( /foo/ ); + } ); + } ); + + + it( 'does not clear style on all elements', function() + { + var element = { style: 'foo' }, + r1 = { style: 'foo' }, + r2 = { style: 'foo' }, + row = [ r1, r2 ]; + + Sut().revokeStyle( {}, element, row ); + + [ element, r1, r2 ].forEach( function( ele ) + { + expect( ele.style ).to.equal( 'foo' ); + } ); + } ); + } ); +} );