1
0
mirror of https://github.com/Jermolene/TiddlyWiki5 synced 2024-12-24 17:10:29 +00:00

Refactored wiki event mechanism to support multiple event types

This commit is contained in:
Jeremy Ruston 2013-03-16 10:42:46 +00:00
parent 623a3ec8f8
commit c3b57507b0
5 changed files with 33 additions and 26 deletions

View File

@ -123,7 +123,7 @@ exports.startup = function() {
var container = document.createElement("div"); var container = document.createElement("div");
document.body.insertBefore(container,document.body.firstChild); document.body.insertBefore(container,document.body.firstChild);
renderTree.renderInDom(container); renderTree.renderInDom(container);
$tw.wiki.addEventListener("",function(changes) { $tw.wiki.addEventListener("change",function(changes) {
renderTree.refreshInDom(changes); renderTree.refreshInDom(changes);
}); });
} else { } else {

View File

@ -69,7 +69,7 @@ Modal.prototype.display = function(title,options) {
headerRenderTree = new $tw.WikiRenderTree(headerParser,{wiki: $tw.wiki}); headerRenderTree = new $tw.WikiRenderTree(headerParser,{wiki: $tw.wiki});
headerRenderTree.execute({tiddlerTitle: title}); headerRenderTree.execute({tiddlerTitle: title});
headerRenderTree.renderInDom(headerTitle); headerRenderTree.renderInDom(headerTitle);
this.wiki.addEventListener("",function(changes) { this.wiki.addEventListener("change",function(changes) {
headerRenderTree.refreshInDom(changes); headerRenderTree.refreshInDom(changes);
}); });
// Render the body of the message // 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 = new $tw.WikiRenderTree(bodyParser,{wiki: $tw.wiki});
bodyRenderTree.execute({tiddlerTitle: title}); bodyRenderTree.execute({tiddlerTitle: title});
bodyRenderTree.renderInDom(modalBody); bodyRenderTree.renderInDom(modalBody);
this.wiki.addEventListener("",function(changes) { this.wiki.addEventListener("change",function(changes) {
bodyRenderTree.refreshInDom(changes); bodyRenderTree.refreshInDom(changes);
}); });
// Setup the link if present // Setup the link if present
@ -105,7 +105,7 @@ Modal.prototype.display = function(title,options) {
footerRenderTree = new $tw.WikiRenderTree(footerParser,{wiki: $tw.wiki}); footerRenderTree = new $tw.WikiRenderTree(footerParser,{wiki: $tw.wiki});
footerRenderTree.execute({tiddlerTitle: title}); footerRenderTree.execute({tiddlerTitle: title});
footerRenderTree.renderInDom(modalFooterButtons); footerRenderTree.renderInDom(modalFooterButtons);
this.wiki.addEventListener("",function(changes) { this.wiki.addEventListener("change",function(changes) {
footerRenderTree.refreshInDom(changes); footerRenderTree.refreshInDom(changes);
}); });
// Add the close event handler // Add the close event handler

View File

@ -7,7 +7,7 @@ Extension methods for the $tw.Wiki object
Adds the following properties to the wiki object: Adds the following properties to the wiki object:
* `eventListeners` is an array of {filter: <string>, 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 * `changedTiddlers` is a hashmap describing changes to named tiddlers since wiki change events were
last dispatched. Each entry is a hashmap containing two fields: last dispatched. Each entry is a hashmap containing two fields:
modified: true/false modified: true/false
@ -85,19 +85,29 @@ exports.deleteTextReference = function(textRef,currTiddlerTitle) {
} }
}; };
exports.addEventListener = function(filter,listener) { exports.addEventListener = function(type,listener) {
this.eventListeners = this.eventListeners || []; this.eventListeners = this.eventListeners || {};
this.eventListeners.push({ this.eventListeners[type] = this.eventListeners[type] || [];
filter: filter, this.eventListeners[type].push(listener);
listener: listener
});
}; };
exports.removeEventListener = function(filter,listener) { exports.removeEventListener = function(type,listener) {
for(var c=this.eventListeners.length-1; c>=0; c--) { var listeners = this.eventListeners[type];
var l = this.eventListeners[c]; if(listeners) {
if(l.filter === filter && l.listener === listener) { var p = listeners.indexOf(listener);
this.eventListeners.splice(c,1); 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<listeners.length; p++) {
var listener = listeners[p];
listener.apply(listener,args);
} }
} }
}; };
@ -124,15 +134,12 @@ exports.enqueueTiddlerEvent = function(title,isDeleted) {
// Trigger events // Trigger events
this.eventListeners = this.eventListeners || []; this.eventListeners = this.eventListeners || [];
if(!this.eventsTriggered) { if(!this.eventsTriggered) {
var me = this; var self = this;
$tw.utils.nextTick(function() { $tw.utils.nextTick(function() {
var changes = me.changedTiddlers; var changes = self.changedTiddlers;
me.changedTiddlers = {}; self.changedTiddlers = {};
me.eventsTriggered = false; self.eventsTriggered = false;
for(var e=0; e<me.eventListeners.length; e++) { self.dispatchEvent("change",changes);
var listener = me.eventListeners[e];
listener.listener(changes);
}
}); });
this.eventsTriggered = true; this.eventsTriggered = true;
} }

View File

@ -408,7 +408,7 @@ $tw.plugins.dropbox.saveTiddlerIndex = function(path,callback) {
Setup synchronisation back to Dropbox Setup synchronisation back to Dropbox
*/ */
$tw.plugins.dropbox.setupSyncer = function(wiki) { $tw.plugins.dropbox.setupSyncer = function(wiki) {
wiki.addEventListener("",function(changes) { wiki.addEventListener("change",function(changes) {
$tw.plugins.dropbox.syncChanges(changes,wiki); $tw.plugins.dropbox.syncChanges(changes,wiki);
}); });
}; };

View File

@ -86,7 +86,7 @@ var TiddlyWebSyncer = function(options) {
// Mark us as not logged in // Mark us as not logged in
this.wiki.addTiddler({title: TiddlyWebSyncer.titleIsLoggedIn,text: "no"}); this.wiki.addTiddler({title: TiddlyWebSyncer.titleIsLoggedIn,text: "no"});
// Listen out for changes to tiddlers // Listen out for changes to tiddlers
this.wiki.addEventListener("",function(changes) { this.wiki.addEventListener("change",function(changes) {
self.syncToServer(changes); self.syncToServer(changes);
}); });
this.log("Initialising with host:",this.host); this.log("Initialising with host:",this.host);