##// END OF EJS Templates
Merge pull request #6544 from jhamrick/notification-widget...
Matthias Bussonnier -
r18035:56a06676 merge
parent child Browse files
Show More
@@ -0,0 +1,116 b''
1 // Test the notification area and widgets
2
3 casper.notebook_test(function () {
4 var that = this;
5 var widget = function (name) {
6 return that.evaluate(function (name) {
7 return (IPython.notification_area.widget(name) !== undefined);
8 }, name);
9 };
10
11 var get_widget = function (name) {
12 return that.evaluate(function (name) {
13 return (IPython.notification_area.get_widget(name) !== undefined);
14 }, name);
15 };
16
17 var new_notification_widget = function (name) {
18 return that.evaluate(function (name) {
19 return (IPython.notification_area.new_notification_widget(name) !== undefined);
20 }, name);
21 };
22
23 var widget_has_class = function (name, class_name) {
24 return that.evaluate(function (name, class_name) {
25 var w = IPython.notification_area.get_widget(name);
26 return w.element.hasClass(class_name);
27 }, name, class_name);
28 };
29
30 var widget_message = function (name) {
31 return that.evaluate(function (name) {
32 var w = IPython.notification_area.get_widget(name);
33 return w.get_message();
34 }, name);
35 };
36
37 this.then(function () {
38 // check that existing widgets are there
39 this.test.assert(get_widget('kernel') && widget('kernel'), 'The kernel notification widget exists');
40 this.test.assert(get_widget('notebook') && widget('notbook'), 'The notebook notification widget exists');
41
42 // try getting a non-existant widget
43 this.test.assertRaises(get_widget, 'foo', 'get_widget: error is thrown');
44
45 // try creating a non-existant widget
46 this.test.assert(widget('bar'), 'widget: new widget is created');
47
48 // try creating a widget that already exists
49 this.test.assertRaises(new_notification_widget, 'kernel', 'new_notification_widget: error is thrown');
50 });
51
52 // test creating 'info' messages
53 this.thenEvaluate(function () {
54 var tnw = IPython.notification_area.widget('test');
55 tnw.info('test info');
56 });
57 this.waitUntilVisible('#notification_test', function () {
58 this.test.assert(widget_has_class('test', 'info'), 'info: class is correct');
59 this.test.assertEquals(widget_message('test'), 'test info', 'info: message is correct');
60 });
61
62 // test creating 'warning' messages
63 this.thenEvaluate(function () {
64 var tnw = IPython.notification_area.widget('test');
65 tnw.warning('test warning');
66 });
67 this.waitUntilVisible('#notification_test', function () {
68 this.test.assert(widget_has_class('test', 'warning'), 'warning: class is correct');
69 this.test.assertEquals(widget_message('test'), 'test warning', 'warning: message is correct');
70 });
71
72 // test creating 'danger' messages
73 this.thenEvaluate(function () {
74 var tnw = IPython.notification_area.widget('test');
75 tnw.danger('test danger');
76 });
77 this.waitUntilVisible('#notification_test', function () {
78 this.test.assert(widget_has_class('test', 'danger'), 'danger: class is correct');
79 this.test.assertEquals(widget_message('test'), 'test danger', 'danger: message is correct');
80 });
81
82 // test message timeout
83 this.thenEvaluate(function () {
84 var tnw = IPython.notification_area.widget('test');
85 tnw.set_message('test timeout', 1000);
86 });
87 this.waitUntilVisible('#notification_test', function () {
88 this.test.assertEquals(widget_message('test'), 'test timeout', 'timeout: message is correct');
89 });
90 this.waitWhileVisible('#notification_test', function () {
91 this.test.assertEquals(widget_message('test'), '', 'timeout: message was cleared');
92 });
93
94 // test click callback
95 this.thenEvaluate(function () {
96 var tnw = IPython.notification_area.widget('test');
97 tnw._clicked = false;
98 tnw.set_message('test click', undefined, function () {
99 tnw._clicked = true;
100 return true;
101 });
102 });
103 this.waitUntilVisible('#notification_test', function () {
104 this.test.assertEquals(widget_message('test'), 'test click', 'callback: message is correct');
105 this.click('#notification_test');
106 });
107 this.waitFor(function () {
108 return this.evaluate(function () {
109 return IPython.notification_area.widget('test')._clicked;
110 });
111 }, function () {
112 this.waitWhileVisible('#notification_test', function () {
113 this.test.assertEquals(widget_message('test'), '', 'callback: message was cleared');
114 });
115 });
116 });
@@ -1,256 +1,290 b''
1 // Copyright (c) IPython Development Team.
1 // Copyright (c) IPython Development Team.
2 // Distributed under the terms of the Modified BSD License.
2 // Distributed under the terms of the Modified BSD License.
3
3
4 define([
4 define([
5 'base/js/namespace',
5 'base/js/namespace',
6 'jquery',
6 'jquery',
7 'base/js/utils',
7 'base/js/utils',
8 'base/js/dialog',
8 'base/js/dialog',
9 'notebook/js/notificationwidget',
9 'notebook/js/notificationwidget',
10 'moment'
10 'moment'
11 ], function(IPython, $, utils, dialog, notificationwidget, moment) {
11 ], function(IPython, $, utils, dialog, notificationwidget, moment) {
12 "use strict";
12 "use strict";
13
13
14 // store reference to the NotificationWidget class
15 var NotificationWidget = notificationwidget.NotificationWidget;
16
17 /**
18 * Construct the NotificationArea object. Options are:
19 * events: $(Events) instance
20 * save_widget: SaveWidget instance
21 * notebook: Notebook instance
22 * keyboard_manager: KeyboardManager instance
23 *
24 * @constructor
25 * @param {string} selector - a jQuery selector string for the
26 * notification area element
27 * @param {Object} [options] - a dictionary of keyword arguments.
28 */
14 var NotificationArea = function (selector, options) {
29 var NotificationArea = function (selector, options) {
15 // Constructor
16 //
17 // Parameters:
18 // selector: string
19 // options: dictionary
20 // Dictionary of keyword arguments.
21 // notebook: Notebook instance
22 // events: $(Events) instance
23 // save_widget: SaveWidget instance
24 this.selector = selector;
30 this.selector = selector;
25 this.events = options.events;
31 this.events = options.events;
26 this.save_widget = options.save_widget;
32 this.save_widget = options.save_widget;
27 this.notebook = options.notebook;
33 this.notebook = options.notebook;
28 this.keyboard_manager = options.keyboard_manager;
34 this.keyboard_manager = options.keyboard_manager;
29 if (this.selector !== undefined) {
35 if (this.selector !== undefined) {
30 this.element = $(selector);
36 this.element = $(selector);
31 }
37 }
32 this.widget_dict = {};
38 this.widget_dict = {};
33 };
39 };
34
40
35 NotificationArea.prototype.temp_message = function (msg, timeout, css_class) {
41 /**
36 var tdiv = $('<div>')
42 * Get a widget by name, creating it if it doesn't exist.
37 .addClass('notification_widget')
43 *
38 .addClass(css_class)
44 * @method widget
39 .hide()
45 * @param {string} name - the widget name
40 .text(msg);
46 */
41
47 NotificationArea.prototype.widget = function (name) {
42 $(this.selector).append(tdiv);
48 if (this.widget_dict[name] === undefined) {
43 var tmout = Math.max(1500,(timeout||1500));
44 tdiv.fadeIn(100);
45
46 setTimeout(function () {
47 tdiv.fadeOut(100, function () {tdiv.remove();});
48 }, tmout);
49 };
50
51 NotificationArea.prototype.widget = function(name) {
52 if(this.widget_dict[name] === undefined) {
53 return this.new_notification_widget(name);
49 return this.new_notification_widget(name);
54 }
50 }
55 return this.get_widget(name);
51 return this.get_widget(name);
56 };
52 };
57
53
58 NotificationArea.prototype.get_widget = function(name) {
54 /**
55 * Get a widget by name, throwing an error if it doesn't exist.
56 *
57 * @method get_widget
58 * @param {string} name - the widget name
59 */
60 NotificationArea.prototype.get_widget = function (name) {
59 if(this.widget_dict[name] === undefined) {
61 if(this.widget_dict[name] === undefined) {
60 throw('no widgets with this name');
62 throw('no widgets with this name');
61 }
63 }
62 return this.widget_dict[name];
64 return this.widget_dict[name];
63 };
65 };
64
66
65 NotificationArea.prototype.new_notification_widget = function(name) {
67 /**
66 if(this.widget_dict[name] !== undefined) {
68 * Create a new notification widget with the given name. The
67 throw('widget with that name already exists ! ');
69 * widget must not already exist.
70 *
71 * @method new_notification_widget
72 * @param {string} name - the widget name
73 */
74 NotificationArea.prototype.new_notification_widget = function (name) {
75 if (this.widget_dict[name] !== undefined) {
76 throw('widget with that name already exists!');
68 }
77 }
69 var div = $('<div/>').attr('id','notification_'+name);
78
79 // create the element for the notification widget and add it
80 // to the notification aread element
81 var div = $('<div/>').attr('id', 'notification_' + name);
70 $(this.selector).append(div);
82 $(this.selector).append(div);
71 this.widget_dict[name] = new notificationwidget.NotificationWidget('#notification_'+name);
83
84 // create the widget object and return it
85 this.widget_dict[name] = new NotificationWidget('#notification_' + name);
72 return this.widget_dict[name];
86 return this.widget_dict[name];
73 };
87 };
74
88
75 NotificationArea.prototype.init_notification_widgets = function() {
89 /**
90 * Initialize the default set of notification widgets.
91 *
92 * @method init_notification_widgets
93 */
94 NotificationArea.prototype.init_notification_widgets = function () {
95 this.init_kernel_notification_widget();
96 this.init_notebook_notification_widget();
97 };
98
99 /**
100 * Initialize the notification widget for kernel status messages.
101 *
102 * @method init_kernel_notification_widget
103 */
104 NotificationArea.prototype.init_kernel_notification_widget = function () {
76 var that = this;
105 var that = this;
77 var knw = this.new_notification_widget('kernel');
106 var knw = this.new_notification_widget('kernel');
78 var $kernel_ind_icon = $("#kernel_indicator_icon");
107 var $kernel_ind_icon = $("#kernel_indicator_icon");
79 var $modal_ind_icon = $("#modal_indicator_icon");
108 var $modal_ind_icon = $("#modal_indicator_icon");
80
109
81 // Command/Edit mode
110 // Command/Edit mode
82 this.events.on('edit_mode.Notebook',function () {
111 this.events.on('edit_mode.Notebook',function () {
83 that.save_widget.update_document_title();
112 that.save_widget.update_document_title();
84 $modal_ind_icon.attr('class','edit_mode_icon').attr('title','Edit Mode');
113 $modal_ind_icon.attr('class','edit_mode_icon').attr('title','Edit Mode');
85 });
114 });
86
115
87 this.events.on('command_mode.Notebook',function () {
116 this.events.on('command_mode.Notebook',function () {
88 that.save_widget.update_document_title();
117 that.save_widget.update_document_title();
89 $modal_ind_icon.attr('class','command_mode_icon').attr('title','Command Mode');
118 $modal_ind_icon.attr('class','command_mode_icon').attr('title','Command Mode');
90 });
119 });
91
120
92 // Implicitly start off in Command mode, switching to Edit mode will trigger event
121 // Implicitly start off in Command mode, switching to Edit mode will trigger event
93 $modal_ind_icon.attr('class','command_mode_icon').attr('title','Command Mode');
122 $modal_ind_icon.attr('class','command_mode_icon').attr('title','Command Mode');
94
123
95 // Kernel events
124 // Kernel events
96 this.events.on('status_idle.Kernel',function () {
125 this.events.on('status_idle.Kernel',function () {
97 that.save_widget.update_document_title();
126 that.save_widget.update_document_title();
98 $kernel_ind_icon.attr('class','kernel_idle_icon').attr('title','Kernel Idle');
127 $kernel_ind_icon.attr('class','kernel_idle_icon').attr('title','Kernel Idle');
99 });
128 });
100
129
101 this.events.on('status_busy.Kernel',function () {
130 this.events.on('status_busy.Kernel',function () {
102 window.document.title='(Busy) '+window.document.title;
131 window.document.title='(Busy) '+window.document.title;
103 $kernel_ind_icon.attr('class','kernel_busy_icon').attr('title','Kernel Busy');
132 $kernel_ind_icon.attr('class','kernel_busy_icon').attr('title','Kernel Busy');
104 });
133 });
105
134
106 this.events.on('status_restarting.Kernel',function () {
135 this.events.on('status_restarting.Kernel',function () {
107 that.save_widget.update_document_title();
136 that.save_widget.update_document_title();
108 knw.set_message("Restarting kernel", 2000);
137 knw.set_message("Restarting kernel", 2000);
109 });
138 });
110
139
111 this.events.on('status_dead.Kernel',function () {
140 this.events.on('status_dead.Kernel',function () {
112 that.save_widget.update_document_title();
141 that.save_widget.update_document_title();
113 knw.danger("Dead kernel");
142 knw.danger("Dead kernel");
114 $kernel_ind_icon.attr('class','kernel_dead_icon').attr('title','Kernel Dead');
143 $kernel_ind_icon.attr('class','kernel_dead_icon').attr('title','Kernel Dead');
115 });
144 });
116
145
117 this.events.on('status_interrupting.Kernel',function () {
146 this.events.on('status_interrupting.Kernel',function () {
118 knw.set_message("Interrupting kernel", 2000);
147 knw.set_message("Interrupting kernel", 2000);
119 });
148 });
120
149
121 // Start the kernel indicator in the busy state, and send a kernel_info request.
150 // Start the kernel indicator in the busy state, and send a kernel_info request.
122 // When the kernel_info reply arrives, the kernel is idle.
151 // When the kernel_info reply arrives, the kernel is idle.
123 $kernel_ind_icon.attr('class','kernel_busy_icon').attr('title','Kernel Busy');
152 $kernel_ind_icon.attr('class','kernel_busy_icon').attr('title','Kernel Busy');
124
153
125 this.events.on('status_started.Kernel', function (evt, data) {
154 this.events.on('status_started.Kernel', function (evt, data) {
126 knw.info("Websockets Connected", 500);
155 knw.info("Websockets Connected", 500);
127 that.events.trigger('status_busy.Kernel');
156 that.events.trigger('status_busy.Kernel');
128 data.kernel.kernel_info(function () {
157 data.kernel.kernel_info(function () {
129 that.events.trigger('status_idle.Kernel');
158 that.events.trigger('status_idle.Kernel');
130 });
159 });
131 });
160 });
132
161
133 this.events.on('status_dead.Kernel',function () {
162 this.events.on('status_dead.Kernel',function () {
134 var msg = 'The kernel has died, and the automatic restart has failed.' +
163 var msg = 'The kernel has died, and the automatic restart has failed.' +
135 ' It is possible the kernel cannot be restarted.' +
164 ' It is possible the kernel cannot be restarted.' +
136 ' If you are not able to restart the kernel, you will still be able to save' +
165 ' If you are not able to restart the kernel, you will still be able to save' +
137 ' the notebook, but running code will no longer work until the notebook' +
166 ' the notebook, but running code will no longer work until the notebook' +
138 ' is reopened.';
167 ' is reopened.';
139
168
140 dialog.modal({
169 dialog.modal({
141 title: "Dead kernel",
170 title: "Dead kernel",
142 body : msg,
171 body : msg,
143 keyboard_manager: that.keyboard_manager,
172 keyboard_manager: that.keyboard_manager,
144 notebook: that.notebook,
173 notebook: that.notebook,
145 buttons : {
174 buttons : {
146 "Manual Restart": {
175 "Manual Restart": {
147 class: "btn-danger",
176 class: "btn-danger",
148 click: function () {
177 click: function () {
149 that.events.trigger('status_restarting.Kernel');
178 that.events.trigger('status_restarting.Kernel');
150 that.notebook.start_kernel();
179 that.notebook.start_kernel();
151 }
180 }
152 },
181 },
153 "Don't restart": {}
182 "Don't restart": {}
154 }
183 }
155 });
184 });
156 });
185 });
157
186
158 this.events.on('websocket_closed.Kernel', function (event, data) {
187 this.events.on('websocket_closed.Kernel', function (event, data) {
159 var kernel = data.kernel;
188 var kernel = data.kernel;
160 var ws_url = data.ws_url;
189 var ws_url = data.ws_url;
161 var early = data.early;
190 var early = data.early;
162 var msg;
191 var msg;
163
192
164 $kernel_ind_icon
193 $kernel_ind_icon
165 .attr('class', 'kernel_disconnected_icon')
194 .attr('class', 'kernel_disconnected_icon')
166 .attr('title', 'No Connection to Kernel');
195 .attr('title', 'No Connection to Kernel');
167
196
168 if (!early) {
197 if (!early) {
169 knw.warning('Reconnecting');
198 knw.warning('Reconnecting');
170 setTimeout(function () {
199 setTimeout(function () {
171 kernel.start_channels();
200 kernel.start_channels();
172 }, 5000);
201 }, 5000);
173 return;
202 return;
174 }
203 }
175 console.log('WebSocket connection failed: ', ws_url);
204 console.log('WebSocket connection failed: ', ws_url);
176 msg = "A WebSocket connection could not be established." +
205 msg = "A WebSocket connection could not be established." +
177 " You will NOT be able to run code. Check your" +
206 " You will NOT be able to run code. Check your" +
178 " network connection or notebook server configuration.";
207 " network connection or notebook server configuration.";
179 dialog.modal({
208 dialog.modal({
180 title: "WebSocket connection failed",
209 title: "WebSocket connection failed",
181 body: msg,
210 body: msg,
182 keyboard_manager: that.keyboard_manager,
211 keyboard_manager: that.keyboard_manager,
183 notebook: that.notebook,
212 notebook: that.notebook,
184 buttons : {
213 buttons : {
185 "OK": {},
214 "OK": {},
186 "Reconnect": {
215 "Reconnect": {
187 click: function () {
216 click: function () {
188 knw.warning('Reconnecting');
217 knw.warning('Reconnecting');
189 setTimeout(function () {
218 setTimeout(function () {
190 kernel.start_channels();
219 kernel.start_channels();
191 }, 5000);
220 }, 5000);
192 }
221 }
193 }
222 }
194 }
223 }
195 });
224 });
196 });
225 });
226 };
197
227
198
228 /**
229 * Initialize the notification widget for notebook status messages.
230 *
231 * @method init_notebook_notification_widget
232 */
233 NotificationArea.prototype.init_notebook_notification_widget = function () {
199 var nnw = this.new_notification_widget('notebook');
234 var nnw = this.new_notification_widget('notebook');
200
235
201 // Notebook events
236 // Notebook events
202 this.events.on('notebook_loading.Notebook', function () {
237 this.events.on('notebook_loading.Notebook', function () {
203 nnw.set_message("Loading notebook",500);
238 nnw.set_message("Loading notebook",500);
204 });
239 });
205 this.events.on('notebook_loaded.Notebook', function () {
240 this.events.on('notebook_loaded.Notebook', function () {
206 nnw.set_message("Notebook loaded",500);
241 nnw.set_message("Notebook loaded",500);
207 });
242 });
208 this.events.on('notebook_saving.Notebook', function () {
243 this.events.on('notebook_saving.Notebook', function () {
209 nnw.set_message("Saving notebook",500);
244 nnw.set_message("Saving notebook",500);
210 });
245 });
211 this.events.on('notebook_saved.Notebook', function () {
246 this.events.on('notebook_saved.Notebook', function () {
212 nnw.set_message("Notebook saved",2000);
247 nnw.set_message("Notebook saved",2000);
213 });
248 });
214 this.events.on('notebook_save_failed.Notebook', function (evt, xhr, status, data) {
249 this.events.on('notebook_save_failed.Notebook', function (evt, xhr, status, data) {
215 nnw.warning(data || "Notebook save failed");
250 nnw.warning(data || "Notebook save failed");
216 });
251 });
217
252
218 // Checkpoint events
253 // Checkpoint events
219 this.events.on('checkpoint_created.Notebook', function (evt, data) {
254 this.events.on('checkpoint_created.Notebook', function (evt, data) {
220 var msg = "Checkpoint created";
255 var msg = "Checkpoint created";
221 if (data.last_modified) {
256 if (data.last_modified) {
222 var d = new Date(data.last_modified);
257 var d = new Date(data.last_modified);
223 msg = msg + ": " + moment(d).format("HH:mm:ss");
258 msg = msg + ": " + moment(d).format("HH:mm:ss");
224 }
259 }
225 nnw.set_message(msg, 2000);
260 nnw.set_message(msg, 2000);
226 });
261 });
227 this.events.on('checkpoint_failed.Notebook', function () {
262 this.events.on('checkpoint_failed.Notebook', function () {
228 nnw.warning("Checkpoint failed");
263 nnw.warning("Checkpoint failed");
229 });
264 });
230 this.events.on('checkpoint_deleted.Notebook', function () {
265 this.events.on('checkpoint_deleted.Notebook', function () {
231 nnw.set_message("Checkpoint deleted", 500);
266 nnw.set_message("Checkpoint deleted", 500);
232 });
267 });
233 this.events.on('checkpoint_delete_failed.Notebook', function () {
268 this.events.on('checkpoint_delete_failed.Notebook', function () {
234 nnw.warning("Checkpoint delete failed");
269 nnw.warning("Checkpoint delete failed");
235 });
270 });
236 this.events.on('checkpoint_restoring.Notebook', function () {
271 this.events.on('checkpoint_restoring.Notebook', function () {
237 nnw.set_message("Restoring to checkpoint...", 500);
272 nnw.set_message("Restoring to checkpoint...", 500);
238 });
273 });
239 this.events.on('checkpoint_restore_failed.Notebook', function () {
274 this.events.on('checkpoint_restore_failed.Notebook', function () {
240 nnw.warning("Checkpoint restore failed");
275 nnw.warning("Checkpoint restore failed");
241 });
276 });
242
277
243 // Autosave events
278 // Autosave events
244 this.events.on('autosave_disabled.Notebook', function () {
279 this.events.on('autosave_disabled.Notebook', function () {
245 nnw.set_message("Autosave disabled", 2000);
280 nnw.set_message("Autosave disabled", 2000);
246 });
281 });
247 this.events.on('autosave_enabled.Notebook', function (evt, interval) {
282 this.events.on('autosave_enabled.Notebook', function (evt, interval) {
248 nnw.set_message("Saving every " + interval / 1000 + "s", 1000);
283 nnw.set_message("Saving every " + interval / 1000 + "s", 1000);
249 });
284 });
250
251 };
285 };
252
286
253 IPython.NotificationArea = NotificationArea;
287 IPython.NotificationArea = NotificationArea;
254
288
255 return {'NotificationArea': NotificationArea};
289 return {'NotificationArea': NotificationArea};
256 });
290 });
@@ -1,104 +1,160 b''
1 // Copyright (c) IPython Development Team.
1 // Copyright (c) IPython Development Team.
2 // Distributed under the terms of the Modified BSD License.
2 // Distributed under the terms of the Modified BSD License.
3
3
4 define([
4 define([
5 'base/js/namespace',
5 'base/js/namespace',
6 'jquery',
6 'jquery',
7 ], function(IPython, $) {
7 ], function(IPython, $) {
8 "use strict";
8 "use strict";
9
9
10 /**
11 * Construct a NotificationWidget object.
12 *
13 * @constructor
14 * @param {string} selector - a jQuery selector string for the
15 * notification widget element
16 */
10 var NotificationWidget = function (selector) {
17 var NotificationWidget = function (selector) {
11 this.selector = selector;
18 this.selector = selector;
12 this.timeout = null;
19 this.timeout = null;
13 this.busy = false;
20 this.busy = false;
14 if (this.selector !== undefined) {
21 if (this.selector !== undefined) {
15 this.element = $(selector);
22 this.element = $(selector);
16 this.style();
23 this.style();
17 }
24 }
18 this.element.hide();
25 this.element.hide();
19 var that = this;
20
21 this.inner = $('<span/>');
26 this.inner = $('<span/>');
22 this.element.append(this.inner);
27 this.element.append(this.inner);
23
24 };
28 };
25
29
30 /**
31 * Add the 'notification_widget' CSS class to the widget element.
32 *
33 * @method style
34 */
26 NotificationWidget.prototype.style = function () {
35 NotificationWidget.prototype.style = function () {
27 this.element.addClass('notification_widget');
36 this.element.addClass('notification_widget');
28 };
37 };
29
38
30 // msg : message to display
39 /**
31 // timeout : time in ms before diseapearing
40 * Set the notification widget message to display for a certain
32 //
41 * amount of time (timeout). The widget will be shown forever if
33 // if timeout <= 0
42 * timeout is <= 0 or undefined. If the widget is clicked while it
34 // click_callback : function called if user click on notification
43 * is still displayed, execute an optional callback
35 // could return false to prevent the notification to be dismissed
44 * (click_callback). If the callback returns false, it will
45 * prevent the notification from being dismissed.
46 *
47 * Options:
48 * class - CSS class name for styling
49 * icon - CSS class name for the widget icon
50 * title - HTML title attribute for the widget
51 *
52 * @method set_message
53 * @param {string} msg - The notification to display
54 * @param {integer} [timeout] - The amount of time in milliseconds to display the widget
55 * @param {function} [click_callback] - The function to run when the widget is clicked
56 * @param {Object} [options] - Additional options
57 */
36 NotificationWidget.prototype.set_message = function (msg, timeout, click_callback, options) {
58 NotificationWidget.prototype.set_message = function (msg, timeout, click_callback, options) {
37 var options = options || {};
59 options = options || {};
38 var callback = click_callback || function() {return true;};
60
39 var that = this;
40 // unbind potential previous callback
61 // unbind potential previous callback
41 this.element.unbind('click');
62 this.element.unbind('click');
42 this.inner.attr('class', options.icon);
63 this.inner.attr('class', options.icon);
43 this.inner.attr('title', options.title);
64 this.inner.attr('title', options.title);
44 this.inner.text(msg);
65 this.inner.text(msg);
45 this.element.fadeIn(100);
66 this.element.fadeIn(100);
46
67
47 // reset previous set style
68 // reset previous set style
48 this.element.removeClass();
69 this.element.removeClass();
49 this.style();
70 this.style();
50 if (options.class){
71 if (options.class) {
51
72 this.element.addClass(options.class);
52 this.element.addClass(options.class)
53 }
73 }
74
75 // clear previous timer
54 if (this.timeout !== null) {
76 if (this.timeout !== null) {
55 clearTimeout(this.timeout);
77 clearTimeout(this.timeout);
56 this.timeout = null;
78 this.timeout = null;
57 }
79 }
58 if (timeout !== undefined && timeout >=0) {
80
81 // set the timer if a timeout is given
82 var that = this;
83 if (timeout !== undefined && timeout >= 0) {
59 this.timeout = setTimeout(function () {
84 this.timeout = setTimeout(function () {
60 that.element.fadeOut(100, function () {that.inner.text('');});
85 that.element.fadeOut(100, function () {that.inner.text('');});
86 that.element.unbind('click');
61 that.timeout = null;
87 that.timeout = null;
62 }, timeout);
88 }, timeout);
63 } else {
89 }
64 this.element.click(function() {
90
65 if( callback() !== false ) {
91 // bind the click callback if it is given
92 if (click_callback !== undefined) {
93 this.element.click(function () {
94 if (click_callback() !== false) {
66 that.element.fadeOut(100, function () {that.inner.text('');});
95 that.element.fadeOut(100, function () {that.inner.text('');});
67 that.element.unbind('click');
68 }
96 }
69 if (that.timeout !== undefined) {
97 that.element.unbind('click');
70 that.timeout = undefined;
98 if (that.timeout !== null) {
71 clearTimeout(that.timeout);
99 clearTimeout(that.timeout);
100 that.timeout = null;
72 }
101 }
73 });
102 });
74 }
103 }
75 };
104 };
76
105
77
106 /**
107 * Display an information message (styled with the 'info'
108 * class). Arguments are the same as in set_message. Default
109 * timeout is 3500 milliseconds.
110 *
111 * @method info
112 */
78 NotificationWidget.prototype.info = function (msg, timeout, click_callback, options) {
113 NotificationWidget.prototype.info = function (msg, timeout, click_callback, options) {
79 var options = options || {};
114 options = options || {};
80 options.class = options.class +' info';
115 options.class = options.class + ' info';
81 var timeout = timeout || 3500;
116 timeout = timeout || 3500;
82 this.set_message(msg, timeout, click_callback, options);
117 this.set_message(msg, timeout, click_callback, options);
83 }
118 };
119
120 /**
121 * Display a warning message (styled with the 'warning'
122 * class). Arguments are the same as in set_message. Messages are
123 * sticky by default.
124 *
125 * @method warning
126 */
84 NotificationWidget.prototype.warning = function (msg, timeout, click_callback, options) {
127 NotificationWidget.prototype.warning = function (msg, timeout, click_callback, options) {
85 var options = options || {};
128 options = options || {};
86 options.class = options.class +' warning';
129 options.class = options.class + ' warning';
87 this.set_message(msg, timeout, click_callback, options);
130 this.set_message(msg, timeout, click_callback, options);
88 }
131 };
132
133 /**
134 * Display a danger message (styled with the 'danger'
135 * class). Arguments are the same as in set_message. Messages are
136 * sticky by default.
137 *
138 * @method danger
139 */
89 NotificationWidget.prototype.danger = function (msg, timeout, click_callback, options) {
140 NotificationWidget.prototype.danger = function (msg, timeout, click_callback, options) {
90 var options = options || {};
141 options = options || {};
91 options.class = options.class +' danger';
142 options.class = options.class + ' danger';
92 this.set_message(msg, timeout, click_callback, options);
143 this.set_message(msg, timeout, click_callback, options);
93 }
144 };
94
95
145
146 /**
147 * Get the text of the widget message.
148 *
149 * @method get_message
150 * @return {string} - the message text
151 */
96 NotificationWidget.prototype.get_message = function () {
152 NotificationWidget.prototype.get_message = function () {
97 return this.inner.html();
153 return this.inner.html();
98 };
154 };
99
155
100 // For backwards compatibility.
156 // For backwards compatibility.
101 IPython.NotificationWidget = NotificationWidget;
157 IPython.NotificationWidget = NotificationWidget;
102
158
103 return {'NotificationWidget': NotificationWidget};
159 return {'NotificationWidget': NotificationWidget};
104 });
160 });
General Comments 0
You need to be logged in to leave comments. Login now