dq.js 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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. for (let i = 0; i < _conditions.length; i++) {
  51. resolveAllAtomicConditions(_conditions[i], _dataMap);
  52. }
  53. }
  54. else if(typeof _conditions === 'object') {
  55. resolveAtomicCondition(_conditions, _dataMap);
  56. }
  57. }
  58. function reduceConditionsIntoResolutions(_conditions) {
  59. for (let i = 0; i < _conditions.length; i++) {
  60. // if simple object, resolve
  61. if(!Array.isArray(_conditions[i]) && typeof _conditions[i] === 'object') {
  62. _conditions.splice(i, 1, _conditions[i].resolution);
  63. }
  64. else if(Array.isArray(_conditions[i])) {
  65. reduceConditionsIntoResolutions(_conditions[i]);
  66. }
  67. }
  68. }
  69. function combineResolutionListsIntoSingleResolutions(_conditions) {
  70. console.log('ALIX 1', _conditions);
  71. for (let i = 0; i < _conditions.length; i++) {
  72. if(Array.isArray(_conditions[i])) {
  73. _conditions[i] = combineResolutionListsIntoSingleResolutions(_conditions[i]);
  74. }
  75. }
  76. console.log('ALIX 2', _conditions);
  77. // at this point, the array will have only booleans and "AND", "OR" combinators
  78. let resolution = _conditions[0];
  79. for (let i = 1; i < _conditions.length; i+=2) {
  80. if(_conditions[i] === 'AND') {
  81. resolution = resolution && _conditions[i + 1];
  82. }
  83. else if(_conditions[i] === 'OR') {
  84. resolution = resolution || _conditions[i + 1];
  85. }
  86. }
  87. return resolution;
  88. }
  89. window.runDQConditions = function(_parent) {
  90. _parent.find('.dq-line.has-pre-condition').each(function() {
  91. let conditions = JSON.parse($(this).find('>.dq-pre-condition').first().text()),
  92. dataMap = JSON.parse($(this).closest('.dq-edit-container').find('>.dq-data-map').first().text());
  93. resolveAllAtomicConditions(conditions, dataMap);
  94. // if object, single condition with key, op and value
  95. if(!Array.isArray(conditions) && typeof conditions === 'object') {
  96. if(conditions.resolution) {
  97. $(this).removeClass('d-none');
  98. }
  99. else {
  100. $(this).addClass('d-none');
  101. }
  102. }
  103. // else if array - means list of conditions with 'and' or 'or' separators - array size MUST be an odd number
  104. else if(Array.isArray(conditions)) {
  105. // goal is to reduce each item in the array into a single boolean - recursively
  106. reduceConditionsIntoResolutions(conditions);
  107. conditions = combineResolutionListsIntoSingleResolutions(conditions);
  108. if(conditions) {
  109. $(this).removeClass('d-none');
  110. }
  111. else {
  112. $(this).addClass('d-none');
  113. }
  114. }
  115. });
  116. }
  117. addMCInitializer('dq-edit', initDQ, '.dq-edit-container');
  118. }).call(window);