##// END OF EJS Templates
Allow timeout and click callback
Jessica B. Hamrick -
Show More
@@ -1,104 +1,108
1 1 // Copyright (c) IPython Development Team.
2 2 // Distributed under the terms of the Modified BSD License.
3 3
4 4 define([
5 5 'base/js/namespace',
6 6 'jquery',
7 7 ], function(IPython, $) {
8 8 "use strict";
9 9
10 10 var NotificationWidget = function (selector) {
11 11 this.selector = selector;
12 12 this.timeout = null;
13 13 this.busy = false;
14 14 if (this.selector !== undefined) {
15 15 this.element = $(selector);
16 16 this.style();
17 17 }
18 18 this.element.hide();
19 var that = this;
20
21 19 this.inner = $('<span/>');
22 20 this.element.append(this.inner);
23
24 21 };
25 22
26 23 NotificationWidget.prototype.style = function () {
27 24 this.element.addClass('notification_widget');
28 25 };
29 26
30 27 // msg : message to display
31 28 // timeout : time in ms before diseapearing
32 29 //
33 30 // if timeout <= 0
34 31 // click_callback : function called if user click on notification
35 32 // could return false to prevent the notification to be dismissed
36 33 NotificationWidget.prototype.set_message = function (msg, timeout, click_callback, options) {
37 var options = options || {};
38 var callback = click_callback || function() {return true;};
39 var that = this;
34 options = options || {};
35
40 36 // unbind potential previous callback
41 37 this.element.unbind('click');
42 38 this.inner.attr('class', options.icon);
43 39 this.inner.attr('title', options.title);
44 40 this.inner.text(msg);
45 41 this.element.fadeIn(100);
46 42
47 43 // reset previous set style
48 44 this.element.removeClass();
49 45 this.style();
50 46 if (options.class){
51
52 this.element.addClass(options.class)
47 this.element.addClass(options.class);
53 48 }
49
50 // clear previous timer
54 51 if (this.timeout !== null) {
55 52 clearTimeout(this.timeout);
56 53 this.timeout = null;
57 54 }
58 if (timeout !== undefined && timeout >=0) {
55
56 // set the timer if a timeout is given
57 var that = this;
58 if (timeout !== undefined && timeout >= 0) {
59 59 this.timeout = setTimeout(function () {
60 60 that.element.fadeOut(100, function () {that.inner.text('');});
61 that.element.unbind('click');
61 62 that.timeout = null;
62 63 }, timeout);
63 } else {
64 }
65
66 // bind the click callback if it is given
67 if (click_callback !== undefined) {
64 68 this.element.click(function() {
65 if( callback() !== false ) {
69 if (click_callback() !== false) {
66 70 that.element.fadeOut(100, function () {that.inner.text('');});
67 71 that.element.unbind('click');
68 72 }
69 if (that.timeout !== undefined) {
70 that.timeout = undefined;
73 if (that.timeout !== null) {
71 74 clearTimeout(that.timeout);
75 that.timeout = null;
72 76 }
73 77 });
74 78 }
75 79 };
76 80
77
78 81 NotificationWidget.prototype.info = function (msg, timeout, click_callback, options) {
79 var options = options || {};
80 options.class = options.class +' info';
81 var timeout = timeout || 3500;
82 options = options || {};
83 options.class = options.class + ' info';
84 timeout = timeout || 3500;
82 85 this.set_message(msg, timeout, click_callback, options);
83 }
86 };
87
84 88 NotificationWidget.prototype.warning = function (msg, timeout, click_callback, options) {
85 var options = options || {};
86 options.class = options.class +' warning';
89 options = options || {};
90 options.class = options.class + ' warning';
87 91 this.set_message(msg, timeout, click_callback, options);
88 }
92 };
93
89 94 NotificationWidget.prototype.danger = function (msg, timeout, click_callback, options) {
90 var options = options || {};
91 options.class = options.class +' danger';
95 options = options || {};
96 options.class = options.class + ' danger';
92 97 this.set_message(msg, timeout, click_callback, options);
93 }
94
98 };
95 99
96 100 NotificationWidget.prototype.get_message = function () {
97 101 return this.inner.html();
98 102 };
99 103
100 104 // For backwards compatibility.
101 105 IPython.NotificationWidget = NotificationWidget;
102 106
103 107 return {'NotificationWidget': NotificationWidget};
104 108 });
General Comments 0
You need to be logged in to leave comments. Login now