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";
|
|
|
|
|
|
|
|
var Modal = function(wiki) {
|
|
|
|
this.wiki = wiki;
|
|
|
|
};
|
|
|
|
|
|
|
|
Modal.prototype.display = function(title) {
|
2012-07-16 11:57:44 +00:00
|
|
|
// Create the wrapper divs
|
2012-07-15 16:37:03 +00:00
|
|
|
var wrapper = document.createElement("div"),
|
2012-07-16 11:57:44 +00:00
|
|
|
modalBackdrop = document.createElement("div"),
|
|
|
|
modalWrapper = document.createElement("div"),
|
|
|
|
modalHeader = document.createElement("div"),
|
|
|
|
headerTitle = document.createElement("h1"),
|
|
|
|
modalBody = document.createElement("div"),
|
|
|
|
modalFooter = document.createElement("div"),
|
2012-07-16 12:47:28 +00:00
|
|
|
modalFooterHelp = document.createElement("span"),
|
|
|
|
modalFooterButtons = document.createElement("span"),
|
2012-07-18 10:45:17 +00:00
|
|
|
tiddler = this.wiki.getTiddler(title),
|
|
|
|
d = $tw.config.preferences.animationDuration + "ms";
|
2012-07-16 11:57:44 +00:00
|
|
|
// Add classes
|
|
|
|
$tw.utils.addClass(modalBackdrop,"modal-backdrop");
|
|
|
|
$tw.utils.addClass(modalWrapper,"modal");
|
|
|
|
$tw.utils.addClass(modalHeader,"modal-header");
|
|
|
|
$tw.utils.addClass(modalBody,"modal-body");
|
|
|
|
$tw.utils.addClass(modalFooter,"modal-footer");
|
|
|
|
// 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);
|
|
|
|
// Render the title of the message
|
2012-07-16 13:04:29 +00:00
|
|
|
var titleText;
|
|
|
|
if(tiddler && tiddler.fields && tiddler.fields.subtitle) {
|
|
|
|
titleText = tiddler.fields.subtitle;
|
|
|
|
} else {
|
|
|
|
titleText = title;
|
|
|
|
}
|
|
|
|
var headerRenderer = this.wiki.parseText("text/x-tiddlywiki-run",titleText);
|
2012-07-16 11:57:44 +00:00
|
|
|
headerRenderer.execute([],title);
|
|
|
|
headerRenderer.renderInDom(headerTitle);
|
|
|
|
this.wiki.addEventListener("",function(changes) {
|
|
|
|
headerRenderer.refreshInDom(changes);
|
2012-07-15 16:37:03 +00:00
|
|
|
});
|
2012-07-16 11:57:44 +00:00
|
|
|
// Render the body of the message
|
|
|
|
var bodyRenderer = this.wiki.parseTiddler(title);
|
|
|
|
bodyRenderer.execute([],title);
|
|
|
|
bodyRenderer.renderInDom(modalBody);
|
|
|
|
this.wiki.addEventListener("",function(changes) {
|
|
|
|
bodyRenderer.refreshInDom(changes);
|
|
|
|
});
|
|
|
|
// Render the footer of the message
|
2012-07-16 12:47:28 +00:00
|
|
|
if(tiddler && tiddler.fields && tiddler.fields.help) {
|
|
|
|
var link = document.createElement("a");
|
|
|
|
link.setAttribute("href",tiddler.fields.help);
|
|
|
|
link.setAttribute("target","_blank");
|
|
|
|
link.appendChild(document.createTextNode("Help"));
|
|
|
|
modalFooterHelp.appendChild(link);
|
|
|
|
modalFooterHelp.style.float = "left";
|
|
|
|
}
|
2012-07-16 13:04:29 +00:00
|
|
|
var footerText;
|
|
|
|
if(tiddler && tiddler.fields && tiddler.fields.footer) {
|
|
|
|
footerText = tiddler.fields.footer;
|
|
|
|
} else {
|
|
|
|
footerText = "<<button close class:'btn btn-primary'><Close>>";
|
|
|
|
}
|
|
|
|
var footerRenderer = this.wiki.parseText("text/x-tiddlywiki-run",footerText);
|
2012-07-16 11:57:44 +00:00
|
|
|
footerRenderer.execute([],title);
|
|
|
|
footerRenderer.renderInDom(modalFooterButtons);
|
|
|
|
this.wiki.addEventListener("",function(changes) {
|
|
|
|
footerRenderer.refreshInDom(changes);
|
|
|
|
});
|
|
|
|
// Add the close event handler
|
2012-07-15 16:37:03 +00:00
|
|
|
wrapper.addEventListener("tw-close",function(event) {
|
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,[
|
|
|
|
{transform: "translateY(" + window.innerHeight + "px)"}
|
|
|
|
]);
|
2012-07-18 10:45:17 +00:00
|
|
|
// Set up an event for the transition end
|
2012-10-26 09:28:32 +00:00
|
|
|
modalWrapper.addEventListener($tw.utils.convertEventName("transitionEnd"),function(event) {
|
2012-07-18 10:45:17 +00:00
|
|
|
if(wrapper.parentNode) {
|
|
|
|
// Remove the modal message from the DOM
|
|
|
|
document.body.removeChild(wrapper);
|
|
|
|
}
|
|
|
|
},false);
|
|
|
|
// Don't let anyone else handle the tw-close message
|
2012-07-15 22:07:07 +00:00
|
|
|
event.stopPropagation();
|
2012-07-15 16:37:03 +00:00
|
|
|
return false;
|
|
|
|
},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%"},
|
|
|
|
{transform: "translateY(" + (-window.innerHeight) + "px)"}
|
|
|
|
]);
|
2012-07-16 11:57:44 +00:00
|
|
|
// Put the message into the document
|
2012-07-15 16:37:03 +00:00
|
|
|
document.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,[
|
|
|
|
{transition: "opacity " + d + " ease-out"}
|
|
|
|
]);
|
|
|
|
$tw.utils.setStyle(modalWrapper,[
|
|
|
|
{transition: $tw.utils.roundTripPropertyName("transform") + " " + $tw.config.preferences.animationDurationMs + " ease-in-out"},
|
|
|
|
]);
|
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,[
|
|
|
|
{opacity: "1"}
|
|
|
|
]);
|
|
|
|
$tw.utils.setStyle(modalWrapper,[
|
|
|
|
{transform: "translateY(0px)"}
|
|
|
|
]);
|
2012-07-15 16:37:03 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
exports.Modal = Modal;
|
|
|
|
|
|
|
|
})();
|