notificationarea.js
347 lines
| 13.7 KiB
| application/javascript
|
JavascriptLexer
Jonathan Frederic
|
r17198 | define([ | ||
'base/js/namespace', | ||||
Jonathan Frederic
|
r17200 | 'jquery', | ||
Jonathan Frederic
|
r17198 | 'base/js/utils', | ||
'base/js/dialog', | ||||
Thomas Kluyver
|
r19017 | 'base/js/notificationarea', | ||
Matthias BUSSONNIER
|
r17474 | 'moment' | ||
Thomas Kluyver
|
r19017 | ], function(IPython, $, utils, dialog, notificationarea, moment) { | ||
Matthias BUSSONNIER
|
r8011 | "use strict"; | ||
Thomas Kluyver
|
r19017 | var NotificationArea = notificationarea.NotificationArea; | ||
var NotebookNotificationArea = function(selector, options) { | ||||
NotificationArea.apply(this, [selector, options]); | ||||
jon
|
r17210 | this.save_widget = options.save_widget; | ||
this.notebook = options.notebook; | ||||
Jonathan Frederic
|
r17212 | this.keyboard_manager = options.keyboard_manager; | ||
Min RK
|
r20199 | }; | ||
Thomas Kluyver
|
r19017 | |||
NotebookNotificationArea.prototype = Object.create(NotificationArea.prototype); | ||||
Jessica B. Hamrick
|
r18004 | /** | ||
* Initialize the default set of notification widgets. | ||||
* | ||||
* @method init_notification_widgets | ||||
*/ | ||||
Thomas Kluyver
|
r19017 | NotebookNotificationArea.prototype.init_notification_widgets = function () { | ||
Jessica B. Hamrick
|
r18004 | this.init_kernel_notification_widget(); | ||
this.init_notebook_notification_widget(); | ||||
}; | ||||
/** | ||||
* Initialize the notification widget for kernel status messages. | ||||
* | ||||
* @method init_kernel_notification_widget | ||||
*/ | ||||
Thomas Kluyver
|
r19017 | NotebookNotificationArea.prototype.init_kernel_notification_widget = function () { | ||
Jonathan Frederic
|
r17198 | var that = this; | ||
Matthias BUSSONNIER
|
r8074 | var knw = this.new_notification_widget('kernel'); | ||
Brian E. Granger
|
r15115 | var $kernel_ind_icon = $("#kernel_indicator_icon"); | ||
Bussonnier Matthias
|
r19671 | var $modal_ind_icon = $("#modal_indicator"); | ||
Jonathan Frederic
|
r20374 | var $readonly_ind_icon = $('#readonly-indicator'); | ||
Min RK
|
r20199 | var $body = $('body'); | ||
Brian E. Granger
|
r15115 | |||
Jonathan Frederic
|
r20374 | // Listen for the notebook loaded event. Set readonly indicator. | ||
this.events.on('notebook_loaded.Notebook', function() { | ||||
if (that.notebook.writable) { | ||||
$readonly_ind_icon.hide(); | ||||
} else { | ||||
$readonly_ind_icon.show(); | ||||
} | ||||
}); | ||||
Brian E. Granger
|
r15115 | // Command/Edit mode | ||
Jessica B. Hamrick
|
r18230 | this.events.on('edit_mode.Notebook', function () { | ||
Jonathan Frederic
|
r17198 | that.save_widget.update_document_title(); | ||
Bussonnier Matthias
|
r19671 | $body.addClass('edit_mode'); | ||
$body.removeClass('command_mode'); | ||||
$modal_ind_icon.attr('title','Edit Mode'); | ||||
Brian E. Granger
|
r15115 | }); | ||
Jessica B. Hamrick
|
r18230 | this.events.on('command_mode.Notebook', function () { | ||
Jonathan Frederic
|
r17198 | that.save_widget.update_document_title(); | ||
Bussonnier Matthias
|
r19671 | $body.removeClass('edit_mode'); | ||
$body.addClass('command_mode'); | ||||
$modal_ind_icon.attr('title','Command Mode'); | ||||
Brian E. Granger
|
r15115 | }); | ||
Matthias BUSSONNIER
|
r8074 | |||
Paul Ivanov
|
r15809 | // Implicitly start off in Command mode, switching to Edit mode will trigger event | ||
Bussonnier Matthias
|
r19671 | $modal_ind_icon.addClass('modal_indicator').attr('title','Command Mode'); | ||
Min RK
|
r20199 | $body.addClass('command_mode'); | ||
Paul Ivanov
|
r15809 | |||
Min RK
|
r20199 | // Kernel events | ||
Jessica B. Hamrick
|
r18220 | |||
Jessica B. Hamrick
|
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); | ||||
Matthias BUSSONNIER
|
r15042 | }); | ||
Matthias BUSSONNIER
|
r8074 | |||
Jessica B. Hamrick
|
r18238 | this.events.on('kernel_reconnecting.Kernel', function () { | ||
Jessica B. Hamrick
|
r18220 | knw.warning("Connecting to kernel"); | ||
}); | ||||
Min RK
|
r18730 | this.events.on('kernel_connection_dead.Kernel', function (evt, info) { | ||
Min RK
|
r18742 | knw.danger("Not Connected", undefined, function () { | ||
Min RK
|
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'}); | ||||
}); | ||||
Jessica B. Hamrick
|
r18238 | this.events.on('kernel_connected.Kernel', function () { | ||
Jessica B. Hamrick
|
r18220 | knw.info("Connected", 500); | ||
Matthias BUSSONNIER
|
r8074 | }); | ||
Jessica B. Hamrick
|
r18238 | this.events.on('kernel_restarting.Kernel', function () { | ||
Jonathan Frederic
|
r17198 | that.save_widget.update_document_title(); | ||
MinRK
|
r10316 | knw.set_message("Restarting kernel", 2000); | ||
Matthias BUSSONNIER
|
r8074 | }); | ||
Jessica B. Hamrick
|
r18238 | this.events.on('kernel_autorestarting.Kernel', function (evt, info) { | ||
Jessica B. Hamrick
|
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) { | ||||
Jessica B. Hamrick
|
r18237 | dialog.kernel_modal({ | ||
Jessica B. Hamrick
|
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" | ||||
} | ||||
Jessica B. Hamrick
|
r18220 | } | ||
Jessica B. Hamrick
|
r18233 | }); | ||
Min RK
|
r20199 | } | ||
Jessica B. Hamrick
|
r18230 | |||
that.save_widget.update_document_title(); | ||||
knw.danger("Dead kernel"); | ||||
$kernel_ind_icon.attr('class','kernel_dead_icon').attr('title','Kernel Dead'); | ||||
Jessica B. Hamrick
|
r18220 | }); | ||
Jessica B. Hamrick
|
r18238 | this.events.on('kernel_interrupting.Kernel', function () { | ||
Brian E. Granger
|
r14816 | knw.set_message("Interrupting kernel", 2000); | ||
Matthias BUSSONNIER
|
r8074 | }); | ||
MinRK
|
r15737 | |||
Jessica B. Hamrick
|
r18238 | this.events.on('kernel_disconnected.Kernel', function () { | ||
Jessica B. Hamrick
|
r18207 | $kernel_ind_icon | ||
.attr('class', 'kernel_disconnected_icon') | ||||
.attr('title', 'No Connection to Kernel'); | ||||
Jessica B. Hamrick
|
r18202 | }); | ||
Jessica B. Hamrick
|
r18238 | this.events.on('kernel_connection_failed.Kernel', function (evt, info) { | ||
Jessica B. Hamrick
|
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."; | ||||
Jessica B. Hamrick
|
r18237 | dialog.kernel_modal({ | ||
Jessica B. Hamrick
|
r18236 | title: "Connection failed", | ||
body: msg, | ||||
keyboard_manager: that.keyboard_manager, | ||||
notebook: that.notebook, | ||||
buttons : { | ||||
"OK": {} | ||||
Jessica B. Hamrick
|
r18207 | } | ||
Jessica B. Hamrick
|
r18236 | }); | ||
} | ||||
MinRK
|
r15737 | }); | ||
Matthias BUSSONNIER
|
r8074 | |||
Jessica B. Hamrick
|
r18238 | this.events.on('kernel_killed.Kernel kernel_killed.Session', function () { | ||
Jessica B. Hamrick
|
r18207 | that.save_widget.update_document_title(); | ||
Min RK
|
r19965 | knw.warning("No kernel"); | ||
$kernel_ind_icon.attr('class','kernel_busy_icon').attr('title','Kernel is not running'); | ||||
Jessica B. Hamrick
|
r18207 | }); | ||
Jessica B. Hamrick
|
r18220 | this.events.on('kernel_dead.Kernel', function () { | ||
MinRK
|
r10895 | |||
Jessica B. Hamrick
|
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.'; | ||||
Jessica B. Hamrick
|
r18237 | dialog.kernel_modal({ | ||
Jessica B. Hamrick
|
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(); | ||||
} | ||||
}, | ||||
MinRK
|
r10895 | "Don't restart": {} | ||
Jessica B. Hamrick
|
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(); | ||||
Matthias BUSSONNIER
|
r8074 | }); | ||
Min RK
|
r20194 | |||
this.events.on("no_kernel.Kernel", function (evt, data) { | ||||
$("#kernel_indicator").find('.kernel_indicator_name').text("No Kernel"); | ||||
}); | ||||
Matthias BUSSONNIER
|
r8074 | |||
Jessica B. Hamrick
|
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; | ||||
Jessica B. Hamrick
|
r18060 | |||
Jessica B. Hamrick
|
r18066 | var showMsg = function () { | ||
Jessica B. Hamrick
|
r18108 | var msg = $('<div/>').append($('<p/>').text(full)); | ||
Jessica B. Hamrick
|
r18229 | var cm, cm_elem, cm_open; | ||
Jessica B. Hamrick
|
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); | ||||
Jessica B. Hamrick
|
r18229 | cm_open = $.proxy(cm.refresh, cm); | ||
Jessica B. Hamrick
|
r18108 | } | ||
Jessica B. Hamrick
|
r18237 | dialog.kernel_modal({ | ||
Jessica B. Hamrick
|
r18063 | title: "Failed to start the kernel", | ||
Jessica B. Hamrick
|
r18066 | body : msg, | ||
Jessica B. Hamrick
|
r18063 | keyboard_manager: that.keyboard_manager, | ||
notebook: that.notebook, | ||||
Jessica B. Hamrick
|
r18229 | open: cm_open, | ||
Jessica B. Hamrick
|
r18063 | buttons : { | ||
"Ok": { class: 'btn-primary' } | ||||
} | ||||
}); | ||||
Jessica B. Hamrick
|
r18108 | |||
Jessica B. Hamrick
|
r18063 | return false; | ||
Jessica B. Hamrick
|
r18066 | }; | ||
that.save_widget.update_document_title(); | ||||
$kernel_ind_icon.attr('class','kernel_dead_icon').attr('title','Kernel Dead'); | ||||
knw.danger(short, undefined, showMsg); | ||||
Jessica B. Hamrick
|
r18060 | }); | ||
Min RK
|
r19921 | this.events.on('kernel_starting.Kernel kernel_created.Session', function () { | ||
Jessica B. Hamrick
|
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..."); | ||||
}); | ||||
Jessica B. Hamrick
|
r18238 | this.events.on('kernel_ready.Kernel', function () { | ||
Jessica B. Hamrick
|
r18230 | that.save_widget.update_document_title(); | ||
$kernel_ind_icon.attr('class','kernel_idle_icon').attr('title','Kernel Idle'); | ||||
knw.info("Kernel ready", 500); | ||||
}); | ||||
Jessica B. Hamrick
|
r18238 | this.events.on('kernel_idle.Kernel', function () { | ||
Jessica B. Hamrick
|
r18207 | that.save_widget.update_document_title(); | ||
$kernel_ind_icon.attr('class','kernel_idle_icon').attr('title','Kernel Idle'); | ||||
}); | ||||
MinRK
|
r17676 | |||
Jessica B. Hamrick
|
r18238 | this.events.on('kernel_busy.Kernel', function () { | ||
Jessica B. Hamrick
|
r18207 | window.document.title='(Busy) '+window.document.title; | ||
$kernel_ind_icon.attr('class','kernel_busy_icon').attr('title','Kernel Busy'); | ||||
Brian E. Granger
|
r9222 | }); | ||
Min RK
|
r20199 | |||
this.events.on('spec_match_found.Kernel', function (evt, data) { | ||||
that.widget('kernelspec').info("Using kernel: " + data.found.spec.display_name, 3000, undefined, { | ||||
title: "Only candidate for language: " + data.selected.language + " was " + data.found.spec.display_name | ||||
}); | ||||
}); | ||||
Jessica B. Hamrick
|
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'); | ||||
Jessica B. Hamrick
|
r18004 | }; | ||
Brian E. Granger
|
r9222 | |||
Jessica B. Hamrick
|
r18004 | /** | ||
* Initialize the notification widget for notebook status messages. | ||||
* | ||||
* @method init_notebook_notification_widget | ||||
*/ | ||||
Thomas Kluyver
|
r19017 | NotebookNotificationArea.prototype.init_notebook_notification_widget = function () { | ||
Matthias BUSSONNIER
|
r8074 | var nnw = this.new_notification_widget('notebook'); | ||
// Notebook events | ||||
Jonathan Frederic
|
r17198 | this.events.on('notebook_loading.Notebook', function () { | ||
Matthias BUSSONNIER
|
r8074 | nnw.set_message("Loading notebook",500); | ||
}); | ||||
Jonathan Frederic
|
r17198 | this.events.on('notebook_loaded.Notebook', function () { | ||
Matthias BUSSONNIER
|
r8074 | nnw.set_message("Notebook loaded",500); | ||
}); | ||||
Jonathan Frederic
|
r17198 | this.events.on('notebook_saving.Notebook', function () { | ||
Matthias BUSSONNIER
|
r8074 | nnw.set_message("Saving notebook",500); | ||
}); | ||||
Jonathan Frederic
|
r17198 | this.events.on('notebook_saved.Notebook', function () { | ||
Matthias BUSSONNIER
|
r8074 | nnw.set_message("Notebook saved",2000); | ||
}); | ||||
Min RK
|
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"); | ||||
Matthias BUSSONNIER
|
r8074 | }); | ||
MinRK
|
r10502 | |||
// Checkpoint events | ||||
Jonathan Frederic
|
r17198 | this.events.on('checkpoint_created.Notebook', function (evt, data) { | ||
MinRK
|
r10502 | var msg = "Checkpoint created"; | ||
if (data.last_modified) { | ||||
var d = new Date(data.last_modified); | ||||
Matthias BUSSONNIER
|
r17474 | msg = msg + ": " + moment(d).format("HH:mm:ss"); | ||
MinRK
|
r10502 | } | ||
nnw.set_message(msg, 2000); | ||||
}); | ||||
Jonathan Frederic
|
r17198 | this.events.on('checkpoint_failed.Notebook', function () { | ||
Matthias BUSSONNIER
|
r17368 | nnw.warning("Checkpoint failed"); | ||
MinRK
|
r10502 | }); | ||
Jonathan Frederic
|
r17198 | this.events.on('checkpoint_deleted.Notebook', function () { | ||
MinRK
|
r10502 | nnw.set_message("Checkpoint deleted", 500); | ||
}); | ||||
Jonathan Frederic
|
r17198 | this.events.on('checkpoint_delete_failed.Notebook', function () { | ||
Matthias BUSSONNIER
|
r17368 | nnw.warning("Checkpoint delete failed"); | ||
MinRK
|
r10502 | }); | ||
Jonathan Frederic
|
r17198 | this.events.on('checkpoint_restoring.Notebook', function () { | ||
MinRK
|
r10502 | nnw.set_message("Restoring to checkpoint...", 500); | ||
}); | ||||
Jonathan Frederic
|
r17198 | this.events.on('checkpoint_restore_failed.Notebook', function () { | ||
Matthias BUSSONNIER
|
r17368 | nnw.warning("Checkpoint restore failed"); | ||
MinRK
|
r10502 | }); | ||
Matthias BUSSONNIER
|
r8074 | |||
MinRK
|
r10505 | // Autosave events | ||
Jonathan Frederic
|
r17198 | this.events.on('autosave_disabled.Notebook', function () { | ||
MinRK
|
r10505 | nnw.set_message("Autosave disabled", 2000); | ||
}); | ||||
Jonathan Frederic
|
r17198 | this.events.on('autosave_enabled.Notebook', function (evt, interval) { | ||
MinRK
|
r10505 | nnw.set_message("Saving every " + interval / 1000 + "s", 1000); | ||
}); | ||||
Matthias BUSSONNIER
|
r8204 | }; | ||
Matthias BUSSONNIER
|
r8048 | |||
Thomas Kluyver
|
r19017 | // Backwards compatibility. | ||
IPython.NotificationArea = NotebookNotificationArea; | ||||
return {'NotebookNotificationArea': NotebookNotificationArea}; | ||||
Jonathan Frederic
|
r17198 | }); | ||