BaseModeler.js 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. import inherits from 'inherits-browser';
  2. import Ids from 'ids';
  3. import BaseViewer from './BaseViewer';
  4. /**
  5. * @typedef {import('./BaseViewer').BaseViewerOptions} BaseViewerOptions
  6. * @typedef {import('./BaseViewer').ModdleElementsById} ModdleElementsById
  7. *
  8. * @typedef {import('./model/Types').ModdleElement} ModdleElement
  9. */
  10. /**
  11. * A base modeler for BPMN 2.0 diagrams.
  12. *
  13. * See {@link Modeler} for a fully-featured modeler.
  14. *
  15. * @param {BaseViewerOptions} [options] The options to configure the modeler.
  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.elementsById);
  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 {BaseViewerOptions} options
  34. *
  35. * @return {Moddle}
  36. */
  37. BaseModeler.prototype._createModdle = function(options) {
  38. var moddle = BaseViewer.prototype._createModdle.call(this, options);
  39. // attach ids to moddle to be able to track and validated ids in the BPMN 2.0
  40. // XML document tree
  41. moddle.ids = new Ids([ 32, 36, 1 ]);
  42. return moddle;
  43. };
  44. /**
  45. * Collect IDs processed during parsing of the definitions object.
  46. *
  47. * @param {ModdleElement} definitions
  48. * @param {ModdleElementsById} elementsById
  49. */
  50. BaseModeler.prototype._collectIds = function(definitions, elementsById) {
  51. var moddle = definitions.$model,
  52. ids = moddle.ids,
  53. id;
  54. // remove references from previous import
  55. ids.clear();
  56. for (id in elementsById) {
  57. ids.claim(id, elementsById[ id ]);
  58. }
  59. };