diff --git a/resources/edit.html b/resources/edit.html index 1d5f65a..8c286ef 100644 --- a/resources/edit.html +++ b/resources/edit.html @@ -8,6 +8,7 @@ + diff --git a/resources/public/js/events.js b/resources/public/js/events.js new file mode 100644 index 0000000..9965745 --- /dev/null +++ b/resources/public/js/events.js @@ -0,0 +1,62 @@ +/** + * Simple Event Bus + * + * Allows to easily hook into various page rendering / markdown parsing stages with custom modules + * + * + * High Level API: + * + * // Subscribe + * events.subscribe(eventName:String, eventHandler:Function) + * + * // Publish + * events.publish(eventName:String, optionalArgument1, optionalArgument2, ..., optionalArgumentN); + * + * + * Sample Usage: + * + * event.subscribe('markdown:parsed', function () { + * console.log('Markdown Parsed'); + * }); + * + * event.subscribe('markdown:parsed', function (title) { + * console.log('Markdown Parsed For Document: ' + title); + * }); + * + * events.publish('markdown:parsed', 'SampleDocument.md'); + * // Markdown Parsed + * // Markdown Parsed For Document: SampleDocument.md + * + */ +(function (global) { + + var eventBus = { + subscribers: [], + }; + + global.events = global.events || { + + subscribe: function (eventName, eventHandler) { + // Initialize an array of event listeners if doesn't exist already + eventBus.subscribers[eventName] = eventBus.subscribers[eventName] || []; + + eventBus.subscribers[eventName].push(eventHandler); + }, + + publish: function (eventName /*, arg1, arg2, ..., argN */) { + var eventArguments = [].slice.call(arguments, 1); + + if (eventArguments.length) { + console.log('[Hooks] "%s" with args %O', eventName, eventArguments); + } else { + console.log('[Hooks] "%s"', eventName); + } + + // Call event handlers with given attributes + (eventBus.subscribers[eventName] || []).forEach(function (eventHandler) { + eventHandler.apply(null, eventArguments); + }); + }, + + }; +}(window)); diff --git a/resources/public/js/publishing.js b/resources/public/js/publishing.js index 56c6e90..e011564 100644 --- a/resources/public/js/publishing.js +++ b/resources/public/js/publishing.js @@ -25,6 +25,9 @@ function enableButton() { } function onLoad() { + // Hook point + events.publish('document:loaded'); + $note = $("note"); $action = $("action").value; $preview = $("draft"); @@ -37,6 +40,9 @@ function onLoad() { timer = setTimeout(function() { $preview.innerHTML = md2html(content); $tableau.innerHTML = content.split(/\s+/).length + " words"; + + // Hook point + events.publish('content:rendered'); }, delay); }; if ($action == "UPDATE") updatePreview(); diff --git a/resources/template.html b/resources/template.html index b127591..c88ce88 100644 --- a/resources/template.html +++ b/resources/template.html @@ -12,5 +12,11 @@ %CONTENT% %FOOTER% + + + +