ConditionalProps.js 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. 'use strict';
  2. var is = require('bpmn-js/lib/util/ModelUtil').is,
  3. isAny = require('bpmn-js/lib/features/modeling/util/ModelingUtil').isAny,
  4. getBusinessObject = require('bpmn-js/lib/util/ModelUtil').getBusinessObject,
  5. escapeHTML = require('../../../Utils').escapeHTML,
  6. domQuery = require('min-dom').query,
  7. cmdHelper = require('../../../helper/CmdHelper'),
  8. elementHelper = require('../../../helper/ElementHelper'),
  9. eventDefinitionHelper = require('../../../helper/EventDefinitionHelper'),
  10. scriptImplementation = require('./implementation/Script');
  11. module.exports = function(group, element, bpmnFactory, translate) {
  12. var bo = getBusinessObject(element);
  13. if (!bo) {
  14. return;
  15. }
  16. var conditionalEventDefinition = eventDefinitionHelper.getConditionalEventDefinition(element);
  17. if (!(is(element, 'bpmn:SequenceFlow') && isConditionalSource(element.source))
  18. && !conditionalEventDefinition) {
  19. return;
  20. }
  21. var script = scriptImplementation('language', 'body', true, translate);
  22. group.entries.push({
  23. id: 'condition',
  24. label: translate('Condition'),
  25. html: '<div class="bpp-row">' +
  26. '<label for="cam-condition-type">'+ escapeHTML(translate('Condition Type')) + '</label>' +
  27. '<div class="bpp-field-wrapper">' +
  28. '<select id="cam-condition-type" name="conditionType" data-value>' +
  29. '<option value="expression">'+ escapeHTML(translate('Expression')) + '</option>' +
  30. '<option value="script">'+ escapeHTML(translate('Script')) + '</option>' +
  31. '<option value="" selected></option>' +
  32. '</select>' +
  33. '</div>' +
  34. '</div>' +
  35. // expression
  36. '<div class="bpp-row">' +
  37. '<label for="cam-condition" data-show="isExpression">' + escapeHTML(translate('Expression')) + '</label>' +
  38. '<div class="bpp-field-wrapper" data-show="isExpression">' +
  39. '<input id="cam-condition" type="text" name="condition" />' +
  40. '<button class="clear" data-action="clear" data-show="canClear">' +
  41. '<span>X</span>' +
  42. '</button>' +
  43. '</div>' +
  44. '<div data-show="isScript">' +
  45. script.template +
  46. '</div>' +
  47. '</div>',
  48. get: function(element, propertyName) {
  49. var conditionalEventDefinition = eventDefinitionHelper.getConditionalEventDefinition(element);
  50. var conditionExpression = conditionalEventDefinition
  51. ? conditionalEventDefinition.condition
  52. : bo.conditionExpression;
  53. var values = {},
  54. conditionType = '';
  55. if (conditionExpression) {
  56. var conditionLanguage = conditionExpression.language;
  57. if (typeof conditionLanguage !== 'undefined') {
  58. conditionType = 'script';
  59. values = script.get(element, conditionExpression);
  60. } else {
  61. conditionType = 'expression';
  62. values.condition = conditionExpression.get('body');
  63. }
  64. }
  65. values.conditionType = conditionType;
  66. return values;
  67. },
  68. set: function(element, values, containerElement) {
  69. var conditionType = values.conditionType;
  70. var commands = [];
  71. var conditionProps = {
  72. body: undefined
  73. };
  74. if (conditionType === 'script') {
  75. conditionProps = script.set(element, values, containerElement);
  76. } else {
  77. var condition = values.condition;
  78. conditionProps.body = condition;
  79. }
  80. var conditionOrConditionExpression;
  81. if (conditionType) {
  82. conditionOrConditionExpression = elementHelper.createElement(
  83. 'bpmn:FormalExpression',
  84. conditionProps,
  85. conditionalEventDefinition || bo,
  86. bpmnFactory
  87. );
  88. var source = element.source;
  89. // if default-flow, remove default-property from source
  90. if (source && source.businessObject.default === bo) {
  91. commands.push(cmdHelper.updateProperties(source, { 'default': undefined }));
  92. }
  93. }
  94. var update = conditionalEventDefinition
  95. ? { condition: conditionOrConditionExpression }
  96. : { conditionExpression: conditionOrConditionExpression };
  97. commands.push(cmdHelper.updateBusinessObject(element, conditionalEventDefinition || bo, update));
  98. return commands;
  99. },
  100. validate: function(element, values) {
  101. var validationResult = {};
  102. if (!values.condition && values.conditionType === 'expression') {
  103. validationResult.condition = translate('Must provide a value');
  104. }
  105. else if (values.conditionType === 'script') {
  106. validationResult = script.validate(element, values);
  107. }
  108. return validationResult;
  109. },
  110. isExpression: function(element, inputNode) {
  111. var conditionType = domQuery('select[name=conditionType]', inputNode);
  112. if (conditionType.selectedIndex >= 0) {
  113. return conditionType.options[conditionType.selectedIndex].value === 'expression';
  114. }
  115. },
  116. isScript: function(element, inputNode) {
  117. var conditionType = domQuery('select[name=conditionType]', inputNode);
  118. if (conditionType.selectedIndex >= 0) {
  119. return conditionType.options[conditionType.selectedIndex].value === 'script';
  120. }
  121. },
  122. clear: function(element, inputNode) {
  123. // clear text input
  124. domQuery('input[name=condition]', inputNode).value='';
  125. return true;
  126. },
  127. canClear: function(element, inputNode) {
  128. var input = domQuery('input[name=condition]', inputNode);
  129. return input.value !== '';
  130. },
  131. script : script,
  132. cssClasses: [ 'bpp-textfield' ]
  133. });
  134. };
  135. // utilities //////////////////////////
  136. var CONDITIONAL_SOURCES = [
  137. 'bpmn:Activity',
  138. 'bpmn:ExclusiveGateway',
  139. 'bpmn:InclusiveGateway',
  140. 'bpmn:ComplexGateway'
  141. ];
  142. function isConditionalSource(element) {
  143. return isAny(element, CONDITIONAL_SOURCES);
  144. }