2015-05-02 11:12:51 +00:00
|
|
|
/*\
|
|
|
|
title: $:/core/modules/startup/windows.js
|
|
|
|
type: application/javascript
|
|
|
|
module-type: startup
|
|
|
|
|
|
|
|
Setup root widget handlers for the messages concerned with opening external browser windows
|
|
|
|
|
|
|
|
\*/
|
|
|
|
(function(){
|
|
|
|
|
|
|
|
/*jslint node: true, browser: true */
|
|
|
|
/*global $tw: false */
|
|
|
|
"use strict";
|
|
|
|
|
|
|
|
// Export name and synchronous status
|
|
|
|
exports.name = "windows";
|
|
|
|
exports.platforms = ["browser"];
|
|
|
|
exports.after = ["startup"];
|
|
|
|
exports.synchronous = true;
|
|
|
|
|
2015-05-03 15:24:02 +00:00
|
|
|
// Global to keep track of open windows (hashmap by title)
|
2020-11-28 21:24:01 +00:00
|
|
|
$tw.windows = {};
|
2022-03-02 17:34:02 +00:00
|
|
|
// Default template to use for new windows
|
|
|
|
var DEFAULT_WINDOW_TEMPLATE = "$:/core/templates/single.tiddler.window";
|
2015-05-03 15:24:02 +00:00
|
|
|
|
2015-05-02 11:12:51 +00:00
|
|
|
exports.startup = function() {
|
2015-05-03 15:24:02 +00:00
|
|
|
// Handle open window message
|
2015-05-02 11:12:51 +00:00
|
|
|
$tw.rootWidget.addEventListener("tm-open-window",function(event) {
|
|
|
|
// Get the parameters
|
2024-10-14 04:53:34 +00:00
|
|
|
var mainRefreshHandler,
|
|
|
|
styleRefreshHandler,
|
2015-05-02 16:22:53 +00:00
|
|
|
title = event.param || event.tiddlerTitle,
|
2015-05-02 11:12:51 +00:00
|
|
|
paramObject = event.paramObject || {},
|
2019-07-04 14:59:34 +00:00
|
|
|
windowTitle = paramObject.windowTitle || title,
|
2022-03-02 17:34:02 +00:00
|
|
|
windowID = paramObject.windowID || title,
|
|
|
|
template = paramObject.template || DEFAULT_WINDOW_TEMPLATE,
|
2015-05-02 11:12:51 +00:00
|
|
|
width = paramObject.width || "700",
|
2015-08-31 13:13:32 +00:00
|
|
|
height = paramObject.height || "600",
|
2022-02-20 11:23:27 +00:00
|
|
|
top = paramObject.top,
|
|
|
|
left = paramObject.left,
|
2022-03-02 17:34:02 +00:00
|
|
|
variables = $tw.utils.extend({},paramObject,{currentTiddler: title, "tv-window-id": windowID});
|
2015-05-02 11:12:51 +00:00
|
|
|
// Open the window
|
2018-11-06 13:54:00 +00:00
|
|
|
var srcWindow,
|
2023-10-18 15:10:04 +00:00
|
|
|
srcDocument;
|
2018-11-06 13:54:00 +00:00
|
|
|
// In case that popup blockers deny opening a new window
|
|
|
|
try {
|
2022-03-02 17:34:02 +00:00
|
|
|
srcWindow = window.open("","external-" + windowID,"scrollbars,width=" + width + ",height=" + height + (top ? ",top=" + top : "" ) + (left ? ",left=" + left : "" )),
|
2015-05-02 11:12:51 +00:00
|
|
|
srcDocument = srcWindow.document;
|
2018-11-06 13:54:00 +00:00
|
|
|
}
|
|
|
|
catch(e) {
|
|
|
|
return;
|
|
|
|
}
|
2022-03-02 17:34:02 +00:00
|
|
|
$tw.windows[windowID] = srcWindow;
|
2015-05-02 16:22:53 +00:00
|
|
|
// Check for reopening the same window
|
|
|
|
if(srcWindow.haveInitialisedWindow) {
|
2023-10-18 15:10:04 +00:00
|
|
|
srcWindow.focus();
|
2015-05-02 16:22:53 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
// Initialise the document
|
2024-03-27 18:20:35 +00:00
|
|
|
srcDocument.write("<!DOCTYPE html><head></head><body class='tc-body tc-single-tiddler-window'></body></html>");
|
2015-05-02 11:12:51 +00:00
|
|
|
srcDocument.close();
|
2019-07-04 14:59:34 +00:00
|
|
|
srcDocument.title = windowTitle;
|
2015-05-02 16:22:53 +00:00
|
|
|
srcWindow.addEventListener("beforeunload",function(event) {
|
2022-03-02 17:34:02 +00:00
|
|
|
delete $tw.windows[windowID];
|
2024-10-14 04:53:34 +00:00
|
|
|
$tw.wiki.removeEventListener("change",styleRefreshHandler);
|
|
|
|
$tw.wiki.removeEventListener("change",mainRefreshHandler);
|
2015-05-02 16:22:53 +00:00
|
|
|
},false);
|
2015-05-02 11:12:51 +00:00
|
|
|
// Set up the styles
|
2016-10-18 15:39:18 +00:00
|
|
|
var styleWidgetNode = $tw.wiki.makeTranscludeWidget("$:/core/ui/PageStylesheet",{
|
|
|
|
document: $tw.fakeDocument,
|
|
|
|
variables: variables,
|
|
|
|
importPageMacros: true}),
|
2015-05-02 11:12:51 +00:00
|
|
|
styleContainer = $tw.fakeDocument.createElement("style");
|
|
|
|
styleWidgetNode.render(styleContainer,null);
|
|
|
|
var styleElement = srcDocument.createElement("style");
|
|
|
|
styleElement.innerHTML = styleContainer.textContent;
|
2024-10-14 04:53:34 +00:00
|
|
|
styleWidgetNode.assignedStyles = styleContainer.textContent;
|
2015-05-02 11:12:51 +00:00
|
|
|
srcDocument.head.insertBefore(styleElement,srcDocument.head.firstChild);
|
|
|
|
// Render the text of the tiddler
|
|
|
|
var parser = $tw.wiki.parseTiddler(template),
|
2015-08-31 13:13:32 +00:00
|
|
|
widgetNode = $tw.wiki.makeWidget(parser,{document: srcDocument, parentWidget: $tw.rootWidget, variables: variables});
|
2015-05-03 15:56:27 +00:00
|
|
|
widgetNode.render(srcDocument.body,srcDocument.body.firstChild);
|
2024-10-14 04:53:34 +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 16:23:18 +00:00
|
|
|
throttledRefreshFn = function(changes,options) {
|
|
|
|
options = options || {};
|
2024-10-14 04:53:34 +00:00
|
|
|
// Check if only tiddlers that are throttled have changed
|
|
|
|
var onlyThrottledTiddlersHaveChanged = true;
|
|
|
|
for(var title in changes) {
|
|
|
|
var tiddler = $tw.wiki.getTiddler(title);
|
2024-10-15 16:23:18 +00:00
|
|
|
if(!$tw.wiki.isVolatileTiddler(title) && (!tiddler || !(tiddler.hasField("draft.of") || tiddler.hasField("throttle.refresh") ||
|
|
|
|
(options.mainCondition && tiddler.hasField("throttle.refresh.main")) || (options.styleCondition && tiddler.hasField("throttle.refresh.style"))))) {
|
2024-10-14 04:53:34 +00:00
|
|
|
onlyThrottledTiddlersHaveChanged = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Defer the change if only drafts have changed
|
2024-10-15 16:23:18 +00:00
|
|
|
if(options.timerId) {
|
|
|
|
clearTimeout(options.timerId);
|
2024-10-14 04:53:34 +00:00
|
|
|
}
|
2024-10-15 16:23:18 +00:00
|
|
|
options.timerId = null;
|
2024-10-14 04:53:34 +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 16:23:18 +00:00
|
|
|
options.timerId = setTimeout(options.throttledRefresh,timeout);
|
|
|
|
$tw.utils.extend(options.deferredChanges,changes);
|
2024-10-14 04:53:34 +00:00
|
|
|
} else {
|
2024-10-15 16:23:18 +00:00
|
|
|
$tw.utils.extend(options.deferredChanges,changes);
|
|
|
|
options.callback();
|
2024-10-14 04:53:34 +00:00
|
|
|
}
|
|
|
|
};
|
2024-10-15 03:09:28 +00:00
|
|
|
var styleRefresh = function() {
|
|
|
|
if(styleWidgetNode.refresh(styleDeferredChanges,styleContainer,null)) {
|
2024-10-14 04:53:34 +00:00
|
|
|
var newStyles = styleContainer.textContent;
|
|
|
|
if(newStyles !== styleWidgetNode.assignedStyles) {
|
|
|
|
styleWidgetNode.assignedStyles = newStyles;
|
|
|
|
styleElement.innerHTML = styleWidgetNode.assignedStyles;
|
|
|
|
}
|
2015-05-02 16:22:53 +00:00
|
|
|
}
|
2024-10-15 03:09:28 +00:00
|
|
|
styleDeferredChanges = Object.create(null);
|
2024-10-14 04:53:34 +00:00
|
|
|
};
|
2024-10-15 03:09:28 +00:00
|
|
|
var mainRefresh = function() {
|
|
|
|
widgetNode.refresh(mainDeferredChanges);
|
|
|
|
mainDeferredChanges = Object.create(null);
|
2024-10-14 04:53:34 +00:00
|
|
|
};
|
2024-10-15 03:23:44 +00:00
|
|
|
var mainThrottledRefresh = $tw.perf.report("throttledMainRefresh",mainRefresh),
|
|
|
|
styleThrottledRefresh = $tw.perf.report("throttledStyleRefresh",styleRefresh);
|
2024-10-14 04:53:34 +00:00
|
|
|
styleRefreshHandler = function(changes) {
|
2024-10-15 16:23:18 +00:00
|
|
|
throttledRefreshFn(changes,{
|
|
|
|
deferredChanged: styleDeferredChanges,
|
|
|
|
timerId: styleTimerId,
|
|
|
|
throttledRefresh: styleThrottledRefresh,
|
|
|
|
callback: styleRefresh,
|
|
|
|
mainCondition: false,
|
|
|
|
styleCondition: true
|
|
|
|
});
|
2024-10-14 04:53:34 +00:00
|
|
|
};
|
|
|
|
mainRefreshHandler = function(changes) {
|
2024-10-15 16:23:18 +00:00
|
|
|
throttledRefreshFn(changes,{
|
|
|
|
deferredChanges: mainDeferredChanges,
|
|
|
|
timerId: mainTimerId,
|
|
|
|
throttledRefresh: mainThrottledRefresh,
|
|
|
|
callback: mainRefresh,
|
|
|
|
mainCondition: true,
|
|
|
|
styleCondition: false
|
|
|
|
});
|
2015-05-02 16:22:53 +00:00
|
|
|
};
|
2024-10-14 04:53:34 +00:00
|
|
|
$tw.wiki.addEventListener("change",styleRefreshHandler);
|
|
|
|
$tw.wiki.addEventListener("change",mainRefreshHandler);
|
2018-11-06 13:34:51 +00:00
|
|
|
// Listen for keyboard shortcuts
|
|
|
|
$tw.utils.addEventListeners(srcDocument,[{
|
|
|
|
name: "keydown",
|
|
|
|
handlerObject: $tw.keyboardManager,
|
|
|
|
handlerMethod: "handleKeydownEvent"
|
|
|
|
}]);
|
2020-11-28 07:26:47 +00:00
|
|
|
srcWindow.document.documentElement.addEventListener("click",$tw.popup,true);
|
2015-05-02 16:22:53 +00:00
|
|
|
srcWindow.haveInitialisedWindow = true;
|
2015-05-02 11:12:51 +00:00
|
|
|
});
|
2022-03-02 17:34:02 +00:00
|
|
|
$tw.rootWidget.addEventListener("tm-close-window",function(event) {
|
|
|
|
var windowID = event.param,
|
|
|
|
win = $tw.windows[windowID];
|
|
|
|
if(win) {
|
|
|
|
win.close();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
var closeAllWindows = function() {
|
2020-11-28 21:24:01 +00:00
|
|
|
$tw.utils.each($tw.windows,function(win) {
|
2015-05-03 15:24:02 +00:00
|
|
|
win.close();
|
|
|
|
});
|
2022-03-02 17:34:02 +00:00
|
|
|
}
|
|
|
|
$tw.rootWidget.addEventListener("tm-close-all-windows",closeAllWindows);
|
|
|
|
// Close open windows when unloading main window
|
|
|
|
$tw.addUnloadTask(closeAllWindows);
|
2015-05-02 11:12:51 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
})();
|