Use highest available source index for set event
commit
70ba408c37
|
@ -1,7 +1,7 @@
|
||||||
/**
|
/**
|
||||||
* Liza client
|
* Liza client
|
||||||
*
|
*
|
||||||
* Copyright (C) 2017 R-T Specialty, LLC.
|
* Copyright (C) 2017, 2018 R-T Specialty, LLC.
|
||||||
*
|
*
|
||||||
* This file is part of the Liza Data Collection Framework
|
* This file is part of the Liza Data Collection Framework
|
||||||
*
|
*
|
||||||
|
@ -2616,19 +2616,6 @@ module.exports = Class( 'Client' )
|
||||||
// perform event (XXX: replace me; see above)
|
// perform event (XXX: replace me; see above)
|
||||||
switch ( event_name )
|
switch ( event_name )
|
||||||
{
|
{
|
||||||
case 'set':
|
|
||||||
var setdata = {};
|
|
||||||
setdata[ data.elementName ] = [];
|
|
||||||
|
|
||||||
for ( var i in data.indexes )
|
|
||||||
{
|
|
||||||
var index = data.indexes[ i ];
|
|
||||||
setdata[ data.elementName ][ index ] = data.value[ index ];
|
|
||||||
}
|
|
||||||
|
|
||||||
this._quote.setData( setdata );
|
|
||||||
break;
|
|
||||||
|
|
||||||
case 'reset':
|
case 'reset':
|
||||||
var reset = {};
|
var reset = {};
|
||||||
reset[ data.elementName ] = data.indexes;
|
reset[ data.elementName ] = data.indexes;
|
||||||
|
|
|
@ -369,6 +369,8 @@ module.exports = Class( 'ClientDependencyFactory',
|
||||||
'show': field_vis_handler,
|
'show': field_vis_handler,
|
||||||
'hide': field_vis_handler,
|
'hide': field_vis_handler,
|
||||||
|
|
||||||
|
'set': requireh( 'ValueSetEventHandler' )( client ),
|
||||||
|
|
||||||
'action$cvv2Dialog': requireh( 'Cvv2DialogEventHandler' )( jquery )
|
'action$cvv2Dialog': requireh( 'Cvv2DialogEventHandler' )( jquery )
|
||||||
} );
|
} );
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,86 @@
|
||||||
|
/**
|
||||||
|
* Value set event handler
|
||||||
|
*
|
||||||
|
* Copyright (C) 2018 R-T Specialty, LLC.
|
||||||
|
*
|
||||||
|
* 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 <http://www.gnu.org/licenses/>.
|
||||||
|
*/
|
||||||
|
|
||||||
|
"use strict";
|
||||||
|
|
||||||
|
const Class = require( 'easejs' ).Class;
|
||||||
|
const EventHandler = require( './EventHandler' );
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Set field values
|
||||||
|
*/
|
||||||
|
module.exports = Class( 'ValueSetEventHandler' )
|
||||||
|
.implement( EventHandler )
|
||||||
|
.extend(
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* Client
|
||||||
|
* @type {Client}
|
||||||
|
*/
|
||||||
|
'private _client': null,
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Initialize with client
|
||||||
|
*
|
||||||
|
* Ideally we don't want the client (Law of Demeter), but at the point
|
||||||
|
* that this class is instantiated, a quote may not yet be
|
||||||
|
* initialized. Refactoring is needed.
|
||||||
|
*
|
||||||
|
* @param {Client} client
|
||||||
|
*/
|
||||||
|
constructor( client )
|
||||||
|
{
|
||||||
|
this._client = client;
|
||||||
|
},
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Set value of specified fields
|
||||||
|
*
|
||||||
|
* If the destination index exceeds the number of indexes of the source
|
||||||
|
* field, the last available source index will be used. This matches
|
||||||
|
* the behavior of assertions.
|
||||||
|
*
|
||||||
|
* @param {string} event_id event id
|
||||||
|
* @param {function(*,Object)} callback continuation to invoke on completion
|
||||||
|
* @param {Object} data event data
|
||||||
|
*
|
||||||
|
* @return {EventHandler} self
|
||||||
|
*/
|
||||||
|
'public handle'(
|
||||||
|
event_id, callback, { elementName: field_name, indexes, value }
|
||||||
|
)
|
||||||
|
{
|
||||||
|
const quote = this._client.getQuote();
|
||||||
|
const maxi = value.length - 1;
|
||||||
|
|
||||||
|
const set_data = indexes.reduce( ( result, desti ) =>
|
||||||
|
{
|
||||||
|
result[ desti ] = value[ Math.min( desti, maxi ) ];
|
||||||
|
return result;
|
||||||
|
}, [] );
|
||||||
|
|
||||||
|
quote.setData( { [field_name]: set_data } );
|
||||||
|
callback();
|
||||||
|
},
|
||||||
|
} );
|
|
@ -493,8 +493,12 @@ module.exports = Class( 'ElementStyler',
|
||||||
while ( i-- )
|
while ( i-- )
|
||||||
{
|
{
|
||||||
const question = elements[ i ][ 0 ];
|
const question = elements[ i ][ 0 ];
|
||||||
|
|
||||||
|
if ( question )
|
||||||
|
{
|
||||||
question.checked = ( question.value === ''+value );
|
question.checked = ( question.value === ''+value );
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
break;
|
break;
|
||||||
|
|
||||||
|
|
|
@ -0,0 +1,99 @@
|
||||||
|
/**
|
||||||
|
* Test case for ValueSetEventHandler
|
||||||
|
*
|
||||||
|
* Copyright (C) 2018 R-T Specialty, LLC.
|
||||||
|
*
|
||||||
|
* 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 <http://www.gnu.org/licenses/>.
|
||||||
|
*/
|
||||||
|
|
||||||
|
"use strict";
|
||||||
|
|
||||||
|
const event = require( '../../' ).client.event;
|
||||||
|
const expect = require( 'chai' ).expect;
|
||||||
|
const Class = require( 'easejs' ).Class;
|
||||||
|
|
||||||
|
const { ValueSetEventHandler: Sut } = event;
|
||||||
|
|
||||||
|
|
||||||
|
describe( 'ValueSetEventHandler', () =>
|
||||||
|
{
|
||||||
|
it( "sets relative indexes up until last source index", done =>
|
||||||
|
{
|
||||||
|
const expected = {
|
||||||
|
foo: [ "zero", "one", "one" ],
|
||||||
|
};
|
||||||
|
|
||||||
|
let qcalled = false;
|
||||||
|
const quote = {
|
||||||
|
setData( data )
|
||||||
|
{
|
||||||
|
expect( data ).to.deep.equal( expected );
|
||||||
|
qcalled = true;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
const callback = () =>
|
||||||
|
{
|
||||||
|
expect( qcalled ).to.be.true;
|
||||||
|
done();
|
||||||
|
};
|
||||||
|
|
||||||
|
Sut( { getQuote: () => quote } )
|
||||||
|
.handle(
|
||||||
|
'',
|
||||||
|
callback,
|
||||||
|
{
|
||||||
|
elementName: 'foo',
|
||||||
|
indexes: [ 0, 1, 2 ],
|
||||||
|
value: [ "zero", "one" ],
|
||||||
|
}
|
||||||
|
);
|
||||||
|
} );
|
||||||
|
|
||||||
|
|
||||||
|
it( "sets only given indexes", done =>
|
||||||
|
{
|
||||||
|
const expected = {
|
||||||
|
bar: [ , "set1", "set2" ],
|
||||||
|
};
|
||||||
|
|
||||||
|
let qcalled = false;
|
||||||
|
const quote = {
|
||||||
|
setData( data )
|
||||||
|
{
|
||||||
|
expect( data ).to.deep.equal( expected );
|
||||||
|
qcalled = true;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
const callback = () =>
|
||||||
|
{
|
||||||
|
expect( qcalled ).to.be.true;
|
||||||
|
done();
|
||||||
|
};
|
||||||
|
|
||||||
|
Sut( { getQuote: () => quote } )
|
||||||
|
.handle(
|
||||||
|
'',
|
||||||
|
callback,
|
||||||
|
{
|
||||||
|
elementName: 'bar',
|
||||||
|
indexes: [ 1, 2 ],
|
||||||
|
value: [ "ignore", "set1", "set2", "extra" ],
|
||||||
|
}
|
||||||
|
);
|
||||||
|
} );
|
||||||
|
} );
|
Loading…
Reference in New Issue