1
0
mirror of https://github.com/Jermolene/TiddlyWiki5 synced 2024-11-05 01:26:18 +00:00
TiddlyWiki5/core/modules/utils/dom/modal.js

165 lines
5.5 KiB
JavaScript
Raw Normal View History

/*\
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;
};
/*
Display a modal dialogue
title: Title of tiddler to display
options: see below
Options include:
downloadLink: Text of a big download link to include
*/
Modal.prototype.display = function(title,options) {
options = options || {};
// Create the wrapper divs
var wrapper = document.createElement("div"),
modalBackdrop = document.createElement("div"),
modalWrapper = document.createElement("div"),
modalHeader = document.createElement("div"),
2013-03-01 09:13:10 +00:00
headerTitle = document.createElement("h3"),
modalBody = document.createElement("div"),
modalLink = document.createElement("a"),
modalFooter = document.createElement("div"),
modalFooterHelp = document.createElement("span"),
modalFooterButtons = document.createElement("span"),
tiddler = this.wiki.getTiddler(title),
d = $tw.config.preferences.animationDuration + "ms";
2013-01-03 17:06:14 +00:00
// Don't do anything if the tiddler doesn't exist
if(!tiddler) {
return;
}
// 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(modalLink,"btn btn-large btn-block btn-success");
$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
var titleText;
if(tiddler && tiddler.fields && tiddler.fields.subtitle) {
titleText = tiddler.fields.subtitle;
} else {
titleText = title;
}
var headerParser = this.wiki.parseText("text/vnd.tiddlywiki-run",titleText,{parseAsInline: true}),
2013-01-03 17:06:14 +00:00
headerRenderTree = new $tw.WikiRenderTree(headerParser,{wiki: $tw.wiki});
headerRenderTree.execute({tiddlerTitle: title});
headerRenderTree.renderInDom(headerTitle);
this.wiki.addEventListener("change",function(changes) {
2013-01-03 17:06:14 +00:00
headerRenderTree.refreshInDom(changes);
});
// Render the body of the message
2013-01-03 17:06:14 +00:00
var bodyParser = this.wiki.parseTiddler(title),
bodyRenderTree = new $tw.WikiRenderTree(bodyParser,{wiki: $tw.wiki});
bodyRenderTree.execute({tiddlerTitle: title});
bodyRenderTree.renderInDom(modalBody);
this.wiki.addEventListener("change",function(changes) {
2013-01-03 17:06:14 +00:00
bodyRenderTree.refreshInDom(changes);
});
// Setup the link if present
if(options.downloadLink) {
modalLink.href = options.downloadLink
modalLink.appendChild(document.createTextNode("Right-click to save changes"));
modalBody.appendChild(modalLink);
}
// Render the footer of the message
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";
}
var footerText;
if(tiddler && tiddler.fields && tiddler.fields.footer) {
footerText = tiddler.fields.footer;
} else {
2013-02-04 15:29:25 +00:00
footerText = '<$button message="tw-close-tiddler" class="btn btn-primary">Close</$button>';
}
var footerParser = this.wiki.parseText("text/vnd.tiddlywiki-run",footerText,{parseAsInline: true}),
2013-01-03 17:06:14 +00:00
footerRenderTree = new $tw.WikiRenderTree(footerParser,{wiki: $tw.wiki});
footerRenderTree.execute({tiddlerTitle: title});
footerRenderTree.renderInDom(modalFooterButtons);
this.wiki.addEventListener("change",function(changes) {
2013-01-03 17:06:14 +00:00
footerRenderTree.refreshInDom(changes);
});
// Add the close event handler
2013-02-04 15:29:25 +00:00
wrapper.addEventListener("tw-close-tiddler",function(event) {
// 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)"}
]);
// Set up an event for the transition end
2012-10-26 09:28:32 +00:00
modalWrapper.addEventListener($tw.utils.convertEventName("transitionEnd"),function(event) {
if(wrapper.parentNode) {
// Remove the modal message from the DOM
document.body.removeChild(wrapper);
}
},false);
2013-02-04 15:29:25 +00:00
// Don't let anyone else handle the tw-close-tiddler message
2012-07-15 22:07:07 +00:00
event.stopPropagation();
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)"}
]);
// Put the message into the document
document.body.appendChild(wrapper);
// 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,[
2012-11-06 17:21:56 +00:00
{transition: $tw.utils.roundTripPropertyName("transform") + " " + $tw.config.preferences.animationDurationMs + " ease-in-out"}
2012-10-26 09:28:32 +00:00
]);
// Force layout
2012-08-02 21:32:34 +00:00
$tw.utils.forceLayout(modalBackdrop);
$tw.utils.forceLayout(modalWrapper);
// Set final animated styles
2012-10-26 09:28:32 +00:00
$tw.utils.setStyle(modalBackdrop,[
{opacity: "0.7"}
2012-10-26 09:28:32 +00:00
]);
$tw.utils.setStyle(modalWrapper,[
{transform: "translateY(0px)"}
]);
};
exports.Modal = Modal;
})();