dq.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. (function() {
  2. window.initDQ = function() {
  3. $(document)
  4. .off('change input paste', '.dq-edit-container input, .dq-edit-container textarea, .dq-edit-container select')
  5. .on('change input paste', '.dq-edit-container input, .dq-edit-container textarea, .dq-edit-container select', function() {
  6. let key = $(this).closest('.dq-line').attr('dq-key'),
  7. dataElem = $(this).closest('.dq-edit-container').find('>.dq-data-map').first(),
  8. current = JSON.parse(dataElem.text());
  9. current[key] = $(this).val();
  10. let serialized = JSON.stringify(current);
  11. dataElem.text(serialized);
  12. $(this).closest('form').find('input[name="data"]').val(serialized);
  13. let fullData = {
  14. lines: JSON.parse($(this).closest('.dq-edit-container').find('>.dq-definition').first().text()),
  15. dataMap: current
  16. };
  17. $(this).closest('form').find('input[name="data"]').val(JSON.stringify(fullData));
  18. runDQConditions($(this).closest('.dq-edit-container'));
  19. });
  20. };
  21. function resolveAtomicCondition(_condition, _dataMap) {
  22. if(!_condition.hasOwnProperty('key') || !_condition.hasOwnProperty('value')) {
  23. _condition.resolution = false;
  24. return;
  25. }
  26. let key = _condition.key, op = _condition.hasOwnProperty('op') ? _condition.op : 'eq';
  27. let lhs = _dataMap[key], rhs = _condition.value;
  28. switch(op) {
  29. case 'eq':
  30. _condition.resolution = (lhs == rhs); // NOTE: using == instead of === on purpose
  31. break;
  32. case 'lt':
  33. _condition.resolution = (+lhs < +rhs);
  34. break;
  35. case 'lte':
  36. _condition.resolution = (+lhs <= +rhs);
  37. break;
  38. case 'gt':
  39. _condition.resolution = (+lhs > +rhs);
  40. break;
  41. case 'gte':
  42. _condition.resolution = (+lhs >= +rhs);
  43. break;
  44. default:
  45. _condition.resolution = false;
  46. }
  47. }
  48. function resolveAllAtomicConditions(_conditions, _dataMap) {
  49. if(Array.isArray(_conditions)) {
  50. }
  51. else {
  52. resolveAtomicCondition(_conditions, _dataMap);
  53. }
  54. }
  55. window.runDQConditions = function(_parent) {
  56. _parent.find('.dq-line.has-pre-condition').each(function() {
  57. let conditions = JSON.parse($(this).find('>.dq-pre-condition').first().text()),
  58. dataMap = JSON.parse($(this).closest('.dq-edit-container').find('>.dq-data-map').first().text());
  59. resolveAllAtomicConditions(conditions, dataMap);
  60. // if object, single condition with key, op and value
  61. if(!Array.isArray(conditions)) {
  62. if(conditions.resolution) {
  63. $(this).removeClass('d-none');
  64. }
  65. else {
  66. $(this).addClass('d-none');
  67. }
  68. }
  69. // else if array - means list of conditions with 'and' or 'or' separators - array size MUST be an odd number
  70. });
  71. }
  72. addMCInitializer('dq-edit', initDQ, '.dq-edit-container');
  73. }).call(window);