From 6d05f3a4a5de0bece9b071c49fc9b6d58dacc3bb Mon Sep 17 00:00:00 2001 From: David Heidrich Date: Wed, 15 Jun 2016 22:30:21 +0200 Subject: [PATCH] feat(ServerSideRendering): opt-in event handling --- README.markdown | 2 ++ src/main/RenderingEvents.js | 13 +++---------- src/main/modules/Marklib.js | 4 ++-- src/test/RenderingEvents.js | 3 ++- 4 files changed, 9 insertions(+), 13 deletions(-) diff --git a/README.markdown b/README.markdown index 229b1d6..e3274ec 100644 --- a/README.markdown +++ b/README.markdown @@ -61,6 +61,8 @@ var renderer = new Marklib.Rendering(document, { Marklib triggers events that can be listened to with `instance.on('event-name')`. Events are build with `wolfy87-eventemitter` (https://github.com/Olical/EventEmitter). The following Events are available: +Before you can actually receive events, you need to register the event handler with `registerEvents` + | Event-Name | Description | Arguments | | ------------- |-------------|-------------| | `click` | triggered when clicked on a marking. | `(originalEvent, instanceHierarchy)` diff --git a/src/main/RenderingEvents.js b/src/main/RenderingEvents.js index ff4c43e..d0b3499 100644 --- a/src/main/RenderingEvents.js +++ b/src/main/RenderingEvents.js @@ -92,15 +92,13 @@ export default class RenderingEvents extends EventEmitter { return null; } - - static globalEmitter() { - return RenderingEvents.staticEventListener; - } } +RenderingEvents.globalEmitter = () => RenderingEvents.staticEventListener; + RenderingEvents.staticEventListener = new EventEmitter(); -const init = () => { +export const registerEvents = () => { const currentHoverInstances = new Set(); const betweenInstances = new Set(); @@ -219,8 +217,3 @@ const init = () => { } }, true); }; - -if (!global.__MARKLIB_EVENTS__) { - global.__MARKLIB_EVENTS__ = true; - init(); -} diff --git a/src/main/modules/Marklib.js b/src/main/modules/Marklib.js index b60afb6..f38f6ea 100644 --- a/src/main/modules/Marklib.js +++ b/src/main/modules/Marklib.js @@ -1,6 +1,6 @@ import Rendering from '../Rendering'; import Util from '../util/Util'; import RenderResult from '../RenderResult'; -import RenderingEvents from '../RenderingEvents'; +import RenderingEvents, { registerEvents } from '../RenderingEvents'; -export { Rendering, Util, RenderResult, RenderingEvents }; +export { Rendering, Util, RenderResult, RenderingEvents, registerEvents }; diff --git a/src/test/RenderingEvents.js b/src/test/RenderingEvents.js index 2271bac..ef9f535 100644 --- a/src/test/RenderingEvents.js +++ b/src/test/RenderingEvents.js @@ -3,8 +3,9 @@ import setup from 'setup'; import Rendering from 'Rendering'; import RenderResult from 'RenderResult'; -import { EVENT_CLICK, EVENT_MOUSEENTER, EVENT_MOUSELEAVE } from 'RenderingEvents'; +import { EVENT_CLICK, EVENT_MOUSEENTER, EVENT_MOUSELEAVE, registerEvents } from 'RenderingEvents'; setup(); +registerEvents(); /** * @return {Rendering}