From 8444961cd2a4cf8fff71b6f995e59f882ab55f71 Mon Sep 17 00:00:00 2001 From: Jermolene Date: Sat, 1 Aug 2015 18:09:00 +0100 Subject: [PATCH] Introduce Story object A lightweight object to encapsulate manipulation of the story and history lists --- core/modules/story.js | 136 ++++++++++++++++++++++++++++++++++++++++++ core/modules/wiki.js | 10 +--- 2 files changed, 138 insertions(+), 8 deletions(-) create mode 100644 core/modules/story.js diff --git a/core/modules/story.js b/core/modules/story.js new file mode 100644 index 000000000..69cab2529 --- /dev/null +++ b/core/modules/story.js @@ -0,0 +1,136 @@ +/*\ +title: $:/core/modules/story.js +type: application/javascript +module-type: global + +Lightweight object for managing interactions with the story and history lists. + +\*/ +(function(){ + +/*jslint node: true, browser: true */ +/*global $tw: false */ +"use strict"; + +/* +Construct Story object with options: +wiki: reference to wiki object to use to resolve tiddler titles +storyTitle: title of story list tiddler +historyTitle: title of history list tiddler +*/ +function Story(options) { + options = options || {}; + this.wiki = options.wiki || $tw.wiki; + this.storyTitle = options.storyTitle || "$:/StoryList"; + this.historyTitle = options.historyTitle || "$:/HistoryList"; +}; + +Story.prototype.navigateTiddler = function(navigateTo,navigateFromTitle,navigateFromClientRect) { + this.addToStory(navigateTo,navigateFromTitle); + this.addToHistory(navigateTo,navigateFromClientRect); +}; + +Story.prototype.getStoryList = function() { + return this.wiki.getTiddlerList(this.storyTitle) || []; +}; + +Story.prototype.addToStory = function(navigateTo,navigateFromTitle,options) { + options = options || {}; + var storyList = this.getStoryList(); + // See if the tiddler is already there + var slot = storyList.indexOf(navigateTo); + // Quit if it already exists in the story river + if(slot >= 0) { + return; + } + // First we try to find the position of the story element we navigated from + var fromIndex = storyList.indexOf(navigateFromTitle); + if(fromIndex >= 0) { + // The tiddler is added from inside the river + // Determine where to insert the tiddler; Fallback is "below" + switch(options.openLinkFromInsideRiver) { + case "top": + slot = 0; + break; + case "bottom": + slot = storyList.length; + break; + case "above": + slot = fromIndex; + break; + case "below": // Intentional fall-through + default: + slot = fromIndex + 1; + break; + } + } else { + // The tiddler is opened from outside the river. Determine where to insert the tiddler; default is "top" + if(options.openLinkFromOutsideRiver === "bottom") { + // Insert at bottom + slot = storyList.length; + } else { + // Insert at top + slot = 0; + } + } + // Add the tiddler + storyList.splice(slot,0,navigateTo); + // Save the story + this.saveStoryList(storyList); +}; + +Story.prototype.saveStoryList = function(storyList) { + var storyTiddler = this.wiki.getTiddler(this.storyTitle); + this.wiki.addTiddler(new $tw.Tiddler( + {title: this.storyTitle}, + storyTiddler, + {list: storyList} + )); +}; + +Story.prototype.addToHistory = function(navigateTo,navigateFromClientRect) { + var titles = $tw.utils.isArray(navigateTo) ? navigateTo : [navigateTo]; + // Add a new record to the top of the history stack + var historyList = this.wiki.getTiddlerData(this.historyTitle,[]); + $tw.utils.each(titles,function(title) { + historyList.push({title: title, fromPageRect: navigateFromClientRect}); + }); + this.wiki.setTiddlerData(this.historyTitle,historyList,{"current-tiddler": titles[titles.length-1]}); +}; + +Story.prototype.storyCloseTiddler = function(targetTitle) { +// TBD +}; + +Story.prototype.storyCloseAllTiddlers = function() { +// TBD +}; + +Story.prototype.storyCloseOtherTiddlers = function(targetTitle) { +// TBD +}; + +Story.prototype.storyEditTiddler = function(targetTitle) { +// TBD +}; + +Story.prototype.storyDeleteTiddler = function(targetTitle) { +// TBD +}; + +Story.prototype.storySaveTiddler = function(targetTitle) { +// TBD +}; + +Story.prototype.storyCancelTiddler = function(targetTitle) { +// TBD +}; + +Story.prototype.storyNewTiddler = function(targetTitle) { +// TBD +}; + +exports.Story = Story; + + +})(); diff --git a/core/modules/wiki.js b/core/modules/wiki.js index ff9d1c360..234ba26d8 100755 --- a/core/modules/wiki.js +++ b/core/modules/wiki.js @@ -1175,14 +1175,8 @@ fromPageRect: page coordinates of the origin of the navigation historyTitle: title of history tiddler (defaults to $:/HistoryList) */ exports.addToHistory = function(title,fromPageRect,historyTitle) { - historyTitle = historyTitle || "$:/HistoryList"; - var titles = $tw.utils.isArray(title) ? title : [title]; - // Add a new record to the top of the history stack - var historyList = this.getTiddlerData(historyTitle,[]); - $tw.utils.each(titles,function(title) { - historyList.push({title: title, fromPageRect: fromPageRect}); - }); - this.setTiddlerData(historyTitle,historyList,{"current-tiddler": titles[titles.length-1]}); + var story = new $tw.Story({wiki: this, historyTitle: historyTitle}); + story.addToHistory(title,fromPageRect); }; /*