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");
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 {

View File

@ -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

View File

@ -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: <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
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<listeners.length; p++) {
var listener = listeners[p];
listener.apply(listener,args);
}
}
};
@ -124,15 +134,12 @@ exports.enqueueTiddlerEvent = function(title,isDeleted) {
// Trigger events
this.eventListeners = this.eventListeners || [];
if(!this.eventsTriggered) {
var me = this;
var self = this;
$tw.utils.nextTick(function() {
var changes = me.changedTiddlers;
me.changedTiddlers = {};
me.eventsTriggered = false;
for(var e=0; e<me.eventListeners.length; e++) {
var listener = me.eventListeners[e];
listener.listener(changes);
}
var changes = self.changedTiddlers;
self.changedTiddlers = {};
self.eventsTriggered = false;
self.dispatchEvent("change",changes);
});
this.eventsTriggered = true;
}

View File

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

View File

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