2013-05-07 17:08:44 +00:00
|
|
|
/*\
|
|
|
|
title: $:/core/modules/utils/dom/notifier.js
|
|
|
|
type: application/javascript
|
|
|
|
module-type: utils
|
|
|
|
|
|
|
|
Notifier 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");
|
2013-10-21 19:14:01 +00:00
|
|
|
|
2013-05-07 17:08:44 +00:00
|
|
|
var Notifier = function(wiki) {
|
|
|
|
this.wiki = wiki;
|
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
|
|
|
Display a notification
|
|
|
|
title: Title of tiddler containing the notification text
|
|
|
|
options: see below
|
|
|
|
Options include:
|
|
|
|
*/
|
|
|
|
Notifier.prototype.display = function(title,options) {
|
|
|
|
options = options || {};
|
|
|
|
// Create the wrapper divs
|
2015-05-06 07:07:12 +00:00
|
|
|
var self = this,
|
|
|
|
notification = document.createElement("div"),
|
2013-05-07 17:08:44 +00:00
|
|
|
tiddler = this.wiki.getTiddler(title),
|
2015-05-06 07:07:12 +00:00
|
|
|
duration = $tw.utils.getAnimationDuration(),
|
|
|
|
refreshHandler;
|
2013-05-07 17:08:44 +00:00
|
|
|
// Don't do anything if the tiddler doesn't exist
|
|
|
|
if(!tiddler) {
|
|
|
|
return;
|
|
|
|
}
|
2022-06-28 16:48:03 +00:00
|
|
|
// Add classes and roles
|
2014-08-28 17:21:08 +00:00
|
|
|
$tw.utils.addClass(notification,"tc-notification");
|
2022-06-28 16:48:03 +00:00
|
|
|
notification.setAttribute("role","alert");
|
2015-05-06 07:07:12 +00:00
|
|
|
// Create the variables
|
|
|
|
var variables = $tw.utils.extend({currentTiddler: title},options.variables);
|
2013-05-07 17:08:44 +00:00
|
|
|
// Render the body of the notification
|
2016-10-18 15:39:18 +00:00
|
|
|
var widgetNode = this.wiki.makeTranscludeWidget(title,{
|
|
|
|
parentWidget: $tw.rootWidget,
|
|
|
|
document: document,
|
|
|
|
variables: variables,
|
|
|
|
importPageMacros: true});
|
2013-10-21 19:14:01 +00:00
|
|
|
widgetNode.render(notification,null);
|
2015-05-06 07:07:12 +00:00
|
|
|
refreshHandler = function(changes) {
|
2013-10-21 19:14:01 +00:00
|
|
|
widgetNode.refresh(changes,notification,null);
|
2015-05-06 07:07:12 +00:00
|
|
|
};
|
|
|
|
this.wiki.addEventListener("change",refreshHandler);
|
2013-05-07 17:08:44 +00:00
|
|
|
// Set the initial styles for the notification
|
|
|
|
$tw.utils.setStyle(notification,[
|
|
|
|
{opacity: "0"},
|
|
|
|
{transformOrigin: "0% 0%"},
|
|
|
|
{transform: "translateY(" + (-window.innerHeight) + "px)"},
|
2013-08-29 11:43:24 +00:00
|
|
|
{transition: "opacity " + duration + "ms ease-out, " + $tw.utils.roundTripPropertyName("transform") + " " + duration + "ms ease-in-out"}
|
2013-05-07 17:08:44 +00:00
|
|
|
]);
|
|
|
|
// Add the notification to the DOM
|
|
|
|
document.body.appendChild(notification);
|
|
|
|
// Force layout
|
|
|
|
$tw.utils.forceLayout(notification);
|
|
|
|
// Set final animated styles
|
|
|
|
$tw.utils.setStyle(notification,[
|
|
|
|
{opacity: "1.0"},
|
|
|
|
{transform: "translateY(0px)"}
|
|
|
|
]);
|
|
|
|
// Set a timer to remove the notification
|
|
|
|
window.setTimeout(function() {
|
2015-05-06 07:07:12 +00:00
|
|
|
// Remove our change event handler
|
|
|
|
self.wiki.removeEventListener("change",refreshHandler);
|
2013-05-07 17:08:44 +00:00
|
|
|
// Force layout and animate the notification away
|
|
|
|
$tw.utils.forceLayout(notification);
|
|
|
|
$tw.utils.setStyle(notification,[
|
|
|
|
{opacity: "0.0"},
|
2013-05-08 12:27:04 +00:00
|
|
|
{transform: "translateX(" + (notification.offsetWidth) + "px)"}
|
2013-05-07 17:08:44 +00:00
|
|
|
]);
|
2014-01-09 22:26:21 +00:00
|
|
|
// Remove the modal message from the DOM once the transition ends
|
|
|
|
setTimeout(function() {
|
2013-05-07 17:08:44 +00:00
|
|
|
if(notification.parentNode) {
|
|
|
|
document.body.removeChild(notification);
|
|
|
|
}
|
2014-01-09 22:26:21 +00:00
|
|
|
},duration);
|
2013-05-07 17:08:44 +00:00
|
|
|
},$tw.config.preferences.notificationDuration);
|
|
|
|
};
|
|
|
|
|
|
|
|
exports.Notifier = Notifier;
|
|
|
|
|
|
|
|
})();
|