2014-05-03 18:50:05 +00:00
|
|
|
/*\
|
2014-05-05 14:25:51 +00:00
|
|
|
title: $:/core/modules/startup/render.js
|
2014-05-03 18:50:05 +00:00
|
|
|
type: application/javascript
|
|
|
|
module-type: startup
|
|
|
|
|
2014-05-05 22:00:09 +00:00
|
|
|
Title, stylesheet and page rendering
|
2014-05-03 18:50:05 +00:00
|
|
|
|
|
|
|
\*/
|
|
|
|
(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 = "render";
|
2014-05-03 18:50:05 +00:00
|
|
|
exports.platforms = ["browser"];
|
2014-05-05 14:25:51 +00:00
|
|
|
exports.after = ["story"];
|
2014-05-03 18:50:05 +00:00
|
|
|
exports.synchronous = true;
|
|
|
|
|
2014-05-05 22:00:09 +00:00
|
|
|
// Default story and history lists
|
2014-08-30 19:44:26 +00:00
|
|
|
var PAGE_TITLE_TITLE = "$:/core/wiki/title";
|
2014-05-05 22:00:09 +00:00
|
|
|
var PAGE_STYLESHEET_TITLE = "$:/core/ui/PageStylesheet";
|
2020-11-07 09:51:01 +00:00
|
|
|
var PAGE_TEMPLATE_TITLE = "$:/core/ui/RootTemplate";
|
2014-05-05 22:00:09 +00:00
|
|
|
|
2014-05-03 20:23:51 +00:00
|
|
|
// Time (in ms) that we defer refreshing changes to draft tiddlers
|
2015-05-11 19:15:42 +00:00
|
|
|
var DRAFT_TIDDLER_TIMEOUT_TITLE = "$:/config/Drafts/TypingTimeout";
|
2019-10-14 09:42:14 +00:00
|
|
|
var THROTTLE_REFRESH_TIMEOUT = 400;
|
2014-05-03 20:23:51 +00:00
|
|
|
|
2014-05-03 18:50:05 +00:00
|
|
|
exports.startup = function() {
|
2014-05-05 22:00:09 +00:00
|
|
|
// Set up the title
|
2024-06-04 16:13:41 +00:00
|
|
|
$tw.titleWidgetNode = $tw.wiki.makeTranscludeWidget(PAGE_TITLE_TITLE, {
|
|
|
|
document: $tw.fakeDocument,
|
|
|
|
parseAsInline: true,
|
|
|
|
importPageMacros: true,
|
|
|
|
});
|
2014-05-05 22:00:09 +00:00
|
|
|
$tw.titleContainer = $tw.fakeDocument.createElement("div");
|
|
|
|
$tw.titleWidgetNode.render($tw.titleContainer,null);
|
|
|
|
document.title = $tw.titleContainer.textContent;
|
|
|
|
$tw.wiki.addEventListener("change",function(changes) {
|
|
|
|
if($tw.titleWidgetNode.refresh(changes,$tw.titleContainer,null)) {
|
|
|
|
document.title = $tw.titleContainer.textContent;
|
|
|
|
}
|
|
|
|
});
|
2014-05-03 18:50:05 +00:00
|
|
|
// Set up the styles
|
2014-05-06 09:14:22 +00:00
|
|
|
$tw.styleWidgetNode = $tw.wiki.makeTranscludeWidget(PAGE_STYLESHEET_TITLE,{document: $tw.fakeDocument});
|
2014-05-03 18:50:05 +00:00
|
|
|
$tw.styleContainer = $tw.fakeDocument.createElement("style");
|
|
|
|
$tw.styleWidgetNode.render($tw.styleContainer,null);
|
2022-03-17 21:19:49 +00:00
|
|
|
$tw.styleWidgetNode.assignedStyles = $tw.styleContainer.textContent;
|
2014-05-03 18:50:05 +00:00
|
|
|
$tw.styleElement = document.createElement("style");
|
2022-03-17 21:19:49 +00:00
|
|
|
$tw.styleElement.innerHTML = $tw.styleWidgetNode.assignedStyles;
|
2014-05-03 18:50:05 +00:00
|
|
|
document.head.insertBefore($tw.styleElement,document.head.firstChild);
|
2024-10-14 04:52:21 +00:00
|
|
|
// Prepare refresh mechanism
|
2024-10-15 03:09:28 +00:00
|
|
|
var mainDeferredChanges = Object.create(null),
|
|
|
|
styleDeferredChanges = Object.create(null),
|
|
|
|
mainTimerId,
|
|
|
|
styleTimerId,
|
2024-10-15 11:47:28 +00:00
|
|
|
throttledRefreshFn = function(changes,options) {
|
|
|
|
options = options || {};
|
2024-10-14 04:52:21 +00:00
|
|
|
// Check if only tiddlers that are throttled have changed
|
|
|
|
var onlyThrottledTiddlersHaveChanged = true;
|
2024-10-15 19:18:56 +00:00
|
|
|
var deferredChanges = options.mainCondition ? mainDeferredChanges : styleDeferredChanges;
|
2024-10-14 04:52:21 +00:00
|
|
|
for(var title in changes) {
|
|
|
|
var tiddler = $tw.wiki.getTiddler(title);
|
2024-10-15 13:38:22 +00:00
|
|
|
if(!$tw.wiki.isVolatileTiddler(title) && (!tiddler || !(tiddler.hasField("draft.of") || tiddler.hasField("throttle.refresh") ||
|
2024-10-15 11:47:28 +00:00
|
|
|
(options.mainCondition && tiddler.hasField("throttle.refresh.main")) || (options.styleCondition && tiddler.hasField("throttle.refresh.style"))))) {
|
2024-10-14 04:52:21 +00:00
|
|
|
onlyThrottledTiddlersHaveChanged = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Defer the change if only drafts have changed
|
2024-10-15 19:18:56 +00:00
|
|
|
if(options.mainCondition) {
|
|
|
|
if(mainTimerId) {
|
|
|
|
clearTimeout(mainTimerId);
|
|
|
|
}
|
|
|
|
mainTimerId = null;
|
|
|
|
} else if(options.styleCondition) {
|
|
|
|
if(styleTimerId) {
|
|
|
|
clearTimeout(styleTimerId);
|
|
|
|
}
|
|
|
|
styleTimerId = null;
|
2024-10-14 04:52:21 +00:00
|
|
|
}
|
|
|
|
if(onlyThrottledTiddlersHaveChanged) {
|
|
|
|
var timeout = parseInt($tw.wiki.getTiddlerText(DRAFT_TIDDLER_TIMEOUT_TITLE,""),10);
|
|
|
|
if(isNaN(timeout)) {
|
|
|
|
timeout = THROTTLE_REFRESH_TIMEOUT;
|
|
|
|
}
|
2024-10-15 19:18:56 +00:00
|
|
|
if(options.mainCondition) {
|
|
|
|
mainTimerId = setTimeout(options.throttledRefresh,timeout);
|
|
|
|
} else if(options.styleCondition) {
|
|
|
|
styleTimerId = setTimeout(options.throttledRefresh,timeout);
|
|
|
|
}
|
|
|
|
$tw.utils.extend(deferredChanges,changes);
|
2024-10-14 04:52:21 +00:00
|
|
|
} else {
|
2024-10-15 19:18:56 +00:00
|
|
|
$tw.utils.extend(deferredChanges,changes);
|
2024-10-15 11:47:28 +00:00
|
|
|
options.callback();
|
2024-10-14 04:52:21 +00:00
|
|
|
}
|
|
|
|
};
|
2024-10-15 03:09:28 +00:00
|
|
|
function refresh() {
|
|
|
|
// Process the refresh
|
|
|
|
$tw.hooks.invokeHook("th-page-refreshing");
|
|
|
|
$tw.pageWidgetNode.refresh(mainDeferredChanges);
|
|
|
|
mainDeferredChanges = Object.create(null);
|
|
|
|
$tw.hooks.invokeHook("th-page-refreshed");
|
|
|
|
}
|
|
|
|
function styleRefresh() {
|
|
|
|
if($tw.styleWidgetNode.refresh(styleDeferredChanges,$tw.styleContainer,null)) {
|
2022-03-17 21:19:49 +00:00
|
|
|
var newStyles = $tw.styleContainer.textContent;
|
|
|
|
if(newStyles !== $tw.styleWidgetNode.assignedStyles) {
|
|
|
|
$tw.styleWidgetNode.assignedStyles = newStyles;
|
|
|
|
$tw.styleElement.innerHTML = $tw.styleWidgetNode.assignedStyles;
|
|
|
|
}
|
2024-10-15 03:09:28 +00:00
|
|
|
}
|
|
|
|
styleDeferredChanges = Object.create(null);
|
2024-10-14 04:52:21 +00:00
|
|
|
}
|
2024-10-15 03:23:44 +00:00
|
|
|
var mainThrottledRefresh = $tw.perf.report("throttledMainRefresh",refresh),
|
|
|
|
styleThrottledRefresh = $tw.perf.report("throttledStyleRefresh",styleRefresh);
|
2024-10-14 04:52:21 +00:00
|
|
|
$tw.wiki.addEventListener("change",$tw.perf.report("styleRefresh",function(changes) {
|
2024-10-15 11:47:28 +00:00
|
|
|
throttledRefreshFn(changes,{
|
|
|
|
throttledRefresh: styleThrottledRefresh,
|
|
|
|
callback: styleRefresh,
|
|
|
|
mainCondition: false,
|
|
|
|
styleCondition: true
|
|
|
|
});
|
2014-05-03 18:50:05 +00:00
|
|
|
}));
|
2014-06-12 17:01:33 +00:00
|
|
|
// Display the $:/core/ui/PageTemplate tiddler to kick off the display
|
2014-05-03 18:50:05 +00:00
|
|
|
$tw.perf.report("mainRender",function() {
|
2020-11-07 09:51:01 +00:00
|
|
|
$tw.pageWidgetNode = $tw.wiki.makeTranscludeWidget(PAGE_TEMPLATE_TITLE,{document: document, parentWidget: $tw.rootWidget, recursionMarker: "no"});
|
2014-05-03 18:50:05 +00:00
|
|
|
$tw.pageContainer = document.createElement("div");
|
2014-08-28 17:21:08 +00:00
|
|
|
$tw.utils.addClass($tw.pageContainer,"tc-page-container-wrapper");
|
2014-05-03 18:50:05 +00:00
|
|
|
document.body.insertBefore($tw.pageContainer,document.body.firstChild);
|
|
|
|
$tw.pageWidgetNode.render($tw.pageContainer,null);
|
2018-01-04 16:01:19 +00:00
|
|
|
$tw.hooks.invokeHook("th-page-refreshed");
|
2014-05-03 18:50:05 +00:00
|
|
|
})();
|
2018-09-27 08:47:55 +00:00
|
|
|
// Remove any splash screen elements
|
|
|
|
var removeList = document.querySelectorAll(".tc-remove-when-wiki-loaded");
|
|
|
|
$tw.utils.each(removeList,function(removeItem) {
|
|
|
|
if(removeItem.parentNode) {
|
|
|
|
removeItem.parentNode.removeChild(removeItem);
|
|
|
|
}
|
|
|
|
});
|
2014-05-03 18:50:05 +00:00
|
|
|
// Add the change event handler
|
|
|
|
$tw.wiki.addEventListener("change",$tw.perf.report("mainRefresh",function(changes) {
|
2024-10-15 11:47:28 +00:00
|
|
|
throttledRefreshFn(changes,{
|
|
|
|
throttledRefresh: mainThrottledRefresh,
|
|
|
|
callback: refresh,
|
|
|
|
mainCondition: true,
|
|
|
|
styleCondition: false
|
|
|
|
});
|
2014-05-03 18:50:05 +00:00
|
|
|
}));
|
|
|
|
// Fix up the link between the root widget and the page container
|
|
|
|
$tw.rootWidget.domNodes = [$tw.pageContainer];
|
|
|
|
$tw.rootWidget.children = [$tw.pageWidgetNode];
|
2020-10-25 16:22:21 +00:00
|
|
|
// Run any post-render startup actions
|
2020-11-26 12:41:24 +00:00
|
|
|
$tw.rootWidget.invokeActionsByTag("$:/tags/StartupAction/PostRender");
|
2014-05-03 18:50:05 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
})();
|