notificationwidget.js
104 lines
| 3.4 KiB
| application/javascript
|
JavascriptLexer
Jonathan Frederic
|
r17200 | // Copyright (c) IPython Development Team. | ||
// Distributed under the terms of the Modified BSD License. | ||||
Brian Granger
|
r6047 | |||
Jonathan Frederic
|
r17200 | define([ | ||
'base/js/namespace', | ||||
'jquery', | ||||
], function(IPython, $) { | ||||
Matthias BUSSONNIER
|
r8206 | "use strict"; | ||
Brian Granger
|
r6047 | |||
var NotificationWidget = function (selector) { | ||||
this.selector = selector; | ||||
this.timeout = null; | ||||
this.busy = false; | ||||
if (this.selector !== undefined) { | ||||
this.element = $(selector); | ||||
this.style(); | ||||
} | ||||
Matthias BUSSONNIER
|
r8025 | this.element.hide(); | ||
var that = this; | ||||
Matthias BUSSONNIER
|
r8074 | |||
Matthias BUSSONNIER
|
r15042 | this.inner = $('<span/>'); | ||
this.element.append(this.inner); | ||||
Brian Granger
|
r6047 | }; | ||
NotificationWidget.prototype.style = function () { | ||||
Matthias BUSSONNIER
|
r17368 | this.element.addClass('notification_widget'); | ||
Brian Granger
|
r6047 | }; | ||
Matthias BUSSONNIER
|
r8074 | // msg : message to display | ||
// timeout : time in ms before diseapearing | ||||
// | ||||
// if timeout <= 0 | ||||
// click_callback : function called if user click on notification | ||||
// could return false to prevent the notification to be dismissed | ||||
Jonathan Frederic
|
r17214 | NotificationWidget.prototype.set_message = function (msg, timeout, click_callback, options) { | ||
Matthias BUSSONNIER
|
r17368 | var options = options || {}; | ||
var callback = click_callback || function() {return true;}; | ||||
Brian Granger
|
r6047 | var that = this; | ||
Matthias BUSSONNIER
|
r17368 | // unbind potential previous callback | ||
this.element.unbind('click'); | ||||
Jonathan Frederic
|
r17214 | this.inner.attr('class', options.icon); | ||
this.inner.attr('title', options.title); | ||||
Matthias BUSSONNIER
|
r15042 | this.inner.text(msg); | ||
Brian Granger
|
r6047 | this.element.fadeIn(100); | ||
Matthias BUSSONNIER
|
r17368 | |||
// reset previous set style | ||||
Matthias BUSSONNIER
|
r17391 | this.element.removeClass(); | ||
Matthias BUSSONNIER
|
r17368 | this.style(); | ||
if (options.class){ | ||||
this.element.addClass(options.class) | ||||
} | ||||
Brian Granger
|
r6047 | if (this.timeout !== null) { | ||
clearTimeout(this.timeout); | ||||
this.timeout = null; | ||||
Matthias BUSSONNIER
|
r8205 | } | ||
Matthias BUSSONNIER
|
r8025 | if (timeout !== undefined && timeout >=0) { | ||
Brian Granger
|
r6047 | this.timeout = setTimeout(function () { | ||
Matthias BUSSONNIER
|
r15042 | that.element.fadeOut(100, function () {that.inner.text('');}); | ||
Brian Granger
|
r6047 | that.timeout = null; | ||
Matthias BUSSONNIER
|
r8025 | }, timeout); | ||
Matthias BUSSONNIER
|
r8074 | } else { | ||
Matthias BUSSONNIER
|
r8205 | this.element.click(function() { | ||
Jonathan Frederic
|
r17200 | if( callback() !== false ) { | ||
Matthias BUSSONNIER
|
r15042 | that.element.fadeOut(100, function () {that.inner.text('');}); | ||
Matthias BUSSONNIER
|
r8205 | that.element.unbind('click'); | ||
Matthias BUSSONNIER
|
r8074 | } | ||
if (that.timeout !== undefined) { | ||||
Matthias BUSSONNIER
|
r8205 | that.timeout = undefined; | ||
Matthias BUSSONNIER
|
r8074 | clearTimeout(that.timeout); | ||
} | ||||
}); | ||||
} | ||||
Brian Granger
|
r6047 | }; | ||
Matthias BUSSONNIER
|
r17368 | |||
NotificationWidget.prototype.info = function (msg, timeout, click_callback, options) { | ||||
var options = options || {}; | ||||
options.class = options.class +' info'; | ||||
var 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); | ||||
} | ||||
Brian Granger
|
r6047 | NotificationWidget.prototype.get_message = function () { | ||
Matthias BUSSONNIER
|
r15042 | return this.inner.html(); | ||
Brian Granger
|
r6047 | }; | ||
Matthias BUSSONNIER
|
r17368 | // For backwards compatibility. | ||
Brian Granger
|
r6047 | IPython.NotificationWidget = NotificationWidget; | ||
Jonathan Frederic
|
r17201 | return {'NotificationWidget': NotificationWidget}; | ||
Jonathan Frederic
|
r17200 | }); | ||