diff --git a/src/graph/Graph.js b/src/graph/Graph.js index 408ab33c..79aa8405 100644 --- a/src/graph/Graph.js +++ b/src/graph/Graph.js @@ -197,6 +197,9 @@ function Graph (container, data, options) { } } +// Extend Graph with an Emitter mixin +Emitter(Graph.prototype); + /** * Get the script path where the vis.js library is located * @@ -508,44 +511,6 @@ Graph.prototype.setOptions = function (options) { this._redraw(); }; -/** - * Add event listener - * @param {String} event Event name. Available events: - * 'select' - * @param {function} callback Callback function, invoked as callback(properties) - * where properties is an optional object containing - * event specific properties. - */ -Graph.prototype.on = function on (event, callback) { - var available = ['select']; - - if (available.indexOf(event) == -1) { - throw new Error('Unknown event "' + event + '". Choose from ' + available.join()); - } - - events.addListener(this, event, callback); -}; - -/** - * Remove an event listener - * @param {String} event Event name - * @param {function} callback Callback function - */ -Graph.prototype.off = function off (event, callback) { - events.removeListener(this, event, callback); -}; - -/** - * fire an event - * @param {String} event The name of an event, for example 'select' - * @param {Object} params Optional object with event parameters - * @private - */ -Graph.prototype._trigger = function (event, params) { - events.trigger(this, event, params); -}; - - /** * Create the main frame for the Graph. * This function is executed once when a Graph object is created. The frame diff --git a/src/graph/SelectionMixin.js b/src/graph/SelectionMixin.js index 7252ac15..0761f9a5 100644 --- a/src/graph/SelectionMixin.js +++ b/src/graph/SelectionMixin.js @@ -213,9 +213,7 @@ var SelectionMixin = { this.selectionObj = {}; if (doNotTrigger == false) { - this._trigger('select', { - nodes: this.getSelection() - }); + this.emit('select', this.getSelection()); } }, @@ -242,9 +240,7 @@ var SelectionMixin = { } if (doNotTrigger == false) { - this._trigger('select', { - nodes: this.getSelection() - }); + this.emit('select', this.getSelection()); } }, @@ -399,9 +395,7 @@ var SelectionMixin = { this._removeFromSelection(object); } if (doNotTrigger == false) { - this._trigger('select', { - nodes: this.getSelection() - }); + this.emit('select', this.getSelection()); } }, @@ -512,7 +506,10 @@ var SelectionMixin = { getSelection : function() { var nodeIds = this.getSelectedNodes(); var edgeIds = this.getSelectedEdges(); - return {nodes:nodeIds, edges:edgeIds}; + return { + nodes: nodeIds, + edges: edgeIds + }; }, /** @@ -636,7 +633,7 @@ var SelectionMixin = { ======= if (changed) { // fire the select event - this._trigger('select', { + this.emit('select', { nodes: this.getSelection() }); }