/*\ title: $:/core/modules/macros/story/story.js type: application/javascript module-type: macro Displays a sequence of tiddlers defined in a JSON structure: { tiddlers: [ {title: , template: } ] } The storyview is a plugin that extends the story macro to implement different navigation experiences. \*/ (function(){ /*jslint node: true, browser: true */ /*global $tw: false */ "use strict"; exports.info = { name: "story", params: { story: {byName: "default", type: "tiddler"}, defaultViewTemplate: {byName: true, type: "tiddler"}, defaultEditTemplate: {byName: true, type: "tiddler"}, storyviewTiddler: {byName: true, type: "tiddler"}, storyview: {byName: true, type: "text"} } }; 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) { if(this.eventMap[event.type]) { this.eventMap[event.type].call(this,event); } }; /* Return the index of the story element that contains the specified tree node. Returns story.length if not found */ exports.findStoryElementContainingNode = function(node) { // Get the DOM node contained by the target node while(node && !node.domNode) { node = node.child; } // Step through the story elements var slot = this.storyNode.children.length; for(var t=0; t=0; t--) { if(story.tiddlers[t].title === event.tiddlerTitle) { storyElement = this.storyNode.children[t]; // Invoke the storyview to animate the closure if(this.storyview && this.storyview.close) { if(!this.storyview.close(storyElement,event)) { // Only delete the DOM element if the storyview.close() returned false storyElement.domNode.parentNode.removeChild(storyElement.domNode); } } // Remove the story element node this.storyNode.children.splice(t,1); // Remove the record in the story story.tiddlers.splice(t,1); } } this.wiki.addTiddler(new $tw.Tiddler(this.wiki.getTiddler(this.params.story),{text: JSON.stringify(story)})); event.stopPropagation(); return false; }; exports.executeMacro = function() { // Create the story frame var story = this.getStory(); this.contentNode = $tw.Tree.Element("div",{"class": "tw-story-content"},this.content); this.contentNode.execute(this.parents,this.tiddlerTitle); this.storyNode = $tw.Tree.Element("div",{"class": "tw-story-frame"},[]); // Create each story element for(var t=0; t t) { // First delete the DOM nodes for(n=t; n story.tiddlers.length) { for(t=story.tiddlers.length; t