Skip to content

Commit

Permalink
fix(Viewer): remove elements di binding on clear call
Browse files Browse the repository at this point in the history
Closes #978
  • Loading branch information
barmac committed Apr 16, 2019
1 parent 00b7d9e commit 161a50f
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 8 deletions.
30 changes: 22 additions & 8 deletions lib/Viewer.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {

Expand All @@ -377,6 +369,28 @@ 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() {
// (0) Remove elements di binding.
this.get('elementRegistry').forEach(function(element) {
var bo = element.businessObject;

if (bo && bo.di) {
delete bo.di;
}
});

// (1) Remove drawn elements.
Diagram.prototype.clear.call(this);
};

/**
* Destroy the viewer instance and remove all its
* remainders from the document tree.
Expand Down
42 changes: 42 additions & 0 deletions test/spec/ViewerSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
Expand Down

0 comments on commit 161a50f

Please sign in to comment.