From 10c042f5373673154a397442f3c28a7975928c92 Mon Sep 17 00:00:00 2001 From: shlomi aharoni Date: Sun, 24 Jan 2016 10:58:50 +0200 Subject: [PATCH 1/2] Fix memory leak when closing notifiers manually --- bootstrap-notify.js | 46 ++++++++------------------------------------- 1 file changed, 8 insertions(+), 38 deletions(-) diff --git a/bootstrap-notify.js b/bootstrap-notify.js index 9187abe..26e3dff 100644 --- a/bootstrap-notify.js +++ b/bootstrap-notify.js @@ -48,7 +48,6 @@ onShown: null, onClose: null, onClosed: null, - onClick: null, icon_type: 'class', template: '' }; @@ -66,7 +65,7 @@ $('[data-notify="container"]').each(function (i, el) { var $el = $(el); - var title = $el.find('[data-notify="title"]').html().trim(); + var title = $el.find('[data-notify="title"]').text().trim(); var message = $el.find('[data-notify="message"]').html().trim(); // The input string might be different than the actual parsed HTML string! @@ -121,7 +120,7 @@ this.init(); } } - + $.extend(Notify.prototype, { init: function () { var self = this; @@ -164,7 +163,6 @@ } $icon.attr('src', commands[cmd]); } - self.settings.content.icon = commands[command]; break; case "progress": var newDelay = self.settings.delay - (self.settings.delay * (commands[cmd] / 100)); @@ -281,7 +279,6 @@ this.$ele.one(this.animations.start, function () { hasAnimation = true; }).one(this.animations.end, function () { - self.$ele.removeClass(self.settings.animate.enter); if ($.isFunction(self.settings.onShown)) { self.settings.onShown.call(this); } @@ -302,14 +299,6 @@ self.close(); }); - if ($.isFunction(self.settings.onClick)) { - this.$ele.on('click', function (event) { - if (event.target != self.$ele.find('[data-notify="dismiss"]')[0]) { - self.settings.onClick.call(this, event); - } - }); - } - this.$ele.mouseover(function () { $(this).data('data-hover', "true"); }).mouseout(function () { @@ -319,7 +308,7 @@ if (this.settings.delay > 0) { self.$ele.data('notify-delay', self.settings.delay); - var timer = setInterval(function () { + this.timerInterval = setInterval(function () { var delay = parseInt(self.$ele.data('notify-delay')) - self.settings.timer; if ((self.$ele.data('data-hover') === 'false' && self.settings.mouse_over === "pause") || self.settings.mouse_over != "pause") { var percent = ((self.settings.delay - delay) / self.settings.delay) * 100; @@ -327,13 +316,13 @@ self.$ele.find('[data-notify="progressbar"] > div').attr('aria-valuenow', percent).css('width', percent + '%'); } if (delay <= -(self.settings.timer)) { - clearInterval(timer); self.close(); } }, self.settings.timer); } }, close: function () { + clearInterval(this.timerInterval); var self = this, posX = parseInt(this.$ele.css(this.settings.placement.from)), hasAnimation = false; @@ -385,31 +374,12 @@ defaults = $.extend(true, {}, defaults, options); return defaults; }; - - $.notifyClose = function (selector) { - - if (typeof selector === "undefined" || selector === "all") { + $.notifyClose = function (command) { + if (typeof command === "undefined" || command === "all") { $('[data-notify]').find('[data-notify="dismiss"]').trigger('click'); - }else if(selector === 'success' || selector === 'info' || selector === 'warning' || selector === 'danger'){ - $('.alert-' + selector + '[data-notify]').find('[data-notify="dismiss"]').trigger('click'); - } else if(selector){ - $(selector + '[data-notify]').find('[data-notify="dismiss"]').trigger('click'); - } - else { - $('[data-notify-position="' + selector + '"]').find('[data-notify="dismiss"]').trigger('click'); + } else { + $('[data-notify-position="' + command + '"]').find('[data-notify="dismiss"]').trigger('click'); } }; - $.notifyCloseExcept = function (selector) { - - if(selector === 'success' || selector === 'info' || selector === 'warning' || selector === 'danger'){ - $('[data-notify]').not('.alert-' + selector).find('[data-notify="dismiss"]').trigger('click'); - } else{ - $('[data-notify]').not(selector).find('[data-notify="dismiss"]').trigger('click'); - } - }; - - })); - - From 6a86e5d2b2e76f337ff0b9284df83630b3eada16 Mon Sep 17 00:00:00 2001 From: shlomi aharoni Date: Sun, 24 Jan 2016 11:12:46 +0200 Subject: [PATCH 2/2] Fix memory leak when closing notifiers manually --- bootstrap-notify.js | 43 ++++++++++++++++++++++++++++++++++++------- 1 file changed, 36 insertions(+), 7 deletions(-) diff --git a/bootstrap-notify.js b/bootstrap-notify.js index 26e3dff..c00ba70 100644 --- a/bootstrap-notify.js +++ b/bootstrap-notify.js @@ -48,6 +48,7 @@ onShown: null, onClose: null, onClosed: null, + onClick: null, icon_type: 'class', template: '' }; @@ -65,7 +66,7 @@ $('[data-notify="container"]').each(function (i, el) { var $el = $(el); - var title = $el.find('[data-notify="title"]').text().trim(); + var title = $el.find('[data-notify="title"]').html().trim(); var message = $el.find('[data-notify="message"]').html().trim(); // The input string might be different than the actual parsed HTML string! @@ -120,7 +121,7 @@ this.init(); } } - + $.extend(Notify.prototype, { init: function () { var self = this; @@ -163,6 +164,7 @@ } $icon.attr('src', commands[cmd]); } + self.settings.content.icon = commands[command]; break; case "progress": var newDelay = self.settings.delay - (self.settings.delay * (commands[cmd] / 100)); @@ -279,6 +281,7 @@ this.$ele.one(this.animations.start, function () { hasAnimation = true; }).one(this.animations.end, function () { + self.$ele.removeClass(self.settings.animate.enter); if ($.isFunction(self.settings.onShown)) { self.settings.onShown.call(this); } @@ -299,6 +302,14 @@ self.close(); }); + if ($.isFunction(self.settings.onClick)) { + this.$ele.on('click', function (event) { + if (event.target != self.$ele.find('[data-notify="dismiss"]')[0]) { + self.settings.onClick.call(this, event); + } + }); + } + this.$ele.mouseover(function () { $(this).data('data-hover', "true"); }).mouseout(function () { @@ -316,6 +327,7 @@ self.$ele.find('[data-notify="progressbar"] > div').attr('aria-valuenow', percent).css('width', percent + '%'); } if (delay <= -(self.settings.timer)) { + self.close(); } }, self.settings.timer); @@ -374,12 +386,29 @@ defaults = $.extend(true, {}, defaults, options); return defaults; }; - $.notifyClose = function (command) { - if (typeof command === "undefined" || command === "all") { + + $.notifyClose = function (selector) { + + if (typeof selector === "undefined" || selector === "all") { $('[data-notify]').find('[data-notify="dismiss"]').trigger('click'); - } else { - $('[data-notify-position="' + command + '"]').find('[data-notify="dismiss"]').trigger('click'); + }else if(selector === 'success' || selector === 'info' || selector === 'warning' || selector === 'danger'){ + $('.alert-' + selector + '[data-notify]').find('[data-notify="dismiss"]').trigger('click'); + } else if(selector){ + $(selector + '[data-notify]').find('[data-notify="dismiss"]').trigger('click'); + } + else { + $('[data-notify-position="' + selector + '"]').find('[data-notify="dismiss"]').trigger('click'); } }; -})); + $.notifyCloseExcept = function (selector) { + + if(selector === 'success' || selector === 'info' || selector === 'warning' || selector === 'danger'){ + $('[data-notify]').not('.alert-' + selector).find('[data-notify="dismiss"]').trigger('click'); + } else{ + $('[data-notify]').not(selector).find('[data-notify="dismiss"]').trigger('click'); + } + }; + + +})); \ No newline at end of file