notificationwidget.js
163 lines
| 5.3 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 | |||
Jessica B. Hamrick
|
r18004 | /** | ||
* Construct a NotificationWidget object. | ||||
* | ||||
* @constructor | ||||
* @param {string} selector - a jQuery selector string for the | ||||
* notification widget element | ||||
*/ | ||||
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(); | ||
Matthias BUSSONNIER
|
r15042 | this.inner = $('<span/>'); | ||
this.element.append(this.inner); | ||||
Brian Granger
|
r6047 | }; | ||
Jessica B. Hamrick
|
r18003 | /** | ||
* Add the 'notification_widget' CSS class to the widget element. | ||||
* | ||||
* @method style | ||||
*/ | ||||
Brian Granger
|
r6047 | NotificationWidget.prototype.style = function () { | ||
Min RK
|
r19298 | // use explicit bootstrap classes here, | ||
// because multiple inheritance in LESS doesn't work | ||||
// for this particular combination | ||||
this.element.addClass('notification_widget btn btn-xs navbar-btn'); | ||||
Brian Granger
|
r6047 | }; | ||
Jessica B. Hamrick
|
r18003 | /** | ||
* Set the notification widget message to display for a certain | ||||
* amount of time (timeout). The widget will be shown forever if | ||||
* timeout is <= 0 or undefined. If the widget is clicked while it | ||||
* is still displayed, execute an optional callback | ||||
* (click_callback). If the callback returns false, it will | ||||
* prevent the notification from being dismissed. | ||||
* | ||||
* Options: | ||||
* class - CSS class name for styling | ||||
* icon - CSS class name for the widget icon | ||||
* title - HTML title attribute for the widget | ||||
* | ||||
* @method set_message | ||||
* @param {string} msg - The notification to display | ||||
* @param {integer} [timeout] - The amount of time in milliseconds to display the widget | ||||
* @param {function} [click_callback] - The function to run when the widget is clicked | ||||
* @param {Object} [options] - Additional options | ||||
*/ | ||||
Jonathan Frederic
|
r17214 | NotificationWidget.prototype.set_message = function (msg, timeout, click_callback, options) { | ||
Jessica B. Hamrick
|
r18002 | options = options || {}; | ||
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(); | ||
Jessica B. Hamrick
|
r18003 | if (options.class) { | ||
Jessica B. Hamrick
|
r18002 | this.element.addClass(options.class); | ||
Matthias BUSSONNIER
|
r17368 | } | ||
Jessica B. Hamrick
|
r18002 | |||
// clear previous timer | ||||
Brian Granger
|
r6047 | if (this.timeout !== null) { | ||
clearTimeout(this.timeout); | ||||
this.timeout = null; | ||||
Matthias BUSSONNIER
|
r8205 | } | ||
Jessica B. Hamrick
|
r18002 | |||
// set the timer if a timeout is given | ||||
var that = this; | ||||
if (timeout !== undefined && timeout >= 0) { | ||||
Brian Granger
|
r6047 | this.timeout = setTimeout(function () { | ||
Matthias BUSSONNIER
|
r15042 | that.element.fadeOut(100, function () {that.inner.text('');}); | ||
Jessica B. Hamrick
|
r18002 | that.element.unbind('click'); | ||
Brian Granger
|
r6047 | that.timeout = null; | ||
Matthias BUSSONNIER
|
r8025 | }, timeout); | ||
Jessica B. Hamrick
|
r18002 | } | ||
// bind the click callback if it is given | ||||
if (click_callback !== undefined) { | ||||
Jessica B. Hamrick
|
r18005 | this.element.click(function () { | ||
Jessica B. Hamrick
|
r18002 | if (click_callback() !== false) { | ||
Matthias BUSSONNIER
|
r15042 | that.element.fadeOut(100, function () {that.inner.text('');}); | ||
Matthias BUSSONNIER
|
r8074 | } | ||
Jessica B. Hamrick
|
r18003 | that.element.unbind('click'); | ||
Jessica B. Hamrick
|
r18002 | if (that.timeout !== null) { | ||
Matthias BUSSONNIER
|
r8074 | clearTimeout(that.timeout); | ||
Jessica B. Hamrick
|
r18002 | that.timeout = null; | ||
Matthias BUSSONNIER
|
r8074 | } | ||
}); | ||||
} | ||||
Brian Granger
|
r6047 | }; | ||
Jessica B. Hamrick
|
r18003 | /** | ||
* Display an information message (styled with the 'info' | ||||
Jessica B. Hamrick
|
r18005 | * class). Arguments are the same as in set_message. Default | ||
* timeout is 3500 milliseconds. | ||||
Jessica B. Hamrick
|
r18003 | * | ||
* @method info | ||||
*/ | ||||
Matthias BUSSONNIER
|
r17368 | NotificationWidget.prototype.info = function (msg, timeout, click_callback, options) { | ||
Jessica B. Hamrick
|
r18002 | options = options || {}; | ||
options.class = options.class + ' info'; | ||||
timeout = timeout || 3500; | ||||
Matthias BUSSONNIER
|
r17368 | this.set_message(msg, timeout, click_callback, options); | ||
Jessica B. Hamrick
|
r18002 | }; | ||
Jessica B. Hamrick
|
r18003 | /** | ||
* Display a warning message (styled with the 'warning' | ||||
Jessica B. Hamrick
|
r18009 | * class). Arguments are the same as in set_message. Messages are | ||
* sticky by default. | ||||
Jessica B. Hamrick
|
r18003 | * | ||
* @method warning | ||||
*/ | ||||
Matthias BUSSONNIER
|
r17368 | NotificationWidget.prototype.warning = function (msg, timeout, click_callback, options) { | ||
Jessica B. Hamrick
|
r18002 | options = options || {}; | ||
options.class = options.class + ' warning'; | ||||
Matthias BUSSONNIER
|
r17368 | this.set_message(msg, timeout, click_callback, options); | ||
Jessica B. Hamrick
|
r18002 | }; | ||
Jessica B. Hamrick
|
r18003 | /** | ||
* Display a danger message (styled with the 'danger' | ||||
Jessica B. Hamrick
|
r18009 | * class). Arguments are the same as in set_message. Messages are | ||
* sticky by default. | ||||
Jessica B. Hamrick
|
r18003 | * | ||
* @method danger | ||||
*/ | ||||
Matthias BUSSONNIER
|
r17368 | NotificationWidget.prototype.danger = function (msg, timeout, click_callback, options) { | ||
Jessica B. Hamrick
|
r18002 | options = options || {}; | ||
options.class = options.class + ' danger'; | ||||
Matthias BUSSONNIER
|
r17368 | this.set_message(msg, timeout, click_callback, options); | ||
Jessica B. Hamrick
|
r18002 | }; | ||
Matthias BUSSONNIER
|
r17368 | |||
Jessica B. Hamrick
|
r18003 | /** | ||
* Get the text of the widget message. | ||||
* | ||||
* @method get_message | ||||
* @return {string} - the message text | ||||
*/ | ||||
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 | }); | ||