CreateBusinessObjectListHandler.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. 'use strict';
  2. var forEach = require('lodash/forEach');
  3. var elementHelper = require('../helper/ElementHelper');
  4. /**
  5. * A handler that implements a BPMN 2.0 property update
  6. * for business objects which are not represented in the
  7. * diagram.
  8. *
  9. * This is useful in the context of the properties panel in
  10. * order to update child elements of elements visible in
  11. * the diagram.
  12. *
  13. * Example: perform an update of a specific event definition
  14. * of an intermediate event.
  15. *
  16. * @class
  17. * @constructor
  18. */
  19. function CreateBusinessObjectListHandler(elementRegistry, bpmnFactory) {
  20. this._elementRegistry = elementRegistry;
  21. this._bpmnFactory = bpmnFactory;
  22. }
  23. CreateBusinessObjectListHandler.$inject = [ 'elementRegistry', 'bpmnFactory' ];
  24. module.exports = CreateBusinessObjectListHandler;
  25. function ensureNotNull(prop, name) {
  26. if (!prop) {
  27. throw new Error(name + ' required');
  28. }
  29. return prop;
  30. }
  31. function ensureList(prop, name) {
  32. if (!prop || Object.prototype.toString.call(prop) !== '[object Array]') {
  33. throw new Error(name + ' needs to be a list');
  34. }
  35. return prop;
  36. }
  37. // api /////////////////////////////////////////////
  38. /**
  39. * Creates a new element under a provided parent and updates / creates a reference to it in
  40. * one atomic action.
  41. *
  42. * @method CreateBusinessObjectListHandler#execute
  43. *
  44. * @param {Object} context
  45. * @param {djs.model.Base} context.element which is the context for the reference
  46. * @param {moddle.referencingObject} context.referencingObject the object which creates the reference
  47. * @param {String} context.referenceProperty the property of the referencingObject which makes the reference
  48. * @param {moddle.newObject} context.newObject the new object to add
  49. * @param {moddle.newObjectContainer} context.newObjectContainer the container for the new object
  50. *
  51. * @return {Array<djs.mode.Base>} the updated element
  52. */
  53. CreateBusinessObjectListHandler.prototype.execute = function(context) {
  54. var currentObject = ensureNotNull(context.currentObject, 'currentObject'),
  55. propertyName = ensureNotNull(context.propertyName, 'propertyName'),
  56. newObjects = ensureList(context.newObjects, 'newObjects'),
  57. changed = [ context.element ]; // this will not change any diagram-js elements
  58. var childObjects = [];
  59. var self = this;
  60. // create new array of business objects
  61. forEach(newObjects, function(obj) {
  62. var element = elementHelper.createElement(obj.type, obj.properties, currentObject, self._bpmnFactory);
  63. childObjects.push(element);
  64. });
  65. context.childObject = childObjects;
  66. // adjust array reference in the parent business object
  67. context.previousChilds = currentObject[propertyName];
  68. currentObject[propertyName] = childObjects;
  69. context.changed = changed;
  70. // indicate changed on objects affected by the update
  71. return changed;
  72. };
  73. /**
  74. * Reverts the update
  75. *
  76. * @method CreateBusinessObjectListHandler#revert
  77. *
  78. * @param {Object} context
  79. *
  80. * @return {djs.mode.Base} the updated element
  81. */
  82. CreateBusinessObjectListHandler.prototype.revert = function(context) {
  83. var currentObject = context.currentObject,
  84. propertyName = context.propertyName,
  85. previousChilds = context.previousChilds;
  86. // remove new element
  87. currentObject.set(propertyName, previousChilds);
  88. return context.changed;
  89. };