Show More
@@ -1,384 +1,384 | |||
|
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 | 14 | // store reference to the NotificationWidget class |
|
15 | 15 | var NotificationWidget = notificationwidget.NotificationWidget; |
|
16 | 16 | |
|
17 | 17 | /** |
|
18 | 18 | * Construct the NotificationArea object. Options are: |
|
19 | 19 | * events: $(Events) instance |
|
20 | 20 | * save_widget: SaveWidget instance |
|
21 | 21 | * notebook: Notebook instance |
|
22 | 22 | * keyboard_manager: KeyboardManager instance |
|
23 | 23 | * |
|
24 | 24 | * @constructor |
|
25 | 25 | * @param {string} selector - a jQuery selector string for the |
|
26 | 26 | * notification area element |
|
27 | 27 | * @param {Object} [options] - a dictionary of keyword arguments. |
|
28 | 28 | */ |
|
29 | 29 | var NotificationArea = function (selector, options) { |
|
30 | 30 | this.selector = selector; |
|
31 | 31 | this.events = options.events; |
|
32 | 32 | this.save_widget = options.save_widget; |
|
33 | 33 | this.notebook = options.notebook; |
|
34 | 34 | this.keyboard_manager = options.keyboard_manager; |
|
35 | 35 | if (this.selector !== undefined) { |
|
36 | 36 | this.element = $(selector); |
|
37 | 37 | } |
|
38 | 38 | this.widget_dict = {}; |
|
39 | 39 | }; |
|
40 | 40 | |
|
41 | 41 | /** |
|
42 | 42 | * Get a widget by name, creating it if it doesn't exist. |
|
43 | 43 | * |
|
44 | 44 | * @method widget |
|
45 | 45 | * @param {string} name - the widget name |
|
46 | 46 | */ |
|
47 | 47 | NotificationArea.prototype.widget = function (name) { |
|
48 | 48 | if (this.widget_dict[name] === undefined) { |
|
49 | 49 | return this.new_notification_widget(name); |
|
50 | 50 | } |
|
51 | 51 | return this.get_widget(name); |
|
52 | 52 | }; |
|
53 | 53 | |
|
54 | 54 | /** |
|
55 | 55 | * Get a widget by name, throwing an error if it doesn't exist. |
|
56 | 56 | * |
|
57 | 57 | * @method get_widget |
|
58 | 58 | * @param {string} name - the widget name |
|
59 | 59 | */ |
|
60 | 60 | NotificationArea.prototype.get_widget = function (name) { |
|
61 | 61 | if(this.widget_dict[name] === undefined) { |
|
62 | 62 | throw('no widgets with this name'); |
|
63 | 63 | } |
|
64 | 64 | return this.widget_dict[name]; |
|
65 | 65 | }; |
|
66 | 66 | |
|
67 | 67 | /** |
|
68 | 68 | * Create a new notification widget with the given name. The |
|
69 | 69 | * widget must not already exist. |
|
70 | 70 | * |
|
71 | 71 | * @method new_notification_widget |
|
72 | 72 | * @param {string} name - the widget name |
|
73 | 73 | */ |
|
74 | 74 | NotificationArea.prototype.new_notification_widget = function (name) { |
|
75 | 75 | if (this.widget_dict[name] !== undefined) { |
|
76 | 76 | throw('widget with that name already exists!'); |
|
77 | 77 | } |
|
78 | 78 | |
|
79 | 79 | // create the element for the notification widget and add it |
|
80 | 80 | // to the notification aread element |
|
81 | 81 | var div = $('<div/>').attr('id', 'notification_' + name); |
|
82 | 82 | $(this.selector).append(div); |
|
83 | 83 | |
|
84 | 84 | // create the widget object and return it |
|
85 | 85 | this.widget_dict[name] = new NotificationWidget('#notification_' + name); |
|
86 | 86 | return this.widget_dict[name]; |
|
87 | 87 | }; |
|
88 | 88 | |
|
89 | 89 | /** |
|
90 | 90 | * Initialize the default set of notification widgets. |
|
91 | 91 | * |
|
92 | 92 | * @method init_notification_widgets |
|
93 | 93 | */ |
|
94 | 94 | NotificationArea.prototype.init_notification_widgets = function () { |
|
95 | 95 | this.init_kernel_notification_widget(); |
|
96 | 96 | this.init_notebook_notification_widget(); |
|
97 | 97 | }; |
|
98 | 98 | |
|
99 | 99 | /** |
|
100 | 100 | * Initialize the notification widget for kernel status messages. |
|
101 | 101 | * |
|
102 | 102 | * @method init_kernel_notification_widget |
|
103 | 103 | */ |
|
104 | 104 | NotificationArea.prototype.init_kernel_notification_widget = function () { |
|
105 | 105 | var that = this; |
|
106 | 106 | var knw = this.new_notification_widget('kernel'); |
|
107 | 107 | var $kernel_ind_icon = $("#kernel_indicator_icon"); |
|
108 | 108 | var $modal_ind_icon = $("#modal_indicator_icon"); |
|
109 | 109 | |
|
110 | 110 | // Command/Edit mode |
|
111 | 111 | this.events.on('edit_mode.Notebook', function () { |
|
112 | 112 | that.save_widget.update_document_title(); |
|
113 | 113 | $modal_ind_icon.attr('class','edit_mode_icon').attr('title','Edit Mode'); |
|
114 | 114 | }); |
|
115 | 115 | |
|
116 | 116 | this.events.on('command_mode.Notebook', function () { |
|
117 | 117 | that.save_widget.update_document_title(); |
|
118 | 118 | $modal_ind_icon.attr('class','command_mode_icon').attr('title','Command Mode'); |
|
119 | 119 | }); |
|
120 | 120 | |
|
121 | 121 | // Implicitly start off in Command mode, switching to Edit mode will trigger event |
|
122 | 122 | $modal_ind_icon.attr('class','command_mode_icon').attr('title','Command Mode'); |
|
123 | 123 | |
|
124 | 124 | // Kernel events |
|
125 | 125 | |
|
126 | 126 | // this can be either kernel_created.Kernel or kernel_created.Session |
|
127 | 127 | this.events.on('kernel_created.Kernel kernel_created.Session', function () { |
|
128 | 128 | knw.info("Kernel Created", 500); |
|
129 | 129 | }); |
|
130 | 130 | |
|
131 | 131 | this.events.on('kernel_reconnecting.Kernel', function () { |
|
132 | 132 | knw.warning("Connecting to kernel"); |
|
133 | 133 | }); |
|
134 | 134 | |
|
135 | 135 | this.events.on('kernel_connection_dead.Kernel', function (evt, info) { |
|
136 |
knw.danger("Connect |
|
|
136 | knw.danger("Not Connected", undefined, function () { | |
|
137 | 137 | // schedule reconnect a short time in the future, don't reconnect immediately |
|
138 | 138 | setTimeout($.proxy(info.kernel.reconnect, info.kernel), 500); |
|
139 | 139 | }, {title: 'click to reconnect'}); |
|
140 | 140 | }); |
|
141 | 141 | |
|
142 | 142 | this.events.on('kernel_connected.Kernel', function () { |
|
143 | 143 | knw.info("Connected", 500); |
|
144 | 144 | }); |
|
145 | 145 | |
|
146 | 146 | this.events.on('kernel_restarting.Kernel', function () { |
|
147 | 147 | that.save_widget.update_document_title(); |
|
148 | 148 | knw.set_message("Restarting kernel", 2000); |
|
149 | 149 | }); |
|
150 | 150 | |
|
151 | 151 | this.events.on('kernel_autorestarting.Kernel', function (evt, info) { |
|
152 | 152 | // Only show the dialog on the first restart attempt. This |
|
153 | 153 | // number gets tracked by the `Kernel` object and passed |
|
154 | 154 | // along here, because we don't want to show the user 5 |
|
155 | 155 | // dialogs saying the same thing (which is the number of |
|
156 | 156 | // times it tries restarting). |
|
157 | 157 | if (info.attempt === 1) { |
|
158 | 158 | |
|
159 | 159 | dialog.kernel_modal({ |
|
160 | 160 | notebook: that.notebook, |
|
161 | 161 | keyboard_manager: that.keyboard_manager, |
|
162 | 162 | title: "Kernel Restarting", |
|
163 | 163 | body: "The kernel appears to have died. It will restart automatically.", |
|
164 | 164 | buttons: { |
|
165 | 165 | OK : { |
|
166 | 166 | class : "btn-primary" |
|
167 | 167 | } |
|
168 | 168 | } |
|
169 | 169 | }); |
|
170 | 170 | }; |
|
171 | 171 | |
|
172 | 172 | that.save_widget.update_document_title(); |
|
173 | 173 | knw.danger("Dead kernel"); |
|
174 | 174 | $kernel_ind_icon.attr('class','kernel_dead_icon').attr('title','Kernel Dead'); |
|
175 | 175 | }); |
|
176 | 176 | |
|
177 | 177 | this.events.on('kernel_interrupting.Kernel', function () { |
|
178 | 178 | knw.set_message("Interrupting kernel", 2000); |
|
179 | 179 | }); |
|
180 | 180 | |
|
181 | 181 | this.events.on('kernel_disconnected.Kernel', function () { |
|
182 | 182 | $kernel_ind_icon |
|
183 | 183 | .attr('class', 'kernel_disconnected_icon') |
|
184 | 184 | .attr('title', 'No Connection to Kernel'); |
|
185 | 185 | }); |
|
186 | 186 | |
|
187 | 187 | this.events.on('kernel_connection_failed.Kernel', function (evt, info) { |
|
188 | 188 | // only show the dialog if this is the first failed |
|
189 | 189 | // connect attempt, because the kernel will continue |
|
190 | 190 | // trying to reconnect and we don't want to spam the user |
|
191 | 191 | // with messages |
|
192 | 192 | if (info.attempt === 1) { |
|
193 | 193 | |
|
194 | 194 | var msg = "A connection to the notebook server could not be established." + |
|
195 | 195 | " The notebook will continue trying to reconnect, but" + |
|
196 | 196 | " until it does, you will NOT be able to run code. Check your" + |
|
197 | 197 | " network connection or notebook server configuration."; |
|
198 | 198 | |
|
199 | 199 | dialog.kernel_modal({ |
|
200 | 200 | title: "Connection failed", |
|
201 | 201 | body: msg, |
|
202 | 202 | keyboard_manager: that.keyboard_manager, |
|
203 | 203 | notebook: that.notebook, |
|
204 | 204 | buttons : { |
|
205 | 205 | "OK": {} |
|
206 | 206 | } |
|
207 | 207 | }); |
|
208 | 208 | } |
|
209 | 209 | }); |
|
210 | 210 | |
|
211 | 211 | this.events.on('kernel_killed.Kernel kernel_killed.Session', function () { |
|
212 | 212 | that.save_widget.update_document_title(); |
|
213 | 213 | knw.danger("Dead kernel"); |
|
214 | 214 | $kernel_ind_icon.attr('class','kernel_dead_icon').attr('title','Kernel Dead'); |
|
215 | 215 | }); |
|
216 | 216 | |
|
217 | 217 | this.events.on('kernel_dead.Kernel', function () { |
|
218 | 218 | |
|
219 | 219 | var showMsg = function () { |
|
220 | 220 | |
|
221 | 221 | var msg = 'The kernel has died, and the automatic restart has failed.' + |
|
222 | 222 | ' It is possible the kernel cannot be restarted.' + |
|
223 | 223 | ' If you are not able to restart the kernel, you will still be able to save' + |
|
224 | 224 | ' the notebook, but running code will no longer work until the notebook' + |
|
225 | 225 | ' is reopened.'; |
|
226 | 226 | |
|
227 | 227 | dialog.kernel_modal({ |
|
228 | 228 | title: "Dead kernel", |
|
229 | 229 | body : msg, |
|
230 | 230 | keyboard_manager: that.keyboard_manager, |
|
231 | 231 | notebook: that.notebook, |
|
232 | 232 | buttons : { |
|
233 | 233 | "Manual Restart": { |
|
234 | 234 | class: "btn-danger", |
|
235 | 235 | click: function () { |
|
236 | 236 | that.notebook.start_session(); |
|
237 | 237 | } |
|
238 | 238 | }, |
|
239 | 239 | "Don't restart": {} |
|
240 | 240 | } |
|
241 | 241 | }); |
|
242 | 242 | |
|
243 | 243 | return false; |
|
244 | 244 | }; |
|
245 | 245 | |
|
246 | 246 | that.save_widget.update_document_title(); |
|
247 | 247 | knw.danger("Dead kernel", undefined, showMsg); |
|
248 | 248 | $kernel_ind_icon.attr('class','kernel_dead_icon').attr('title','Kernel Dead'); |
|
249 | 249 | |
|
250 | 250 | showMsg(); |
|
251 | 251 | }); |
|
252 | 252 | |
|
253 | 253 | this.events.on('kernel_dead.Session', function (evt, info) { |
|
254 | 254 | var full = info.xhr.responseJSON.message; |
|
255 | 255 | var short = info.xhr.responseJSON.short_message || 'Kernel error'; |
|
256 | 256 | var traceback = info.xhr.responseJSON.traceback; |
|
257 | 257 | |
|
258 | 258 | var showMsg = function () { |
|
259 | 259 | var msg = $('<div/>').append($('<p/>').text(full)); |
|
260 | 260 | var cm, cm_elem, cm_open; |
|
261 | 261 | |
|
262 | 262 | if (traceback) { |
|
263 | 263 | cm_elem = $('<div/>') |
|
264 | 264 | .css('margin-top', '1em') |
|
265 | 265 | .css('padding', '1em') |
|
266 | 266 | .addClass('output_scroll'); |
|
267 | 267 | msg.append(cm_elem); |
|
268 | 268 | cm = CodeMirror(cm_elem.get(0), { |
|
269 | 269 | mode: "python", |
|
270 | 270 | readOnly : true |
|
271 | 271 | }); |
|
272 | 272 | cm.setValue(traceback); |
|
273 | 273 | cm_open = $.proxy(cm.refresh, cm); |
|
274 | 274 | } |
|
275 | 275 | |
|
276 | 276 | dialog.kernel_modal({ |
|
277 | 277 | title: "Failed to start the kernel", |
|
278 | 278 | body : msg, |
|
279 | 279 | keyboard_manager: that.keyboard_manager, |
|
280 | 280 | notebook: that.notebook, |
|
281 | 281 | open: cm_open, |
|
282 | 282 | buttons : { |
|
283 | 283 | "Ok": { class: 'btn-primary' } |
|
284 | 284 | } |
|
285 | 285 | }); |
|
286 | 286 | |
|
287 | 287 | return false; |
|
288 | 288 | }; |
|
289 | 289 | |
|
290 | 290 | that.save_widget.update_document_title(); |
|
291 | 291 | $kernel_ind_icon.attr('class','kernel_dead_icon').attr('title','Kernel Dead'); |
|
292 | 292 | knw.danger(short, undefined, showMsg); |
|
293 | 293 | }); |
|
294 | 294 | |
|
295 | 295 | this.events.on('kernel_starting.Kernel', function () { |
|
296 | 296 | window.document.title='(Starting) '+window.document.title; |
|
297 | 297 | $kernel_ind_icon.attr('class','kernel_busy_icon').attr('title','Kernel Busy'); |
|
298 | 298 | knw.set_message("Kernel starting, please wait..."); |
|
299 | 299 | }); |
|
300 | 300 | |
|
301 | 301 | this.events.on('kernel_ready.Kernel', function () { |
|
302 | 302 | that.save_widget.update_document_title(); |
|
303 | 303 | $kernel_ind_icon.attr('class','kernel_idle_icon').attr('title','Kernel Idle'); |
|
304 | 304 | knw.info("Kernel ready", 500); |
|
305 | 305 | }); |
|
306 | 306 | |
|
307 | 307 | this.events.on('kernel_idle.Kernel', function () { |
|
308 | 308 | that.save_widget.update_document_title(); |
|
309 | 309 | $kernel_ind_icon.attr('class','kernel_idle_icon').attr('title','Kernel Idle'); |
|
310 | 310 | }); |
|
311 | 311 | |
|
312 | 312 | this.events.on('kernel_busy.Kernel', function () { |
|
313 | 313 | window.document.title='(Busy) '+window.document.title; |
|
314 | 314 | $kernel_ind_icon.attr('class','kernel_busy_icon').attr('title','Kernel Busy'); |
|
315 | 315 | }); |
|
316 | 316 | |
|
317 | 317 | // Start the kernel indicator in the busy state, and send a kernel_info request. |
|
318 | 318 | // When the kernel_info reply arrives, the kernel is idle. |
|
319 | 319 | $kernel_ind_icon.attr('class','kernel_busy_icon').attr('title','Kernel Busy'); |
|
320 | 320 | }; |
|
321 | 321 | |
|
322 | 322 | /** |
|
323 | 323 | * Initialize the notification widget for notebook status messages. |
|
324 | 324 | * |
|
325 | 325 | * @method init_notebook_notification_widget |
|
326 | 326 | */ |
|
327 | 327 | NotificationArea.prototype.init_notebook_notification_widget = function () { |
|
328 | 328 | var nnw = this.new_notification_widget('notebook'); |
|
329 | 329 | |
|
330 | 330 | // Notebook events |
|
331 | 331 | this.events.on('notebook_loading.Notebook', function () { |
|
332 | 332 | nnw.set_message("Loading notebook",500); |
|
333 | 333 | }); |
|
334 | 334 | this.events.on('notebook_loaded.Notebook', function () { |
|
335 | 335 | nnw.set_message("Notebook loaded",500); |
|
336 | 336 | }); |
|
337 | 337 | this.events.on('notebook_saving.Notebook', function () { |
|
338 | 338 | nnw.set_message("Saving notebook",500); |
|
339 | 339 | }); |
|
340 | 340 | this.events.on('notebook_saved.Notebook', function () { |
|
341 | 341 | nnw.set_message("Notebook saved",2000); |
|
342 | 342 | }); |
|
343 | 343 | this.events.on('notebook_save_failed.Notebook', function (evt, xhr, status, data) { |
|
344 | 344 | nnw.warning(data || "Notebook save failed"); |
|
345 | 345 | }); |
|
346 | 346 | |
|
347 | 347 | // Checkpoint events |
|
348 | 348 | this.events.on('checkpoint_created.Notebook', function (evt, data) { |
|
349 | 349 | var msg = "Checkpoint created"; |
|
350 | 350 | if (data.last_modified) { |
|
351 | 351 | var d = new Date(data.last_modified); |
|
352 | 352 | msg = msg + ": " + moment(d).format("HH:mm:ss"); |
|
353 | 353 | } |
|
354 | 354 | nnw.set_message(msg, 2000); |
|
355 | 355 | }); |
|
356 | 356 | this.events.on('checkpoint_failed.Notebook', function () { |
|
357 | 357 | nnw.warning("Checkpoint failed"); |
|
358 | 358 | }); |
|
359 | 359 | this.events.on('checkpoint_deleted.Notebook', function () { |
|
360 | 360 | nnw.set_message("Checkpoint deleted", 500); |
|
361 | 361 | }); |
|
362 | 362 | this.events.on('checkpoint_delete_failed.Notebook', function () { |
|
363 | 363 | nnw.warning("Checkpoint delete failed"); |
|
364 | 364 | }); |
|
365 | 365 | this.events.on('checkpoint_restoring.Notebook', function () { |
|
366 | 366 | nnw.set_message("Restoring to checkpoint...", 500); |
|
367 | 367 | }); |
|
368 | 368 | this.events.on('checkpoint_restore_failed.Notebook', function () { |
|
369 | 369 | nnw.warning("Checkpoint restore failed"); |
|
370 | 370 | }); |
|
371 | 371 | |
|
372 | 372 | // Autosave events |
|
373 | 373 | this.events.on('autosave_disabled.Notebook', function () { |
|
374 | 374 | nnw.set_message("Autosave disabled", 2000); |
|
375 | 375 | }); |
|
376 | 376 | this.events.on('autosave_enabled.Notebook', function (evt, interval) { |
|
377 | 377 | nnw.set_message("Saving every " + interval / 1000 + "s", 1000); |
|
378 | 378 | }); |
|
379 | 379 | }; |
|
380 | 380 | |
|
381 | 381 | IPython.NotificationArea = NotificationArea; |
|
382 | 382 | |
|
383 | 383 | return {'NotificationArea': NotificationArea}; |
|
384 | 384 | }); |
General Comments 0
You need to be logged in to leave comments.
Login now