From 180dd71e8154d9a5415a7d7d11f4e8da934ed6a2 Mon Sep 17 00:00:00 2001 From: "Jessica B. Hamrick" Date: Thu, 25 Sep 2014 11:08:27 -0700 Subject: [PATCH] Allow timeout and click callback --- .../static/notebook/js/notificationwidget.js | 60 ++++++++++--------- 1 file changed, 32 insertions(+), 28 deletions(-) diff --git a/IPython/html/static/notebook/js/notificationwidget.js b/IPython/html/static/notebook/js/notificationwidget.js index d4ed891af..e08013b7c 100644 --- a/IPython/html/static/notebook/js/notificationwidget.js +++ b/IPython/html/static/notebook/js/notificationwidget.js @@ -16,11 +16,8 @@ define([ this.style(); } this.element.hide(); - var that = this; - this.inner = $(''); this.element.append(this.inner); - }; NotificationWidget.prototype.style = function () { @@ -34,9 +31,8 @@ define([ // click_callback : function called if user click on notification // could return false to prevent the notification to be dismissed NotificationWidget.prototype.set_message = function (msg, timeout, click_callback, options) { - var options = options || {}; - var callback = click_callback || function() {return true;}; - var that = this; + options = options || {}; + // unbind potential previous callback this.element.unbind('click'); this.inner.attr('class', options.icon); @@ -48,50 +44,58 @@ define([ this.element.removeClass(); this.style(); if (options.class){ - - this.element.addClass(options.class) + this.element.addClass(options.class); } + + // clear previous timer if (this.timeout !== null) { clearTimeout(this.timeout); this.timeout = null; } - if (timeout !== undefined && timeout >=0) { + + // set the timer if a timeout is given + var that = this; + if (timeout !== undefined && timeout >= 0) { this.timeout = setTimeout(function () { that.element.fadeOut(100, function () {that.inner.text('');}); + that.element.unbind('click'); that.timeout = null; }, timeout); - } else { + } + + // bind the click callback if it is given + if (click_callback !== undefined) { this.element.click(function() { - if( callback() !== false ) { + if (click_callback() !== false) { that.element.fadeOut(100, function () {that.inner.text('');}); that.element.unbind('click'); } - if (that.timeout !== undefined) { - that.timeout = undefined; + if (that.timeout !== null) { clearTimeout(that.timeout); + that.timeout = null; } }); } }; - NotificationWidget.prototype.info = function (msg, timeout, click_callback, options) { - var options = options || {}; - options.class = options.class +' info'; - var timeout = timeout || 3500; + options = options || {}; + options.class = options.class + ' info'; + timeout = timeout || 3500; this.set_message(msg, timeout, click_callback, options); - } - NotificationWidget.prototype.warning = function (msg, timeout, click_callback, options) { - var options = options || {}; - options.class = options.class +' warning'; - this.set_message(msg, timeout, click_callback, options); - } - NotificationWidget.prototype.danger = function (msg, timeout, click_callback, options) { - var options = options || {}; - options.class = options.class +' danger'; - this.set_message(msg, timeout, click_callback, options); - } + }; + NotificationWidget.prototype.warning = function (msg, timeout, click_callback, options) { + options = options || {}; + options.class = options.class + ' warning'; + this.set_message(msg, timeout, click_callback, options); + }; + + NotificationWidget.prototype.danger = function (msg, timeout, click_callback, options) { + options = options || {}; + options.class = options.class + ' danger'; + this.set_message(msg, timeout, click_callback, options); + }; NotificationWidget.prototype.get_message = function () { return this.inner.html();