123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384 |
- (function() {
- window.initDQ = function() {
- $(document)
- .off('change input paste', '.dq-edit-container input, .dq-edit-container textarea, .dq-edit-container select')
- .on('change input paste', '.dq-edit-container input, .dq-edit-container textarea, .dq-edit-container select', function() {
- let key = $(this).closest('.dq-line').attr('dq-key'),
- dataElem = $(this).closest('.dq-edit-container').find('>.dq-data-map').first(),
- current = JSON.parse(dataElem.text());
- current[key] = $(this).val();
- let serialized = JSON.stringify(current);
- dataElem.text(serialized);
- $(this).closest('form').find('input[name="data"]').val(serialized);
- let fullData = {
- lines: JSON.parse($(this).closest('.dq-edit-container').find('>.dq-definition').first().text()),
- dataMap: current
- };
- $(this).closest('form').find('input[name="data"]').val(JSON.stringify(fullData));
- runDQConditions($(this).closest('.dq-edit-container'));
- });
- };
- function resolveAtomicCondition(_condition, _dataMap) {
- if(!_condition.hasOwnProperty('key') || !_condition.hasOwnProperty('value')) {
- _condition.resolution = false;
- return;
- }
- let key = _condition.key, op = _condition.hasOwnProperty('op') ? _condition.op : 'eq';
- let lhs = _dataMap[key], rhs = _condition.value;
- switch(op) {
- case 'eq':
- _condition.resolution = (lhs == rhs); // NOTE: using == instead of === on purpose
- break;
- case 'lt':
- _condition.resolution = (+lhs < +rhs);
- break;
- case 'lte':
- _condition.resolution = (+lhs <= +rhs);
- break;
- case 'gt':
- _condition.resolution = (+lhs > +rhs);
- break;
- case 'gte':
- _condition.resolution = (+lhs >= +rhs);
- break;
- default:
- _condition.resolution = false;
- }
- }
- function resolveAllAtomicConditions(_conditions, _dataMap) {
- if(Array.isArray(_conditions)) {
- }
- else {
- resolveAtomicCondition(_conditions, _dataMap);
- }
- }
- window.runDQConditions = function(_parent) {
- _parent.find('.dq-line.has-pre-condition').each(function() {
- let conditions = JSON.parse($(this).find('>.dq-pre-condition').first().text()),
- dataMap = JSON.parse($(this).closest('.dq-edit-container').find('>.dq-data-map').first().text());
- resolveAllAtomicConditions(conditions, dataMap);
- // if object, single condition with key, op and value
- if(!Array.isArray(conditions)) {
- if(conditions.resolution) {
- $(this).removeClass('d-none');
- }
- else {
- $(this).addClass('d-none');
- }
- }
- // else if array - means list of conditions with 'and' or 'or' separators - array size MUST be an odd number
- });
- }
- addMCInitializer('dq-edit', initDQ, '.dq-edit-container');
- }).call(window);
|