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,
|
2012-02-16 20:38:10 +00:00
|
|
|
Renderer = require("../Renderer.js").Renderer,
|
2012-02-02 15:13:50 +00:00
|
|
|
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;
|
|
|
|
};
|
|
|
|
|
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: {
|
2012-02-16 20:38:10 +00:00
|
|
|
"tw-navigate": function(event,macroNode) {
|
|
|
|
var storyTiddler = macroNode.store.getTiddler(macroNode.params.story);
|
|
|
|
macroNode.store.addTiddler(new Tiddler(storyTiddler,{text: event.navigateTo + "\n" + storyTiddler.text}));
|
2012-02-02 18:00:42 +00:00
|
|
|
$("html,body").animate({
|
|
|
|
scrollTop: 0
|
|
|
|
}, 400);
|
2012-02-09 13:36:21 +00:00
|
|
|
event.preventDefault();
|
|
|
|
return false;
|
2012-02-02 18:00:42 +00:00
|
|
|
}
|
|
|
|
},
|
2012-02-16 20:38:10 +00:00
|
|
|
execute: function(macroNode,tiddler,store) {
|
|
|
|
var tiddlers = parseStory(store.getTiddlerText(macroNode.params.story)),
|
|
|
|
content = [];
|
2012-02-01 12:37:29 +00:00
|
|
|
for(var t=0; t<tiddlers.length; t++) {
|
2012-02-16 20:38:10 +00:00
|
|
|
var paramFn = {target: tiddlers[t],template: macroNode.params.template},
|
|
|
|
dependencies = {include: {}};
|
|
|
|
dependencies.include[tiddlers[t]] = 1;
|
|
|
|
if(macroNode.params.template) {
|
|
|
|
dependencies.include[macroNode.params.template] = 1;
|
2012-01-13 16:50:11 +00:00
|
|
|
}
|
2012-02-16 20:38:10 +00:00
|
|
|
var m = Renderer.MacroNode("tiddler",paramFn,null,dependencies,store);
|
|
|
|
m.execute(tiddler);
|
|
|
|
content.push(m);
|
2012-01-13 16:50:11 +00:00
|
|
|
}
|
2012-02-16 20:38:10 +00:00
|
|
|
return content;
|
2012-02-01 12:37:29 +00:00
|
|
|
},
|
2012-02-17 12:36:08 +00:00
|
|
|
refresh: function(changes,macroNode,tiddler,store) {
|
2012-02-01 12:37:29 +00:00
|
|
|
/*jslint browser: true */
|
|
|
|
// Get the tiddlers we're supposed to be displaying
|
2012-02-16 20:38:10 +00:00
|
|
|
var targetTiddlers = parseStory(store.getTiddlerText(macroNode.params.story)),
|
|
|
|
template = macroNode.params.template,
|
|
|
|
t,n,domNode,
|
|
|
|
findTiddler = function (childIndex,tiddlerTitle,templateTitle) {
|
|
|
|
while(childIndex < macroNode.content.length) {
|
|
|
|
var params = macroNode.content[childIndex].params;
|
|
|
|
if(params.target === tiddlerTitle) {
|
|
|
|
if(!templateTitle || params.template === templateTitle) {
|
|
|
|
return childIndex;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
childIndex++;
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
};
|
|
|
|
for(t=0; t<targetTiddlers.length; t++) {
|
2012-02-01 12:37:29 +00:00
|
|
|
// See if the node we want is already there
|
2012-02-16 20:38:10 +00:00
|
|
|
var tiddlerNode = findTiddler(t,targetTiddlers[t],template);
|
2012-02-01 12:37:29 +00:00
|
|
|
if(tiddlerNode === null) {
|
|
|
|
// If not, render the tiddler
|
2012-02-16 20:38:10 +00:00
|
|
|
var paramFn = {target: targetTiddlers[t],template: template},
|
|
|
|
dependencies = {include: {}};
|
|
|
|
dependencies.include[targetTiddlers[t]] = 1;
|
|
|
|
if(template) {
|
|
|
|
dependencies.include[template] = 1;
|
|
|
|
}
|
|
|
|
var m = Renderer.MacroNode("tiddler",paramFn,null,dependencies,store);
|
|
|
|
m.execute(store.getTiddler(targetTiddlers[t]));
|
|
|
|
m.renderInDom(macroNode.domNode,macroNode.domNode.childNodes[t]);
|
|
|
|
macroNode.content.splice(t,0,m);
|
2012-02-01 12:37:29 +00:00
|
|
|
} else {
|
|
|
|
// Delete any nodes preceding the one we want
|
2012-02-16 20:38:10 +00:00
|
|
|
if(tiddlerNode > t) {
|
|
|
|
// First delete the DOM nodes
|
|
|
|
for(n=t; n<tiddlerNode; n++) {
|
|
|
|
domNode = macroNode.content[n].domNode;
|
|
|
|
domNode.parentNode.removeChild(domNode);
|
|
|
|
}
|
|
|
|
// Then delete the actual renderer nodes
|
|
|
|
macroNode.content.splice(t,tiddlerNode-t);
|
2012-02-01 12:37:29 +00:00
|
|
|
}
|
2012-02-17 12:36:08 +00:00
|
|
|
// Refresh the DOM node we're reusing
|
|
|
|
macroNode.content[t].refreshInDom(changes);
|
2012-02-01 12:37:29 +00:00
|
|
|
}
|
|
|
|
}
|
2012-02-16 20:38:10 +00:00
|
|
|
// Remove any left over nodes
|
|
|
|
if(macroNode.content.length > targetTiddlers.length) {
|
|
|
|
for(t=targetTiddlers.length; t<macroNode.content.length; t++) {
|
|
|
|
domNode = macroNode.content[t].domNode;
|
|
|
|
domNode.parentNode.removeChild(domNode);
|
|
|
|
}
|
|
|
|
macroNode.content.splice(targetTiddlers.length,macroNode.content.length-targetTiddlers.length);
|
2012-02-01 12:37:29 +00:00
|
|
|
}
|
2012-01-13 16:50:11 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
})();
|