BaseModeler.js 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. import inherits from 'inherits';
  2. import Ids from 'ids';
  3. import BaseViewer from './BaseViewer';
  4. /**
  5. * A base modeler for BPMN 2.0 diagrams.
  6. *
  7. * Have a look at {@link Modeler} for a bundle that includes actual features.
  8. *
  9. * @param {Object} [options] configuration options to pass to the viewer
  10. * @param {DOMElement} [options.container] the container to render the viewer in, defaults to body.
  11. * @param {string|number} [options.width] the width of the viewer
  12. * @param {string|number} [options.height] the height of the viewer
  13. * @param {Object} [options.moddleExtensions] extension packages to provide
  14. * @param {Array<didi.Module>} [options.modules] a list of modules to override the default modules
  15. * @param {Array<didi.Module>} [options.additionalModules] a list of modules to use with the default modules
  16. */
  17. export default function BaseModeler(options) {
  18. BaseViewer.call(this, options);
  19. // hook ID collection into the modeler
  20. this.on('import.parse.complete', function(event) {
  21. if (!event.error) {
  22. this._collectIds(event.definitions, event.context);
  23. }
  24. }, this);
  25. this.on('diagram.destroy', function() {
  26. this.get('moddle').ids.clear();
  27. }, this);
  28. }
  29. inherits(BaseModeler, BaseViewer);
  30. /**
  31. * Create a moddle instance, attaching ids to it.
  32. *
  33. * @param {Object} options
  34. */
  35. BaseModeler.prototype._createModdle = function(options) {
  36. var moddle = BaseViewer.prototype._createModdle.call(this, options);
  37. // attach ids to moddle to be able to track
  38. // and validated ids in the BPMN 2.0 XML document
  39. // tree
  40. moddle.ids = new Ids([ 32, 36, 1 ]);
  41. return moddle;
  42. };
  43. /**
  44. * Collect ids processed during parsing of the
  45. * definitions object.
  46. *
  47. * @param {ModdleElement} definitions
  48. * @param {Context} context
  49. */
  50. BaseModeler.prototype._collectIds = function(definitions, context) {
  51. var moddle = definitions.$model,
  52. ids = moddle.ids,
  53. id;
  54. // remove references from previous import
  55. ids.clear();
  56. for (id in context.elementsById) {
  57. ids.claim(id, context.elementsById[id]);
  58. }
  59. };