1
0
Fork 0

Extract NaFieldStyler show/hide actions into protected methods

* src/ui/styler/NaFieldStyler.js (showField, hideField): Added
* test/ui/styler/NaFieldStyler.js: Added respective tests
master
Mike Gerwitz 2016-04-04 23:59:57 -04:00
parent e1c2194962
commit befca68110
2 changed files with 111 additions and 75 deletions

View File

@ -72,15 +72,10 @@ module.exports = Class( 'NaFieldStyler' )
*/
'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 = '';
if ( this.isSubField( field ) )
{
this.hideField( element, [] );
field.getParent().removeChild( element );
// this is a child of another field; don't consider it a
@ -89,11 +84,7 @@ module.exports = Class( 'NaFieldStyler' )
return;
}
for ( var i in row )
{
this.addClass( row[ i ], 'hidden' );
row[ i ].style = '';
}
this.hideField( element, row );
},
@ -108,16 +99,15 @@ module.exports = Class( 'NaFieldStyler' )
*/
'public revokeStyle': function( field, element, row )
{
this.removeClass( element, 'hidden' );
if ( this.isSubField( field ) )
{
this.showField( element, [] );
field.getParent().appendChild( element );
return;
}
this.removeClass( row, 'hidden' );
this.showField( element, row );
},
@ -140,5 +130,29 @@ module.exports = Class( 'NaFieldStyler' )
// ES3-compatible (don't use classList)
return !!( parent && /\bwidget\b/.test( parent.className ) );
},
'virtual protected hideField': function( 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 = '';
}
},
'virtual protected showField': function( element, row )
{
this.removeClass( element, 'hidden' );
this.removeClass( row, 'hidden' );
}
} );

View File

@ -27,6 +27,71 @@ var styler = require( '../../../' ).ui.styler,
describe( 'ui.styler.NaFieldStyler', function()
{
function testApplyHidden()
{
var element = { className: '' },
r1 = { className: '' },
r2 = { className: '' },
row = [ r1, r2 ];
Sut().applyStyle( getStubField( element ), element, row );
[ element, r1, r2 ].forEach( function( ele )
{
expect( ele.className ).to.match( /\bhidden\b/ );
} );
}
function testApplyClear()
{
var element = { style: 'foo' },
r1 = { style: 'foo' },
r2 = { style: 'foo' },
row = [ r1, r2 ];
Sut().applyStyle( getStubField( element ), element, row );
[ element, r1, r2 ].forEach( function( ele )
{
expect( ele.style ).to.equal( '' );
} );
}
function testRevokeHidden()
{
var element = { className: 'foo hidden' },
r1 = { className: 'foo hidden' },
r2 = { className: 'foo hidden' },
row = [ r1, r2 ];
Sut().revokeStyle( getStubField( element ), element, row );
[ element, r1, r2 ].forEach( function( ele )
{
expect( ele.className ).to.not.match( /\bhidden\b/ );
expect( ele.className ).to.match( /foo/ );
} );
}
function testRevokeStyle()
{
var element = { style: 'foo' },
r1 = { style: 'foo' },
r2 = { style: 'foo' },
row = [ r1, r2 ];
Sut().revokeStyle( getStubField( element ), element, row );
[ element, r1, r2 ].forEach( function( ele )
{
expect( ele.style ).to.equal( 'foo' );
} );
}
describe( '#getId', function()
{
it( 'returns unique identifier', function()
@ -38,36 +103,8 @@ describe( 'ui.styler.NaFieldStyler', function()
describe( '#applyStyle', function()
{
it( 'sets hidden class on all elements', function()
{
var element = { className: '' },
r1 = { className: '' },
r2 = { className: '' },
row = [ r1, r2 ];
Sut().applyStyle( getStubField( element ), 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( getStubField( element ), element, row );
[ element, r1, r2 ].forEach( function( ele )
{
expect( ele.style ).to.equal( '' );
} );
} );
it( 'sets hidden class on all elements', testApplyHidden );
it( 'clears style on all elements', testApplyClear );
it( 'does not set class on subfield parents', function()
@ -142,37 +179,8 @@ describe( 'ui.styler.NaFieldStyler', function()
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( getStubField( element ), 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( getStubField( element ), element, row );
[ element, r1, r2 ].forEach( function( ele )
{
expect( ele.style ).to.equal( 'foo' );
} );
} );
it( 'removes hidden class on all elements', testRevokeHidden );
it( 'does not clear style on all elements', testRevokeStyle );
it( 'does not remove hidden class on subfield parents', function()
@ -274,6 +282,20 @@ describe( 'ui.styler.NaFieldStyler', function()
.to.be.false;
} );
} );
describe( '#hideField', function()
{
it( 'sets hidden class on all elements', testApplyHidden );
it( 'clears style on all elements', testApplyClear );
} );
describe( '#showField', function()
{
it( 'removes hidden class on all elements', testRevokeHidden );
it( 'does not clear style on all elements', testRevokeStyle );
} );
} );
} );