From 0c71ad30a0c945679851e73b15647f634f2b9bb8 Mon Sep 17 00:00:00 2001 From: Maciej Barelkowski Date: Tue, 16 Apr 2019 08:59:33 +0200 Subject: [PATCH] feat(Viewer): remove businessObject#di binding on #clear Closes #978 --- lib/Viewer.js | 35 ++++++++++++++++++++++++++-------- test/spec/ViewerSpec.js | 42 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 69 insertions(+), 8 deletions(-) diff --git a/lib/Viewer.js b/lib/Viewer.js index 63d47b72..60c9df2f 100644 --- a/lib/Viewer.js +++ b/lib/Viewer.js @@ -345,14 +345,6 @@ Viewer.prototype.saveSVG = function(options, done) { * @method Viewer#invoke */ -/** - * Remove all drawn elements from the viewer. - * - * After calling this method the viewer can still - * be reused for opening another diagram. - * - * @method Viewer#clear - */ Viewer.prototype.importDefinitions = function(definitions, done) { @@ -377,6 +369,33 @@ Viewer.prototype.getModules = function() { return this._modules; }; +/** + * Remove all drawn elements from the viewer. + * + * After calling this method the viewer can still + * be reused for opening another diagram. + * + * @method Viewer#clear + */ +Viewer.prototype.clear = function() { + + // remove businessObject#di binding + // + // this is necessary, as we establish the bindings + // in the BpmnTreeWalker (and assume none are given + // on reimport) + this.get('elementRegistry').forEach(function(element) { + var bo = element.businessObject; + + if (bo && bo.di) { + delete bo.di; + } + }); + + // remove drawn elements + Diagram.prototype.clear.call(this); +}; + /** * Destroy the viewer instance and remove all its * remainders from the document tree. diff --git a/test/spec/ViewerSpec.js b/test/spec/ViewerSpec.js index ea95e975..7ed9f7eb 100644 --- a/test/spec/ViewerSpec.js +++ b/test/spec/ViewerSpec.js @@ -1082,6 +1082,48 @@ describe('Viewer', function() { }); + describe('#clear', function() { + + it('should not throw if diagram is already empty', function() { + + // given + var viewer = new Viewer({ container: container }); + + function clearDiagram() { + viewer.clear(); + } + + // then + expect(clearDiagram).to.not.throw(); + }); + + + it('should remove di property', function(done) { + + var xml = require('../fixtures/bpmn/simple.bpmn'); + + var viewer = new Viewer({ container: container }), + elementRegistry = viewer.get('elementRegistry'); + + viewer.importXML(xml, function(err, warnings) { + + var elements = elementRegistry.getAll(); + + // when + viewer.clear(); + + // then + expect(elements.some(function(el) { + return el && el.businessObject && el.businessObject.di; + }), 'at least one element still has di').to.be.false; + + done(err, warnings); + }); + }); + + }); + + it('default export', function() { expect(ViewerDefaultExport).to.equal(Viewer); });