notificationwidget.js
77 lines
| 2.5 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.button(); | ||
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 () { | ||||
MinRK
|
r10932 | this.element.addClass('notification_widget pull-right'); | ||
Brian Granger
|
r6047 | this.element.addClass('border-box-sizing'); | ||
}; | ||||
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) { | ||
options = options || {}; | ||||
Matthias BUSSONNIER
|
r8205 | var callback = click_callback || function() {return false;}; | ||
Brian Granger
|
r6047 | var that = this; | ||
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); | ||
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 | }; | ||
NotificationWidget.prototype.get_message = function () { | ||||
Matthias BUSSONNIER
|
r15042 | return this.inner.html(); | ||
Brian Granger
|
r6047 | }; | ||
Jonathan Frederic
|
r17200 | // For backwards compatability. | ||
Brian Granger
|
r6047 | IPython.NotificationWidget = NotificationWidget; | ||
Jonathan Frederic
|
r17201 | return {'NotificationWidget': NotificationWidget}; | ||
Jonathan Frederic
|
r17200 | }); | ||