diff --git a/core/modules/startup.js b/core/modules/startup.js index 96b2d4e2b..77d974794 100644 --- a/core/modules/startup.js +++ b/core/modules/startup.js @@ -123,7 +123,7 @@ exports.startup = function() { var container = document.createElement("div"); document.body.insertBefore(container,document.body.firstChild); renderTree.renderInDom(container); - $tw.wiki.addEventListener("",function(changes) { + $tw.wiki.addEventListener("change",function(changes) { renderTree.refreshInDom(changes); }); } else { diff --git a/core/modules/utils/dom/modal.js b/core/modules/utils/dom/modal.js index 9a0e6dfc2..5e909715a 100644 --- a/core/modules/utils/dom/modal.js +++ b/core/modules/utils/dom/modal.js @@ -69,7 +69,7 @@ Modal.prototype.display = function(title,options) { headerRenderTree = new $tw.WikiRenderTree(headerParser,{wiki: $tw.wiki}); headerRenderTree.execute({tiddlerTitle: title}); headerRenderTree.renderInDom(headerTitle); - this.wiki.addEventListener("",function(changes) { + this.wiki.addEventListener("change",function(changes) { headerRenderTree.refreshInDom(changes); }); // Render the body of the message @@ -77,7 +77,7 @@ Modal.prototype.display = function(title,options) { bodyRenderTree = new $tw.WikiRenderTree(bodyParser,{wiki: $tw.wiki}); bodyRenderTree.execute({tiddlerTitle: title}); bodyRenderTree.renderInDom(modalBody); - this.wiki.addEventListener("",function(changes) { + this.wiki.addEventListener("change",function(changes) { bodyRenderTree.refreshInDom(changes); }); // Setup the link if present @@ -105,7 +105,7 @@ Modal.prototype.display = function(title,options) { footerRenderTree = new $tw.WikiRenderTree(footerParser,{wiki: $tw.wiki}); footerRenderTree.execute({tiddlerTitle: title}); footerRenderTree.renderInDom(modalFooterButtons); - this.wiki.addEventListener("",function(changes) { + this.wiki.addEventListener("change",function(changes) { footerRenderTree.refreshInDom(changes); }); // Add the close event handler diff --git a/core/modules/wiki.js b/core/modules/wiki.js index acaaa64bb..ddb841321 100644 --- a/core/modules/wiki.js +++ b/core/modules/wiki.js @@ -7,7 +7,7 @@ Extension methods for the $tw.Wiki object Adds the following properties to the wiki object: -* `eventListeners` is an array of {filter: , listener: fn} +* `eventListeners` is a hashmap by type of arrays of listener functions * `changedTiddlers` is a hashmap describing changes to named tiddlers since wiki change events were last dispatched. Each entry is a hashmap containing two fields: modified: true/false @@ -85,19 +85,29 @@ exports.deleteTextReference = function(textRef,currTiddlerTitle) { } }; -exports.addEventListener = function(filter,listener) { - this.eventListeners = this.eventListeners || []; - this.eventListeners.push({ - filter: filter, - listener: listener - }); +exports.addEventListener = function(type,listener) { + this.eventListeners = this.eventListeners || {}; + this.eventListeners[type] = this.eventListeners[type] || []; + this.eventListeners[type].push(listener); }; -exports.removeEventListener = function(filter,listener) { - for(var c=this.eventListeners.length-1; c>=0; c--) { - var l = this.eventListeners[c]; - if(l.filter === filter && l.listener === listener) { - this.eventListeners.splice(c,1); +exports.removeEventListener = function(type,listener) { + var listeners = this.eventListeners[type]; + if(listeners) { + var p = listeners.indexOf(listener); + if(p !== -1) { + listeners.splice(p,1); + } + } +}; + +exports.dispatchEvent = function(type /*, args */) { + var args = Array.prototype.slice.call(arguments,1), + listeners = this.eventListeners[type]; + if(listeners) { + for(var p=0; p