Show More
@@ -1,171 +1,174 b'' | |||
|
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 | /** |
|
11 | 11 | * Construct a NotificationWidget object. |
|
12 | 12 | * |
|
13 | 13 | * @constructor |
|
14 | 14 | * @param {string} selector - a jQuery selector string for the |
|
15 | 15 | * notification widget element |
|
16 | 16 | */ |
|
17 | 17 | var NotificationWidget = function (selector) { |
|
18 | 18 | this.selector = selector; |
|
19 | 19 | this.timeout = null; |
|
20 | 20 | this.busy = false; |
|
21 | 21 | if (this.selector !== undefined) { |
|
22 | 22 | this.element = $(selector); |
|
23 | 23 | this.style(); |
|
24 | 24 | } |
|
25 | 25 | this.element.hide(); |
|
26 | 26 | this.inner = $('<span/>'); |
|
27 | 27 | this.element.append(this.inner); |
|
28 | 28 | }; |
|
29 | 29 | |
|
30 | 30 | /** |
|
31 | 31 | * Add the 'notification_widget' CSS class to the widget element. |
|
32 | 32 | * |
|
33 | 33 | * @method style |
|
34 | 34 | */ |
|
35 | 35 | NotificationWidget.prototype.style = function () { |
|
36 | 36 | // use explicit bootstrap classes here, |
|
37 | 37 | // because multiple inheritance in LESS doesn't work |
|
38 | 38 | // for this particular combination |
|
39 | 39 | this.element.addClass('notification_widget btn btn-xs navbar-btn'); |
|
40 | 40 | }; |
|
41 | 41 | |
|
42 | 42 | /** |
|
43 | 43 | * hide the widget and empty the text |
|
44 | 44 | **/ |
|
45 | 45 | NotificationWidget.prototype.hide = function () { |
|
46 | 46 | var that = this; |
|
47 | 47 | this.element.fadeOut(100, function(){that.inner.text('');}); |
|
48 | 48 | }; |
|
49 | 49 | |
|
50 | 50 | /** |
|
51 | 51 | * Set the notification widget message to display for a certain |
|
52 | 52 | * amount of time (timeout). The widget will be shown forever if |
|
53 | 53 | * timeout is <= 0 or undefined. If the widget is clicked while it |
|
54 | 54 | * is still displayed, execute an optional callback |
|
55 | 55 | * (click_callback). If the callback returns false, it will |
|
56 | 56 | * prevent the notification from being dismissed. |
|
57 | 57 | * |
|
58 | 58 | * Options: |
|
59 | 59 | * class - CSS class name for styling |
|
60 | 60 | * icon - CSS class name for the widget icon |
|
61 | 61 | * title - HTML title attribute for the widget |
|
62 | 62 | * |
|
63 | 63 | * @method set_message |
|
64 | 64 | * @param {string} msg - The notification to display |
|
65 | 65 | * @param {integer} [timeout] - The amount of time in milliseconds to display the widget |
|
66 | 66 | * @param {function} [click_callback] - The function to run when the widget is clicked |
|
67 | 67 | * @param {Object} [options] - Additional options |
|
68 | 68 | */ |
|
69 | 69 | NotificationWidget.prototype.set_message = function (msg, timeout, click_callback, options) { |
|
70 | 70 | options = options || {}; |
|
71 | 71 | |
|
72 | 72 | // unbind potential previous callback |
|
73 | 73 | this.element.unbind('click'); |
|
74 | 74 | this.inner.attr('class', options.icon); |
|
75 | 75 | this.inner.attr('title', options.title); |
|
76 | 76 | this.inner.text(msg); |
|
77 | 77 | this.element.fadeIn(100); |
|
78 | 78 | |
|
79 | 79 | // reset previous set style |
|
80 | 80 | this.element.removeClass(); |
|
81 | 81 | this.style(); |
|
82 | 82 | if (options.class) { |
|
83 | 83 | this.element.addClass(options.class); |
|
84 | 84 | } |
|
85 | 85 | |
|
86 | 86 | // clear previous timer |
|
87 | 87 | if (this.timeout !== null) { |
|
88 | 88 | clearTimeout(this.timeout); |
|
89 | 89 | this.timeout = null; |
|
90 | 90 | } |
|
91 | 91 | |
|
92 | 92 | // set the timer if a timeout is given |
|
93 | 93 | var that = this; |
|
94 | 94 | if (timeout !== undefined && timeout >= 0) { |
|
95 | 95 | this.timeout = setTimeout(function () { |
|
96 | 96 | that.element.fadeOut(100, function () {that.inner.text('');}); |
|
97 | 97 | that.element.unbind('click'); |
|
98 | 98 | that.timeout = null; |
|
99 | 99 | }, timeout); |
|
100 | 100 | } |
|
101 | 101 | |
|
102 | // bind the click callback if it is given | |
|
103 |
if (click_callback |
|
|
102 | // if no click callback assume we will just dismiss the notification | |
|
103 | if (click_callback === undefined) { | |
|
104 | click_callback = function(){return true}; | |
|
105 | } | |
|
106 | // on click, remove widget if click callback say so | |
|
107 | // and unbind click event. | |
|
104 | 108 |
|
|
105 | 109 |
|
|
106 | 110 |
|
|
107 | } | |
|
108 | 111 | that.element.unbind('click'); |
|
112 | } | |
|
109 | 113 |
|
|
110 | 114 |
|
|
111 | 115 |
|
|
112 | 116 |
|
|
113 | 117 |
|
|
114 | } | |
|
115 | 118 | }; |
|
116 | 119 | |
|
117 | 120 | /** |
|
118 | 121 | * Display an information message (styled with the 'info' |
|
119 | 122 | * class). Arguments are the same as in set_message. Default |
|
120 | 123 | * timeout is 3500 milliseconds. |
|
121 | 124 | * |
|
122 | 125 | * @method info |
|
123 | 126 | */ |
|
124 | 127 | NotificationWidget.prototype.info = function (msg, timeout, click_callback, options) { |
|
125 | 128 | options = options || {}; |
|
126 | 129 | options.class = options.class + ' info'; |
|
127 | 130 | timeout = timeout || 3500; |
|
128 | 131 | this.set_message(msg, timeout, click_callback, options); |
|
129 | 132 | }; |
|
130 | 133 | |
|
131 | 134 | /** |
|
132 | 135 | * Display a warning message (styled with the 'warning' |
|
133 | 136 | * class). Arguments are the same as in set_message. Messages are |
|
134 | 137 | * sticky by default. |
|
135 | 138 | * |
|
136 | 139 | * @method warning |
|
137 | 140 | */ |
|
138 | 141 | NotificationWidget.prototype.warning = function (msg, timeout, click_callback, options) { |
|
139 | 142 | options = options || {}; |
|
140 | 143 | options.class = options.class + ' warning'; |
|
141 | 144 | this.set_message(msg, timeout, click_callback, options); |
|
142 | 145 | }; |
|
143 | 146 | |
|
144 | 147 | /** |
|
145 | 148 | * Display a danger message (styled with the 'danger' |
|
146 | 149 | * class). Arguments are the same as in set_message. Messages are |
|
147 | 150 | * sticky by default. |
|
148 | 151 | * |
|
149 | 152 | * @method danger |
|
150 | 153 | */ |
|
151 | 154 | NotificationWidget.prototype.danger = function (msg, timeout, click_callback, options) { |
|
152 | 155 | options = options || {}; |
|
153 | 156 | options.class = options.class + ' danger'; |
|
154 | 157 | this.set_message(msg, timeout, click_callback, options); |
|
155 | 158 | }; |
|
156 | 159 | |
|
157 | 160 | /** |
|
158 | 161 | * Get the text of the widget message. |
|
159 | 162 | * |
|
160 | 163 | * @method get_message |
|
161 | 164 | * @return {string} - the message text |
|
162 | 165 | */ |
|
163 | 166 | NotificationWidget.prototype.get_message = function () { |
|
164 | 167 | return this.inner.html(); |
|
165 | 168 | }; |
|
166 | 169 | |
|
167 | 170 | // For backwards compatibility. |
|
168 | 171 | IPython.NotificationWidget = NotificationWidget; |
|
169 | 172 | |
|
170 | 173 | return {'NotificationWidget': NotificationWidget}; |
|
171 | 174 | }); |
General Comments 0
You need to be logged in to leave comments.
Login now