2012-01-13 16:50:11 +00:00
|
|
|
/*\
|
|
|
|
title: js/macros/story.js
|
|
|
|
|
|
|
|
\*/
|
|
|
|
(function(){
|
|
|
|
|
2012-02-02 18:00:42 +00:00
|
|
|
/*jslint node: true, jquery: true */
|
2012-01-13 16:50:11 +00:00
|
|
|
"use strict";
|
|
|
|
|
2012-02-02 15:13:50 +00:00
|
|
|
var Tiddler = require("../Tiddler.js").Tiddler,
|
|
|
|
utils = require("../Utils.js");
|
2012-01-13 16:50:11 +00:00
|
|
|
|
2012-02-01 12:37:29 +00:00
|
|
|
// Parse the text of a story tiddler into an array of tiddler titles
|
|
|
|
var parseStory = function(storyText) {
|
|
|
|
var storyLines = storyText.split("\n"),
|
|
|
|
tiddlers = [];
|
|
|
|
for(var t=0; t<storyLines.length; t++) {
|
|
|
|
var title = storyLines[t].trim();
|
|
|
|
if(title !== "") {
|
|
|
|
tiddlers.push(title);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return tiddlers;
|
|
|
|
};
|
|
|
|
|
|
|
|
// Search the children of a node looking for the required tiddler rendering
|
|
|
|
var searchTiddlerNode = function(node,renderTiddler,renderTemplate) {
|
|
|
|
while(node !== null) {
|
|
|
|
if(node.getAttribute && node.getAttribute("data-tw-render-tiddler") === renderTiddler) {
|
2012-02-01 16:12:49 +00:00
|
|
|
if(!renderTemplate || (renderTemplate && node.getAttribute("data-tw-render-template") == renderTemplate)) {
|
2012-02-01 12:37:29 +00:00
|
|
|
return node;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
node = node.nextSibling;
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
};
|
|
|
|
|
2012-01-13 16:50:11 +00:00
|
|
|
exports.macro = {
|
|
|
|
name: "story",
|
|
|
|
types: ["text/html","text/plain"],
|
|
|
|
params: {
|
2012-01-24 18:11:38 +00:00
|
|
|
story: {byName: "default", type: "tiddler", optional: false},
|
|
|
|
template: {byName: true, type: "tiddler", optional: true}
|
2012-01-13 16:50:11 +00:00
|
|
|
},
|
2012-02-02 18:00:42 +00:00
|
|
|
events: {
|
|
|
|
"tw-navigate": function(event,node,tiddler,store,params) {
|
|
|
|
var storyTiddler = store.getTiddler(params.story);
|
|
|
|
store.addTiddler(new Tiddler(storyTiddler,{text: event.navigateTo + "\n" + storyTiddler.text}));
|
|
|
|
$("html,body").animate({
|
|
|
|
scrollTop: 0
|
|
|
|
}, 400);
|
|
|
|
}
|
|
|
|
},
|
2012-01-30 20:04:47 +00:00
|
|
|
render: function(type,tiddler,store,params) {
|
2012-02-01 12:37:29 +00:00
|
|
|
var tiddlers = parseStory(store.getTiddlerText(params.story)),
|
2012-01-13 16:50:11 +00:00
|
|
|
output = [];
|
2012-02-01 12:37:29 +00:00
|
|
|
for(var t=0; t<tiddlers.length; t++) {
|
|
|
|
if(params.template) {
|
2012-02-01 16:12:49 +00:00
|
|
|
output.push(store.renderTiddler(type,tiddlers[t],params.template));
|
2012-02-01 12:37:29 +00:00
|
|
|
} else {
|
|
|
|
output.push(store.renderTiddler(type,tiddlers[t]));
|
2012-01-13 16:50:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return output.join("\n");
|
2012-02-01 12:37:29 +00:00
|
|
|
},
|
|
|
|
rerender: function(node,changes,type,tiddler,store,params) {
|
|
|
|
/*jslint browser: true */
|
|
|
|
// Get the tiddlers we're supposed to be displaying
|
|
|
|
var targetTiddlers = parseStory(store.getTiddlerText(params.story)),
|
|
|
|
currNode = node.firstChild,
|
|
|
|
nextNode;
|
|
|
|
for(var t=0; t<targetTiddlers.length; t++) {
|
|
|
|
// See if the node we want is already there
|
|
|
|
var tiddlerNode = searchTiddlerNode(currNode,targetTiddlers[t],params.template);
|
|
|
|
if(tiddlerNode === null) {
|
|
|
|
// If not, render the tiddler
|
|
|
|
var tmpNode = document.createElement("div");
|
2012-02-02 17:48:09 +00:00
|
|
|
store.renderTiddlerInNode(tmpNode,targetTiddlers[t],params.template);
|
2012-02-01 12:37:29 +00:00
|
|
|
tiddlerNode = tmpNode.firstChild;
|
|
|
|
node.insertBefore(tiddlerNode,currNode);
|
|
|
|
} else {
|
|
|
|
// Delete any nodes preceding the one we want
|
|
|
|
while(currNode !== tiddlerNode) {
|
|
|
|
nextNode = currNode.nextSibling;
|
|
|
|
node.removeChild(currNode);
|
|
|
|
currNode = nextNode;
|
|
|
|
}
|
|
|
|
// Refresh it
|
|
|
|
store.refreshDomNode(tiddlerNode,changes);
|
|
|
|
currNode = currNode.nextSibling;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Remove any unused nodes
|
|
|
|
while(currNode !== null) {
|
|
|
|
nextNode = currNode.nextSibling;
|
|
|
|
node.removeChild(currNode);
|
|
|
|
currNode = nextNode;
|
|
|
|
}
|
2012-01-13 16:50:11 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
})();
|
|
|
|
|