Merge pull request #2192 from Carreau/notification

Introduce Notification Area

This allow several notification widget to be next one to the other in the notification area.
The Api allow also to register a callback if the user click on the notification. and allows sticky and non sticky notification.
This commit is contained in:
Bussonnier Matthias 2012-08-25 06:37:17 -07:00
commit 45f79e0509
6 changed files with 190 additions and 77 deletions

View File

@ -51,10 +51,20 @@ span#notebook_name {
position: relative;
}
#notification {
#notification_area {
position: absolute;
right: 3px;
top: 3px;
right: 0px;
top: 0px;
height: 25px;
padding: 3px 0px;
padding-right: 3px;
z-index: 10;
}
.notification_widget{
float : right;
right: 0px;
top: 1px;
height: 25px;
padding: 3px 6px;
z-index: 10;
@ -391,4 +401,4 @@ pre, code, kbd, samp { white-space: pre-wrap; }
.js-error {
color: darkred;
}
}

View File

@ -99,7 +99,7 @@ var IPython = (function (IPython) {
" or if the url does not look right, there could be an error in the" +
" server's configuration.";
} else {
IPython.notification_widget.set_message('Reconnecting Websockets', 1000);
IPython.notification_area.widget('kernel').set_message('Reconnecting Websockets', 1000);
this.start_channels();
return;
}

View File

@ -31,7 +31,8 @@ $(document).ready(function () {
IPython.menubar = new IPython.MenuBar('#menubar')
IPython.toolbar = new IPython.ToolBar('#toolbar')
IPython.tooltip = new IPython.Tooltip()
IPython.notification_widget = new IPython.NotificationWidget('#notification')
IPython.notification_area = new IPython.NotificationArea('#notification_area')
IPython.notification_area.init_notification_widgets();
IPython.layout_manager.do_resize();

View File

@ -0,0 +1,141 @@
//----------------------------------------------------------------------------
// 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);
}
this.widget_dict = {};
};
NotificationArea.prototype.temp_message = function (msg, timeout, css_class) {
var uuid = utils.uuid();
if( css_class == 'danger') {css_class = 'ui-state-error';}
if( css_class == 'warning') {css_class = 'ui-state-highlight';}
var tdiv = $('<div>')
.attr('id',uuid)
.addClass('notification_widget ui-widget ui-widget-content ui-corner-all')
.addClass('border-box-sizing')
.addClass(css_class)
.hide()
.text(msg);
$(this.selector).append(tdiv);
var tmout = Math.max(1500,(timeout||1500));
tdiv.fadeIn(100);
setTimeout(function () {
tdiv.fadeOut(100, function () {tdiv.remove();});
}, tmout);
};
NotificationArea.prototype.widget = function(name) {
if(this.widget_dict[name] == undefined) {
return this.new_notification_widget(name);
}
return this.get_widget(name);
};
NotificationArea.prototype.get_widget = function(name) {
if(this.widget_dict[name] == undefined) {
throw('no widgets with this name');
}
return this.widget_dict[name];
};
NotificationArea.prototype.new_notification_widget = function(name) {
if(this.widget_dict[name] != undefined) {
throw('widget with that name already exists ! ');
}
var div = $('<div/>').attr('id','notification_'+name);
$(this.selector).append(div);
this.widget_dict[name] = new IPython.NotificationWidget('#notification_'+name);
return this.widget_dict[name];
};
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();
knw.set_message("Restarting kernel",1000);
});
$([IPython.events]).on('status_interrupting.Kernel',function () {
knw.set_message("Interrupting kernel");
});
$([IPython.events]).on('status_dead.Kernel',function () {
var dialog = $('<div/>');
dialog.html('The kernel has died, would you like to restart it? If you do not restart the kernel, you will be able to save the notebook, but running code will not work until the notebook is reopened.');
$(document).append(dialog);
dialog.dialog({
resizable: false,
modal: true,
title: "Dead kernel",
buttons : {
"Restart": function () {
$([IPython.events]).trigger('status_restarting.Kernel');
IPython.notebook.start_kernel();
$(this).dialog('close');
},
"Continue running": function () {
$(this).dialog('close');
}
}
});
});
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");
});
};
IPython.NotificationArea = NotificationArea;
return IPython;
}(IPython));

View File

