notificationarea.js
247 lines
| 9.4 KiB
| application/javascript
|
JavascriptLexer
Jonathan Frederic
|
r17198 | // Copyright (c) IPython Development Team. | ||
// Distributed under the terms of the Modified BSD License. | ||||
define([ | ||||
'base/js/namespace', | ||||
Jonathan Frederic
|
r17200 | 'jquery', | ||
Jonathan Frederic
|
r17198 | 'base/js/utils', | ||
'base/js/dialog', | ||||
Jonathan Frederic
|
r17200 | 'notebook/js/notificationwidget', | ||
Jonathan Frederic
|
r17202 | ], function(IPython, $, utils, dialog, notificationwidget) { | ||
Matthias BUSSONNIER
|
r8011 | "use strict"; | ||
jon
|
r17210 | var NotificationArea = function (selector, options) { | ||
jon
|
r17211 | // Constructor | ||
// | ||||
// Parameters: | ||||
// selector: string | ||||
// options: dictionary | ||||
// Dictionary of keyword arguments. | ||||
// notebook: Notebook instance | ||||
// events: $(Events) instance | ||||
// save_widget: SaveWidget instance | ||||
Matthias BUSSONNIER
|
r8011 | this.selector = selector; | ||
jon
|
r17210 | this.events = options.events; | ||
this.save_widget = options.save_widget; | ||||
this.notebook = options.notebook; | ||||
Jonathan Frederic
|
r17212 | this.keyboard_manager = options.keyboard_manager; | ||
Matthias BUSSONNIER
|
r8011 | if (this.selector !== undefined) { | ||
this.element = $(selector); | ||||
} | ||||
Matthias BUSSONNIER
|
r8074 | this.widget_dict = {}; | ||
Matthias BUSSONNIER
|
r8011 | }; | ||
NotificationArea.prototype.temp_message = function (msg, timeout, css_class) { | ||||
var uuid = utils.uuid(); | ||||
Matthias BUSSONNIER
|
r8204 | if( css_class == 'danger') {css_class = 'ui-state-error';} | ||
if( css_class == 'warning') {css_class = 'ui-state-highlight';} | ||||
Matthias BUSSONNIER
|
r8011 | var tdiv = $('<div>') | ||
.attr('id',uuid) | ||||
Matthias BUSSONNIER
|
r8074 | .addClass('notification_widget ui-widget ui-widget-content ui-corner-all') | ||
Matthias BUSSONNIER
|
r8011 | .addClass('border-box-sizing') | ||
.addClass(css_class) | ||||
.hide() | ||||
.text(msg); | ||||
$(this.selector).append(tdiv); | ||||
Matthias BUSSONNIER
|
r8012 | var tmout = Math.max(1500,(timeout||1500)); | ||
Matthias BUSSONNIER
|
r8011 | tdiv.fadeIn(100); | ||
setTimeout(function () { | ||||
tdiv.fadeOut(100, function () {tdiv.remove();}); | ||||
Matthias BUSSONNIER
|
r8204 | }, tmout); | ||
Matthias BUSSONNIER
|
r8011 | }; | ||
Matthias BUSSONNIER
|
r8204 | NotificationArea.prototype.widget = function(name) { | ||
Paul Ivanov
|
r15847 | if(this.widget_dict[name] === undefined) { | ||
Matthias BUSSONNIER
|
r8204 | return this.new_notification_widget(name); | ||
Matthias BUSSONNIER
|
r8074 | } | ||
Mikhail Korobov
|
r8839 | return this.get_widget(name); | ||
Matthias BUSSONNIER
|
r8204 | }; | ||
Matthias BUSSONNIER
|
r8074 | |||
NotificationArea.prototype.get_widget = function(name) { | ||||
Paul Ivanov
|
r15847 | if(this.widget_dict[name] === undefined) { | ||
Matthias BUSSONNIER
|
r8074 | throw('no widgets with this name'); | ||
} | ||||
return this.widget_dict[name]; | ||||
Matthias BUSSONNIER
|
r8204 | }; | ||
Matthias BUSSONNIER
|
r8074 | |||
Matthias BUSSONNIER
|
r8048 | NotificationArea.prototype.new_notification_widget = function(name) { | ||
Paul Ivanov
|
r15847 | if(this.widget_dict[name] !== undefined) { | ||
Matthias BUSSONNIER
|
r8074 | throw('widget with that name already exists ! '); | ||
} | ||||
Matthias BUSSONNIER
|
r8048 | var div = $('<div/>').attr('id','notification_'+name); | ||
Matthias BUSSONNIER
|
r8204 | $(this.selector).append(div); | ||
Jonathan Frederic
|
r17202 | this.widget_dict[name] = new notificationwidget.NotificationWidget('#notification_'+name); | ||
Matthias BUSSONNIER
|
r8074 | return this.widget_dict[name]; | ||
Matthias BUSSONNIER
|
r8204 | }; | ||
Matthias BUSSONNIER
|
r8074 | |||
NotificationArea.prototype.init_notification_widgets = 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"); | ||
var $modal_ind_icon = $("#modal_indicator_icon"); | ||||
// Command/Edit mode | ||||
Jonathan Frederic
|
r17198 | this.events.on('edit_mode.Notebook',function () { | ||
that.save_widget.update_document_title(); | ||||
Paul Ivanov
|
r15847 | $modal_ind_icon.attr('class','edit_mode_icon').attr('title','Edit Mode'); | ||
Brian E. Granger
|
r15115 | }); | ||
Jonathan Frederic
|
r17198 | this.events.on('command_mode.Notebook',function () { | ||
that.save_widget.update_document_title(); | ||||
Paul Ivanov
|
r15847 | $modal_ind_icon.attr('class','command_mode_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 | ||
Paul Ivanov
|
r15847 | $modal_ind_icon.attr('class','command-mode_icon').attr('title','Command Mode'); | ||
Paul Ivanov
|
r15809 | |||
Matthias BUSSONNIER
|
r8074 | // Kernel events | ||
Jonathan Frederic
|
r17198 | this.events.on('status_idle.Kernel',function () { | ||
that.save_widget.update_document_title(); | ||||
Paul Ivanov
|
r15847 | $kernel_ind_icon.attr('class','kernel_idle_icon').attr('title','Kernel Idle'); | ||
Matthias BUSSONNIER
|
r15042 | }); | ||
Matthias BUSSONNIER
|
r8074 | |||
Jonathan Frederic
|
r17198 | this.events.on('status_busy.Kernel',function () { | ||
Matthias BUSSONNIER
|
r8074 | window.document.title='(Busy) '+window.document.title; | ||
Paul Ivanov
|
r15847 | $kernel_ind_icon.attr('class','kernel_busy_icon').attr('title','Kernel Busy'); | ||
Matthias BUSSONNIER
|
r8074 | }); | ||
Jonathan Frederic
|
r17198 | this.events.on('status_restarting.Kernel',function () { | ||
that.save_widget.update_document_title(); | ||||
MinRK
|
r10316 | knw.set_message("Restarting kernel", 2000); | ||
Matthias BUSSONNIER
|
r8074 | }); | ||
Jonathan Frederic
|
r17198 | this.events.on('status_interrupting.Kernel',function () { | ||
Brian E. Granger
|
r14816 | knw.set_message("Interrupting kernel", 2000); | ||
Matthias BUSSONNIER
|
r8074 | }); | ||
MinRK
|
r15737 | |||
// Start the kernel indicator in the busy state, and send a kernel_info request. | ||||
// When the kernel_info reply arrives, the kernel is idle. | ||||
Paul Ivanov
|
r15847 | $kernel_ind_icon.attr('class','kernel_busy_icon').attr('title','Kernel Busy'); | ||
MinRK
|
r15737 | |||
Jonathan Frederic
|
r17198 | this.events.on('status_started.Kernel', function (evt, data) { | ||
MinRK
|
r15737 | data.kernel.kernel_info(function () { | ||
Jonathan Frederic
|
r17198 | that.events.trigger('status_idle.Kernel'); | ||
MinRK
|
r15737 | }); | ||
}); | ||||
Matthias BUSSONNIER
|
r8074 | |||
Jonathan Frederic
|
r17198 | this.events.on('status_dead.Kernel',function () { | ||
MinRK
|
r10895 | var msg = 'The kernel has died, and the automatic restart has failed.' + | ||
MinRK
|
r10316 | ' 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' + | ||||
MinRK
|
r10895 | ' is reopened.'; | ||
Jonathan Frederic
|
r17202 | dialog.modal({ | ||
Matthias BUSSONNIER
|
r8074 | title: "Dead kernel", | ||
MinRK
|
r10895 | body : msg, | ||
Jonathan Frederic
|
r17212 | keyboard_manager: that.keyboard_manager, | ||
notebook: that.notebook, | ||||
Matthias BUSSONNIER
|
r8074 | buttons : { | ||
MinRK
|
r10895 | "Manual Restart": { | ||
class: "btn-danger", | ||||
click: function () { | ||||
Jonathan Frederic
|
r17198 | that.events.trigger('status_restarting.Kernel'); | ||
that.notebook.start_kernel(); | ||||
MinRK
|
r10895 | } | ||
Matthias BUSSONNIER
|
r8074 | }, | ||
MinRK
|
r10895 | "Don't restart": {} | ||
Matthias BUSSONNIER
|
r8074 | } | ||
}); | ||||
}); | ||||
Jonathan Frederic
|
r17198 | this.events.on('websocket_closed.Kernel', function (event, data) { | ||
Brian E. Granger
|
r9222 | var kernel = data.kernel; | ||
var ws_url = data.ws_url; | ||||
var early = data.early; | ||||
var msg; | ||||
if (!early) { | ||||
knw.set_message('Reconnecting WebSockets', 1000); | ||||
setTimeout(function () { | ||||
kernel.start_channels(); | ||||
}, 5000); | ||||
return; | ||||
} | ||||
Paul Ivanov
|
r15847 | console.log('WebSocket connection failed: ', ws_url); | ||
Kieran O'Mahony
|
r14955 | msg = "A WebSocket connection could not be established." + | ||
Brian E. Granger
|
r9222 | " You will NOT be able to run code. Check your" + | ||
" network connection or notebook server configuration."; | ||||
Jonathan Frederic
|
r17202 | dialog.modal({ | ||
Brian E. Granger
|
r9222 | title: "WebSocket connection failed", | ||
MinRK
|
r10895 | body: msg, | ||
Jonathan Frederic
|
r17212 | keyboard_manager: that.keyboard_manager, | ||
notebook: that.notebook, | ||||
Brian E. Granger
|
r9222 | buttons : { | ||
MinRK
|
r10895 | "OK": {}, | ||
"Reconnect": { | ||||
click: function () { | ||||
knw.set_message('Reconnecting WebSockets', 1000); | ||||
setTimeout(function () { | ||||
kernel.start_channels(); | ||||
}, 5000); | ||||
} | ||||
Brian E. Granger
|
r9222 | } | ||
} | ||||
}); | ||||
}); | ||||
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); | ||
}); | ||||
Jonathan Frederic
|
r17198 | this.events.on('notebook_save_failed.Notebook', function (evt, xhr, status, data) { | ||
Matthias BUSSONNIER
|
r17368 | nnw.warning(data || "Notebook save 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); | ||||
msg = msg + ": " + d.format("HH:MM:ss"); | ||||
} | ||||
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 | |||
Matthias BUSSONNIER
|
r8011 | IPython.NotificationArea = NotificationArea; | ||
Jonathan Frederic
|
r17201 | return {'NotificationArea': NotificationArea}; | ||
Jonathan Frederic
|
r17198 | }); | ||