// Copyright (c) IPython Development Team.1// Distributed under the terms of the Modified BSD License.23define([4'base/js/namespace',5'jquery',6], function(IPython, $) {7"use strict";89/**10* Construct a NotificationWidget object.11*12* @constructor13* @param {string} selector - a jQuery selector string for the14* notification widget element15*/16var NotificationWidget = function (selector) {17this.selector = selector;18this.timeout = null;19this.busy = false;20if (this.selector !== undefined) {21this.element = $(selector);22this.style();23}24this.element.hide();25this.inner = $('<span/>');26this.element.append(this.inner);27};2829/**30* Add the 'notification_widget' CSS class to the widget element.31*32* @method style33*/34NotificationWidget.prototype.style = function () {35// use explicit bootstrap classes here,36// because multiple inheritance in LESS doesn't work37// for this particular combination38this.element.addClass('notification_widget btn btn-xs navbar-btn');39};4041/**42* hide the widget and empty the text43**/44NotificationWidget.prototype.hide = function () {45var that = this;46this.element.fadeOut(100, function(){that.inner.text('');});47};4849/**50* Set the notification widget message to display for a certain51* amount of time (timeout). The widget will be shown forever if52* timeout is <= 0 or undefined. If the widget is clicked while it53* is still displayed, execute an optional callback54* (click_callback). If the callback returns false, it will55* prevent the notification from being dismissed.56*57* Options:58* class - CSS class name for styling59* icon - CSS class name for the widget icon60* title - HTML title attribute for the widget61*62* @method set_message63* @param {string} msg - The notification to display64* @param {integer} [timeout] - The amount of time in milliseconds to display the widget65* @param {function} [click_callback] - The function to run when the widget is clicked66* @param {Object} [options] - Additional options67*/68NotificationWidget.prototype.set_message = function (msg, timeout, click_callback, options) {69options = options || {};7071// unbind potential previous callback72this.element.unbind('click');73this.inner.attr('class', options.icon);74this.inner.attr('title', options.title);75this.inner.text(msg);76this.element.fadeIn(100);7778// reset previous set style79this.element.removeClass();80this.style();81if (options.class) {82this.element.addClass(options.class);83}8485// clear previous timer86if (this.timeout !== null) {87clearTimeout(this.timeout);88this.timeout = null;89}9091// set the timer if a timeout is given92var that = this;93if (timeout !== undefined && timeout >= 0) {94this.timeout = setTimeout(function () {95that.element.fadeOut(100, function () {that.inner.text('');});96that.element.unbind('click');97that.timeout = null;98}, timeout);99}100101// if no click callback assume we will just dismiss the notification102if (click_callback === undefined) {103click_callback = function(){return true};104}105// on click, remove widget if click callback say so106// and unbind click event.107this.element.click(function () {108if (click_callback() !== false) {109that.element.fadeOut(100, function () {that.inner.text('');});110that.element.unbind('click');111}112if (that.timeout !== null) {113clearTimeout(that.timeout);114that.timeout = null;115}116});117};118119/**120* Display an information message (styled with the 'info'121* class). Arguments are the same as in set_message. Default122* timeout is 3500 milliseconds.123*124* @method info125*/126NotificationWidget.prototype.info = function (msg, timeout, click_callback, options) {127options = options || {};128options.class = options.class + ' info';129timeout = timeout || 3500;130this.set_message(msg, timeout, click_callback, options);131};132133/**134* Display a warning message (styled with the 'warning'135* class). Arguments are the same as in set_message. Messages are136* sticky by default.137*138* @method warning139*/140NotificationWidget.prototype.warning = function (msg, timeout, click_callback, options) {141options = options || {};142options.class = options.class + ' warning';143this.set_message(msg, timeout, click_callback, options);144};145146/**147* Display a danger message (styled with the 'danger'148* class). Arguments are the same as in set_message. Messages are149* sticky by default.150*151* @method danger152*/153NotificationWidget.prototype.danger = function (msg, timeout, click_callback, options) {154options = options || {};155options.class = options.class + ' danger';156this.set_message(msg, timeout, click_callback, options);157};158159/**160* Get the text of the widget message.161*162* @method get_message163* @return {string} - the message text164*/165NotificationWidget.prototype.get_message = function () {166return this.inner.html();167};168169// For backwards compatibility.170IPython.NotificationWidget = NotificationWidget;171172return {'NotificationWidget': NotificationWidget};173});174175176