Show More
@@ -1,108 +1,150 | |||
|
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 | 19 | this.inner = $('<span/>'); |
|
20 | 20 | this.element.append(this.inner); |
|
21 | 21 | }; |
|
22 | 22 | |
|
23 | /** | |
|
24 | * Add the 'notification_widget' CSS class to the widget element. | |
|
25 | * | |
|
26 | * @method style | |
|
27 | */ | |
|
23 | 28 | NotificationWidget.prototype.style = function () { |
|
24 | 29 | this.element.addClass('notification_widget'); |
|
25 | 30 | }; |
|
26 | 31 | |
|
27 | // msg : message to display | |
|
28 | // timeout : time in ms before diseapearing | |
|
29 | // | |
|
30 | // if timeout <= 0 | |
|
31 | // click_callback : function called if user click on notification | |
|
32 | // could return false to prevent the notification to be dismissed | |
|
32 | /** | |
|
33 | * Set the notification widget message to display for a certain | |
|
34 | * amount of time (timeout). The widget will be shown forever if | |
|
35 | * timeout is <= 0 or undefined. If the widget is clicked while it | |
|
36 | * is still displayed, execute an optional callback | |
|
37 | * (click_callback). If the callback returns false, it will | |
|
38 | * prevent the notification from being dismissed. | |
|
39 | * | |
|
40 | * Options: | |
|
41 | * class - CSS class name for styling | |
|
42 | * icon - CSS class name for the widget icon | |
|
43 | * title - HTML title attribute for the widget | |
|
44 | * | |
|
45 | * @method set_message | |
|
46 | * @param {string} msg - The notification to display | |
|
47 | * @param {integer} [timeout] - The amount of time in milliseconds to display the widget | |
|
48 | * @param {function} [click_callback] - The function to run when the widget is clicked | |
|
49 | * @param {Object} [options] - Additional options | |
|
50 | */ | |
|
33 | 51 | NotificationWidget.prototype.set_message = function (msg, timeout, click_callback, options) { |
|
34 | 52 | options = options || {}; |
|
35 | 53 | |
|
36 | 54 | // unbind potential previous callback |
|
37 | 55 | this.element.unbind('click'); |
|
38 | 56 | this.inner.attr('class', options.icon); |
|
39 | 57 | this.inner.attr('title', options.title); |
|
40 | 58 | this.inner.text(msg); |
|
41 | 59 | this.element.fadeIn(100); |
|
42 | 60 | |
|
43 | 61 | // reset previous set style |
|
44 | 62 | this.element.removeClass(); |
|
45 | 63 | this.style(); |
|
46 | if (options.class){ | |
|
64 | if (options.class) { | |
|
47 | 65 | this.element.addClass(options.class); |
|
48 | 66 | } |
|
49 | 67 | |
|
50 | 68 | // clear previous timer |
|
51 | 69 | if (this.timeout !== null) { |
|
52 | 70 | clearTimeout(this.timeout); |
|
53 | 71 | this.timeout = null; |
|
54 | 72 | } |
|
55 | 73 | |
|
56 | 74 | // set the timer if a timeout is given |
|
57 | 75 | var that = this; |
|
58 | 76 | if (timeout !== undefined && timeout >= 0) { |
|
59 | 77 | this.timeout = setTimeout(function () { |
|
60 | 78 | that.element.fadeOut(100, function () {that.inner.text('');}); |
|
61 | 79 | that.element.unbind('click'); |
|
62 | 80 | that.timeout = null; |
|
63 | 81 | }, timeout); |
|
64 | 82 | } |
|
65 | 83 | |
|
66 | 84 | // bind the click callback if it is given |
|
67 | 85 | if (click_callback !== undefined) { |
|
68 | 86 | this.element.click(function() { |
|
69 | 87 | if (click_callback() !== false) { |
|
70 | 88 | that.element.fadeOut(100, function () {that.inner.text('');}); |
|
71 | that.element.unbind('click'); | |
|
72 | 89 | } |
|
90 | that.element.unbind('click'); | |
|
73 | 91 | if (that.timeout !== null) { |
|
74 | 92 | clearTimeout(that.timeout); |
|
75 | 93 | that.timeout = null; |
|
76 | 94 | } |
|
77 | 95 | }); |
|
78 | 96 | } |
|
79 | 97 | }; |
|
80 | 98 | |
|
99 | /** | |
|
100 | * Display an information message (styled with the 'info' | |
|
101 | * class). Arguments are the same as in set_message. | |
|
102 | * | |
|
103 | * @method info | |
|
104 | */ | |
|
81 | 105 | NotificationWidget.prototype.info = function (msg, timeout, click_callback, options) { |
|
82 | 106 | options = options || {}; |
|
83 | 107 | options.class = options.class + ' info'; |
|
84 | 108 | timeout = timeout || 3500; |
|
85 | 109 | this.set_message(msg, timeout, click_callback, options); |
|
86 | 110 | }; |
|
87 | 111 | |
|
112 | /** | |
|
113 | * Display a warning message (styled with the 'warning' | |
|
114 | * class). Arguments are the same as in set_message. | |
|
115 | * | |
|
116 | * @method warning | |
|
117 | */ | |
|
88 | 118 | NotificationWidget.prototype.warning = function (msg, timeout, click_callback, options) { |
|
89 | 119 | options = options || {}; |
|
90 | 120 | options.class = options.class + ' warning'; |
|
91 | 121 | this.set_message(msg, timeout, click_callback, options); |
|
92 | 122 | }; |
|
93 | 123 | |
|
124 | /** | |
|
125 | * Display a danger message (styled with the 'danger' | |
|
126 | * class). Arguments are the same as in set_message. | |
|
127 | * | |
|
128 | * @method danger | |
|
129 | */ | |
|
94 | 130 | NotificationWidget.prototype.danger = function (msg, timeout, click_callback, options) { |
|
95 | 131 | options = options || {}; |
|
96 | 132 | options.class = options.class + ' danger'; |
|
97 | 133 | this.set_message(msg, timeout, click_callback, options); |
|
98 | 134 | }; |
|
99 | 135 | |
|
136 | /** | |
|
137 | * Get the text of the widget message. | |
|
138 | * | |
|
139 | * @method get_message | |
|
140 | * @return {string} - the message text | |
|
141 | */ | |
|
100 | 142 | NotificationWidget.prototype.get_message = function () { |
|
101 | 143 | return this.inner.html(); |
|
102 | 144 | }; |
|
103 | 145 | |
|
104 | 146 | // For backwards compatibility. |
|
105 | 147 | IPython.NotificationWidget = NotificationWidget; |
|
106 | 148 | |
|
107 | 149 | return {'NotificationWidget': NotificationWidget}; |
|
108 | 150 | }); |
General Comments 0
You need to be logged in to leave comments.
Login now