/*\ title: $:/core/modules/macros/navigator.js type: application/javascript module-type: macro Traps navigation events to update a story tiddler and history tiddler. Can also optionally capture navigation target in a specified text reference. \*/ (function(){ /*jslint node: true, browser: true */ /*global $tw: false */ "use strict"; exports.info = { name: "navigator", params: { story: {byName: "default", type: "text"}, // Actually a tiddler, but we don't want it to be a dependency history: {byName: "default", type: "text"}, // Actually a tiddler, but we don't want it to be a dependency set: {byName: true, type: "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); } }; 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)})); } }; 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); } }; 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)})); } }; exports.handleEvent = function(event) { if(this.eventMap[event.type]) { this.eventMap[event.type].call(this,event); } }; exports.eventMap = {}; // Navigate to a specified tiddler exports.eventMap["tw-navigate"] = function(event) { // Update the story tiddler if specified if(this.hasParameter("story")) { this.getStory(); var t,tiddler,slot; // See if the tiddler is already there for(t=0; t=0; t--) { if(this.story.tiddlers[t].title === event.tiddlerTitle) { this.story.tiddlers.splice(t,1); } } this.saveStory(); } event.stopPropagation(); return false; }; exports.executeMacro = function() { var attributes = {}; if(this.classes) { attributes["class"] = this.classes.slice(0); } for(var t=0; t