EventDefinitionReference.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. 'use strict';
  2. var cmdHelper = require('../../../../helper/CmdHelper');
  3. var domQuery = require('min-dom').query,
  4. domify = require('min-dom').domify,
  5. domAttr = require('min-dom').attr;
  6. var forEach = require('lodash/forEach'),
  7. find = require('lodash/find');
  8. var elementHelper = require('../../../../helper/ElementHelper');
  9. var utils = require('../../../../Utils'),
  10. escapeHTML = utils.escapeHTML;
  11. var selector = 'select[name=selectedElement]';
  12. /**
  13. * Get select box containing all elements.
  14. *
  15. * @param {DOMElement} node
  16. *
  17. * @return {DOMElement} the select box
  18. */
  19. function getSelectBox(node) {
  20. return domQuery(selector, node.parentElement);
  21. }
  22. /**
  23. * Find element by given id.
  24. *
  25. * @param {ModdleElement} eventDefinition
  26. *
  27. * @return {ModdleElement} an element
  28. */
  29. function findElementById(eventDefinition, type, id) {
  30. var elements = utils.findRootElementsByType(eventDefinition, type);
  31. return find(elements, function(element) {
  32. return element.id === id;
  33. });
  34. }
  35. /**
  36. * Create an entry to modify the reference to an element from an
  37. * event definition.
  38. *
  39. * @param {djs.model.Base} element
  40. * @param {ModdleElement} definition
  41. * @param {BpmnFactory} bpmnFactory
  42. * @param {Object} options
  43. * @param {string} options.label the label of the entry
  44. * @param {string} options.description the description of the entry
  45. * @param {string} options.elementName the name of the element
  46. * @param {string} options.elementType the type of the element
  47. * @param {string} options.referenceProperty the name of referencing property
  48. * @param {string} options.newElementIdPrefix the prefix of a new created element
  49. *
  50. * @return {Array<Object>} return an array containing the entries
  51. */
  52. module.exports = function(element, definition, bpmnFactory, options) {
  53. var elementName = options.elementName || '',
  54. elementType = options.elementType,
  55. referenceProperty = options.referenceProperty;
  56. var newElementIdPrefix = options.newElementIdPrefix || 'elem_';
  57. var label = options.label || '',
  58. description = options.description || '';
  59. var entries = [];
  60. entries.push({
  61. id: 'event-definitions-' + elementName,
  62. description: description,
  63. html: '<div class="bpp-row bpp-select">' +
  64. '<label for="camunda-' + escapeHTML(elementName) + '">' + escapeHTML(label) + '</label>' +
  65. '<div class="bpp-field-wrapper">' +
  66. '<select id="camunda-' + escapeHTML(elementName) + '" name="selectedElement" data-value>' +
  67. '</select>' +
  68. '<button class="add" id="addElement" data-action="addElement"><span>+</span></button>' +
  69. '</div>' +
  70. '</div>',
  71. get: function(element, entryNode) {
  72. utils.updateOptionsDropDown(selector, definition, elementType, entryNode);
  73. var reference = definition.get(referenceProperty);
  74. return {
  75. selectedElement: (reference && reference.id) || ''
  76. };
  77. },
  78. set: function(element, values) {
  79. var selection = values.selectedElement;
  80. var props = {};
  81. if (!selection || typeof selection === 'undefined') {
  82. // remove reference to element
  83. props[referenceProperty] = undefined;
  84. return cmdHelper.updateBusinessObject(element, definition, props);
  85. }
  86. var commands = [];
  87. var selectedElement = findElementById(definition, elementType, selection);
  88. if (!selectedElement) {
  89. var root = utils.getRoot(definition);
  90. // create a new element
  91. selectedElement = elementHelper.createElement(elementType, { name: selection }, root, bpmnFactory);
  92. commands.push(cmdHelper.addAndRemoveElementsFromList(element, root, 'rootElements', null, [ selectedElement ]));
  93. }
  94. // update reference to element
  95. props[referenceProperty] = selectedElement;
  96. commands.push(cmdHelper.updateBusinessObject(element, definition, props));
  97. return commands;
  98. },
  99. addElement: function(element, inputNode) {
  100. // note: this generated id will be used as name
  101. // of the element and not as id
  102. var id = utils.nextId(newElementIdPrefix);
  103. var optionTemplate = domify('<option value="' + escapeHTML(id) + '"> (id='+escapeHTML(id)+')' + '</option>');
  104. // add new option
  105. var selectBox = getSelectBox(inputNode);
  106. selectBox.insertBefore(optionTemplate, selectBox.firstChild);
  107. // select new element in the select box
  108. forEach(selectBox, function(option) {
  109. if (option.value === id) {
  110. domAttr(option, 'selected', 'selected');
  111. } else {
  112. domAttr(option, 'selected', null);
  113. }
  114. });
  115. return true;
  116. }
  117. });
  118. return entries;
  119. };