2014-05-03 20:23:51 +00:00
|
|
|
/*\
|
2014-05-05 14:25:51 +00:00
|
|
|
title: $:/core/modules/startup/story.js
|
2014-05-03 20:23:51 +00:00
|
|
|
type: application/javascript
|
|
|
|
module-type: startup
|
|
|
|
|
|
|
|
Load core modules
|
|
|
|
|
|
|
|
\*/
|
|
|
|
(function(){
|
|
|
|
|
|
|
|
/*jslint node: true, browser: true */
|
|
|
|
/*global $tw: false */
|
|
|
|
"use strict";
|
|
|
|
|
|
|
|
// Export name and synchronous status
|
2014-05-05 14:25:51 +00:00
|
|
|
exports.name = "story";
|
2014-05-03 20:23:51 +00:00
|
|
|
exports.after = ["startup"];
|
|
|
|
exports.synchronous = true;
|
|
|
|
|
|
|
|
// Default story and history lists
|
|
|
|
var DEFAULT_STORY_TITLE = "$:/StoryList";
|
|
|
|
var DEFAULT_HISTORY_TITLE = "$:/HistoryList";
|
|
|
|
|
|
|
|
// Default tiddlers
|
|
|
|
var DEFAULT_TIDDLERS_TITLE = "$:/DefaultTiddlers";
|
|
|
|
|
|
|
|
exports.startup = function() {
|
|
|
|
// Open startup tiddlers
|
|
|
|
openStartupTiddlers();
|
|
|
|
if($tw.browser) {
|
|
|
|
// 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();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
// Listen for changes to the browser location hash
|
|
|
|
window.addEventListener("hashchange",function() {
|
2014-05-05 18:21:57 +00:00
|
|
|
var hash = $tw.utils.getLocationHash();
|
|
|
|
if(hash !== $tw.locationHash) {
|
|
|
|
$tw.locationHash = hash;
|
2014-05-03 20:23:51 +00:00
|
|
|
openStartupTiddlers({defaultToCurrentStory: true});
|
|
|
|
}
|
|
|
|
},false)
|
2014-05-05 14:25:51 +00:00
|
|
|
// Listen for the tw-home message
|
|
|
|
$tw.rootWidget.addEventListener("tw-home",function(event) {
|
|
|
|
var storyFilter = $tw.wiki.getTiddlerText(DEFAULT_TIDDLERS_TITLE),
|
|
|
|
storyList = $tw.wiki.filterTiddlers(storyFilter);
|
|
|
|
$tw.wiki.addTiddler({title: DEFAULT_STORY_TITLE, text: "", list: storyList},$tw.wiki.getModificationFields());
|
|
|
|
});
|
2014-05-03 20:23:51 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
|
|
|
Process the location hash to open the specified tiddlers. Options:
|
|
|
|
defaultToCurrentStory: If true, the current story is retained as the default, instead of opening the default tiddlers
|
|
|
|
*/
|
|
|
|
function openStartupTiddlers(options) {
|
|
|
|
options = options || {};
|
|
|
|
// Decode the hash portion of our URL
|
|
|
|
var target,
|
|
|
|
storyFilter;
|
|
|
|
if($tw.locationHash.length > 1) {
|
|
|
|
var hash = $tw.locationHash.substr(1),
|
|
|
|
split = hash.indexOf(":");
|
|
|
|
if(split === -1) {
|
|
|
|
target = decodeURIComponent(hash.trim());
|
|
|
|
} else {
|
|
|
|
target = decodeURIComponent(hash.substr(0,split).trim());
|
|
|
|
storyFilter = decodeURIComponent(hash.substr(split + 1).trim());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// If a target tiddler was specified add it to the history stack
|
|
|
|
if(target && target !== "") {
|
|
|
|
// The target tiddler doesn't need double square brackets, but we'll silently remove them if they're present
|
|
|
|
if(target.indexOf("[[") === 0 && target.substr(-2) === "]]") {
|
|
|
|
target = target.substr(2,target.length - 4);
|
|
|
|
}
|
|
|
|
$tw.wiki.addToHistory(target);
|
|
|
|
}
|
|
|
|
// Use the story filter specified in the hash, or the default tiddlers
|
|
|
|
if(!storyFilter || storyFilter === "") {
|
|
|
|
if(options.defaultToCurrentStory) {
|
|
|
|
var currStoryList = $tw.wiki.getTiddlerList(DEFAULT_STORY_TITLE);
|
|
|
|
storyFilter = $tw.utils.stringifyList(currStoryList);
|
|
|
|
} else {
|
2014-05-05 14:25:51 +00:00
|
|
|
storyFilter = $tw.wiki.getTiddlerText(DEFAULT_TIDDLERS_TITLE);
|
2014-05-03 20:23:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
var storyList = $tw.wiki.filterTiddlers(storyFilter);
|
|
|
|
// If the target tiddler isn't included then splice it in at the top
|
|
|
|
if(target && storyList.indexOf(target) === -1) {
|
|
|
|
storyList.unshift(target);
|
|
|
|
}
|
|
|
|
// Save the story list
|
|
|
|
$tw.wiki.addTiddler({title: DEFAULT_STORY_TITLE, text: "", list: storyList},$tw.wiki.getModificationFields());
|
|
|
|
}
|
|
|
|
|
|
|
|
function updateLocationHash() {
|
2014-05-05 20:23:29 +00:00
|
|
|
// Get the story and the history stack
|
2014-05-03 20:23:51 +00:00
|
|
|
var storyList = $tw.wiki.getTiddlerList(DEFAULT_STORY_TITLE),
|
|
|
|
historyList = $tw.wiki.getTiddlerData(DEFAULT_HISTORY_TITLE,[]);
|
|
|
|
var targetTiddler = "";
|
2014-05-05 20:23:29 +00:00
|
|
|
// The target tiddler is the one at the top of the stack
|
2014-05-03 20:23:51 +00:00
|
|
|
if(historyList.length > 0) {
|
|
|
|
targetTiddler = historyList[historyList.length-1].title;
|
|
|
|
}
|
2014-05-05 20:23:29 +00:00
|
|
|
// Blank the target tiddler if it isn't present in the story
|
|
|
|
if(storyList.indexOf(targetTiddler) === -1) {
|
|
|
|
targetTiddler = "";
|
|
|
|
}
|
|
|
|
// Assemble the location hash
|
2014-05-03 20:23:51 +00:00
|
|
|
$tw.locationHash = "#" + encodeURIComponent(targetTiddler) + ":" + encodeURIComponent($tw.utils.stringifyList(storyList));
|
|
|
|
// Only change the location hash if we must, thus avoiding unnecessary onhashchange events
|
2014-05-05 18:21:57 +00:00
|
|
|
if($tw.utils.getLocationHash() !== $tw.locationHash) {
|
2014-05-05 20:51:54 +00:00
|
|
|
// We use replace so that browser history isn't affected
|
|
|
|
window.location.replace(window.location.toString().split("#")[0] + $tw.locationHash);
|
2014-05-03 20:23:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
})();
|