notificationarea.js
325 lines
| 12.8 KiB
| application/javascript
|
JavascriptLexer
|
r17198 | define([ | ||
'base/js/namespace', | ||||
|
r17200 | 'jquery', | ||
|
r17198 | 'base/js/utils', | ||
'base/js/dialog', | ||||
|
r19017 | 'base/js/notificationarea', | ||
|
r17474 | 'moment' | ||
|
r19017 | ], function(IPython, $, utils, dialog, notificationarea, moment) { | ||
|
r8011 | "use strict"; | ||
|
r19017 | var NotificationArea = notificationarea.NotificationArea; | ||
var NotebookNotificationArea = function(selector, options) { | ||||
NotificationArea.apply(this, [selector, options]); | ||||
|
r17210 | this.save_widget = options.save_widget; | ||
this.notebook = options.notebook; | ||||
|
r17212 | this.keyboard_manager = options.keyboard_manager; | ||
|
r19017 | } | ||
NotebookNotificationArea.prototype = Object.create(NotificationArea.prototype); | ||||
|
r18004 | /** | ||
* Initialize the default set of notification widgets. | ||||
* | ||||
* @method init_notification_widgets | ||||
*/ | ||||
|
r19017 | NotebookNotificationArea.prototype.init_notification_widgets = function () { | ||
|
r18004 | this.init_kernel_notification_widget(); | ||
this.init_notebook_notification_widget(); | ||||
}; | ||||
/** | ||||
* Initialize the notification widget for kernel status messages. | ||||
* | ||||
* @method init_kernel_notification_widget | ||||
*/ | ||||
|
r19017 | NotebookNotificationArea.prototype.init_kernel_notification_widget = function () { | ||
|
r17198 | var that = this; | ||
|
r8074 | var knw = this.new_notification_widget('kernel'); | ||
|
r15115 | var $kernel_ind_icon = $("#kernel_indicator_icon"); | ||
var $modal_ind_icon = $("#modal_indicator_icon"); | ||||
// Command/Edit mode | ||||
|
r18230 | this.events.on('edit_mode.Notebook', function () { | ||
|
r17198 | that.save_widget.update_document_title(); | ||
|
r15847 | $modal_ind_icon.attr('class','edit_mode_icon').attr('title','Edit Mode'); | ||
|
r15115 | }); | ||
|
r18230 | this.events.on('command_mode.Notebook', function () { | ||
|
r17198 | that.save_widget.update_document_title(); | ||
|
r15847 | $modal_ind_icon.attr('class','command_mode_icon').attr('title','Command Mode'); | ||
|
r15115 | }); | ||
|
r8074 | |||
|
r15809 | // Implicitly start off in Command mode, switching to Edit mode will trigger event | ||
|
r17433 | $modal_ind_icon.attr('class','command_mode_icon').attr('title','Command Mode'); | ||
|
r15809 | |||
|
r18207 | // Kernel events | ||
|
r18220 | |||
|
r18230 | // this can be either kernel_created.Kernel or kernel_created.Session | ||
this.events.on('kernel_created.Kernel kernel_created.Session', function () { | ||||
knw.info("Kernel Created", 500); | ||||
|
r15042 | }); | ||
|
r8074 | |||
|
r18238 | this.events.on('kernel_reconnecting.Kernel', function () { | ||
|
r18220 | knw.warning("Connecting to kernel"); | ||
}); | ||||
|
r18730 | this.events.on('kernel_connection_dead.Kernel', function (evt, info) { | ||
|
r18742 | knw.danger("Not Connected", undefined, function () { | ||
|
r18730 | // schedule reconnect a short time in the future, don't reconnect immediately | ||
setTimeout($.proxy(info.kernel.reconnect, info.kernel), 500); | ||||
}, {title: 'click to reconnect'}); | ||||
}); | ||||
|
r18238 | this.events.on('kernel_connected.Kernel', function () { | ||
|
r18220 | knw.info("Connected", 500); | ||
|
r8074 | }); | ||
|
r18238 | this.events.on('kernel_restarting.Kernel', function () { | ||
|
r17198 | that.save_widget.update_document_title(); | ||
|
r10316 | knw.set_message("Restarting kernel", 2000); | ||
|
r8074 | }); | ||
|
r18238 | this.events.on('kernel_autorestarting.Kernel', function (evt, info) { | ||
|
r18235 | // Only show the dialog on the first restart attempt. This | ||
// number gets tracked by the `Kernel` object and passed | ||||
// along here, because we don't want to show the user 5 | ||||
// dialogs saying the same thing (which is the number of | ||||
// times it tries restarting). | ||||
if (info.attempt === 1) { | ||||
|
r18237 | dialog.kernel_modal({ | ||
|
r18233 | notebook: that.notebook, | ||
keyboard_manager: that.keyboard_manager, | ||||
title: "Kernel Restarting", | ||||
body: "The kernel appears to have died. It will restart automatically.", | ||||
buttons: { | ||||
OK : { | ||||
class : "btn-primary" | ||||
} | ||||
|
r18220 | } | ||
|
r18233 | }); | ||
}; | ||||
|
r18230 | |||
that.save_widget.update_document_title(); | ||||
knw.danger("Dead kernel"); | ||||
$kernel_ind_icon.attr('class','kernel_dead_icon').attr('title','Kernel Dead'); | ||||
|
r18220 | }); | ||
|
r18238 | this.events.on('kernel_interrupting.Kernel', function () { | ||
|
r14816 | knw.set_message("Interrupting kernel", 2000); | ||
|
r8074 | }); | ||
|
r15737 | |||
|
r18238 | this.events.on('kernel_disconnected.Kernel', function () { | ||
|
r18207 | $kernel_ind_icon | ||
.attr('class', 'kernel_disconnected_icon') | ||||
.attr('title', 'No Connection to Kernel'); | ||||
|
r18202 | }); | ||
|
r18238 | this.events.on('kernel_connection_failed.Kernel', function (evt, info) { | ||
|
r18236 | // only show the dialog if this is the first failed | ||
// connect attempt, because the kernel will continue | ||||
// trying to reconnect and we don't want to spam the user | ||||
// with messages | ||||
if (info.attempt === 1) { | ||||
var msg = "A connection to the notebook server could not be established." + | ||||
" The notebook will continue trying to reconnect, but" + | ||||
" until it does, you will NOT be able to run code. Check your" + | ||||
" network connection or notebook server configuration."; | ||||
|
r18237 | dialog.kernel_modal({ | ||
|
r18236 | title: "Connection failed", | ||
body: msg, | ||||
keyboard_manager: that.keyboard_manager, | ||||
notebook: that.notebook, | ||||
buttons : { | ||||
"OK": {} | ||||
|
r18207 | } | ||
|
r18236 | }); | ||
} | ||||
|
r15737 | }); | ||
|
r8074 | |||
|
r18238 | this.events.on('kernel_killed.Kernel kernel_killed.Session', function () { | ||
|
r18207 | that.save_widget.update_document_title(); | ||
knw.danger("Dead kernel"); | ||||
$kernel_ind_icon.attr('class','kernel_dead_icon').attr('title','Kernel Dead'); | ||||
}); | ||||
|
r18220 | this.events.on('kernel_dead.Kernel', function () { | ||
|
r10895 | |||
|
r18233 | var showMsg = function () { | ||
var msg = 'The kernel has died, and the automatic restart has failed.' + | ||||
' It is possible the kernel cannot be restarted.' + | ||||
' If you are not able to restart the kernel, you will still be able to save' + | ||||
' the notebook, but running code will no longer work until the notebook' + | ||||
' is reopened.'; | ||||
|
r18237 | dialog.kernel_modal({ | ||
|
r18233 | title: "Dead kernel", | ||
body : msg, | ||||
keyboard_manager: that.keyboard_manager, | ||||
notebook: that.notebook, | ||||
buttons : { | ||||
"Manual Restart": { | ||||
class: "btn-danger", | ||||
click: function () { | ||||
that.notebook.start_session(); | ||||
} | ||||
}, | ||||
|
r10895 | "Don't restart": {} | ||
|
r18233 | } | ||
}); | ||||
return false; | ||||
}; | ||||
that.save_widget.update_document_title(); | ||||
knw.danger("Dead kernel", undefined, showMsg); | ||||
$kernel_ind_icon.attr('class','kernel_dead_icon').attr('title','Kernel Dead'); | ||||
showMsg(); | ||||
|
r8074 | }); | ||
|
r18227 | this.events.on('kernel_dead.Session', function (evt, info) { | ||
var full = info.xhr.responseJSON.message; | ||||
var short = info.xhr.responseJSON.short_message || 'Kernel error'; | ||||
var traceback = info.xhr.responseJSON.traceback; | ||||
|
r18060 | |||
|
r18066 | var showMsg = function () { | ||
|
r18108 | var msg = $('<div/>').append($('<p/>').text(full)); | ||
|
r18229 | var cm, cm_elem, cm_open; | ||
|
r18108 | |||
if (traceback) { | ||||
cm_elem = $('<div/>') | ||||
.css('margin-top', '1em') | ||||
.css('padding', '1em') | ||||
.addClass('output_scroll'); | ||||
msg.append(cm_elem); | ||||
cm = CodeMirror(cm_elem.get(0), { | ||||
mode: "python", | ||||
readOnly : true | ||||
}); | ||||
cm.setValue(traceback); | ||||
|
r18229 | cm_open = $.proxy(cm.refresh, cm); | ||
|
r18108 | } | ||
|
r18237 | dialog.kernel_modal({ | ||
|
r18063 | title: "Failed to start the kernel", | ||
|
r18066 | body : msg, | ||
|
r18063 | keyboard_manager: that.keyboard_manager, | ||
notebook: that.notebook, | ||||
|
r18229 | open: cm_open, | ||
|
r18063 | buttons : { | ||
"Ok": { class: 'btn-primary' } | ||||
} | ||||
}); | ||||
|
r18108 | |||
|
r18063 | return false; | ||
|
r18066 | }; | ||
that.save_widget.update_document_title(); | ||||
$kernel_ind_icon.attr('class','kernel_dead_icon').attr('title','Kernel Dead'); | ||||
knw.danger(short, undefined, showMsg); | ||||
|
r18060 | }); | ||
|
r18238 | this.events.on('kernel_starting.Kernel', function () { | ||
|
r18230 | window.document.title='(Starting) '+window.document.title; | ||
$kernel_ind_icon.attr('class','kernel_busy_icon').attr('title','Kernel Busy'); | ||||
knw.set_message("Kernel starting, please wait..."); | ||||
}); | ||||
|
r18238 | this.events.on('kernel_ready.Kernel', function () { | ||
|
r18230 | that.save_widget.update_document_title(); | ||
$kernel_ind_icon.attr('class','kernel_idle_icon').attr('title','Kernel Idle'); | ||||
knw.info("Kernel ready", 500); | ||||
}); | ||||
|
r18238 | this.events.on('kernel_idle.Kernel', function () { | ||
|
r18207 | that.save_widget.update_document_title(); | ||
$kernel_ind_icon.attr('class','kernel_idle_icon').attr('title','Kernel Idle'); | ||||
}); | ||||
|
r17676 | |||
|
r18238 | this.events.on('kernel_busy.Kernel', function () { | ||
|
r18207 | window.document.title='(Busy) '+window.document.title; | ||
$kernel_ind_icon.attr('class','kernel_busy_icon').attr('title','Kernel Busy'); | ||||
|
r9222 | }); | ||
|
r18207 | |||
// Start the kernel indicator in the busy state, and send a kernel_info request. | ||||
// When the kernel_info reply arrives, the kernel is idle. | ||||
$kernel_ind_icon.attr('class','kernel_busy_icon').attr('title','Kernel Busy'); | ||||
|
r18004 | }; | ||
|
r9222 | |||
|
r18004 | /** | ||
* Initialize the notification widget for notebook status messages. | ||||
* | ||||
* @method init_notebook_notification_widget | ||||
*/ | ||||
|
r19017 | NotebookNotificationArea.prototype.init_notebook_notification_widget = function () { | ||
|
r8074 | var nnw = this.new_notification_widget('notebook'); | ||
// Notebook events | ||||
|
r17198 | this.events.on('notebook_loading.Notebook', function () { | ||
|
r8074 | nnw.set_message("Loading notebook",500); | ||
}); | ||||
|
r17198 | this.events.on('notebook_loaded.Notebook', function () { | ||
|
r8074 | nnw.set_message("Notebook loaded",500); | ||
}); | ||||
|
r17198 | this.events.on('notebook_saving.Notebook', function () { | ||
|
r8074 | nnw.set_message("Saving notebook",500); | ||
}); | ||||
|
r17198 | this.events.on('notebook_saved.Notebook', function () { | ||
|
r8074 | nnw.set_message("Notebook saved",2000); | ||
}); | ||||
|
r19005 | this.events.on('notebook_save_failed.Notebook', function (evt, error) { | ||
nnw.warning(error.message || "Notebook save failed"); | ||||
}); | ||||
this.events.on('notebook_copy_failed.Notebook', function (evt, error) { | ||||
nnw.warning(error.message || "Notebook copy failed"); | ||||
|
r8074 | }); | ||
|
r10502 | |||
|
r19164 | nnw.set_message(msg, 1000); | ||
|
r19282 | this.events.on('toggle_other_client_output.Notebook', function(evt, ignored) { | ||
var msg = (ignored? "Ignoring": "Showing") + " output from other clients"; | ||||
|
r19164 | }); | ||
|
r10502 | // Checkpoint events | ||
|
r17198 | this.events.on('checkpoint_created.Notebook', function (evt, data) { | ||
|
r10502 | var msg = "Checkpoint created"; | ||
if (data.last_modified) { | ||||
var d = new Date(data.last_modified); | ||||
|
r17474 | msg = msg + ": " + moment(d).format("HH:mm:ss"); | ||
|
r10502 | } | ||
nnw.set_message(msg, 2000); | ||||
}); | ||||
|
r17198 | this.events.on('checkpoint_failed.Notebook', function () { | ||
|
r17368 | nnw.warning("Checkpoint failed"); | ||
|
r10502 | }); | ||
|
r17198 | this.events.on('checkpoint_deleted.Notebook', function () { | ||
|
r10502 | nnw.set_message("Checkpoint deleted", 500); | ||
}); | ||||
|
r17198 | this.events.on('checkpoint_delete_failed.Notebook', function () { | ||
|
r17368 | nnw.warning("Checkpoint delete failed"); | ||
|
r10502 | }); | ||
|
r17198 | this.events.on('checkpoint_restoring.Notebook', function () { | ||
|
r10502 | nnw.set_message("Restoring to checkpoint...", 500); | ||
}); | ||||
|
r17198 | this.events.on('checkpoint_restore_failed.Notebook', function () { | ||
|
r17368 | nnw.warning("Checkpoint restore failed"); | ||
|
r10502 | }); | ||
|
r8074 | |||
|
r10505 | // Autosave events | ||
|
r17198 | this.events.on('autosave_disabled.Notebook', function () { | ||
|
r10505 | nnw.set_message("Autosave disabled", 2000); | ||
}); | ||||
|
r17198 | this.events.on('autosave_enabled.Notebook', function (evt, interval) { | ||
|
r10505 | nnw.set_message("Saving every " + interval / 1000 + "s", 1000); | ||
}); | ||||
|
r8204 | }; | ||
|
r8048 | |||
|
r19017 | // Backwards compatibility. | ||
IPython.NotificationArea = NotebookNotificationArea; | ||||
return {'NotebookNotificationArea': NotebookNotificationArea}; | ||||
|
r17198 | }); | ||