UpdateBusinessObjectHandler.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. 'use strict';
  2. var reduce = require('lodash/transform'),
  3. is = require('bpmn-js/lib/util/ModelUtil').is,
  4. keys = require('lodash/keys'),
  5. forEach = require('lodash/forEach');
  6. /**
  7. * A handler that implements a BPMN 2.0 property update
  8. * for business objects which are not represented in the
  9. * diagram.
  10. *
  11. * This is useful in the context of the properties panel in
  12. * order to update child elements of elements visible in
  13. * the diagram.
  14. *
  15. * Example: perform an update of a specific event definition
  16. * of an intermediate event.
  17. *
  18. * @class
  19. * @constructor
  20. */
  21. function UpdateBusinessObjectHandler(elementRegistry) {
  22. this._elementRegistry = elementRegistry;
  23. }
  24. UpdateBusinessObjectHandler.$inject = [ 'elementRegistry' ];
  25. module.exports = UpdateBusinessObjectHandler;
  26. /**
  27. * returns the root element
  28. */
  29. function getRoot(businessObject) {
  30. var parent = businessObject;
  31. while (parent.$parent) {
  32. parent = parent.$parent;
  33. }
  34. return parent;
  35. }
  36. function getProperties(businessObject, propertyNames) {
  37. return reduce(propertyNames, function(result, key) {
  38. result[key] = businessObject.get(key);
  39. return result;
  40. }, {});
  41. }
  42. function setProperties(businessObject, properties) {
  43. forEach(properties, function(value, key) {
  44. businessObject.set(key, value);
  45. });
  46. }
  47. // api /////////////////////////////////////////////
  48. /**
  49. * Updates a business object with a list of new properties
  50. *
  51. * @method UpdateBusinessObjectHandler#execute
  52. *
  53. * @param {Object} context
  54. * @param {djs.model.Base} context.element the element which has a child business object updated
  55. * @param {moddle.businessObject} context.businessObject the businessObject to update
  56. * @param {Object} context.properties a list of properties to set on the businessObject
  57. *
  58. * @return {Array<djs.mode.Base>} the updated element
  59. */
  60. UpdateBusinessObjectHandler.prototype.execute = function(context) {
  61. var element = context.element,
  62. businessObject = context.businessObject,
  63. rootElements = getRoot(businessObject).rootElements,
  64. referenceType = context.referenceType,
  65. referenceProperty = context.referenceProperty,
  66. changed = [ element ]; // this will not change any diagram-js elements
  67. if (!element) {
  68. throw new Error('element required');
  69. }
  70. if (!businessObject) {
  71. throw new Error('businessObject required');
  72. }
  73. var properties = context.properties,
  74. oldProperties = context.oldProperties || getProperties(businessObject, keys(properties));
  75. // check if there the update needs an external element for reference
  76. if (typeof referenceType !== 'undefined' && typeof referenceProperty !== 'undefined') {
  77. forEach(rootElements, function(rootElement) {
  78. if (is(rootElement, referenceType)) {
  79. if (rootElement.id === properties[referenceProperty]) {
  80. properties[referenceProperty] = rootElement;
  81. }
  82. }
  83. });
  84. }
  85. // update properties
  86. setProperties(businessObject, properties);
  87. // store old values
  88. context.oldProperties = oldProperties;
  89. context.changed = changed;
  90. // indicate changed on objects affected by the update
  91. return changed;
  92. };
  93. /**
  94. * Reverts the update
  95. *
  96. * @method UpdateBusinessObjectHandler#revert
  97. *
  98. * @param {Object} context
  99. *
  100. * @return {djs.mode.Base} the updated element
  101. */
  102. UpdateBusinessObjectHandler.prototype.revert = function(context) {
  103. var oldProperties = context.oldProperties,
  104. businessObject = context.businessObject;
  105. // update properties
  106. setProperties(businessObject, oldProperties);
  107. return context.changed;
  108. };