1
0
mirror of https://github.com/Jermolene/TiddlyWiki5 synced 2024-12-26 10:00:34 +00:00

Add support for permalink and permaview

This commit is contained in:
Jermolene 2014-07-24 15:49:09 +01:00
parent eecb9126cd
commit 1f16ef6fa8

View File

@ -35,7 +35,10 @@ exports.startup = function() {
// Set up location hash update // Set up location hash update
$tw.wiki.addEventListener("change",function(changes) { $tw.wiki.addEventListener("change",function(changes) {
if($tw.utils.hop(changes,DEFAULT_STORY_TITLE) || $tw.utils.hop(changes,DEFAULT_HISTORY_TITLE)) { 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 // Listen for changes to the browser location hash
@ -52,6 +55,22 @@ exports.startup = function() {
storyList = $tw.wiki.filterTiddlers(storyFilter); storyList = $tw.wiki.filterTiddlers(storyFilter);
$tw.wiki.addTiddler({title: DEFAULT_STORY_TITLE, text: "", list: storyList},$tw.wiki.getModificationFields()); $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,13 +126,21 @@ function openStartupTiddlers(options) {
} }
} }
function updateLocationHash() { /*
var updateAddressBar = $tw.wiki.getTiddlerText(CONFIG_UPDATE_ADDRESS_BAR,"permaview").trim(); options: See below
if(updateAddressBar !== "no") { 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 // Get the story and the history stack
var storyList = $tw.wiki.getTiddlerList(DEFAULT_STORY_TITLE), var storyList = $tw.wiki.getTiddlerList(DEFAULT_STORY_TITLE),
historyList = $tw.wiki.getTiddlerData(DEFAULT_HISTORY_TITLE,[]); historyList = $tw.wiki.getTiddlerData(DEFAULT_HISTORY_TITLE,[]),
var targetTiddler = ""; targetTiddler = "";
if(options.targetTiddler) {
targetTiddler = options.targetTiddler;
} else {
// The target tiddler is the one at the top of the stack // The target tiddler is the one at the top of the stack
if(historyList.length > 0) { if(historyList.length > 0) {
targetTiddler = historyList[historyList.length-1].title; targetTiddler = historyList[historyList.length-1].title;
@ -122,15 +149,16 @@ function updateLocationHash() {
if(storyList.indexOf(targetTiddler) === -1) { if(storyList.indexOf(targetTiddler) === -1) {
targetTiddler = ""; targetTiddler = "";
} }
}
// Assemble the location hash // Assemble the location hash
if(updateAddressBar === "permalink") { if(options.updateAddressBar === "permalink") {
$tw.locationHash = "#" + encodeURIComponent(targetTiddler) $tw.locationHash = "#" + encodeURIComponent(targetTiddler)
} else { } else {
$tw.locationHash = "#" + encodeURIComponent(targetTiddler) + ":" + encodeURIComponent($tw.utils.stringifyList(storyList)); $tw.locationHash = "#" + encodeURIComponent(targetTiddler) + ":" + encodeURIComponent($tw.utils.stringifyList(storyList));
} }
// Only change the location hash if we must, thus avoiding unnecessary onhashchange events // Only change the location hash if we must, thus avoiding unnecessary onhashchange events
if($tw.utils.getLocationHash() !== $tw.locationHash) { 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 // Assign the location hash so that history is updated
window.location.hash = $tw.locationHash; window.location.hash = $tw.locationHash;
} else { } else {