notificationarea.js
210 lines
| 7.8 KiB
| application/javascript
|
JavascriptLexer
|
r8011 | //---------------------------------------------------------------------------- | ||
// Copyright (C) 2012 The IPython Development Team | ||||
// | ||||
// Distributed under the terms of the BSD License. The full license is in | ||||
// the file COPYING, distributed as part of this software. | ||||
//---------------------------------------------------------------------------- | ||||
//============================================================================ | ||||
// Notification widget | ||||
//============================================================================ | ||||
var IPython = (function (IPython) { | ||||
"use strict"; | ||||
var utils = IPython.utils; | ||||
var NotificationArea = function (selector) { | ||||
this.selector = selector; | ||||
if (this.selector !== undefined) { | ||||
this.element = $(selector); | ||||
} | ||||
|
r8074 | this.widget_dict = {}; | ||
|
r8011 | }; | ||
NotificationArea.prototype.temp_message = function (msg, timeout, css_class) { | ||||
var uuid = utils.uuid(); | ||||
|
r8204 | if( css_class == 'danger') {css_class = 'ui-state-error';} | ||
if( css_class == 'warning') {css_class = 'ui-state-highlight';} | ||||
|
r8011 | var tdiv = $('<div>') | ||
.attr('id',uuid) | ||||
|
r8074 | .addClass('notification_widget ui-widget ui-widget-content ui-corner-all') | ||
|
r8011 | .addClass('border-box-sizing') | ||
.addClass(css_class) | ||||
.hide() | ||||
.text(msg); | ||||
$(this.selector).append(tdiv); | ||||
|
r8012 | var tmout = Math.max(1500,(timeout||1500)); | ||
|
r8011 | tdiv.fadeIn(100); | ||
setTimeout(function () { | ||||
tdiv.fadeOut(100, function () {tdiv.remove();}); | ||||
|
r8204 | }, tmout); | ||
|
r8011 | }; | ||
|
r8204 | NotificationArea.prototype.widget = function(name) { | ||
if(this.widget_dict[name] == undefined) { | ||||
return this.new_notification_widget(name); | ||||
|
r8074 | } | ||
|
r8839 | return this.get_widget(name); | ||
|
r8204 | }; | ||
|
r8074 | |||
NotificationArea.prototype.get_widget = function(name) { | ||||
|
r8204 | if(this.widget_dict[name] == undefined) { | ||
|
r8074 | throw('no widgets with this name'); | ||
} | ||||
return this.widget_dict[name]; | ||||
|
r8204 | }; | ||
|
r8074 | |||
|
r8048 | NotificationArea.prototype.new_notification_widget = function(name) { | ||
|
r8204 | if(this.widget_dict[name] != undefined) { | ||
|
r8074 | throw('widget with that name already exists ! '); | ||
} | ||||
|
r8048 | var div = $('<div/>').attr('id','notification_'+name); | ||
|
r8204 | $(this.selector).append(div); | ||
this.widget_dict[name] = new IPython.NotificationWidget('#notification_'+name); | ||||
|
r8074 | return this.widget_dict[name]; | ||
|
r8204 | }; | ||
|
r8074 | |||
NotificationArea.prototype.init_notification_widgets = function() { | ||||
var knw = this.new_notification_widget('kernel'); | ||||
// Kernel events | ||||
$([IPython.events]).on('status_idle.Kernel',function () { | ||||
IPython.save_widget.update_document_title(); | ||||
knw.set_message('Kernel Idle',200); | ||||
} | ||||
); | ||||
$([IPython.events]).on('status_busy.Kernel',function () { | ||||
window.document.title='(Busy) '+window.document.title; | ||||
knw.set_message("Kernel busy"); | ||||
}); | ||||
$([IPython.events]).on('status_restarting.Kernel',function () { | ||||
IPython.save_widget.update_document_title(); | ||||
|
r10316 | knw.set_message("Restarting kernel", 2000); | ||
|
r8074 | }); | ||
$([IPython.events]).on('status_interrupting.Kernel',function () { | ||||
|
r14816 | knw.set_message("Interrupting kernel", 2000); | ||
|
r8074 | }); | ||
$([IPython.events]).on('status_dead.Kernel',function () { | ||||
|
r10895 | var msg = 'The kernel has died, and the automatic restart has failed.' + | ||
|
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' + | ||||
|
r10895 | ' is reopened.'; | ||
IPython.dialog.modal({ | ||||
|
r8074 | title: "Dead kernel", | ||
|
r10895 | body : msg, | ||
|
r8074 | buttons : { | ||
|
r10895 | "Manual Restart": { | ||
class: "btn-danger", | ||||
click: function () { | ||||
$([IPython.events]).trigger('status_restarting.Kernel'); | ||||
IPython.notebook.start_kernel(); | ||||
} | ||||
|
r8074 | }, | ||
|
r10895 | "Don't restart": {} | ||
|
r8074 | } | ||
}); | ||||
}); | ||||
|
r9222 | $([IPython.events]).on('websocket_closed.Kernel', function (event, data) { | ||
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; | ||||
} | ||||
console.log('WebSocket connection failed: ', ws_url) | ||||
msg = "A WebSocket connection to could not be established." + | ||||
" You will NOT be able to run code. Check your" + | ||||
" network connection or notebook server configuration."; | ||||
|
r10895 | IPython.dialog.modal({ | ||
|
r9222 | title: "WebSocket connection failed", | ||
|
r10895 | body: msg, | ||
|
r9222 | buttons : { | ||
|
r10895 | "OK": {}, | ||
"Reconnect": { | ||||
click: function () { | ||||
knw.set_message('Reconnecting WebSockets', 1000); | ||||
setTimeout(function () { | ||||
kernel.start_channels(); | ||||
}, 5000); | ||||
} | ||||
|
r9222 | } | ||
} | ||||
}); | ||||
}); | ||||
|
r8074 | var nnw = this.new_notification_widget('notebook'); | ||
// Notebook events | ||||
$([IPython.events]).on('notebook_loading.Notebook', function () { | ||||
nnw.set_message("Loading notebook",500); | ||||
}); | ||||
$([IPython.events]).on('notebook_loaded.Notebook', function () { | ||||
nnw.set_message("Notebook loaded",500); | ||||
}); | ||||
$([IPython.events]).on('notebook_saving.Notebook', function () { | ||||
nnw.set_message("Saving notebook",500); | ||||
}); | ||||
$([IPython.events]).on('notebook_saved.Notebook', function () { | ||||
nnw.set_message("Notebook saved",2000); | ||||
}); | ||||
$([IPython.events]).on('notebook_save_failed.Notebook', function () { | ||||
nnw.set_message("Notebook save failed"); | ||||
}); | ||||
|
r10502 | |||
// Checkpoint events | ||||
$([IPython.events]).on('checkpoint_created.Notebook', function (evt, data) { | ||||
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); | ||||
}); | ||||
$([IPython.events]).on('checkpoint_failed.Notebook', function () { | ||||
nnw.set_message("Checkpoint failed"); | ||||
}); | ||||
$([IPython.events]).on('checkpoint_deleted.Notebook', function () { | ||||
nnw.set_message("Checkpoint deleted", 500); | ||||
}); | ||||
$([IPython.events]).on('checkpoint_delete_failed.Notebook', function () { | ||||
nnw.set_message("Checkpoint delete failed"); | ||||
}); | ||||
$([IPython.events]).on('checkpoint_restoring.Notebook', function () { | ||||
nnw.set_message("Restoring to checkpoint...", 500); | ||||
}); | ||||
$([IPython.events]).on('checkpoint_restore_failed.Notebook', function () { | ||||
nnw.set_message("Checkpoint restore failed"); | ||||
}); | ||||
|
r8074 | |||
|
r10505 | // Autosave events | ||
$([IPython.events]).on('autosave_disabled.Notebook', function () { | ||||
nnw.set_message("Autosave disabled", 2000); | ||||
}); | ||||
$([IPython.events]).on('autosave_enabled.Notebook', function (evt, interval) { | ||||
nnw.set_message("Saving every " + interval / 1000 + "s", 1000); | ||||
}); | ||||
|
r8204 | }; | ||
|
r8048 | |||
|
r8011 | IPython.NotificationArea = NotificationArea; | ||
return IPython; | ||||
}(IPython)); | ||||