CreateAndReferenceHandler.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. 'use strict';
  2. var elementHelper = require('../helper/ElementHelper');
  3. /**
  4. * A handler capable of creating a new element under a provided parent
  5. * and updating / creating a reference to it in one atomic action.
  6. *
  7. * @class
  8. * @constructor
  9. */
  10. function CreateAndReferenceElementHandler(elementRegistry, bpmnFactory) {
  11. this._elementRegistry = elementRegistry;
  12. this._bpmnFactory = bpmnFactory;
  13. }
  14. CreateAndReferenceElementHandler.$inject = [ 'elementRegistry', 'bpmnFactory' ];
  15. module.exports = CreateAndReferenceElementHandler;
  16. // api ////////////////////
  17. /**
  18. * Creates a new element under a provided parent and updates / creates a reference to it in
  19. * one atomic action.
  20. *
  21. * @method CreateAndReferenceElementHandler#execute
  22. *
  23. * @param {Object} context
  24. * @param {djs.model.Base} context.element which is the context for the reference
  25. * @param {moddle.referencingObject} context.referencingObject the object which creates the reference
  26. * @param {String} context.referenceProperty the property of the referencingObject which makes the reference
  27. * @param {moddle.newObject} context.newObject the new object to add
  28. * @param {moddle.newObjectContainer} context.newObjectContainer the container for the new object
  29. *
  30. * @returns {Array<djs.mode.Base>} the updated element
  31. */
  32. CreateAndReferenceElementHandler.prototype.execute = function(context) {
  33. var referencingObject = ensureNotNull(context.referencingObject, 'referencingObject'),
  34. referenceProperty = ensureNotNull(context.referenceProperty, 'referenceProperty'),
  35. newObject = ensureNotNull(context.newObject, 'newObject'),
  36. newObjectContainer = ensureNotNull(context.newObjectContainer, 'newObjectContainer'),
  37. newObjectParent = ensureNotNull(context.newObjectParent, 'newObjectParent'),
  38. changed = [ context.element ]; // this will not change any diagram-js elements
  39. // create new object
  40. var referencedObject = elementHelper
  41. .createElement(newObject.type, newObject.properties, newObjectParent, this._bpmnFactory);
  42. context.referencedObject = referencedObject;
  43. // add to containing list
  44. newObjectContainer.push(referencedObject);
  45. // adjust reference attribute
  46. context.previousReference = referencingObject[referenceProperty];
  47. referencingObject[referenceProperty] = referencedObject;
  48. context.changed = changed;
  49. // indicate changed on objects affected by the update
  50. return changed;
  51. };
  52. /**
  53. * Reverts the update
  54. *
  55. * @method CreateAndReferenceElementHandler#revert
  56. *
  57. * @param {Object} context
  58. *
  59. * @returns {djs.mode.Base} the updated element
  60. */
  61. CreateAndReferenceElementHandler.prototype.revert = function(context) {
  62. var referencingObject = context.referencingObject,
  63. referenceProperty = context.referenceProperty,
  64. previousReference = context.previousReference,
  65. referencedObject = context.referencedObject,
  66. newObjectContainer = context.newObjectContainer;
  67. // reset reference
  68. referencingObject.set(referenceProperty, previousReference);
  69. // remove new element
  70. newObjectContainer.splice(newObjectContainer.indexOf(referencedObject), 1);
  71. return context.changed;
  72. };
  73. // helpers //////////////
  74. function ensureNotNull(prop, name) {
  75. if (!prop) {
  76. throw new Error(name + ' required');
  77. }
  78. return prop;
  79. }