1
0
mirror of https://github.com/Jermolene/TiddlyWiki5 synced 2024-11-30 13:29:56 +00:00

Refactoring navigation handling

Not done yet, but the tests are passing so it's a good point to check
in. The idea is to let macros register for events to handle both the
click and the navigation request.
This commit is contained in:
Jeremy Ruston 2012-02-02 15:13:50 +00:00
parent 9206894e9a
commit 449f322a86
4 changed files with 77 additions and 12 deletions

View File

@ -28,14 +28,14 @@ Navigators.prototype.registerNavigator = function(name,nav) {
Navigators.prototype.install = function(selector,navname) { Navigators.prototype.install = function(selector,navname) {
var nav = this.navigators[navname]; var nav = this.navigators[navname];
this.document.addEventListener("click",function(e) { this.document.addEventListener("click",function(event) {
var el = e.target, var el = event.target,
matchesSelector = el.matchesSelector || el.mozMatchesSelector || matchesSelector = el.matchesSelector || el.mozMatchesSelector ||
el.webkitMatchesSelector || el.oMatchesSelector || el.msMatchesSelector; el.webkitMatchesSelector || el.oMatchesSelector || el.msMatchesSelector;
if(matchesSelector && matchesSelector.call(el,selector)) { if(matchesSelector && matchesSelector.call(el,selector)) {
var r = nav.navigateTo(el.getAttribute("href")); var r = nav.navigateTo(el.getAttribute("href"),event);
if(!r) { if(!r) {
e.preventDefault(); event.preventDefault();
} else { } else {
el.setAttribute("target","_blank"); el.setAttribute("target","_blank");
} }

View File

@ -15,15 +15,14 @@ var StoryNavigator = function(navigators) {
this.navigators = navigators; this.navigators = navigators;
}; };
StoryNavigator.prototype.navigateTo = function(title) { StoryNavigator.prototype.navigateTo = function(title,event) {
var store = this.navigators.store, var store = this.navigators.store,
tiddler = store.getTiddler(title), tiddler = store.getTiddler(title);
storyTiddler = store.getTiddler("StoryTiddlers"); // This is a hack, obviously
if(tiddler) { if(tiddler) {
store.addTiddler(new Tiddler(storyTiddler,{text: title + "\n" + storyTiddler.text})); store.invokeMacroMethod(event.target,"navigateTo",{
$("html,body").animate({ event: event,
scrollTop: 0 target: title
}, 400); });
return false; return false;
} else { } else {
return true; return true;

View File

@ -472,6 +472,62 @@ WikiStore.prototype.refreshDomNode = function(node,changes,renderer,tiddler) {
} }
}; };
/*
Search up the parents of the specified node, looking for a macro that accepts the specified method
*/
WikiStore.prototype.invokeMacroMethod = function(node,method,args) {
// Walk up the DOM chain to get the context tiddler for each node
var contextStack = [],
macroStack = [],
nodeStack = [],
parentWalker = function(node) {
if(node.parentNode !== null) {
var parentContext = parentWalker(node.parentNode),
contextInfo = {tiddler: parentContext.tiddler, template: parentContext.template},
macroInfo = {};
if(node.getAttribute) {
if(node.hasAttribute("data-tw-render-tiddler")) {
contextInfo.tiddler = node.getAttribute("data-tw-render-tiddler");
contextInfo.template = node.getAttribute("data-tw-render-template");
}
macroInfo.macro = node.getAttribute("data-tw-macro");
macroInfo.step = node.getAttribute("data-tw-render-step");
}
nodeStack.push(node);
contextStack.push(contextInfo);
macroStack.push(macroInfo);
return contextInfo;
} else {
return {tiddler: null, template: null};
}
};
parentWalker(node);
// Walk back up the DOM chain to find a macro that can handle this method
var t;
for(t=nodeStack.length-1; t>=0; t--) {
var macro = macroStack[t].macro;
if(macro && this.macros[macro].hasOwnProperty(method)) {
var renderTitle = contextStack[t].tiddler,
renderTemplate = contextStack[t].template,
renderStep = macroStack[t].step;
if(typeof renderTemplate !== "string") {
renderTemplate = renderTitle;
}
var renderTiddler = this.getTiddler(renderTitle),
renderer = this.compileTiddler(renderTemplate,"text/html");
var ret = this.macros[macro][method](args,
nodeStack[t],
renderTiddler,
this,
renderer.renderSteps[renderStep].params(renderTiddler,renderer,this,utils));
if(ret) {
return true;
}
}
}
return false; // Didn't manage to dispatch it
};
exports.WikiStore = WikiStore; exports.WikiStore = WikiStore;
})(); })();

View File

@ -7,7 +7,8 @@ title: js/macros/story.js
/*jslint node: true */ /*jslint node: true */
"use strict"; "use strict";
var utils = require("../Utils.js"); var Tiddler = require("../Tiddler.js").Tiddler,
utils = require("../Utils.js");
// Parse the text of a story tiddler into an array of tiddler titles // Parse the text of a story tiddler into an array of tiddler titles
var parseStory = function(storyText) { var parseStory = function(storyText) {
@ -87,6 +88,15 @@ exports.macro = {
node.removeChild(currNode); node.removeChild(currNode);
currNode = nextNode; currNode = nextNode;
} }
},
navigateTo: function(args,node,tiddler,store,params) {
/*jslint jquery: true */
var storyTiddler = store.getTiddler(params.story);
store.addTiddler(new Tiddler(storyTiddler,{text: args.target + "\n" + storyTiddler.text}));
$("html,body").animate({
scrollTop: 0
}, 400);
return true;
} }
}; };