feat(Viewer): add #off to remove event listeners

Closes #470
This commit is contained in:
Nico Rehwaldt 2016-02-16 10:31:12 +01:00
parent 851dfb67d0
commit d09ec1c19d
2 changed files with 104 additions and 5 deletions

View File

@ -25,7 +25,7 @@ function initListeners(diagram, listeners) {
var events = diagram.get('eventBus'); var events = diagram.get('eventBus');
listeners.forEach(function(l) { listeners.forEach(function(l) {
events.on(l.event, l.priority, l.handler, l.that); events.on(l.event, l.priority, l.callback, l.that);
}); });
} }
@ -365,17 +365,54 @@ Viewer.prototype.destroy = function() {
/** /**
* Register an event listener on the viewer * Register an event listener on the viewer
* *
* Remove a previously added listener via {@link #off(event, callback)}.
*
* @param {String} event * @param {String} event
* @param {Function} handler * @param {Number} [priority]
* @param {Function} callback
* @param {Object} [that]
*/ */
Viewer.prototype.on = function(event, priority, handler, that) { Viewer.prototype.on = function(event, priority, callback, that) {
var diagram = this.diagram, var diagram = this.diagram,
listeners = this.__listeners = this.__listeners || []; listeners = this.__listeners = this.__listeners || [];
listeners.push({ event: event, priority: priority, handler: handler, that: that }); if (typeof priority === 'function') {
that = callback;
callback = priority;
priority = 1000;
}
listeners.push({ event: event, priority: priority, callback: callback, that: that });
if (diagram) { if (diagram) {
diagram.get('eventBus').on(event, priority, handler, that); return diagram.get('eventBus').on(event, priority, callback, that);
}
};
/**
* De-register an event callback
*
* @param {String} event
* @param {Function} callback
*/
Viewer.prototype.off = function(event, callback) {
var filter,
diagram = this.diagram;
if (callback) {
filter = function(l) {
return !(l.event === event && l.callback === callback);
};
} else {
filter = function(l) {
return l.event !== event;
};
}
this.__listeners = (this.__listeners || []).filter(filter);
if (diagram) {
diagram.get('eventBus').off(event, callback);
} }
}; };

View File

@ -626,6 +626,68 @@ describe('Viewer', function() {
}); });
describe.only('#off', function() {
var xml = require('../fixtures/bpmn/simple.bpmn');
it('should remove listener permanently', function(done) {
// given
var viewer = new Viewer({ container: container });
var handler = function() {
return 'bar';
};
viewer.on('foo', 1000, handler);
// when
viewer.off('foo');
// then
viewer.importXML(xml, function(err) {
var eventBus = viewer.get('eventBus');
var result = eventBus.fire('foo');
expect(result).not.to.exist;
done();
});
});
it('should remove listener on existing diagram instance', function(done) {
// given
var viewer = new Viewer({ container: container });
var handler = function() {
return 'bar';
};
viewer.on('foo', 1000, handler);
// when
viewer.importXML(xml, function(err) {
var eventBus = viewer.get('eventBus');
// when
viewer.off('foo', handler);
var result = eventBus.fire('foo');
expect(result).not.to.exist;
done();
});
});
});
describe('#destroy', function() { describe('#destroy', function() {
it('should remove traces in document tree', function() { it('should remove traces in document tree', function() {