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";
|
|
|
|
|
2014-05-13 13:16:58 +00:00
|
|
|
// Config
|
|
|
|
var CONFIG_UPDATE_ADDRESS_BAR = "$:/config/Navigation/UpdateAddressBar"; // Can be "no", "permalink", "permaview"
|
|
|
|
var CONFIG_UPDATE_HISTORY = "$:/config/Navigation/UpdateHistory"; // Can be "yes" or "no"
|
2018-08-16 18:39:18 +00:00
|
|
|
var CONFIG_PERMALINKVIEW_COPY_TO_CLIPBOARD = "$:/config/Navigation/Permalinkview/CopyToClipboard"; // Can be "yes" (default) or "no"
|
|
|
|
var CONFIG_PERMALINKVIEW_UPDATE_ADDRESS_BAR = "$:/config/Navigation/Permalinkview/UpdateAddressBar"; // Can be "yes" (default) or "no"
|
|
|
|
|
2014-05-13 13:16:58 +00:00
|
|
|
|
2018-11-27 18:43:25 +00:00
|
|
|
// Links to help, if there is no param
|
|
|
|
var HELP_OPEN_EXTERNAL_WINDOW = "http://tiddlywiki.com/#WidgetMessage%3A%20tm-open-external-window";
|
|
|
|
|
2014-05-03 20:23:51 +00:00
|
|
|
exports.startup = function() {
|
|
|
|
// Open startup tiddlers
|
2019-03-04 16:59:15 +00:00
|
|
|
openStartupTiddlers({
|
|
|
|
disableHistory: $tw.boot.disableStartupNavigation
|
|
|
|
});
|
2014-05-03 20:23:51 +00:00
|
|
|
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)) {
|
2014-07-24 14:49:09 +00:00
|
|
|
updateLocationHash({
|
|
|
|
updateAddressBar: $tw.wiki.getTiddlerText(CONFIG_UPDATE_ADDRESS_BAR,"permaview").trim(),
|
|
|
|
updateHistory: $tw.wiki.getTiddlerText(CONFIG_UPDATE_HISTORY,"no").trim()
|
|
|
|
});
|
2014-05-03 20:23:51 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
// 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;
|
2022-07-05 17:39:14 +00:00
|
|
|
if(hash !== "#") {
|
|
|
|
openStartupTiddlers({defaultToCurrentStory: true});
|
|
|
|
}
|
2014-05-03 20:23:51 +00:00
|
|
|
}
|
2014-08-30 19:44:26 +00:00
|
|
|
},false);
|
2014-08-28 20:43:44 +00:00
|
|
|
// Listen for the tm-browser-refresh message
|
|
|
|
$tw.rootWidget.addEventListener("tm-browser-refresh",function(event) {
|
2014-08-19 11:12:36 +00:00
|
|
|
window.location.reload(true);
|
|
|
|
});
|
2018-11-27 18:43:25 +00:00
|
|
|
// Listen for tm-open-external-window message
|
|
|
|
$tw.rootWidget.addEventListener("tm-open-external-window",function(event) {
|
|
|
|
var paramObject = event.paramObject || {},
|
|
|
|
strUrl = event.param || HELP_OPEN_EXTERNAL_WINDOW,
|
|
|
|
strWindowName = paramObject.windowName,
|
|
|
|
strWindowFeatures = paramObject.windowFeatures;
|
|
|
|
window.open(strUrl, strWindowName, strWindowFeatures);
|
|
|
|
});
|
2016-10-08 09:10:54 +00:00
|
|
|
// Listen for the tm-print message
|
|
|
|
$tw.rootWidget.addEventListener("tm-print",function(event) {
|
2016-10-20 08:44:52 +00:00
|
|
|
(event.event.view || window).print();
|
2016-10-08 09:10:54 +00:00
|
|
|
});
|
2014-08-28 20:43:44 +00:00
|
|
|
// Listen for the tm-home message
|
|
|
|
$tw.rootWidget.addEventListener("tm-home",function(event) {
|
2014-07-30 21:31:15 +00:00
|
|
|
window.location.hash = "";
|
2014-08-19 11:12:36 +00:00
|
|
|
var storyFilter = $tw.wiki.getTiddlerText(DEFAULT_TIDDLERS_TITLE),
|
|
|
|
storyList = $tw.wiki.filterTiddlers(storyFilter);
|
2014-11-22 16:11:56 +00:00
|
|
|
//invoke any hooks that might change the default story list
|
|
|
|
storyList = $tw.hooks.invokeHook("th-opening-default-tiddlers-list",storyList);
|
2014-08-19 11:12:36 +00:00
|
|
|
$tw.wiki.addTiddler({title: DEFAULT_STORY_TITLE, text: "", list: storyList},$tw.wiki.getModificationFields());
|
2014-10-07 13:07:41 +00:00
|
|
|
if(storyList[0]) {
|
2018-11-27 18:43:25 +00:00
|
|
|
$tw.wiki.addToHistory(storyList[0]);
|
2014-10-07 13:07:41 +00:00
|
|
|
}
|
2014-05-05 14:25:51 +00:00
|
|
|
});
|
2014-08-28 20:43:44 +00:00
|
|
|
// Listen for the tm-permalink message
|
|
|
|
$tw.rootWidget.addEventListener("tm-permalink",function(event) {
|
2014-07-24 14:49:09 +00:00
|
|
|
updateLocationHash({
|
2018-08-16 18:39:18 +00:00
|
|
|
updateAddressBar: $tw.wiki.getTiddlerText(CONFIG_PERMALINKVIEW_UPDATE_ADDRESS_BAR,"yes").trim() === "yes" ? "permalink" : "none",
|
2014-07-24 14:49:09 +00:00
|
|
|
updateHistory: $tw.wiki.getTiddlerText(CONFIG_UPDATE_HISTORY,"no").trim(),
|
2018-08-16 18:39:18 +00:00
|
|
|
targetTiddler: event.param || event.tiddlerTitle,
|
2024-05-29 14:06:33 +00:00
|
|
|
copyToClipboard: $tw.wiki.getTiddlerText(CONFIG_PERMALINKVIEW_COPY_TO_CLIPBOARD,"yes").trim() === "yes" ? "permalink" : "none",
|
|
|
|
successNotification: event.paramObject && event.paramObject.successNotification,
|
|
|
|
failureNotification: event.paramObject && event.paramObject.failureNotification
|
2014-07-24 14:49:09 +00:00
|
|
|
});
|
|
|
|
});
|
2014-08-28 20:43:44 +00:00
|
|
|
// Listen for the tm-permaview message
|
|
|
|
$tw.rootWidget.addEventListener("tm-permaview",function(event) {
|
2014-07-24 14:49:09 +00:00
|
|
|
updateLocationHash({
|
2018-08-16 18:39:18 +00:00
|
|
|
updateAddressBar: $tw.wiki.getTiddlerText(CONFIG_PERMALINKVIEW_UPDATE_ADDRESS_BAR,"yes").trim() === "yes" ? "permaview" : "none",
|
2014-07-24 14:49:09 +00:00
|
|
|
updateHistory: $tw.wiki.getTiddlerText(CONFIG_UPDATE_HISTORY,"no").trim(),
|
2018-08-16 18:39:18 +00:00
|
|
|
targetTiddler: event.param || event.tiddlerTitle,
|
2024-05-29 14:06:33 +00:00
|
|
|
copyToClipboard: $tw.wiki.getTiddlerText(CONFIG_PERMALINKVIEW_COPY_TO_CLIPBOARD,"yes").trim() === "yes" ? "permaview" : "none",
|
|
|
|
successNotification: event.paramObject && event.paramObject.successNotification,
|
|
|
|
failureNotification: event.paramObject && event.paramObject.failureNotification
|
2021-05-30 18:20:17 +00:00
|
|
|
});
|
2014-07-24 14:49:09 +00:00
|
|
|
});
|
2014-05-03 20:23:51 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
|
|
|
Process the location hash to open the specified tiddlers. Options:
|
2019-03-04 16:59:15 +00:00
|
|
|
disableHistory: if true $:/History is NOT updated
|
2014-05-03 20:23:51 +00:00
|
|
|
defaultToCurrentStory: If true, the current story is retained as the default, instead of opening the default tiddlers
|
|
|
|
*/
|
|
|
|
function openStartupTiddlers(options) {
|
|
|
|
options = options || {};
|
2014-05-29 22:17:21 +00:00
|
|
|
// Work out the target tiddler and the story filter. "null" means "unspecified"
|
|
|
|
var target = null,
|
2014-05-28 16:07:41 +00:00
|
|
|
storyFilter = null;
|
2014-05-03 20:23:51 +00:00
|
|
|
if($tw.locationHash.length > 1) {
|
|
|
|
var hash = $tw.locationHash.substr(1),
|
|
|
|
split = hash.indexOf(":");
|
|
|
|
if(split === -1) {
|
2023-11-24 10:13:57 +00:00
|
|
|
target = $tw.utils.decodeURIComponentSafe(hash.trim());
|
2014-05-03 20:23:51 +00:00
|
|
|
} else {
|
2023-11-24 10:13:57 +00:00
|
|
|
target = $tw.utils.decodeURIComponentSafe(hash.substr(0,split).trim());
|
|
|
|
storyFilter = $tw.utils.decodeURIComponentSafe(hash.substr(split + 1).trim());
|
2014-05-03 20:23:51 +00:00
|
|
|
}
|
|
|
|
}
|
2014-05-28 16:07:41 +00:00
|
|
|
// If the story wasn't specified use the current tiddlers or a blank story
|
|
|
|
if(storyFilter === null) {
|
2014-05-03 20:23:51 +00:00
|
|
|
if(options.defaultToCurrentStory) {
|
|
|
|
var currStoryList = $tw.wiki.getTiddlerList(DEFAULT_STORY_TITLE);
|
|
|
|
storyFilter = $tw.utils.stringifyList(currStoryList);
|
|
|
|
} else {
|
2014-05-29 22:17:21 +00:00
|
|
|
if(target && target !== "") {
|
|
|
|
storyFilter = "";
|
|
|
|
} else {
|
|
|
|
storyFilter = $tw.wiki.getTiddlerText(DEFAULT_TIDDLERS_TITLE);
|
|
|
|
}
|
2014-05-03 20:23:51 +00:00
|
|
|
}
|
|
|
|
}
|
2014-05-29 22:17:21 +00:00
|
|
|
// Process the story filter to get the story list
|
2014-05-03 20:23:51 +00:00
|
|
|
var storyList = $tw.wiki.filterTiddlers(storyFilter);
|
2015-09-08 15:16:50 +00:00
|
|
|
// Invoke any hooks that want to change the default story list
|
2014-11-22 16:11:56 +00:00
|
|
|
storyList = $tw.hooks.invokeHook("th-opening-default-tiddlers-list",storyList);
|
2014-05-03 20:23:51 +00:00
|
|
|
// 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());
|
2019-03-04 16:59:15 +00:00
|
|
|
// Update history
|
2020-11-02 22:52:02 +00:00
|
|
|
var story = new $tw.Story({
|
|
|
|
wiki: $tw.wiki,
|
|
|
|
storyTitle: DEFAULT_STORY_TITLE,
|
|
|
|
historyTitle: DEFAULT_HISTORY_TITLE
|
|
|
|
});
|
2019-03-04 16:59:15 +00:00
|
|
|
if(!options.disableHistory) {
|
|
|
|
// 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);
|
|
|
|
}
|
2020-11-02 22:52:02 +00:00
|
|
|
story.addToHistory(target);
|
2019-03-04 16:59:15 +00:00
|
|
|
} else if(storyList.length > 0) {
|
2020-11-02 22:52:02 +00:00
|
|
|
story.addToHistory(storyList[0]);
|
2021-05-30 18:20:17 +00:00
|
|
|
}
|
2014-05-26 21:56:28 +00:00
|
|
|
}
|
2014-05-03 20:23:51 +00:00
|
|
|
}
|
|
|
|
|
2014-07-24 14:49:09 +00:00
|
|
|
/*
|
|
|
|
options: See below
|
|
|
|
options.updateAddressBar: "permalink", "permaview" or "no" (defaults to "permaview")
|
|
|
|
options.updateHistory: "yes" or "no" (defaults to "no")
|
2018-08-16 18:39:18 +00:00
|
|
|
options.copyToClipboard: "permalink", "permaview" or "no" (defaults to "no")
|
2014-07-24 14:49:09 +00:00
|
|
|
options.targetTiddler: optional title of target tiddler for permalink
|
2024-05-29 14:06:33 +00:00
|
|
|
options.successNotification: optional title of tiddler to use as the notification in case of success
|
|
|
|
options.failureNotification: optional title of tiddler to use as the notification in case of failure
|
2014-07-24 14:49:09 +00:00
|
|
|
*/
|
|
|
|
function updateLocationHash(options) {
|
2018-08-16 18:39:18 +00:00
|
|
|
// Get the story and the history stack
|
|
|
|
var storyList = $tw.wiki.getTiddlerList(DEFAULT_STORY_TITLE),
|
|
|
|
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) {
|
2014-05-13 13:16:58 +00:00
|
|
|
targetTiddler = "";
|
|
|
|
}
|
2018-08-16 18:39:18 +00:00
|
|
|
}
|
|
|
|
// Assemble the location hash
|
|
|
|
switch(options.updateAddressBar) {
|
|
|
|
case "permalink":
|
2023-11-24 10:13:57 +00:00
|
|
|
$tw.locationHash = "#" + encodeURIComponent(targetTiddler);
|
2018-08-16 18:39:18 +00:00
|
|
|
break;
|
|
|
|
case "permaview":
|
2023-11-24 10:13:57 +00:00
|
|
|
$tw.locationHash = "#" + encodeURIComponent(targetTiddler) + ":" + encodeURIComponent($tw.utils.stringifyList(storyList));
|
2018-08-16 18:39:18 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
// Copy URL to the clipboard
|
2024-05-29 14:06:33 +00:00
|
|
|
var url = "";
|
2018-08-16 18:39:18 +00:00
|
|
|
switch(options.copyToClipboard) {
|
|
|
|
case "permalink":
|
2024-05-29 14:06:33 +00:00
|
|
|
url = $tw.utils.getLocationPath() + "#" + encodeURIComponent(targetTiddler);
|
2018-08-16 18:39:18 +00:00
|
|
|
break;
|
|
|
|
case "permaview":
|
2024-05-29 14:06:33 +00:00
|
|
|
url = $tw.utils.getLocationPath() + "#" + encodeURIComponent(targetTiddler) + ":" + encodeURIComponent($tw.utils.stringifyList(storyList));
|
2018-08-16 18:39:18 +00:00
|
|
|
break;
|
|
|
|
}
|
2024-05-29 14:06:33 +00:00
|
|
|
if(url) {
|
|
|
|
$tw.utils.copyToClipboard(url,{successNotification: options.successNotification, failureNotification: options.failureNotification});
|
|
|
|
}
|
2018-08-16 18:39:18 +00:00
|
|
|
// Only change the location hash if we must, thus avoiding unnecessary onhashchange events
|
|
|
|
if($tw.utils.getLocationHash() !== $tw.locationHash) {
|
|
|
|
if(options.updateHistory === "yes") {
|
|
|
|
// Assign the location hash so that history is updated
|
|
|
|
window.location.hash = $tw.locationHash;
|
|
|
|
} else {
|
|
|
|
// We use replace so that browser history isn't affected
|
|
|
|
window.location.replace(window.location.toString().split("#")[0] + $tw.locationHash);
|
2014-05-13 13:16:58 +00:00
|
|
|
}
|
2014-05-03 20:23:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
})();
|