|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +var EventEmitter = require('events').EventEmitter; |
| 4 | + |
| 5 | +var Events = { |
| 6 | + |
| 7 | + init: function(plotObj) { |
| 8 | + |
| 9 | + /* |
| 10 | + * If we have already instantiated an emitter for this plot |
| 11 | + * return early. |
| 12 | + */ |
| 13 | + if (plotObj._ev instanceof EventEmitter) return plotObj; |
| 14 | + |
| 15 | + var ev = new EventEmitter(); |
| 16 | + |
| 17 | + /* |
| 18 | + * Assign to plot._ev while we still live in a land |
| 19 | + * where plot is a DOM element with stuff attached to it. |
| 20 | + * In the future we can make plot the event emitter itself. |
| 21 | + */ |
| 22 | + plotObj._ev = ev; |
| 23 | + |
| 24 | + /* |
| 25 | + * Assign bound methods from the ev to the plot object. These methods |
| 26 | + * will reference the 'this' of plot._ev even though they are methods |
| 27 | + * of plot. This will keep the event machinery away from the plot object |
| 28 | + * which currently is often a DOM element but presents an API that will |
| 29 | + * continue to function when plot becomes an emitter. Not all EventEmitter |
| 30 | + * methods have been bound to `plot` as some do not currently add value to |
| 31 | + * the Plotly event API. |
| 32 | + */ |
| 33 | + plotObj.on = ev.on.bind(ev); |
| 34 | + plotObj.once = ev.once.bind(ev); |
| 35 | + plotObj.removeListener = ev.removeListener.bind(ev); |
| 36 | + plotObj.removeAllListeners = ev.removeAllListeners.bind(ev); |
| 37 | + |
| 38 | + /* |
| 39 | + * We must wrap emit to continue to support JQuery events. The idea |
| 40 | + * is to check to see if the user is using JQuery events, if they are |
| 41 | + * we emit JQuery events to trigger user handlers as well as the EventEmitter |
| 42 | + * events. |
| 43 | + */ |
| 44 | + plotObj.emit = function(event, data) { |
| 45 | + if (typeof $ !== 'undefined') { |
| 46 | + $(plotObj).trigger(event, data); |
| 47 | + } |
| 48 | + |
| 49 | + ev.emit(event, data); |
| 50 | + }; |
| 51 | + |
| 52 | + return plotObj; |
| 53 | + }, |
| 54 | + |
| 55 | + /* |
| 56 | + * This function behaves like jQueries triggerHandler. It calls |
| 57 | + * all handlers for a particular event and returns the return value |
| 58 | + * of the LAST handler. This function also triggers jQuery's |
| 59 | + * triggerHandler for backwards compatibility. |
| 60 | + */ |
| 61 | + triggerHandler: function(plotObj, event, data) { |
| 62 | + var jQueryHandlerValue; |
| 63 | + var nodeEventHandlerValue; |
| 64 | + /* |
| 65 | + * If Jquery exists run all its handlers for this event and |
| 66 | + * collect the return value of the LAST handler function |
| 67 | + */ |
| 68 | + if (typeof $ !== 'undefined') { |
| 69 | + jQueryHandlerValue = $(plotObj).triggerHandler(event, data); |
| 70 | + } |
| 71 | + |
| 72 | + /* |
| 73 | + * Now run all the node style event handlers |
| 74 | + */ |
| 75 | + var ev = plotObj._ev; |
| 76 | + if (!ev) return; |
| 77 | + |
| 78 | + var handlers = ev._events[event]; |
| 79 | + if (!handlers) return; |
| 80 | + |
| 81 | + /* |
| 82 | + * handlers can be function or an array of functions |
| 83 | + */ |
| 84 | + if (typeof handlers === 'function') handlers = [handlers]; |
| 85 | + var lastHandler = handlers.pop(); |
| 86 | + |
| 87 | + /* |
| 88 | + * Call all the handlers except the last one. |
| 89 | + */ |
| 90 | + for (var i = 0; i < handlers.length; i++) { |
| 91 | + handlers[i](data); |
| 92 | + } |
| 93 | + |
| 94 | + /* |
| 95 | + * Now call the final handler and collect its value |
| 96 | + */ |
| 97 | + nodeEventHandlerValue = lastHandler(data); |
| 98 | + |
| 99 | + /* |
| 100 | + * Return either the jquery handler value if it exists or the |
| 101 | + * nodeEventHandler value. Jquery event value superceeds nodejs |
| 102 | + * events for backwards compatability reasons. |
| 103 | + */ |
| 104 | + return jQueryHandlerValue !== undefined ? jQueryHandlerValue : |
| 105 | + nodeEventHandlerValue; |
| 106 | + } |
| 107 | +}; |
| 108 | + |
| 109 | +module.exports = Events; |
0 commit comments