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-17 14:11:25 +00:00
|
|
|
Dependencies = require("../Dependencies.js").Dependencies,
|
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",
|
|
|
|
params: {
|
2012-02-17 17:32:32 +00:00
|
|
|
story: {byName: "default", type: "tiddler"},
|
|
|
|
template: {byName: true, type: "tiddler"}
|
2012-01-13 16:50:11 +00:00
|
|
|
},
|
2012-02-02 18:00:42 +00:00
|
|
|
events: {
|
2012-02-21 21:57:30 +00:00
|
|
|
"tw-navigate": function(event) {
|
|
|
|
var storyTiddler = this.store.getTiddler(this.params.story);
|
|
|
|
this.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-03-15 09:32:53 +00:00
|
|
|
event.stopPropagation();
|
2012-02-09 13:36:21 +00:00
|
|
|
return false;
|
2012-02-02 18:00:42 +00:00
|
|
|
}
|
|
|
|
},
|
2012-02-21 21:57:30 +00:00
|
|
|
execute: function() {
|
|
|
|
var tiddlers = parseStory(this.store.getTiddlerText(this.params.story)),
|
2012-02-16 20:38:10 +00:00
|
|
|
content = [];
|
2012-02-01 12:37:29 +00:00
|
|
|
for(var t=0; t<tiddlers.length; t++) {
|
2012-02-20 18:05:10 +00:00
|
|
|
var m = Renderer.MacroNode("tiddler",
|
2012-02-21 21:57:30 +00:00
|
|
|
{target: tiddlers[t],template: this.params.template},
|
2012-02-20 18:05:10 +00:00
|
|
|
null,
|
2012-02-21 21:57:30 +00:00
|
|
|
this.store);
|
|
|
|
m.execute(this.parents,this.store.getTiddler(this.tiddlerTitle));
|
2012-02-16 20:38:10 +00:00
|
|
|
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-22 23:27:21 +00:00
|
|
|
refreshInDom: function(changes) {
|
2012-02-01 12:37:29 +00:00
|
|
|
/*jslint browser: true */
|
|
|
|
// Get the tiddlers we're supposed to be displaying
|
2012-02-21 21:57:30 +00:00
|
|
|
var self = this,
|
|
|
|
targetTiddlers = parseStory(this.store.getTiddlerText(this.params.story)),
|
|
|
|
template = this.params.template,
|
2012-02-16 20:38:10 +00:00
|
|
|
t,n,domNode,
|
|
|
|
findTiddler = function (childIndex,tiddlerTitle,templateTitle) {
|
2012-02-21 21:57:30 +00:00
|
|
|
while(childIndex < self.content.length) {
|
|
|
|
var params = self.content[childIndex].params;
|
2012-02-16 20:38:10 +00:00
|
|
|
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-20 18:05:10 +00:00
|
|
|
var m = Renderer.MacroNode("tiddler",
|
|
|
|
{target: targetTiddlers[t],template: template},
|
|
|
|
null,
|
2012-02-21 21:57:30 +00:00
|
|
|
this.store);
|
|
|
|
m.execute(this.parents,this.store.getTiddler(targetTiddlers[t]));
|
|
|
|
m.renderInDom(this.domNode,this.domNode.childNodes[t]);
|
|
|
|
this.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++) {
|
2012-02-21 21:57:30 +00:00
|
|
|
domNode = this.content[n].domNode;
|
2012-02-16 20:38:10 +00:00
|
|
|
domNode.parentNode.removeChild(domNode);
|
|
|
|
}
|
|
|
|
// Then delete the actual renderer nodes
|
2012-02-21 21:57:30 +00:00
|
|
|
this.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
|
2012-02-21 21:57:30 +00:00
|
|
|
this.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
|
2012-02-21 21:57:30 +00:00
|
|
|
if(this.content.length > targetTiddlers.length) {
|
|
|
|
for(t=targetTiddlers.length; t<this.content.length; t++) {
|
|
|
|
domNode = this.content[t].domNode;
|
2012-02-16 20:38:10 +00:00
|
|
|
domNode.parentNode.removeChild(domNode);
|
|
|
|
}
|
2012-02-21 21:57:30 +00:00
|
|
|
this.content.splice(targetTiddlers.length,this.content.length-targetTiddlers.length);
|
2012-02-01 12:37:29 +00:00
|
|
|
}
|
2012-01-13 16:50:11 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
})();
|