2012-07-15 16:37:03 +00:00
|
|
|
/*\
|
|
|
|
title: $:/core/modules/utils/dom/modal.js
|
|
|
|
type: application/javascript
|
|
|
|
module-type: utils
|
|
|
|
|
|
|
|
Modal message mechanism
|
|
|
|
|
|
|
|
\*/
|
|
|
|
(function(){
|
|
|
|
|
|
|
|
/*jslint node: true, browser: true */
|
|
|
|
/*global $tw: false */
|
|
|
|
"use strict";
|
|
|
|
|
2013-11-08 08:47:00 +00:00
|
|
|
var widget = require("$:/core/modules/widgets/widget.js");
|
2020-11-16 16:27:46 +00:00
|
|
|
var navigator = require("$:/core/modules/widgets/navigator.js");
|
2013-10-21 19:14:01 +00:00
|
|
|
|
2012-07-15 16:37:03 +00:00
|
|
|
var Modal = function(wiki) {
|
|
|
|
this.wiki = wiki;
|
2013-06-04 15:19:47 +00:00
|
|
|
this.modalCount = 0;
|
2012-07-15 16:37:03 +00:00
|
|
|
};
|
|
|
|
|
2012-11-16 19:30:30 +00:00
|
|
|
/*
|
|
|
|
Display a modal dialogue
|
|
|
|
title: Title of tiddler to display
|
|
|
|
options: see below
|
|
|
|
Options include:
|
|
|
|
downloadLink: Text of a big download link to include
|
2022-12-03 17:26:44 +00:00
|
|
|
event: widget event
|
|
|
|
variables: from event.paramObject
|
2012-11-16 19:30:30 +00:00
|
|
|
*/
|
|
|
|
Modal.prototype.display = function(title,options) {
|
|
|
|
options = options || {};
|
2018-11-18 20:57:04 +00:00
|
|
|
this.srcDocument = options.variables && (options.variables.rootwindow === "true" ||
|
|
|
|
options.variables.rootwindow === "yes") ? document :
|
2021-02-13 10:28:31 +00:00
|
|
|
(options.event && options.event.event && options.event.event.target ? options.event.event.target.ownerDocument : document);
|
2018-11-18 20:57:04 +00:00
|
|
|
this.srcWindow = this.srcDocument.defaultView;
|
2013-08-28 14:15:56 +00:00
|
|
|
var self = this,
|
2015-05-06 07:07:12 +00:00
|
|
|
refreshHandler,
|
2013-09-10 14:28:15 +00:00
|
|
|
duration = $tw.utils.getAnimationDuration(),
|
|
|
|
tiddler = this.wiki.getTiddler(title);
|
|
|
|
// Don't do anything if the tiddler doesn't exist
|
|
|
|
if(!tiddler) {
|
|
|
|
return;
|
|
|
|
}
|
2014-11-07 14:54:46 +00:00
|
|
|
// Create the variables
|
2020-11-16 16:27:46 +00:00
|
|
|
var variables = $tw.utils.extend({
|
|
|
|
currentTiddler: title,
|
|
|
|
"tv-story-list": (options.event && options.event.widget ? options.event.widget.getVariable("tv-story-list") : ""),
|
|
|
|
"tv-history-list": (options.event && options.event.widget ? options.event.widget.getVariable("tv-history-list") : "")
|
|
|
|
},options.variables);
|
|
|
|
|
2012-07-16 11:57:44 +00:00
|
|
|
// Create the wrapper divs
|
2018-11-18 20:57:04 +00:00
|
|
|
var wrapper = this.srcDocument.createElement("div"),
|
|
|
|
modalBackdrop = this.srcDocument.createElement("div"),
|
|
|
|
modalWrapper = this.srcDocument.createElement("div"),
|
|
|
|
modalHeader = this.srcDocument.createElement("div"),
|
|
|
|
headerTitle = this.srcDocument.createElement("h3"),
|
|
|
|
modalBody = this.srcDocument.createElement("div"),
|
|
|
|
modalLink = this.srcDocument.createElement("a"),
|
|
|
|
modalFooter = this.srcDocument.createElement("div"),
|
|
|
|
modalFooterHelp = this.srcDocument.createElement("span"),
|
|
|
|
modalFooterButtons = this.srcDocument.createElement("span");
|
2013-09-10 14:28:15 +00:00
|
|
|
// Up the modal count and adjust the body class
|
|
|
|
this.modalCount++;
|
|
|
|
this.adjustPageClass();
|
2012-07-16 11:57:44 +00:00
|
|
|
// Add classes
|
2014-08-28 16:34:02 +00:00
|
|
|
$tw.utils.addClass(wrapper,"tc-modal-wrapper");
|
2020-03-12 16:58:14 +00:00
|
|
|
if(tiddler.fields && tiddler.fields.class) {
|
|
|
|
$tw.utils.addClass(wrapper,tiddler.fields.class);
|
|
|
|
}
|
2014-08-28 16:34:02 +00:00
|
|
|
$tw.utils.addClass(modalBackdrop,"tc-modal-backdrop");
|
|
|
|
$tw.utils.addClass(modalWrapper,"tc-modal");
|
|
|
|
$tw.utils.addClass(modalHeader,"tc-modal-header");
|
|
|
|
$tw.utils.addClass(modalBody,"tc-modal-body");
|
|
|
|
$tw.utils.addClass(modalFooter,"tc-modal-footer");
|
2012-07-16 11:57:44 +00:00
|
|
|
// Join them together
|
|
|
|
wrapper.appendChild(modalBackdrop);
|
|
|
|
wrapper.appendChild(modalWrapper);
|
|
|
|
modalHeader.appendChild(headerTitle);
|
|
|
|
modalWrapper.appendChild(modalHeader);
|
|
|
|
modalWrapper.appendChild(modalBody);
|
|
|
|
modalFooter.appendChild(modalFooterHelp);
|
|
|
|
modalFooter.appendChild(modalFooterButtons);
|
|
|
|
modalWrapper.appendChild(modalFooter);
|
2020-11-16 16:27:46 +00:00
|
|
|
var navigatorTree = {
|
|
|
|
"type": "navigator",
|
|
|
|
"attributes": {
|
|
|
|
"story": {
|
|
|
|
"name": "story",
|
|
|
|
"type": "string",
|
|
|
|
"value": variables["tv-story-list"]
|
|
|
|
},
|
|
|
|
"history": {
|
|
|
|
"name": "history",
|
|
|
|
"type": "string",
|
|
|
|
"value": variables["tv-history-list"]
|
|
|
|
}
|
|
|
|
},
|
|
|
|
"tag": "$navigator",
|
|
|
|
"isBlock": true,
|
|
|
|
"children": []
|
|
|
|
};
|
|
|
|
var navigatorWidgetNode = new navigator.navigator(navigatorTree, {
|
|
|
|
wiki: this.wiki,
|
|
|
|
document : this.srcDocument,
|
|
|
|
parentWidget: $tw.rootWidget
|
|
|
|
});
|
|
|
|
navigatorWidgetNode.render(modalBody,null);
|
2021-05-30 18:20:17 +00:00
|
|
|
|
2012-07-16 11:57:44 +00:00
|
|
|
// Render the title of the message
|
2014-07-04 20:07:35 +00:00
|
|
|
var headerWidgetNode = this.wiki.makeTranscludeWidget(title,{
|
|
|
|
field: "subtitle",
|
2015-03-18 11:41:10 +00:00
|
|
|
mode: "inline",
|
2014-07-04 20:07:35 +00:00
|
|
|
children: [{
|
|
|
|
type: "text",
|
|
|
|
attributes: {
|
|
|
|
text: {
|
|
|
|
type: "string",
|
|
|
|
value: title
|
|
|
|
}}}],
|
2020-11-16 16:27:46 +00:00
|
|
|
parentWidget: navigatorWidgetNode,
|
2018-11-18 20:57:04 +00:00
|
|
|
document: this.srcDocument,
|
2016-10-18 15:39:18 +00:00
|
|
|
variables: variables,
|
|
|
|
importPageMacros: true
|
2014-07-04 20:07:35 +00:00
|
|
|
});
|
2013-10-29 15:01:36 +00:00
|
|
|
headerWidgetNode.render(headerTitle,null);
|
2012-07-16 11:57:44 +00:00
|
|
|
// Render the body of the message
|
2014-07-04 20:07:35 +00:00
|
|
|
var bodyWidgetNode = this.wiki.makeTranscludeWidget(title,{
|
2020-11-16 16:27:46 +00:00
|
|
|
parentWidget: navigatorWidgetNode,
|
2018-11-18 20:57:04 +00:00
|
|
|
document: this.srcDocument,
|
2016-10-18 15:39:18 +00:00
|
|
|
variables: variables,
|
|
|
|
importPageMacros: true
|
2014-07-04 20:07:35 +00:00
|
|
|
});
|
2020-11-16 16:27:46 +00:00
|
|
|
|
2013-10-21 19:14:01 +00:00
|
|
|
bodyWidgetNode.render(modalBody,null);
|
2012-11-16 19:30:30 +00:00
|
|
|
// Setup the link if present
|
|
|
|
if(options.downloadLink) {
|
2014-08-30 19:44:26 +00:00
|
|
|
modalLink.href = options.downloadLink;
|
2018-11-18 20:57:04 +00:00
|
|
|
modalLink.appendChild(this.srcDocument.createTextNode("Right-click to save changes"));
|
2012-11-16 19:30:30 +00:00
|
|
|
modalBody.appendChild(modalLink);
|
|
|
|
}
|
2012-07-16 11:57:44 +00:00
|
|
|
// Render the footer of the message
|
2020-03-12 16:58:14 +00:00
|
|
|
if(tiddler.fields && tiddler.fields.help) {
|
2018-11-18 20:57:04 +00:00
|
|
|
var link = this.srcDocument.createElement("a");
|
2012-07-16 12:47:28 +00:00
|
|
|
link.setAttribute("href",tiddler.fields.help);
|
|
|
|
link.setAttribute("target","_blank");
|
2016-05-05 10:49:40 +00:00
|
|
|
link.setAttribute("rel","noopener noreferrer");
|
2018-11-18 20:57:04 +00:00
|
|
|
link.appendChild(this.srcDocument.createTextNode("Help"));
|
2012-07-16 12:47:28 +00:00
|
|
|
modalFooterHelp.appendChild(link);
|
|
|
|
modalFooterHelp.style.float = "left";
|
|
|
|
}
|
2014-07-04 20:07:35 +00:00
|
|
|
var footerWidgetNode = this.wiki.makeTranscludeWidget(title,{
|
|
|
|
field: "footer",
|
2015-03-18 11:41:10 +00:00
|
|
|
mode: "inline",
|
2014-07-04 20:07:35 +00:00
|
|
|
children: [{
|
|
|
|
type: "button",
|
|
|
|
attributes: {
|
|
|
|
message: {
|
|
|
|
type: "string",
|
2014-08-28 20:43:44 +00:00
|
|
|
value: "tm-close-tiddler"
|
2014-07-04 20:07:35 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
children: [{
|
|
|
|
type: "text",
|
|
|
|
attributes: {
|
|
|
|
text: {
|
|
|
|
type: "string",
|
2016-04-14 22:03:14 +00:00
|
|
|
value: $tw.language.getString("Buttons/Close/Caption")
|
2014-07-04 20:07:35 +00:00
|
|
|
}}}
|
|
|
|
]}],
|
2020-11-16 16:27:46 +00:00
|
|
|
parentWidget: navigatorWidgetNode,
|
2018-11-18 20:57:04 +00:00
|
|
|
document: this.srcDocument,
|
2016-10-18 15:39:18 +00:00
|
|
|
variables: variables,
|
|
|
|
importPageMacros: true
|
2014-07-04 20:07:35 +00:00
|
|
|
});
|
2013-10-21 19:14:01 +00:00
|
|
|
footerWidgetNode.render(modalFooterButtons,null);
|
2015-05-06 07:07:12 +00:00
|
|
|
// Set up the refresh handler
|
|
|
|
refreshHandler = function(changes) {
|
|
|
|
headerWidgetNode.refresh(changes,modalHeader,null);
|
|
|
|
bodyWidgetNode.refresh(changes,modalBody,null);
|
2013-10-21 19:14:01 +00:00
|
|
|
footerWidgetNode.refresh(changes,modalFooterButtons,null);
|
2015-05-06 07:07:12 +00:00
|
|
|
};
|
|
|
|
this.wiki.addEventListener("change",refreshHandler);
|
2012-07-16 11:57:44 +00:00
|
|
|
// Add the close event handler
|
2013-10-21 19:14:01 +00:00
|
|
|
var closeHandler = function(event) {
|
2015-05-06 07:07:12 +00:00
|
|
|
// Remove our refresh handler
|
|
|
|
self.wiki.removeEventListener("change",refreshHandler);
|
2013-06-04 15:19:47 +00:00
|
|
|
// Decrease the modal count and adjust the body class
|
|
|
|
self.modalCount--;
|
|
|
|
self.adjustPageClass();
|
2012-07-18 10:45:17 +00:00
|
|
|
// Force layout and animate the modal message away
|
2012-08-02 21:32:34 +00:00
|
|
|
$tw.utils.forceLayout(modalBackdrop);
|
|
|
|
$tw.utils.forceLayout(modalWrapper);
|
2012-10-26 09:28:32 +00:00
|
|
|
$tw.utils.setStyle(modalBackdrop,[
|
|
|
|
{opacity: "0"}
|
|
|
|
]);
|
|
|
|
$tw.utils.setStyle(modalWrapper,[
|
2018-11-18 20:57:04 +00:00
|
|
|
{transform: "translateY(" + self.srcWindow.innerHeight + "px)"}
|
2012-10-26 09:28:32 +00:00
|
|
|
]);
|
2012-07-18 10:45:17 +00:00
|
|
|
// Set up an event for the transition end
|
2018-11-18 20:57:04 +00:00
|
|
|
self.srcWindow.setTimeout(function() {
|
2012-07-18 10:45:17 +00:00
|
|
|
if(wrapper.parentNode) {
|
|
|
|
// Remove the modal message from the DOM
|
2018-11-18 20:57:04 +00:00
|
|
|
self.srcDocument.body.removeChild(wrapper);
|
2012-07-18 10:45:17 +00:00
|
|
|
}
|
2013-08-29 11:43:24 +00:00
|
|
|
},duration);
|
2014-08-28 20:43:44 +00:00
|
|
|
// Don't let anyone else handle the tm-close-tiddler message
|
2012-07-15 16:37:03 +00:00
|
|
|
return false;
|
2013-10-21 19:14:01 +00:00
|
|
|
};
|
2014-08-28 20:43:44 +00:00
|
|
|
headerWidgetNode.addEventListener("tm-close-tiddler",closeHandler,false);
|
|
|
|
bodyWidgetNode.addEventListener("tm-close-tiddler",closeHandler,false);
|
|
|
|
footerWidgetNode.addEventListener("tm-close-tiddler",closeHandler,false);
|
2022-12-03 17:26:44 +00:00
|
|
|
// Whether to close the modal dialog when the mask (area outside the modal) is clicked
|
|
|
|
if(tiddler.fields && (tiddler.fields["mask-closable"] === "yes" || tiddler.fields["mask-closable"] === "true")) {
|
|
|
|
modalBackdrop.addEventListener("click",closeHandler,false);
|
|
|
|
}
|
2012-07-17 17:28:47 +00:00
|
|
|
// Set the initial styles for the message
|
2012-10-26 09:28:32 +00:00
|
|
|
$tw.utils.setStyle(modalBackdrop,[
|
|
|
|
{opacity: "0"}
|
|
|
|
]);
|
|
|
|
$tw.utils.setStyle(modalWrapper,[
|
|
|
|
{transformOrigin: "0% 0%"},
|
2018-11-18 20:57:04 +00:00
|
|
|
{transform: "translateY(" + (-this.srcWindow.innerHeight) + "px)"}
|
2012-10-26 09:28:32 +00:00
|
|
|
]);
|
2012-07-16 11:57:44 +00:00
|
|
|
// Put the message into the document
|
2018-11-18 20:57:04 +00:00
|
|
|
this.srcDocument.body.appendChild(wrapper);
|
2012-07-18 10:45:17 +00:00
|
|
|
// Set up animation for the styles
|
2012-10-26 09:28:32 +00:00
|
|
|
$tw.utils.setStyle(modalBackdrop,[
|
2013-08-29 11:43:24 +00:00
|
|
|
{transition: "opacity " + duration + "ms ease-out"}
|
2012-10-26 09:28:32 +00:00
|
|
|
]);
|
|
|
|
$tw.utils.setStyle(modalWrapper,[
|
2013-08-28 14:15:56 +00:00
|
|
|
{transition: $tw.utils.roundTripPropertyName("transform") + " " + duration + "ms ease-in-out"}
|
2012-10-26 09:28:32 +00:00
|
|
|
]);
|
2012-07-18 10:45:17 +00:00
|
|
|
// Force layout
|
2012-08-02 21:32:34 +00:00
|
|
|
$tw.utils.forceLayout(modalBackdrop);
|
|
|
|
$tw.utils.forceLayout(modalWrapper);
|
2012-07-18 10:45:17 +00:00
|
|
|
// Set final animated styles
|
2012-10-26 09:28:32 +00:00
|
|
|
$tw.utils.setStyle(modalBackdrop,[
|
2013-02-10 18:44:00 +00:00
|
|
|
{opacity: "0.7"}
|
2012-10-26 09:28:32 +00:00
|
|
|
]);
|
|
|
|
$tw.utils.setStyle(modalWrapper,[
|
|
|
|
{transform: "translateY(0px)"}
|
|
|
|
]);
|
2012-07-15 16:37:03 +00:00
|
|
|
};
|
|
|
|
|
2013-06-04 15:19:47 +00:00
|
|
|
Modal.prototype.adjustPageClass = function() {
|
2018-11-18 20:57:04 +00:00
|
|
|
var windowContainer = $tw.pageContainer ? ($tw.pageContainer === this.srcDocument.body.firstChild ? $tw.pageContainer : this.srcDocument.body.firstChild) : null;
|
|
|
|
if(windowContainer) {
|
|
|
|
$tw.utils.toggleClass(windowContainer,"tc-modal-displayed",this.modalCount > 0);
|
2013-06-04 15:19:47 +00:00
|
|
|
}
|
2021-06-27 15:24:06 +00:00
|
|
|
$tw.utils.toggleClass(this.srcDocument.body,"tc-modal-prevent-scroll",this.modalCount > 0);
|
2013-06-04 15:19:47 +00:00
|
|
|
};
|
|
|
|
|
2012-07-15 16:37:03 +00:00
|
|
|
exports.Modal = Modal;
|
|
|
|
|
|
|
|
})();
|