From 1f16ef6fa88b51d2dad7c8e57fcff014950a7442 Mon Sep 17 00:00:00 2001 From: Jermolene Date: Thu, 24 Jul 2014 15:49:09 +0100 Subject: [PATCH] Add support for permalink and permaview --- core/modules/startup/story.js | 56 ++++++++++++++++++++++++++--------- 1 file changed, 42 insertions(+), 14 deletions(-) diff --git a/core/modules/startup/story.js b/core/modules/startup/story.js index 3aff4ff85..a09db8365 100644 --- a/core/modules/startup/story.js +++ b/core/modules/startup/story.js @@ -35,7 +35,10 @@ exports.startup = function() { // Set up location hash update $tw.wiki.addEventListener("change",function(changes) { if($tw.utils.hop(changes,DEFAULT_STORY_TITLE) || $tw.utils.hop(changes,DEFAULT_HISTORY_TITLE)) { - updateLocationHash(); + updateLocationHash({ + updateAddressBar: $tw.wiki.getTiddlerText(CONFIG_UPDATE_ADDRESS_BAR,"permaview").trim(), + updateHistory: $tw.wiki.getTiddlerText(CONFIG_UPDATE_HISTORY,"no").trim() + }); } }); // Listen for changes to the browser location hash @@ -52,6 +55,22 @@ exports.startup = function() { storyList = $tw.wiki.filterTiddlers(storyFilter); $tw.wiki.addTiddler({title: DEFAULT_STORY_TITLE, text: "", list: storyList},$tw.wiki.getModificationFields()); }); + // Listen for the tw-permalink message + $tw.rootWidget.addEventListener("tw-permalink",function(event) { + updateLocationHash({ + updateAddressBar: "permalink", + updateHistory: $tw.wiki.getTiddlerText(CONFIG_UPDATE_HISTORY,"no").trim(), + targetTiddler: event.param || event.tiddlerTitle + }); + }); + // Listen for the tw-permaview message + $tw.rootWidget.addEventListener("tw-permaview",function(event) { + updateLocationHash({ + updateAddressBar: "permaview", + updateHistory: $tw.wiki.getTiddlerText(CONFIG_UPDATE_HISTORY,"no").trim(), + targetTiddler: event.param || event.tiddlerTitle + }); + }); } }; @@ -107,30 +126,39 @@ function openStartupTiddlers(options) { } } -function updateLocationHash() { - var updateAddressBar = $tw.wiki.getTiddlerText(CONFIG_UPDATE_ADDRESS_BAR,"permaview").trim(); - if(updateAddressBar !== "no") { +/* +options: See below +options.updateAddressBar: "permalink", "permaview" or "no" (defaults to "permaview") +options.updateHistory: "yes" or "no" (defaults to "no") +options.targetTiddler: optional title of target tiddler for permalink +*/ +function updateLocationHash(options) { + if(options.updateAddressBar !== "no") { // Get the story and the history stack var storyList = $tw.wiki.getTiddlerList(DEFAULT_STORY_TITLE), - historyList = $tw.wiki.getTiddlerData(DEFAULT_HISTORY_TITLE,[]); - var targetTiddler = ""; - // The target tiddler is the one at the top of the stack - if(historyList.length > 0) { - targetTiddler = historyList[historyList.length-1].title; - } - // Blank the target tiddler if it isn't present in the story - if(storyList.indexOf(targetTiddler) === -1) { + historyList = $tw.wiki.getTiddlerData(DEFAULT_HISTORY_TITLE,[]), targetTiddler = ""; + if(options.targetTiddler) { + targetTiddler = options.targetTiddler; + } else { + // The target tiddler is the one at the top of the stack + if(historyList.length > 0) { + targetTiddler = historyList[historyList.length-1].title; + } + // Blank the target tiddler if it isn't present in the story + if(storyList.indexOf(targetTiddler) === -1) { + targetTiddler = ""; + } } // Assemble the location hash - if(updateAddressBar === "permalink") { + if(options.updateAddressBar === "permalink") { $tw.locationHash = "#" + encodeURIComponent(targetTiddler) } else { $tw.locationHash = "#" + encodeURIComponent(targetTiddler) + ":" + encodeURIComponent($tw.utils.stringifyList(storyList)); } // Only change the location hash if we must, thus avoiding unnecessary onhashchange events if($tw.utils.getLocationHash() !== $tw.locationHash) { - if($tw.wiki.getTiddlerText(CONFIG_UPDATE_HISTORY,"no").trim() === "yes") { + if(options.updateHistory === "yes") { // Assign the location hash so that history is updated window.location.hash = $tw.locationHash; } else {