From f573a36071c96a5738bec2e47bf601b3ac4470ae Mon Sep 17 00:00:00 2001 From: Nico Rehwaldt Date: Wed, 16 Mar 2016 17:11:38 +0100 Subject: [PATCH] chore(Modeler): remove bpmn-moddle id-support * Handle id tracking ourself. * Do not reinstantiate moddle on re-import (now stateless) * <3 Closes #493 --- .wiredeps | 2 +- lib/Modeler.js | 19 ++++++++++++++++--- lib/Viewer.js | 8 ++++++-- package.json | 2 +- test/spec/ModelerSpec.js | 31 +++++++++++++++++++++++++++++++ 5 files changed, 55 insertions(+), 7 deletions(-) diff --git a/.wiredeps b/.wiredeps index 2373d0ad..c35f8251 100644 --- a/.wiredeps +++ b/.wiredeps @@ -51,7 +51,7 @@ "version": "bpmn-io/diagram-js-direct-editing" }, "ids": { - "version": "0.1.2", + "version": "0.2.0", "dependencies": { "hat": { "version": "0.0.3" diff --git a/lib/Modeler.js b/lib/Modeler.js index d32702a4..d766dcc6 100644 --- a/lib/Modeler.js +++ b/lib/Modeler.js @@ -2,8 +2,7 @@ var inherits = require('inherits'); -var IdSupport = require('bpmn-moddle/lib/id-support'), - Ids = require('ids'); +var Ids = require('ids'); var Viewer = require('./Viewer'); @@ -113,11 +112,25 @@ Modeler.prototype.createDiagram = function(done) { Modeler.prototype.createModdle = function() { var moddle = Viewer.prototype.createModdle.call(this); - IdSupport.extend(moddle, new Ids([ 32, 36, 1 ])); + moddle.ids = new Ids([ 32, 36, 1 ]); return moddle; }; +Modeler.prototype._parsedXML = function(definitions, context) { + + var moddle = definitions.$model, + ids = moddle.ids, + id; + + // remove references from previous import + ids.clear(); + + for (id in context.elementsById) { + ids.claim(id, context.elementsById[id]); + } +}; + Modeler.prototype._interactionModules = [ // non-modeling components diff --git a/lib/Viewer.js b/lib/Viewer.js index f38761de..d7da7dfb 100644 --- a/lib/Viewer.js +++ b/lib/Viewer.js @@ -112,6 +112,8 @@ function Viewer(options) { this.options = options = assign({}, DEFAULT_OPTIONS, options || {}); + this.moddle = this.createModdle(); + var parent = options.container; // support jquery element @@ -157,8 +159,6 @@ Viewer.prototype.importXML = function(xml, done) { var self = this; - this.moddle = this.createModdle(); - this.moddle.fromXML(xml, 'bpmn:Definitions', function(err, definitions, context) { if (err) { @@ -166,6 +166,10 @@ Viewer.prototype.importXML = function(xml, done) { return done(err); } + if (typeof self._parsedXML === 'function') { + self._parsedXML(definitions, context); + } + var parseWarnings = context.warnings; self.importDefinitions(definitions, function(err, importWarnings) { diff --git a/package.json b/package.json index d3a4b351..1f5236f8 100644 --- a/package.json +++ b/package.json @@ -67,7 +67,7 @@ "bpmn-moddle": "^0.10.4", "diagram-js": "^0.13.3", "diagram-js-direct-editing": "^0.13.0", - "ids": "^0.1.2", + "ids": "^0.2.0", "inherits": "^2.0.1", "lodash": "^3.0.1", "min-dom": "^0.2.0", diff --git a/test/spec/ModelerSpec.js b/test/spec/ModelerSpec.js index 20cfeab7..6e4ea7a2 100644 --- a/test/spec/ModelerSpec.js +++ b/test/spec/ModelerSpec.js @@ -107,6 +107,37 @@ describe('Modeler', function() { }); + describe('ids', function() { + + it('should populate ids on import', function(done) { + + // given + var xml = require('../fixtures/bpmn/simple.bpmn'); + + var modeler = new Modeler({ container: container }); + + + // when + modeler.importXML(xml, function(err) { + + var moddle = modeler.get('moddle'); + var elementRegistry = modeler.get('elementRegistry'); + + var subProcess = elementRegistry.get('SubProcess_1').businessObject; + var bpmnEdge = elementRegistry.get('SequenceFlow_3').businessObject.di; + + // then + expect(moddle.ids.assigned('SubProcess_1')).to.eql(subProcess); + expect(moddle.ids.assigned('BPMNEdge_SequenceFlow_3')).to.eql(bpmnEdge); + + done(); + }); + + }); + + }); + + describe('translate support', function() { var xml = require('../fixtures/bpmn/simple.bpmn');