Merge pull request #2828 from ellisonbg/jscleanup

Clean up notebook Javascript
This commit is contained in:
Brian E. Granger 2013-01-23 10:14:18 -08:00
commit 0abfc848ef
9 changed files with 109 additions and 70 deletions

View File

@ -156,6 +156,7 @@ div.cell {
padding: 5px 5px 5px 0px;
/* This acts as a spacer between cells, that is outside the border */
margin: 2px 0px 2px 0px;
outline: none;
}
div.code_cell {
@ -173,7 +174,9 @@ div.prompt {
padding: 0.4em;
margin: 0px;
font-family: monospace;
text-align:right;
text-align: right;
/* This has to match that of the the CodeMirror class line-height below */
line-height: 1.231;
}
div.input {
@ -245,6 +248,21 @@ div.output_area {
page-break-inside: avoid;
}
/* This is needed to protect the pre formating from global settings such
as that of bootstrap */
div.output_area pre {
font-family: monospace;
margin: 0;
padding: 0;
border: 0;
font-size: 100%;
font: inherit;
vertical-align: baseline;
color: black;
background-color: white;
}
/* This class is for the output subarea inside the output_area and after
the prompt div. */
div.output_subarea {
@ -259,6 +277,8 @@ div.output_text {
text-align: left;
color: black;
font-family: monospace;
/* This has to match that of the the CodeMirror class line-height below */
line-height: 1.231;
}
/* stdout/stderr are 'text' as well as 'stream', but pyout/pyerr are *not* streams */

View File

@ -202,7 +202,6 @@ var IPython = (function (IPython) {
for (var k in CellToolbar._presets) {
keys.push(k);
}
console.log(keys);
return keys;
};

View File

@ -149,6 +149,6 @@
example_preset.push('example.help')
CellToolbar.register_preset('Example',example_preset);
console.log('Example extension for metadata editting loaded.');
console.log('Example extension for metadata editing loaded.');
}(IPython));

View File

@ -67,18 +67,16 @@ var IPython = (function (IPython) {
var Completer = function (cell) {
this.editor = cell.code_mirror;
var that = this;
$([IPython.events]).on('status_busy.Kernel', function () {
that.skip_kernel_completion = true;
});
$([IPython.events]).on('status_idle.Kernel', function () {
that.skip_kernel_completion = false;
});
};
this.cell = cell;
this.editor = cell.code_mirror;
var that = this;
$([IPython.events]).on('status_busy.Kernel', function () {
that.skip_kernel_completion = true;
});
$([IPython.events]).on('status_idle.Kernel', function () {
that.skip_kernel_completion = false;
});
};
Completer.prototype.startCompletion = function () {
@ -128,7 +126,7 @@ var IPython = (function (IPython) {
var callbacks = {
'complete_reply': $.proxy(this.finish_completing, this)
};
IPython.notebook.kernel.complete(line, cur.ch, callbacks);
this.cell.kernel.complete(line, cur.ch, callbacks);
}
};

View File

@ -110,36 +110,11 @@ var IPython = (function (IPython) {
};
Kernel.prototype._websocket_closed = function(ws_url, early){
var msg;
var parent_item = $('body');
if (early) {
msg = "Websocket connection to " + ws_url + " could not be established." +
" You will NOT be able to run code." +
" Your browser may not be compatible with the websocket version in the server," +
" or if the url does not look right, there could be an error in the" +
" server's configuration.";
} else {
IPython.notification_area.widget('kernel').set_message('Reconnecting Websockets', 1000);
this.start_channels();
return;
}
var dialog = $('<div/>');
dialog.html(msg);
parent_item.append(dialog);
dialog.dialog({
resizable: false,
modal: true,
title: "Websocket closed",
closeText: "",
close: function(event, ui) {$(this).dialog('destroy').remove();},
buttons : {
"OK": function () {
$(this).dialog('close');
}
}
});
Kernel.prototype._websocket_closed = function(ws_url, early) {
this.stop_channels();
$([IPython.events]).trigger('websocket_closed.Kernel',
{ws_url: ws_url, kernel: this, early: early}
);
};
/**
@ -152,7 +127,7 @@ var IPython = (function (IPython) {
var that = this;
this.stop_channels();
var ws_url = this.ws_url + this.kernel_url;
console.log("Starting WS:", ws_url);
console.log("Starting WebSockets:", ws_url);
this.shell_channel = new this.WebSocket(ws_url + "/shell");
this.iopub_channel = new this.WebSocket(ws_url + "/iopub");
send_cookie = function(){
@ -182,9 +157,13 @@ var IPython = (function (IPython) {
this.iopub_channel.onopen = send_cookie;
this.iopub_channel.onclose = ws_closed_early;
// switch from early-close to late-close message after 1s
setTimeout(function(){
that.shell_channel.onclose = ws_closed_late;
that.iopub_channel.onclose = ws_closed_late;
setTimeout(function() {
if (that.shell_channel !== null) {
that.shell_channel.onclose = ws_closed_late;
}
if (that.iopub_channel !== null) {
that.iopub_channel.onclose = ws_closed_late;
}
}, 1000);
};

View File

@ -93,25 +93,71 @@ var IPython = (function (IPython) {
$([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.');
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",
close: function(event, ui) {$(this).dialog('destroy').remove();},
buttons : {
"Restart": function () {
$([IPython.events]).trigger('status_restarting.Kernel');
IPython.notebook.start_kernel();
$(this).dialog('close');
},
"Continue running": function () {
"Don't restart": function () {
$(this).dialog('close');
}
}
});
});
$([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.";
var dialog = $('<div/>');
dialog.html(msg);
$(document).append(dialog);
dialog.dialog({
resizable: false,
modal: true,
title: "WebSocket connection failed",
closeText: "",
close: function(event, ui) {$(this).dialog('destroy').remove();},
buttons : {
"OK": function () {
$(this).dialog('close');
},
"Reconnect": function () {
knw.set_message('Reconnecting WebSockets', 1000);
setTimeout(function () {
kernel.start_channels();
}, 5000);
$(this).dialog('close');
}
}
});
});
var nnw = this.new_notification_widget('notebook');
// Notebook events

View File

@ -79,7 +79,7 @@ var IPython = (function (IPython) {
this.element.resize(function () {
// FIXME: Firefox on Linux misbehaves, so automatic scrolling is disabled
if ( $.browser.mozilla ) {
if ( IPython.utils.browser[0] === "Firefox" ) {
return;
}
// maybe scroll output,

View File

@ -111,7 +111,6 @@ var IPython = (function (IPython) {
}, function (cell) {
that.cancel_stick();
that.showInPager(cell);
that._cmfocus();
}];
// call after all the tabs function above have bee call to clean their effects
// if necessary
@ -126,7 +125,7 @@ var IPython = (function (IPython) {
// reexecute last call in pager by appending ? to show back in pager
var that = this;
var empty = function () {};
IPython.notebook.kernel.execute(
cell.kernel.execute(
that.name + '?', {
'execute_reply': empty,
'output': empty,
@ -136,7 +135,6 @@ var IPython = (function (IPython) {
'silent': false
});
this.remove_and_cancel_tooltip();
this._cmfocus();
}
// grow the tooltip verticaly
@ -144,7 +142,6 @@ var IPython = (function (IPython) {
this.text.removeClass('smalltooltip');
this.text.addClass('bigtooltip');
$('#expanbutton').hide('slow');
this._cmfocus();
}
// deal with all the logic of hiding the tooltip
@ -170,7 +167,6 @@ var IPython = (function (IPython) {
}
this.cancel_pending();
this.reset_tabs_function();
this._cmfocus();
}
// cancel autocall done after '(' for example.
@ -212,7 +208,7 @@ var IPython = (function (IPython) {
var callbacks = {
'object_info_reply': $.proxy(this._show, this)
}
var msg_id = IPython.notebook.kernel.object_info_request(re.exec(func), callbacks);
var msg_id = cell.kernel.object_info_request(re.exec(func), callbacks);
}
// make an imediate completion request
@ -357,16 +353,6 @@ var IPython = (function (IPython) {
this.text.scrollTop(0);
}
// convenient funciton to have the correct code_mirror back into focus
Tooltip.prototype._cmfocus = function () {
var cm = this.code_mirror;
if (cm == IPython.notebook.get_selected_cell())
{
setTimeout(function () {
cm.focus();
}, 50);
}
}
IPython.Tooltip = Tooltip;

View File

@ -272,6 +272,15 @@ IPython.utils = (function (IPython) {
return Math.floor(points*pixel_per_point);
};
// http://stackoverflow.com/questions/2400935/browser-detection-in-javascript
browser = (function() {
var N= navigator.appName, ua= navigator.userAgent, tem;
var M= ua.match(/(opera|chrome|safari|firefox|msie)\/?\s*(\.?\d+(\.\d+)*)/i);
if (M && (tem= ua.match(/version\/([\.\d]+)/i))!= null) M[2]= tem[1];
M= M? [M[1], M[2]]: [N, navigator.appVersion,'-?'];
return M;
})();
return {
regex_split : regex_split,
@ -282,7 +291,9 @@ IPython.utils = (function (IPython) {
fixCarriageReturn : fixCarriageReturn,
wrapUrls : wrapUrls,
autoLinkUrls : autoLinkUrls,
points_to_pixels : points_to_pixels
points_to_pixels : points_to_pixels,
browser : browser
};
}(IPython));