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