UpdateBusinessObjectListHandler.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. 'use strict';
  2. var forEach = require('lodash/forEach');
  3. /**
  4. * A handler that implements a BPMN 2.0 property update
  5. * for business object lists which are not represented in the
  6. * diagram.
  7. *
  8. * This is useful in the context of the properties panel in
  9. * order to update child elements of elements visible in
  10. * the diagram.
  11. *
  12. * Example: perform an update of a specific event definition
  13. * of an intermediate event.
  14. *
  15. * @class
  16. * @constructor
  17. */
  18. function UpdateBusinessObjectListHandler(elementRegistry, bpmnFactory) {
  19. this._elementRegistry = elementRegistry;
  20. this._bpmnFactory = bpmnFactory;
  21. }
  22. UpdateBusinessObjectListHandler.$inject = [ 'elementRegistry', 'bpmnFactory' ];
  23. module.exports = UpdateBusinessObjectListHandler;
  24. function ensureNotNull(prop, name) {
  25. if (!prop) {
  26. throw new Error(name + 'required');
  27. }
  28. return prop;
  29. }
  30. // api /////////////////////////////////////////////
  31. /**
  32. * Updates a element under a provided parent.
  33. */
  34. UpdateBusinessObjectListHandler.prototype.execute = function(context) {
  35. var currentObject = ensureNotNull(context.currentObject, 'currentObject'),
  36. propertyName = ensureNotNull(context.propertyName, 'propertyName'),
  37. updatedObjectList = context.updatedObjectList,
  38. objectsToRemove = context.objectsToRemove || [],
  39. objectsToAdd = context.objectsToAdd || [],
  40. changed = [ context.element], // this will not change any diagram-js elements
  41. referencePropertyName;
  42. if (context.referencePropertyName) {
  43. referencePropertyName = context.referencePropertyName;
  44. }
  45. var objectList = currentObject[propertyName];
  46. // adjust array reference in the parent business object
  47. context.previousList = currentObject[propertyName];
  48. if (updatedObjectList) {
  49. currentObject[propertyName] = updatedObjectList;
  50. } else {
  51. var listCopy = [];
  52. // remove all objects which should be removed
  53. forEach(objectList, function(object) {
  54. if (objectsToRemove.indexOf(object) == -1) {
  55. listCopy.push(object);
  56. }
  57. });
  58. // add all objects which should be added
  59. listCopy = listCopy.concat(objectsToAdd);
  60. // set property to new list
  61. if (listCopy.length > 0 || !referencePropertyName) {
  62. // as long as there are elements in the list update the list
  63. currentObject[propertyName] = listCopy;
  64. } else if (referencePropertyName) {
  65. // remove the list when it is empty
  66. var parentObject = currentObject.$parent;
  67. parentObject.set(referencePropertyName, undefined);
  68. }
  69. }
  70. context.changed = changed;
  71. // indicate changed on objects affected by the update
  72. return changed;
  73. };
  74. /**
  75. * Reverts the update
  76. *
  77. * @method CreateBusinessObjectListHandler#revert
  78. *
  79. * @param {Object} context
  80. *
  81. * @return {djs.mode.Base} the updated element
  82. */
  83. UpdateBusinessObjectListHandler.prototype.revert = function(context) {
  84. var currentObject = context.currentObject,
  85. propertyName = context.propertyName,
  86. previousList = context.previousList,
  87. parentObject = currentObject.$parent;
  88. if (context.referencePropertyName) {
  89. parentObject.set(context.referencePropertyName, currentObject);
  90. }
  91. // remove new element
  92. currentObject.set(propertyName, previousList);
  93. return context.changed;
  94. };