mirror of
https://github.com/Jermolene/TiddlyWiki5
synced 2024-12-28 11:00:27 +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:
parent
9206894e9a
commit
449f322a86
@ -28,14 +28,14 @@ Navigators.prototype.registerNavigator = function(name,nav) {
|
||||
|
||||
Navigators.prototype.install = function(selector,navname) {
|
||||
var nav = this.navigators[navname];
|
||||
this.document.addEventListener("click",function(e) {
|
||||
var el = e.target,
|
||||
this.document.addEventListener("click",function(event) {
|
||||
var el = event.target,
|
||||
matchesSelector = el.matchesSelector || el.mozMatchesSelector ||
|
||||
el.webkitMatchesSelector || el.oMatchesSelector || el.msMatchesSelector;
|
||||
if(matchesSelector && matchesSelector.call(el,selector)) {
|
||||
var r = nav.navigateTo(el.getAttribute("href"));
|
||||
var r = nav.navigateTo(el.getAttribute("href"),event);
|
||||
if(!r) {
|
||||
e.preventDefault();
|
||||
event.preventDefault();
|
||||
} else {
|
||||
el.setAttribute("target","_blank");
|
||||
}
|
||||
|
@ -15,15 +15,14 @@ var StoryNavigator = function(navigators) {
|
||||
this.navigators = navigators;
|
||||
};
|
||||
|
||||
StoryNavigator.prototype.navigateTo = function(title) {
|
||||
StoryNavigator.prototype.navigateTo = function(title,event) {
|
||||
var store = this.navigators.store,
|
||||
tiddler = store.getTiddler(title),
|
||||
storyTiddler = store.getTiddler("StoryTiddlers"); // This is a hack, obviously
|
||||
tiddler = store.getTiddler(title);
|
||||
if(tiddler) {
|
||||
store.addTiddler(new Tiddler(storyTiddler,{text: title + "\n" + storyTiddler.text}));
|
||||
$("html,body").animate({
|
||||
scrollTop: 0
|
||||
}, 400);
|
||||
store.invokeMacroMethod(event.target,"navigateTo",{
|
||||
event: event,
|
||||
target: title
|
||||
});
|
||||
return false;
|
||||
} else {
|
||||
return true;
|
||||
|
@ -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;
|
||||
|
||||
})();
|
||||
|
@ -7,7 +7,8 @@ title: js/macros/story.js
|
||||
/*jslint node: true */
|
||||
"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
|
||||
var parseStory = function(storyText) {
|
||||
@ -87,6 +88,15 @@ exports.macro = {
|
||||
node.removeChild(currNode);
|
||||
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;
|
||||
}
|
||||
};
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user