1
0
mirror of https://github.com/Jermolene/TiddlyWiki5 synced 2024-06-25 23:03:15 +00:00

Fix event handler leak for modals and notifications

Also add support for passing custom variables into notifications.

Fixes #1694
This commit is contained in:
Jermolene 2015-05-06 08:07:12 +01:00
parent 03f3b1fdb4
commit 86e901f375
2 changed files with 21 additions and 13 deletions

View File

@ -29,6 +29,7 @@ Options include:
Modal.prototype.display = function(title,options) { Modal.prototype.display = function(title,options) {
options = options || {}; options = options || {};
var self = this, var self = this,
refreshHandler,
duration = $tw.utils.getAnimationDuration(), duration = $tw.utils.getAnimationDuration(),
tiddler = this.wiki.getTiddler(title); tiddler = this.wiki.getTiddler(title);
// Don't do anything if the tiddler doesn't exist // Don't do anything if the tiddler doesn't exist
@ -83,9 +84,6 @@ Modal.prototype.display = function(title,options) {
variables: variables variables: variables
}); });
headerWidgetNode.render(headerTitle,null); headerWidgetNode.render(headerTitle,null);
this.wiki.addEventListener("change",function(changes) {
headerWidgetNode.refresh(changes,modalHeader,null);
});
// Render the body of the message // Render the body of the message
var bodyWidgetNode = this.wiki.makeTranscludeWidget(title,{ var bodyWidgetNode = this.wiki.makeTranscludeWidget(title,{
parentWidget: $tw.rootWidget, parentWidget: $tw.rootWidget,
@ -93,9 +91,6 @@ Modal.prototype.display = function(title,options) {
variables: variables variables: variables
}); });
bodyWidgetNode.render(modalBody,null); bodyWidgetNode.render(modalBody,null);
this.wiki.addEventListener("change",function(changes) {
bodyWidgetNode.refresh(changes,modalBody,null);
});
// Setup the link if present // Setup the link if present
if(options.downloadLink) { if(options.downloadLink) {
modalLink.href = options.downloadLink; modalLink.href = options.downloadLink;
@ -135,11 +130,17 @@ Modal.prototype.display = function(title,options) {
variables: variables variables: variables
}); });
footerWidgetNode.render(modalFooterButtons,null); footerWidgetNode.render(modalFooterButtons,null);
this.wiki.addEventListener("change",function(changes) { // Set up the refresh handler
refreshHandler = function(changes) {
headerWidgetNode.refresh(changes,modalHeader,null);
bodyWidgetNode.refresh(changes,modalBody,null);
footerWidgetNode.refresh(changes,modalFooterButtons,null); footerWidgetNode.refresh(changes,modalFooterButtons,null);
}); };
this.wiki.addEventListener("change",refreshHandler);
// Add the close event handler // Add the close event handler
var closeHandler = function(event) { var closeHandler = function(event) {
// Remove our refresh handler
self.wiki.removeEventListener("change",refreshHandler);
// Decrease the modal count and adjust the body class // Decrease the modal count and adjust the body class
self.modalCount--; self.modalCount--;
self.adjustPageClass(); self.adjustPageClass();

View File

@ -27,21 +27,26 @@ Options include:
Notifier.prototype.display = function(title,options) { Notifier.prototype.display = function(title,options) {
options = options || {}; options = options || {};
// Create the wrapper divs // Create the wrapper divs
var notification = document.createElement("div"), var self = this,
notification = document.createElement("div"),
tiddler = this.wiki.getTiddler(title), tiddler = this.wiki.getTiddler(title),
duration = $tw.utils.getAnimationDuration(); duration = $tw.utils.getAnimationDuration(),
refreshHandler;
// Don't do anything if the tiddler doesn't exist // Don't do anything if the tiddler doesn't exist
if(!tiddler) { if(!tiddler) {
return; return;
} }
// Add classes // Add classes
$tw.utils.addClass(notification,"tc-notification"); $tw.utils.addClass(notification,"tc-notification");
// Create the variables
var variables = $tw.utils.extend({currentTiddler: title},options.variables);
// Render the body of the notification // Render the body of the notification
var widgetNode = this.wiki.makeTranscludeWidget(title,{parentWidget: $tw.rootWidget, document: document}); var widgetNode = this.wiki.makeTranscludeWidget(title,{parentWidget: $tw.rootWidget, document: document, variables: variables});
widgetNode.render(notification,null); widgetNode.render(notification,null);
this.wiki.addEventListener("change",function(changes) { refreshHandler = function(changes) {
widgetNode.refresh(changes,notification,null); widgetNode.refresh(changes,notification,null);
}); };
this.wiki.addEventListener("change",refreshHandler);
// Set the initial styles for the notification // Set the initial styles for the notification
$tw.utils.setStyle(notification,[ $tw.utils.setStyle(notification,[
{opacity: "0"}, {opacity: "0"},
@ -60,6 +65,8 @@ Notifier.prototype.display = function(title,options) {
]); ]);
// Set a timer to remove the notification // Set a timer to remove the notification
window.setTimeout(function() { window.setTimeout(function() {
// Remove our change event handler
self.wiki.removeEventListener("change",refreshHandler);
// Force layout and animate the notification away // Force layout and animate the notification away
$tw.utils.forceLayout(notification); $tw.utils.forceLayout(notification);
$tw.utils.setStyle(notification,[ $tw.utils.setStyle(notification,[