@ -10,7 +10,7 @@
//============================================================================
var IPython = (function (IPython) {
"use strict";
var utils = IPython.utils;
@ -21,92 +21,51 @@ var IPython = (function (IPython) {
if (this.selector !== undefined) {
this.element = $(selector);
this.style();
this.bind_events();
}
this.element.button();
this.element.hide();
var that = this;
};
NotificationWidget.prototype.style = function () {
this.element.addClass('ui-widget ui-widget-content ui-corner-all');
this.element.addClass('notification_widget ui-widget ui-widget-content ui-corner-all');
this.element.addClass('border-box-sizing');
};
NotificationWidget.prototype.bind_events = function () {
var that = this;
// Kernel events
$([IPython.events]).on('status_idle.Kernel',function () {
IPython.save_widget.update_document_title();
if (that.get_message() === 'Kernel busy') {
that.element.fadeOut(100, function () {
that.element.html('');
});
};
});
$([IPython.events]).on('status_busy.Kernel',function () {
window.document.title='(Busy) '+window.document.title;
that.set_message("Kernel busy");
});
$([IPython.events]).on('status_restarting.Kernel',function () {
IPython.save_widget.update_document_title();
that.set_message("Restarting kernel",500);
});
$([IPython.events]).on('status_interrupting.Kernel',function () {
that.set_message("Interrupting kernel",500);
});
$([IPython.events]).on('status_dead.Kernel',function () {
var dialog = $('<div/>');
dialog.html('The kernel has died, would you like to restart it? If you do not restart the kernel, you will be able to save the notebook, but running code will not work until the notebook is reopened.');
$(document).append(dialog);
dialog.dialog({
resizable: false,
modal: true,
title: "Dead kernel",
buttons : {
"Restart": function () {
$([IPython.events]).trigger('status_restarting.Kernel');
IPython.notebook.start_kernel();
$(this).dialog('close');
},
"Continue running": function () {
$(this).dialog('close');
}
}
});
});
// Notebook events
$([IPython.events]).on('notebook_loading.Notebook', function () {
that.set_message("Loading notebook",500);
});
$([IPython.events]).on('notebook_loaded.Notebook', function () {
that.set_message("Notebook loaded",500);
});
$([IPython.events]).on('notebook_saving.Notebook', function () {
that.set_message("Saving notebook",500);
});
$([IPython.events]).on('notebook_saved.Notebook', function () {
that.set_message("Notebook saved",2000);
});
$([IPython.events]).on('notebook_save_failed.Notebook', function () {
that.set_message("Notebook save failed",2000);
});
};
NotificationWidget.prototype.set_message = function (msg, timeout) {
// msg : message to display
// timeout : time in ms before diseapearing
//
// if timeout <= 0
// click_callback : function called if user click on notification
// could return false to prevent the notification to be dismissed
NotificationWidget.prototype.set_message = function (msg, timeout, click_callback) {
var callback = click_callback || function() {return false;};
var that = this;
this.element.html(msg);
this.element.fadeIn(100);
if (this.timeout !== null) {
clearTimeout(this.timeout);
this.timeout = null;
};
if (timeout !== undefined) {
}
if (timeout !== undefined && timeout >=0) {
this.timeout = setTimeout(function () {
that.element.fadeOut(100, function () {that.element.html('');});
that.timeout = null;
}, timeout)
};
}, timeout);
} else {
this.element.click(function() {
if( callback() != false ) {
that.element.fadeOut(100, function () {that.element.html('');});
that.element.unbind('click');
}
if (that.timeout !== undefined) {
that.timeout = undefined;
clearTimeout(that.timeout);
}
});
}
};

View File

@ -152,7 +152,8 @@ data-notebook-id={{notebook_id}}
</ul>
</div>
<div id="notification"></div>
<div id="notification_area">
</div>
</div>
@ -242,6 +243,7 @@ data-notebook-id={{notebook_id}}
<script src="{{ static_url("js/toolbar.js") }}" type="text/javascript" charset="utf-8"></script>
<script src="{{ static_url("js/notebook.js") }}" type="text/javascript" charset="utf-8"></script>
<script src="{{ static_url("js/notificationwidget.js") }}" type="text/javascript" charset="utf-8"></script>
<script src="{{ static_url("js/notificationarea.js") }}" type="text/javascript" charset="utf-8"></script>
<script src="{{ static_url("js/tooltip.js") }}" type="text/javascript" charset="utf-8"></script>
<script src="{{ static_url("js/notebookmain.js") }}" type="text/javascript" charset="utf-8"></script>