1
0
mirror of https://github.com/Jermolene/TiddlyWiki5 synced 2024-06-16 10:29:54 +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) {
options = options || {};
var self = this,
refreshHandler,
duration = $tw.utils.getAnimationDuration(),
tiddler = this.wiki.getTiddler(title);
// Don't do anything if the tiddler doesn't exist
@ -83,9 +84,6 @@ Modal.prototype.display = function(title,options) {
variables: variables
});
headerWidgetNode.render(headerTitle,null);
this.wiki.addEventListener("change",function(changes) {
headerWidgetNode.refresh(changes,modalHeader,null);
});
// Render the body of the message
var bodyWidgetNode = this.wiki.makeTranscludeWidget(title,{
parentWidget: $tw.rootWidget,
@ -93,9 +91,6 @@ Modal.prototype.display = function(title,options) {
variables: variables
});
bodyWidgetNode.render(modalBody,null);
this.wiki.addEventListener("change",function(changes) {
bodyWidgetNode.refresh(changes,modalBody,null);
});
// Setup the link if present
if(options.downloadLink) {
modalLink.href = options.downloadLink;
@ -135,11 +130,17 @@ Modal.prototype.display = function(title,options) {
variables: variables
});
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);
});
};
this.wiki.addEventListener("change",refreshHandler);
// Add the close event handler
var closeHandler = function(event) {
// Remove our refresh handler
self.wiki.removeEventListener("change",refreshHandler);
// Decrease the modal count and adjust the body class
self.modalCount--;
self.adjustPageClass();

View File

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