1
0
mirror of https://github.com/Jermolene/TiddlyWiki5 synced 2024-12-24 17:10:29 +00:00

Refactored story tiddler JSON handling

This commit is contained in:
Jeremy Ruston 2012-06-02 09:40:49 +01:00
parent 599d01caa9
commit a2958c5183

View File

@ -31,6 +31,18 @@ exports.info = {
events: ["tw-navigate","tw-EditTiddler","tw-SaveTiddler"] events: ["tw-navigate","tw-EditTiddler","tw-SaveTiddler"]
}; };
exports.getStory = function() {
var storyTiddler = this.wiki.getTiddler(this.params.story),
story = {tiddlers: []};
if(storyTiddler && $tw.utils.hop(storyTiddler.fields,"text")) {
return JSON.parse(storyTiddler.fields.text);
} else {
return {
tiddlers: []
};
}
};
exports.handleEvent = function(event) { exports.handleEvent = function(event) {
if(this.eventMap[event.type]) { if(this.eventMap[event.type]) {
this.eventMap[event.type].call(this,event); this.eventMap[event.type].call(this,event);
@ -42,13 +54,8 @@ exports.eventMap = {};
// Navigate to a specified tiddler // Navigate to a specified tiddler
exports.eventMap["tw-navigate"] = function(event) { exports.eventMap["tw-navigate"] = function(event) {
var template = this.params.defaultViewTemplate || "$:/templates/ViewTemplate", var template = this.params.defaultViewTemplate || "$:/templates/ViewTemplate",
storyTiddler = this.wiki.getTiddler(this.params.story), story = this.getStory(),
story = {tiddlers: []},
navTiddler,t,tiddler; navTiddler,t,tiddler;
// Get the story
if(storyTiddler && $tw.utils.hop(storyTiddler.fields,"text")) {
story = JSON.parse(storyTiddler.fields.text);
}
// See if the tiddler we want is already there // See if the tiddler we want is already there
for(t=0; t<story.tiddlers.length; t++) { for(t=0; t<story.tiddlers.length; t++) {
if(story.tiddlers[t].title === event.navigateTo) { if(story.tiddlers[t].title === event.navigateTo) {
@ -63,7 +70,7 @@ exports.eventMap["tw-navigate"] = function(event) {
} else { } else {
// Add the tiddler to the bottom of the story (subsequently there will be a refreshInDom() call which is when we'll actually do the navigation) // Add the tiddler to the bottom of the story (subsequently there will be a refreshInDom() call which is when we'll actually do the navigation)
story.tiddlers.unshift({title: event.navigateTo, template: template}); story.tiddlers.unshift({title: event.navigateTo, template: template});
this.wiki.addTiddler(new $tw.Tiddler(storyTiddler,{text: JSON.stringify(story)})); this.wiki.addTiddler(new $tw.Tiddler(this.wiki.getTiddler(this.params.story),{text: JSON.stringify(story)}));
// Record the details of the navigation for us to pick up in refreshInDom() // Record the details of the navigation for us to pick up in refreshInDom()
this.lastNavigationEvent = event; this.lastNavigationEvent = event;
} }
@ -76,11 +83,7 @@ exports.eventMap["tw-EditTiddler"] = function(event) {
var template, storyTiddler, story, storyRecord, tiddler, t; var template, storyTiddler, story, storyRecord, tiddler, t;
// Put the specified tiddler into edit mode // Put the specified tiddler into edit mode
template = this.params.defaultEditTemplate || "$:/templates/EditTemplate"; template = this.params.defaultEditTemplate || "$:/templates/EditTemplate";
storyTiddler = this.wiki.getTiddler(this.params.story); story = this.getStory();
story = {tiddlers: []};
if(storyTiddler && $tw.utils.hop(storyTiddler.fields,"text")) {
story = JSON.parse(storyTiddler.fields.text);
}
for(t=0; t<story.tiddlers.length; t++) { for(t=0; t<story.tiddlers.length; t++) {
storyRecord = story.tiddlers[t]; storyRecord = story.tiddlers[t];
if(storyRecord.title === event.tiddlerTitle && storyRecord.template !== template) { if(storyRecord.title === event.tiddlerTitle && storyRecord.template !== template) {
@ -99,7 +102,7 @@ exports.eventMap["tw-EditTiddler"] = function(event) {
})); }));
} }
} }
this.wiki.addTiddler(new $tw.Tiddler(storyTiddler,{text: JSON.stringify(story)})); this.wiki.addTiddler(new $tw.Tiddler(this.wiki.getTiddler(this.params.story),{text: JSON.stringify(story)}));
event.stopPropagation(); event.stopPropagation();
return false; return false;
}; };
@ -108,12 +111,8 @@ exports.eventMap["tw-EditTiddler"] = function(event) {
exports.eventMap["tw-SaveTiddler"] = function(event) { exports.eventMap["tw-SaveTiddler"] = function(event) {
var template, storyTiddler, story, storyRecord, tiddler, storyTiddlerModified, t; var template, storyTiddler, story, storyRecord, tiddler, storyTiddlerModified, t;
template = this.params.defaultEditTemplate || "$:/templates/ViewTemplate"; template = this.params.defaultEditTemplate || "$:/templates/ViewTemplate";
storyTiddler = this.wiki.getTiddler(this.params.story); story = this.getStory();
story = {tiddlers: []};
storyTiddlerModified = false; storyTiddlerModified = false;
if(storyTiddler && $tw.utils.hop(storyTiddler.fields,"text")) {
story = JSON.parse(storyTiddler.fields.text);
}
for(t=0; t<story.tiddlers.length; t++) { for(t=0; t<story.tiddlers.length; t++) {
storyRecord = story.tiddlers[t]; storyRecord = story.tiddlers[t];
if(storyRecord.title === event.tiddlerTitle && storyRecord.template !== template) { if(storyRecord.title === event.tiddlerTitle && storyRecord.template !== template) {
@ -138,18 +137,18 @@ exports.eventMap["tw-SaveTiddler"] = function(event) {
} }
} }
if(!storyTiddlerModified) { if(!storyTiddlerModified) {
this.wiki.addTiddler(new $tw.Tiddler(storyTiddler,{text: JSON.stringify(story)})); this.wiki.addTiddler(new $tw.Tiddler(this.wiki.getTiddler(this.params.story),{text: JSON.stringify(story)}));
} }
event.stopPropagation(); event.stopPropagation();
return false; return false;
}; };
exports.executeMacro = function() { exports.executeMacro = function() {
var storyJson = JSON.parse(this.wiki.getTiddlerText(this.params.story)), var story = this.getStory(),
storyNode = $tw.Tree.Element("div",{},[]); storyNode = $tw.Tree.Element("div",{},[]);
for(var t=0; t<storyJson.tiddlers.length; t++) { for(var t=0; t<story.tiddlers.length; t++) {
var m = $tw.Tree.Macro("tiddler",{ var m = $tw.Tree.Macro("tiddler",{
srcParams: {target: storyJson.tiddlers[t].title,template: storyJson.tiddlers[t].template}, srcParams: {target: story.tiddlers[t].title,template: story.tiddlers[t].template},
wiki: this.wiki wiki: this.wiki
}); });
m.execute(this.parents,this.tiddlerTitle); m.execute(this.parents,this.tiddlerTitle);
@ -178,7 +177,7 @@ exports.refreshInDom = function(changes) {
if(this.dependencies.hasChanged(changes,this.tiddlerTitle)) { if(this.dependencies.hasChanged(changes,this.tiddlerTitle)) {
// Get the tiddlers we're supposed to be displaying // Get the tiddlers we're supposed to be displaying
var self = this, var self = this,
story = JSON.parse(this.wiki.getTiddlerText(this.params.story)), story = this.getStory(),
template = this.params.template, template = this.params.template,
n,domNode, n,domNode,
findTiddler = function (childIndex,tiddlerTitle,templateTitle) { findTiddler = function (childIndex,tiddlerTitle,templateTitle) {