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:
parent
623a3ec8f8
commit
c3b57507b0
@ -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 {
|
||||
|
@ -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
|
||||
|
@ -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;
|
||||
}
|
||||
|
@ -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);
|
||||
});
|
||||
};
|
||||
|
@ -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);
|
||||
|
Loading…
Reference in New Issue
Block a user