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)
|
|
|
|
var windows = {};
|
|
|
|
|
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
|
2015-05-02 16:22:53 +00:00
|
|
|
var refreshHandler,
|
|
|
|
title = event.param || event.tiddlerTitle,
|
2015-05-02 11:12:51 +00:00
|
|
|
paramObject = event.paramObject || {},
|
2015-05-03 13:58:33 +00:00
|
|
|
template = paramObject.template || "$:/core/templates/single.tiddler.window",
|
2015-05-02 11:12:51 +00:00
|
|
|
width = paramObject.width || "700",
|
2015-08-31 13:13:32 +00:00
|
|
|
height = paramObject.height || "600",
|
|
|
|
variables = $tw.utils.extend({},paramObject,{currentTiddler: title});
|
2015-05-02 11:12:51 +00:00
|
|
|
// Open the window
|
2015-05-05 20:47:37 +00:00
|
|
|
var srcWindow = window.open("","external-" + title,"scrollbars,width=" + width + ",height=" + height),
|
2015-05-02 11:12:51 +00:00
|
|
|
srcDocument = srcWindow.document;
|
2015-05-03 15:24:02 +00:00
|
|
|
windows[title] = srcWindow;
|
2015-05-02 16:22:53 +00:00
|
|
|
// Check for reopening the same window
|
|
|
|
if(srcWindow.haveInitialisedWindow) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
// Initialise the document
|
2015-05-03 14:05:26 +00:00
|
|
|
srcDocument.write("<html><head></head><body class='tc-body tc-single-tiddler-window'></body></html>");
|
2015-05-02 11:12:51 +00:00
|
|
|
srcDocument.close();
|
2015-05-03 07:49:37 +00:00
|
|
|
srcDocument.title = title;
|
2015-05-02 16:22:53 +00:00
|
|
|
srcWindow.addEventListener("beforeunload",function(event) {
|
2015-05-03 15:24:02 +00:00
|
|
|
delete windows[title];
|
2015-05-02 16:22:53 +00:00
|
|
|
$tw.wiki.removeEventListener("change",refreshHandler);
|
|
|
|
},false);
|
2015-05-02 11:12:51 +00:00
|
|
|
// Set up the styles
|
2015-08-31 13:13:32 +00:00
|
|
|
var styleWidgetNode = $tw.wiki.makeTranscludeWidget("$:/core/ui/PageStylesheet",{document: $tw.fakeDocument, variables: variables}),
|
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;
|
|
|
|
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);
|
2015-05-02 16:22:53 +00:00
|
|
|
// Function to handle refreshes
|
|
|
|
refreshHandler = function(changes) {
|
|
|
|
if(styleWidgetNode.refresh(changes,styleContainer,null)) {
|
|
|
|
styleElement.innerHTML = styleContainer.textContent;
|
|
|
|
}
|
2015-05-02 11:12:51 +00:00
|
|
|
widgetNode.refresh(changes);
|
2015-05-02 16:22:53 +00:00
|
|
|
};
|
|
|
|
$tw.wiki.addEventListener("change",refreshHandler);
|
|
|
|
srcWindow.haveInitialisedWindow = true;
|
2015-05-02 11:12:51 +00:00
|
|
|
});
|
2015-05-03 15:24:02 +00:00
|
|
|
// Close open windows when unloading main window
|
|
|
|
$tw.addUnloadTask(function() {
|
|
|
|
$tw.utils.each(windows,function(win) {
|
|
|
|
win.close();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2015-05-02 11:12:51 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
})();
|