From a2caf46b1e292a97fecb0bbb7530d1e9d599c642 Mon Sep 17 00:00:00 2001 From: Jeremy Ruston Date: Sun, 22 Jul 2012 22:03:06 +0100 Subject: [PATCH] Added new wiki methods for reading and writing a toddler as data --- core/modules/macros/navigator.js | 16 ++++----------- core/modules/macros/story/story.js | 14 ++----------- core/modules/startup.js | 4 ++-- core/modules/wiki.js | 32 +++++++++++++++++++++++++++++- 4 files changed, 39 insertions(+), 27 deletions(-) diff --git a/core/modules/macros/navigator.js b/core/modules/macros/navigator.js index f4b866491..e29d221af 100644 --- a/core/modules/macros/navigator.js +++ b/core/modules/macros/navigator.js @@ -22,30 +22,22 @@ exports.info = { }; exports.getStory = function() { - var storyTiddler = this.wiki.getTiddler(this.params.story); - this.story = {tiddlers: []}; - if(storyTiddler && $tw.utils.hop(storyTiddler.fields,"text")) { - this.story = JSON.parse(storyTiddler.fields.text); - } + this.story = this.wiki.getTiddlerData(this.params.story,{tiddlers: []}); }; exports.saveStory = function() { if(this.hasParameter("story")) { - this.wiki.addTiddler(new $tw.Tiddler(this.wiki.getTiddler(this.params.story),{title: this.params.story, text: JSON.stringify(this.story)})); + this.wiki.setTiddlerData(this.params.story,this.story); } }; exports.getHistory = function() { - var historyTiddler = this.wiki.getTiddler(this.params.history); - this.history = {stack: []}; - if(historyTiddler && $tw.utils.hop(historyTiddler.fields,"text")) { - this.history = JSON.parse(historyTiddler.fields.text); - } + this.history = this.wiki.getTiddlerData(this.params.history,{stack: []}); }; exports.saveHistory = function() { if(this.hasParameter("history")) { - this.wiki.addTiddler(new $tw.Tiddler(this.wiki.getTiddler(this.params.history),{title: this.params.history, text: JSON.stringify(this.history)})); + this.wiki.setTiddlerData(this.params.history,this.history); } }; diff --git a/core/modules/macros/story/story.js b/core/modules/macros/story/story.js index c179230f6..d8a31bfa1 100644 --- a/core/modules/macros/story/story.js +++ b/core/modules/macros/story/story.js @@ -52,21 +52,11 @@ exports.info = { Get the data from the JSON story tiddler */ exports.getStory = function() { - var storyTiddler = this.wiki.getTiddler(this.params.story); - this.story = { - tiddlers: [] - }; - if(storyTiddler && $tw.utils.hop(storyTiddler.fields,"text")) { - this.story = JSON.parse(storyTiddler.fields.text); - } + this.story = this.wiki.getTiddlerData(this.params.story,{tiddlers: []}); }; exports.getHistory = function() { - var historyTiddler = this.wiki.getTiddler(this.params.history); - this.history = {stack: []}; - if(historyTiddler && $tw.utils.hop(historyTiddler.fields,"text")) { - this.history = JSON.parse(historyTiddler.fields.text); - } + this.history = this.wiki.getTiddlerData(this.params.history,{stack: []}); }; exports.getViewTemplate = function() { diff --git a/core/modules/startup.js b/core/modules/startup.js index e6daa234f..9a15d3dd6 100644 --- a/core/modules/startup.js +++ b/core/modules/startup.js @@ -75,8 +75,8 @@ exports.startup = function() { story.tiddlers[t] = {title: defaultTiddlers[t]}; history.stack[defaultTiddlers.length - t - 1] = {title: defaultTiddlers[t], fromTitle: defaultTiddlers[t+1]}; } - $tw.wiki.addTiddler(new $tw.Tiddler({title: storyTitle,text: JSON.stringify(story)})); - $tw.wiki.addTiddler(new $tw.Tiddler({title: historyTitle,text: JSON.stringify(history)})); + $tw.wiki.addTiddler(new $tw.Tiddler({title: storyTitle,type: "application/json", text: JSON.stringify(story)})); + $tw.wiki.addTiddler(new $tw.Tiddler({title: historyTitle,type: "application/json", text: JSON.stringify(history)})); // If we're being viewed on a data: URI then give instructions for how to save if(document.location.protocol === "data:") { var event = document.createEvent("Event"); diff --git a/core/modules/wiki.js b/core/modules/wiki.js index b92ec8404..2917a18de 100644 --- a/core/modules/wiki.js +++ b/core/modules/wiki.js @@ -317,6 +317,36 @@ exports.getTiddlersWithTag = function(tag) { return titles; }; +/* +Get a tiddlers content as a JavaScript object. How this is done depends on the type of the tiddler: + +application/json: the tiddler JSON is parsed into an object + +Other types currently just return null. +*/ +exports.getTiddlerData = function(title,defaultData) { + var tiddler = this.tiddlers[title], + data; + if(tiddler && tiddler.fields.text && tiddler.fields.type === "application/json") { + // JSON tiddler + try { + data = JSON.parse(tiddler.fields.text); + } catch(ex) { + return defaultData; + } + return data; + } + return defaultData; +}; + +/* +Set a tiddlers content to a JavaScript object. Currently this is done by setting the tiddler's type to "application/json" and setting the text to the JSON text of the data. +*/ +exports.setTiddlerData = function(title,data) { + var tiddler = this.getTiddler(title); + this.addTiddler(new $tw.Tiddler(tiddler,{title: title, type: "application/json", text: JSON.stringify(data)})); +}; + // Return the named cache object for a tiddler. If the cache doesn't exist then the initializer function is invoked to create it exports.getCacheForTiddler = function(title,cacheName,initializer) { this.caches = this.caches || {}; @@ -398,7 +428,7 @@ Parse text in a specified format and render it into another format outputType: content type for the output textType: content type of the input text text: input text - options: see below + options: see wiki.parseText() Options are: defaultType: Default MIME type to use if the specified one is unknown */