1
0
Fork 0

StackedGroupUi header visibility and bucket default init

master
Jeffrey Fisher 2018-06-19 10:29:16 -04:00
commit dd90c3c9f1
4 changed files with 208 additions and 18 deletions

View File

@ -55,15 +55,49 @@ module.exports = Class( 'ProgramInit',
'public init'( program, doc_data ) 'public init'( program, doc_data )
{ {
const defaults = program.defaults || {}; const defaults = program.defaults || {};
const data = doc_data || {};
const groups = program.meta.groups;
// initialize to an array with a single element of the default value Object.keys( program.groupExclusiveFields ).forEach( group =>
return Promise.resolve( {
Object.keys( defaults ).reduce( let i = program.groupExclusiveFields[ group ].length;
( data, key ) => ( data[ key ] === undefined )
? ( data[ key ] = [ defaults[ key ] ], data ) while ( i-- )
: data, {
doc_data || {} const field = program.groupExclusiveFields[ group ][ i ];
) const default_value = defaults[ field ];
);
if ( !defaults.hasOwnProperty( field ))
{
continue;
}
// Initialize with existing document data if any
// If no document data, initialize with default value
if ( data[ field ] === undefined )
{
data[ field ] = [ default_value ];
}
// If min rows on the group is greater than the data
// currently in the bucket, then populate the rest
// of the data with the default data until the
// arrays are the same length
if ( groups.hasOwnProperty( group ) &&
data[ field ].length < groups[ group ].min )
{
let index = data[ field ].length;
while ( index < groups[ group ].min )
{
data[ field ][ index ] = default_value;
index++;
}
}
}
});
return Promise.resolve( data );
}, },
} ); } );

View File

@ -791,8 +791,13 @@ module.exports = Class( 'GroupUi' )
: $element; : $element;
}, },
/**
'public hideField': function( field, index ) * Hides the field based on field name and index
*
* @param field
* @param index
*/
'virtual public hideField': function( field, index )
{ {
if ( this.isFieldVisible( field, index ) === false ) if ( this.isFieldVisible( field, index ) === false )
{ {
@ -813,8 +818,26 @@ module.exports = Class( 'GroupUi' )
.applyStyle( this._naStyler ); .applyStyle( this._naStyler );
}, },
/**
* Returns a boolean depending on if there are visible fields
* based off of the visCount
*
* @param index
* @returns {boolean}
*/
'protected hasVisibleField'( index )
{
return this._visCount[ index ] > 0 ? true : false;
},
'public showField': function( field, index )
/**
* Shows the field based on field name and index
*
* @param field
* @param index
*/
'virtual public showField': function( field, index )
{ {
if ( this.isFieldVisible( field, index ) === true ) if ( this.isFieldVisible( field, index ) === true )
{ {

View File

@ -100,4 +100,43 @@ module.exports = Class( 'StackedGroupUi' )
return this.__super( index ); return this.__super( index );
}, },
/**
* Hide the header if there are no visible fields
*
* @param field
* @param index
*/
'public override hideField'( field, index )
{
this.__super( field, index );
if ( !this.hasVisibleField( index ) )
{
const header = this._$container.find( 'dl' )[ index ];
header.classList.add( 'hidden' );
}
},
/**
* Show the header if there are visible fields
*
* @param field
* @param index
*/
'public override showField'( field, index )
{
this.__super( field, index );
if ( this.hasVisibleField( index ) )
{
const header = this._$container.find( 'dl' )[ index ];
header.classList.remove( 'hidden' );
}
}
} ); } );

View File

@ -34,6 +34,12 @@ describe( 'ProgramInit', () =>
{ {
label: "initializes defaults", label: "initializes defaults",
defaults: { a: "one", b: "two" }, defaults: { a: "one", b: "two" },
meta: {
groups: {}
},
groupExclusiveFields: {
Something: [ "a", "b" ]
},
doc_data: {}, doc_data: {},
expected: { expected: {
a: [ "one" ], a: [ "one" ],
@ -43,18 +49,36 @@ describe( 'ProgramInit', () =>
{ {
label: "does nothing with no data or defaults", label: "does nothing with no data or defaults",
defaults: {}, defaults: {},
meta: {
groups: {}
},
groupExclusiveFields: {},
doc_data: {}, doc_data: {},
expected: {}, expected: {},
}, },
{ {
label: "produces empty object given undefined data", label: "produces empty object given undefined data",
defaults: {}, defaults: {},
meta: {
groups: {}
},
groupExclusiveFields: {},
doc_data: undefined, doc_data: undefined,
expected: {}, expected: {},
}, },
{ {
label: "keeps existing data with defaults", label: "keeps existing data with defaults",
defaults: { foo: "init" }, defaults: {
foo: "init",
bar: "test"
},
meta: {
groups: {}
},
groupExclusiveFields: {
Something: [ "foo" ],
SomethingElse: [ "bar" ]
},
doc_data: { bar: [ "baz" ] }, doc_data: { bar: [ "baz" ] },
expected: { expected: {
foo: [ "init" ], foo: [ "init" ],
@ -62,22 +86,90 @@ describe( 'ProgramInit', () =>
}, },
}, },
{ {
label: "keeps existing doc data with no defaults", label: "keeps existing data with no defaults",
defaults: {}, defaults: {},
meta: {
groups: {}
},
groupExclusiveFields: {
SomethingElse: [ "bar" ]
},
doc_data: { bar: [ "baz" ] },
expected: {
bar: [ "baz" ],
},
},
{
label: "does not overwrite existing data with defaults",
defaults: { foo: "init" },
meta: {
groups: {}
},
groupExclusiveFields: {
Something: [ "foo" ]
},
doc_data: { foo: [ "bar" ] }, doc_data: { foo: [ "bar" ] },
expected: { expected: {
foo: [ "bar" ], foo: [ "bar" ],
}, },
}, },
{ {
label: "does not overwrite existing data with defaults", label: "does not overwrite existing data with defaults and multiple rows",
defaults: { foo: "init" }, defaults: { foo: "init" },
doc_data: { foo: [ "bar" ] }, meta: {
groups: {
Something: {
min: 3
}
}
},
groupExclusiveFields: {
Something: [ "foo" ]
},
doc_data: { foo: [ "bar", "baz", "test" ] },
expected: { expected: {
foo: [ "bar" ], foo: [ "bar", "baz", "test" ],
}, },
}, },
].forEach( ({ label, doc_data, id, defaults, expected }) => {
label: "initializes with default bucket data",
defaults: { foo: "init" },
meta: {
groups: {
Something: {
min: 5
}
}
},
groupExclusiveFields: {
Something: [ "foo" ]
},
doc_data: {},
expected: {
foo: [ "init", "init", "init", "init", "init" ],
},
},
{
label: "fix missing bucket data values",
defaults: { foo: "init" },
meta: {
groups: {
Something: {
min: 5
}
}
},
groupExclusiveFields: {
Something: [ "foo" ]
},
doc_data: {
foo: [ "1", "2", "3", "4" ],
},
expected: {
foo: [ "1", "2", "3", "4", "init" ],
},
},
].forEach( ({ label, doc_data, id, defaults, meta, groupExclusiveFields, expected }) =>
{ {
it( label, () => it( label, () =>
{ {
@ -86,6 +178,8 @@ describe( 'ProgramInit', () =>
const program = { const program = {
id: "foo", id: "foo",
defaults: defaults, defaults: defaults,
meta: meta,
groupExclusiveFields : groupExclusiveFields
}; };
return expect( sut.init( program, doc_data ) ) return expect( sut.init( program, doc_data ) )