Helper.js 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. 'use strict';
  2. var getBusinessObject = require('bpmn-js/lib/util/ModelUtil').getBusinessObject;
  3. var is = require('bpmn-js/lib/util/ModelUtil').is,
  4. isAny = require('bpmn-js/lib/features/modeling/util/ModelingUtil').isAny;
  5. var find = require('lodash/find');
  6. var TEMPLATE_ATTR = 'activiti:modelerTemplate';
  7. /**
  8. * The BPMN 2.0 extension attribute name under
  9. * which the element template is stored.
  10. *
  11. * @type {String}
  12. */
  13. module.exports.TEMPLATE_ATTR = TEMPLATE_ATTR;
  14. /**
  15. * Get template id for a given diagram element.
  16. *
  17. * @param {djs.model.Base} element
  18. *
  19. * @return {String}
  20. */
  21. function getTemplateId(element) {
  22. var bo = getBusinessObject(element);
  23. if (bo) {
  24. return bo.get(TEMPLATE_ATTR);
  25. }
  26. }
  27. module.exports.getTemplateId = getTemplateId;
  28. /**
  29. * Get template of a given element.
  30. *
  31. * @param {Element} element
  32. * @param {ElementTemplates} elementTemplates
  33. *
  34. * @return {TemplateDefinition}
  35. */
  36. function getTemplate(element, elementTemplates) {
  37. var id = getTemplateId(element);
  38. return id && elementTemplates.get(id);
  39. }
  40. module.exports.getTemplate = getTemplate;
  41. /**
  42. * Get default template for a given element.
  43. *
  44. * @param {Element} element
  45. * @param {ElementTemplates} elementTemplates
  46. *
  47. * @return {TemplateDefinition}
  48. */
  49. function getDefaultTemplate(element, elementTemplates) {
  50. // return first default template, if any exists
  51. return (
  52. elementTemplates.getAll().filter(function(t) {
  53. return isAny(element, t.appliesTo) && t.isDefault;
  54. })
  55. )[0];
  56. }
  57. module.exports.getDefaultTemplate = getDefaultTemplate;
  58. /**
  59. * Find extension with given type in
  60. * BPMN element, diagram element or ExtensionElement.
  61. *
  62. * @param {ModdleElement|djs.model.Base} element
  63. * @param {String} type
  64. *
  65. * @return {ModdleElement} the extension
  66. */
  67. function findExtension(element, type) {
  68. var bo = getBusinessObject(element);
  69. var extensionElements;
  70. if (is(bo, 'bpmn:ExtensionElements')) {
  71. extensionElements = bo;
  72. } else {
  73. extensionElements = bo.extensionElements;
  74. }
  75. if (!extensionElements) {
  76. return null;
  77. }
  78. return find(extensionElements.get('values'), function(e) {
  79. return is(e, type);
  80. });
  81. }
  82. module.exports.findExtension = findExtension;
  83. function findExtensions(element, types) {
  84. var extensionElements = getExtensionElements(element);
  85. if (!extensionElements) {
  86. return [];
  87. }
  88. return extensionElements.get('values').filter(function(e) {
  89. return isAny(e, types);
  90. });
  91. }
  92. module.exports.findExtensions = findExtensions;
  93. function findActivitiInOut(element, binding) {
  94. var extensionElements = getExtensionElements(element);
  95. if (!extensionElements) {
  96. return;
  97. }
  98. var matcher;
  99. if (binding.type === 'activiti:in') {
  100. matcher = function(e) {
  101. return is(e, 'activiti:In') && isInOut(e, binding);
  102. };
  103. } else
  104. if (binding.type === 'activiti:out') {
  105. matcher = function(e) {
  106. return is(e, 'activiti:Out') && isInOut(e, binding);
  107. };
  108. } else
  109. if (binding.type === 'activiti:in:businessKey') {
  110. matcher = function(e) {
  111. return is(e, 'activiti:In') && 'businessKey' in e;
  112. };
  113. }
  114. return find(extensionElements.get('values'), matcher);
  115. }
  116. module.exports.findActivitiInOut = findActivitiInOut;
  117. function findActivitiProperty(activitiProperties, binding) {
  118. return find(activitiProperties.get('values'), function(p) {
  119. return p.name === binding.name;
  120. });
  121. }
  122. module.exports.findActivitiProperty = findActivitiProperty;
  123. function findInputParameter(inputOutput, binding) {
  124. var parameters = inputOutput.get('inputParameters');
  125. return find(parameters, function(p) {
  126. return p.name === binding.name;
  127. });
  128. }
  129. module.exports.findInputParameter = findInputParameter;
  130. function findOutputParameter(inputOutput, binding) {
  131. var parameters = inputOutput.get('outputParameters');
  132. return find(parameters, function(p) {
  133. var value = p.value;
  134. if (!binding.scriptFormat) {
  135. return value === binding.source;
  136. }
  137. var definition = p.definition;
  138. if (!definition || binding.scriptFormat !== definition.scriptFormat) {
  139. return false;
  140. }
  141. return definition.value === binding.source;
  142. });
  143. }
  144. module.exports.findOutputParameter = findOutputParameter;
  145. // helpers /////////////////////////////////
  146. function getExtensionElements(element) {
  147. var bo = getBusinessObject(element);
  148. if (is(bo, 'bpmn:ExtensionElements')) {
  149. return bo;
  150. } else {
  151. return bo.extensionElements;
  152. }
  153. }
  154. function isInOut(element, binding) {
  155. if (binding.type === 'activiti:in') {
  156. // find based on target attribute
  157. if (binding.target) {
  158. return element.target === binding.target;
  159. }
  160. }
  161. if (binding.type === 'activiti:out') {
  162. // find based on source / sourceExpression
  163. if (binding.source) {
  164. return element.source === binding.source;
  165. }
  166. if (binding.sourceExpression) {
  167. return element.sourceExpression === binding.sourceExpression;
  168. }
  169. }
  170. // find based variables / local combination
  171. if (binding.variables) {
  172. return element.variables === 'all' && (
  173. binding.variables !== 'local' || element.local
  174. );
  175. }
  176. }