Clean up javascript based on js2-mode feedback.

This commit is contained in:
Stefan van der Walt 2011-11-23 17:43:56 -05:00
parent 58b2fd5eca
commit 6ffc2ffd4b
10 changed files with 111 additions and 107 deletions

View File

@ -45,16 +45,16 @@ var IPython = (function (IPython) {
Cell.prototype.bind_events = function () {
var that = this;
var nb = that.notebook
var nb = that.notebook;
that.element.click(function (event) {
if (that.selected === false) {
nb.select(nb.find_cell_index(that));
};
}
});
that.element.focusin(function (event) {
if (that.selected === false) {
nb.select(nb.find_cell_index(that));
};
}
});
};

View File

@ -44,7 +44,7 @@ var IPython = (function (IPython) {
var output = $('<div></div>').addClass('output vbox');
cell.append(input).append(output);
this.element = cell;
this.collapse()
this.collapse();
};
//TODO, try to diminish the number of parameters.
@ -123,7 +123,7 @@ var IPython = (function (IPython) {
return true;
} else {
return false;
};
}
} else if (event.keyCode === 76 && event.ctrlKey && event.shiftKey
&& event.type == 'keydown') {
// toggle line numbers with Ctrl-Shift-L
@ -138,10 +138,11 @@ var IPython = (function (IPython) {
if (ed_cur.line !== cc_cur.line || ed_cur.ch !== cc_cur.ch) {
this.is_completing = false;
this.completion_cursor = null;
};
};
}
}
return false;
};
return false;
};
CodeCell.prototype.remove_and_cancell_tooltip = function(timeout)
@ -345,7 +346,7 @@ var IPython = (function (IPython) {
} else {
this.code_mirror.setOption('lineNumbers', false);
}
this.code_mirror.refresh()
this.code_mirror.refresh();
};
CodeCell.prototype.select = function () {
@ -447,7 +448,7 @@ var IPython = (function (IPython) {
CodeCell.prototype.append_display_data = function (json) {
var toinsert = this.create_output_area();
this.append_mime_type(json, toinsert)
this.append_mime_type(json, toinsert);
this.element.find('div.output').append(toinsert);
// If we just output latex, typeset it.
if ( (json.latex !== undefined) || (json.html !== undefined) ) {
@ -477,7 +478,7 @@ var IPython = (function (IPython) {
var toinsert = $("<div/>").addClass("box_flex1 output_subarea output_html rendered_html");
toinsert.append(html);
element.append(toinsert);
}
};
CodeCell.prototype.append_text = function (data, element, extra_class) {
@ -517,7 +518,7 @@ var IPython = (function (IPython) {
var toinsert = $("<div/>").addClass("box_flex1 output_subarea output_latex");
toinsert.append(latex);
element.append(toinsert);
}
};
CodeCell.prototype.clear_output = function (stdout, stderr, other) {
@ -589,7 +590,7 @@ var IPython = (function (IPython) {
CodeCell.prototype.set_input_prompt = function (number) {
var n = number || '&nbsp;';
this.input_prompt_number = n
this.input_prompt_number = n;
this.element.find('div.input_prompt').html('In&nbsp;[' + n + ']:');
};
@ -653,7 +654,7 @@ var IPython = (function (IPython) {
data.input = this.get_code();
data.cell_type = 'code';
if (this.input_prompt_number !== ' ') {
data.prompt_number = this.input_prompt_number
data.prompt_number = this.input_prompt_number;
};
var outputs = [];
var len = this.outputs.length;

View File

@ -23,9 +23,9 @@ var IPython = (function (IPython) {
this.session_id = utils.uuid();
if (typeof(WebSocket) !== 'undefined') {
this.WebSocket = WebSocket
this.WebSocket = WebSocket;
} else if (typeof(MozWebSocket) !== 'undefined') {
this.WebSocket = MozWebSocket
this.WebSocket = MozWebSocket;
} else {
alert('Your browser does not have WebSocket support, please try Chrome, Safari or Firefox ≥ 6. Firefox 4 and 5 are also supported by you have to enable WebSockets in about:config.');
};
@ -44,13 +44,13 @@ var IPython = (function (IPython) {
parent_header : {}
};
return msg;
}
};
Kernel.prototype.start = function (notebook_id, callback) {
var that = this;
if (!this.running) {
var qs = $.param({notebook:notebook_id});
var url = this.base_url + '?' + qs
var url = this.base_url + '?' + qs;
$.post(url,
function (kernel_id) {
that._handle_start_kernel(kernel_id, callback);
@ -95,10 +95,10 @@ var IPython = (function (IPython) {
" You will NOT be able to run code.<br/>" +
" 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."
" server's configuration.";
} else {
msg = "Websocket connection closed unexpectedly.<br/>" +
" The kernel will no longer be responsive."
" The kernel will no longer be responsive.";
}
var dialog = $('<div/>');
dialog.html(msg);
@ -114,7 +114,7 @@ var IPython = (function (IPython) {
}
});
}
};
Kernel.prototype.start_channels = function () {
var that = this;
@ -125,7 +125,7 @@ var IPython = (function (IPython) {
this.iopub_channel = new this.WebSocket(ws_url + "/iopub");
send_cookie = function(){
this.send(document.cookie);
}
};
var already_called_onclose = false; // only alert once
ws_closed_early = function(evt){
if (already_called_onclose){
@ -135,7 +135,7 @@ var IPython = (function (IPython) {
if ( ! evt.wasClean ){
that._websocket_closed(ws_url, true);
}
}
};
ws_closed_late = function(evt){
if (already_called_onclose){
return;
@ -144,7 +144,7 @@ var IPython = (function (IPython) {
if ( ! evt.wasClean ){
that._websocket_closed(ws_url, false);
}
}
};
this.shell_channel.onopen = send_cookie;
this.shell_channel.onclose = ws_closed_early;
this.iopub_channel.onopen = send_cookie;
@ -159,12 +159,12 @@ var IPython = (function (IPython) {
Kernel.prototype.stop_channels = function () {
if (this.shell_channel !== null) {
this.shell_channel.onclose = function (evt) {null};
this.shell_channel.onclose = function (evt) {};
this.shell_channel.close();
this.shell_channel = null;
};
if (this.iopub_channel !== null) {
this.iopub_channel.onclose = function (evt) {null};
this.iopub_channel.onclose = function (evt) {};
this.iopub_channel.close();
this.iopub_channel = null;
};
@ -189,12 +189,12 @@ var IPython = (function (IPython) {
silent : false,
user_variables : [],
user_expressions : {},
allow_stdin : false,
allow_stdin : false
};
var msg = this.get_msg("execute_request", content);
this.shell_channel.send(JSON.stringify(msg));
return msg.header.msg_id;
}
};
Kernel.prototype.complete = function (line, cursor_pos) {
@ -206,7 +206,7 @@ var IPython = (function (IPython) {
var msg = this.get_msg("complete_request", content);
this.shell_channel.send(JSON.stringify(msg));
return msg.header.msg_id;
}
};
Kernel.prototype.interrupt = function () {
@ -221,7 +221,7 @@ var IPython = (function (IPython) {
this.running = false;
var settings = {
cache : false,
type : "DELETE",
type : "DELETE"
};
$.ajax(this.kernel_url, settings);
};

View File

@ -20,7 +20,7 @@ IPython.namespace = function (ns_string) {
for (i=0; i<parts.length; i+=1) {
// Create property if it doesn't exist
if (typeof parent[parts[i]] === "undefined") {
parent[parts[i]] == {};
parent[parts[i]] = {};
}
}
return parent;

View File

@ -59,7 +59,7 @@ var IPython = (function (IPython) {
var that = this;
$(document).keydown(function (event) {
// console.log(event);
if (that.read_only) return;
if (that.read_only) return false;
if (event.which === 27) {
// Intercept escape at highest level to avoid closing
// websocket connection with firefox
@ -165,6 +165,7 @@ var IPython = (function (IPython) {
that.control_key_active = false;
return true;
};
return true;
});
this.element.bind('collapse_pager', function () {
@ -203,6 +204,7 @@ var IPython = (function (IPython) {
if (that.dirty && ! that.read_only) {
return "You have unsaved changes that will be lost if you leave this page.";
};
return true;
});
};
@ -266,12 +268,12 @@ var IPython = (function (IPython) {
Notebook.prototype.cell_elements = function () {
return this.element.children("div.cell");
}
};
Notebook.prototype.ncells = function (cell) {
return this.cell_elements().length;
}
};
// TODO: we are often calling cells as cells()[i], which we should optimize
@ -280,7 +282,7 @@ var IPython = (function (IPython) {
return this.cell_elements().toArray().map(function (e) {
return $(e).data("cell");
});
}
};
Notebook.prototype.find_cell_index = function (cell) {
@ -296,7 +298,7 @@ var IPython = (function (IPython) {
Notebook.prototype.index_or_selected = function (index) {
return index || this.selected_index() || 0;
}
};
Notebook.prototype.select = function (index) {
@ -354,7 +356,7 @@ var IPython = (function (IPython) {
Notebook.prototype.selected_cell = function () {
return this.cell_elements().eq(this.selected_index()).data("cell");
}
};
// Cell insertion, deletion and moving.
@ -392,7 +394,7 @@ var IPython = (function (IPython) {
this.cell_elements().eq(index).after(cell.element);
};
this.dirty = true;
return this
return this;
};
@ -423,14 +425,14 @@ var IPython = (function (IPython) {
};
this.dirty = true;
return this;
}
};
Notebook.prototype.move_cell_down = function (index) {
var i = index || this.selected_index();
if (i !== null && i < (this.ncells()-1) && i >= 0) {
var pivot = this.cell_elements().eq(i+1)
var tomove = this.cell_elements().eq(i)
var pivot = this.cell_elements().eq(i+1);
var tomove = this.cell_elements().eq(i);
if (pivot !== null && tomove !== null) {
tomove.detach();
pivot.after(tomove);
@ -439,7 +441,7 @@ var IPython = (function (IPython) {
};
this.dirty = true;
return this;
}
};
Notebook.prototype.sort_cells = function () {
@ -447,7 +449,7 @@ var IPython = (function (IPython) {
var sindex = this.selected_index();
var swapped;
do {
swapped = false
swapped = false;
for (var i=1; i<ncells; i++) {
current = this.cell_elements().eq(i).data("cell");
previous = this.cell_elements().eq(i-1).data("cell");
@ -470,7 +472,7 @@ var IPython = (function (IPython) {
this.insert_cell_above(cell, i);
this.select(this.find_cell_index(cell));
return cell;
}
};
Notebook.prototype.insert_code_cell_below = function (index) {
@ -481,7 +483,7 @@ var IPython = (function (IPython) {
this.insert_cell_below(cell, i);
this.select(this.find_cell_index(cell));
return cell;
}
};
Notebook.prototype.insert_html_cell_above = function (index) {
@ -492,7 +494,7 @@ var IPython = (function (IPython) {
this.insert_cell_above(cell, i);
this.select(this.find_cell_index(cell));
return cell;
}
};
Notebook.prototype.insert_html_cell_below = function (index) {
@ -503,7 +505,7 @@ var IPython = (function (IPython) {
this.insert_cell_below(cell, i);
this.select(this.find_cell_index(cell));
return cell;
}
};
Notebook.prototype.insert_markdown_cell_above = function (index) {
@ -514,7 +516,7 @@ var IPython = (function (IPython) {
this.insert_cell_above(cell, i);
this.select(this.find_cell_index(cell));
return cell;
}
};
Notebook.prototype.insert_markdown_cell_below = function (index) {
@ -525,7 +527,7 @@ var IPython = (function (IPython) {
this.insert_cell_below(cell, i);
this.select(this.find_cell_index(cell));
return cell;
}
};
Notebook.prototype.to_code = function (index) {
@ -553,11 +555,11 @@ var IPython = (function (IPython) {
var target_cell = null;
if (source_cell instanceof IPython.CodeCell) {
this.insert_markdown_cell_below(i);
var target_cell = this.cells()[i+1];
target_cell = this.cells()[i+1];
var text = source_cell.get_code();
} else if (source_cell instanceof IPython.HTMLCell) {
this.insert_markdown_cell_below(i);
var target_cell = this.cells()[i+1];
target_cell = this.cells()[i+1];
var text = source_cell.get_source();
if (text === source_cell.placeholder) {
text = target_cell.placeholder;
@ -581,11 +583,11 @@ var IPython = (function (IPython) {
var target_cell = null;
if (source_cell instanceof IPython.CodeCell) {
this.insert_html_cell_below(i);
var target_cell = this.cells()[i+1];
target_cell = this.cells()[i+1];
var text = source_cell.get_code();
} else if (source_cell instanceof IPython.MarkdownCell) {
this.insert_html_cell_below(i);
var target_cell = this.cells()[i+1];
target_cell = this.cells()[i+1];
var text = source_cell.get_source();
if (text === source_cell.placeholder) {
text = target_cell.placeholder;
@ -643,7 +645,7 @@ var IPython = (function (IPython) {
var cells = this.cells();
len = cells.length;
for (var i=0; i<len; i++) {
cells[i].set_autoindent(state)
cells[i].set_autoindent(state);
};
};
@ -662,7 +664,7 @@ var IPython = (function (IPython) {
// Other cell functions: line numbers, ...
Notebook.prototype.cell_toggle_line_numbers = function() {
this.selected_cell().toggle_line_numbers()
this.selected_cell().toggle_line_numbers();
};
// Kernel related things
@ -864,8 +866,8 @@ var IPython = (function (IPython) {
Notebook.prototype.execute_selected_cell = function (options) {
// add_new: should a new cell be added if we are at the end of the nb
// terminal: execute in terminal mode, which stays in the current cell
default_options = {terminal: false, add_new: true}
$.extend(default_options, options)
default_options = {terminal: false, add_new: true};
$.extend(default_options, options);
var that = this;
var cell = that.selected_cell();
var cell_index = that.find_cell_index(cell);
@ -938,7 +940,8 @@ var IPython = (function (IPython) {
Notebook.prototype.fromJSON = function (data) {
var ncells = this.ncells();
for (var i=0; i<ncells; i++) {
var i;
for (i=0; i<ncells; i++) {
// Always delete cell 0 as they get renumbered as they are deleted.
this.delete_cell(0);
};
@ -951,7 +954,7 @@ var IPython = (function (IPython) {
ncells = new_cells.length;
var cell_data = null;
var new_cell = null;
for (var i=0; i<ncells; i++) {
for (i=0; i<ncells; i++) {
cell_data = new_cells[i];
if (cell_data.cell_type == 'code') {
new_cell = this.insert_code_cell_below();
@ -979,8 +982,8 @@ var IPython = (function (IPython) {
// Only handle 1 worksheet for now.
worksheets : [{cells:cell_array}],
metadata : this.metadata
}
return data
};
return data;
};
Notebook.prototype.save_notebook = function () {
@ -1002,7 +1005,7 @@ var IPython = (function (IPython) {
error : $.proxy(this.notebook_save_failed,this)
};
IPython.save_widget.status_saving();
var url = $('body').data('baseProjectUrl') + 'notebooks/' + notebook_id
var url = $('body').data('baseProjectUrl') + 'notebooks/' + notebook_id;
$.ajax(url, settings);
};
};
@ -1012,7 +1015,7 @@ var IPython = (function (IPython) {
this.dirty = false;
IPython.save_widget.notebook_saved();
IPython.save_widget.status_save();
}
};
Notebook.prototype.notebook_save_failed = function (xhr, status, error_msg) {
@ -1020,7 +1023,7 @@ var IPython = (function (IPython) {
// TODO: Handle different types of errors (timeout etc.)
alert('An unexpected error occured while saving the notebook.');
IPython.save_widget.reset_status();
}
};
Notebook.prototype.load_notebook = function (callback) {
@ -1040,9 +1043,9 @@ var IPython = (function (IPython) {
}
};
IPython.save_widget.status_loading();
var url = $('body').data('baseProjectUrl') + 'notebooks/' + notebook_id
var url = $('body').data('baseProjectUrl') + 'notebooks/' + notebook_id;
$.ajax(url, settings);
}
};
Notebook.prototype.notebook_loaded = function (data, status, xhr) {

View File

@ -67,7 +67,7 @@ var IPython = (function (IPython) {
dataType : "json",
success : $.proxy(this.list_loaded, this)
};
var url = $('body').data('baseProjectUrl') + 'notebooks'
var url = $('body').data('baseProjectUrl') + 'notebooks';
$.ajax(url, settings);
};
@ -174,7 +174,7 @@ var IPython = (function (IPython) {
parent_item.remove();
}
};
var url = $('body').data('baseProjectUrl') + 'notebooks/' + notebook_id
var url = $('body').data('baseProjectUrl') + 'notebooks/' + notebook_id;
$.ajax(url, settings);
$(this).dialog('close');
},
@ -222,7 +222,7 @@ var IPython = (function (IPython) {
};
var qs = $.param({name:nbname, format:nbformat});
var url = $('body').data('baseProjectUrl') + 'notebooks?' + qs
var url = $('body').data('baseProjectUrl') + 'notebooks?' + qs;
$.ajax(url, settings);
});
var cancel_button = $('<button>Cancel</button>').button().

View File

@ -15,7 +15,7 @@ $(document).ready(function () {
MathJax.Hub.Config({
tex2jax: {
inlineMath: [ ['$','$'], ["\\(","\\)"] ],
displayMath: [ ['$$','$$'], ["\\[","\\]"] ],
displayMath: [ ['$$','$$'], ["\\[","\\]"] ]
},
displayAlign: 'left', // Change this to 'center' to center equations.
"HTML-CSS": {
@ -76,7 +76,7 @@ $(document).ready(function () {
// and finally unhide the panel contents after collapse
setTimeout(function(){
IPython.left_panel.left_panel_element.css('visibility', 'visible');
}, 200)
}, 200);
}
},100);
});

View File

@ -15,7 +15,7 @@ var IPython = (function (IPython) {
var SaveWidget = function (selector) {
this.selector = selector;
this.notebook_name_blacklist_re = /[\/\\]/
this.notebook_name_blacklist_re = /[\/\\]/;
this.last_saved_name = '';
if (this.selector !== undefined) {
this.element = $(selector);
@ -70,14 +70,14 @@ var IPython = (function (IPython) {
SaveWidget.prototype.get_notebook_name = function () {
return this.element.find('input#notebook_name').attr('value');
}
};
SaveWidget.prototype.set_notebook_name = function (nbname) {
this.element.find('input#notebook_name').attr('value',nbname);
this.set_document_title();
this.last_saved_name = nbname;
}
};
SaveWidget.prototype.set_document_title = function () {

View File

@ -34,7 +34,7 @@ var IPython = (function (IPython) {
mode: this.code_mirror_mode,
theme: 'default',
value: this.placeholder,
readOnly: this.read_only,
readOnly: this.read_only
});
// The tabindex=-1 makes this div focusable.
var render_area = $('<div/>').addClass('text_cell_render').
@ -52,8 +52,8 @@ var IPython = (function (IPython) {
if (that.rendered) {
that.edit();
event.preventDefault();
};
};
}
}
});
};
@ -77,8 +77,8 @@ var IPython = (function (IPython) {
this.rendered = false;
if (this.get_source() === this.placeholder) {
this.set_source('');
};
};
}
}
};
@ -145,13 +145,13 @@ var IPython = (function (IPython) {
this.set_rendered(data.rendered || '');
this.rendered = false;
this.render();
};
};
}
}
};
TextCell.prototype.toJSON = function () {
var data = {}
var data = {};
data.cell_type = this.cell_type;
data.source = this.get_source();
return data;
@ -173,13 +173,13 @@ var IPython = (function (IPython) {
HTMLCell.prototype.render = function () {
if (this.rendered === false) {
var text = this.get_source();
if (text === "") {text = this.placeholder;};
if (text === "") { text = this.placeholder; }
this.set_rendered(text);
MathJax.Hub.Queue(["Typeset",MathJax.Hub]);
this.element.find('div.text_cell_input').hide();
this.element.find("div.text_cell_render").show();
this.rendered = true;
};
}
};
@ -198,7 +198,7 @@ var IPython = (function (IPython) {
MarkdownCell.prototype.render = function () {
if (this.rendered === false) {
var text = this.get_source();
if (text === "") {text = this.placeholder;};
if (text === "") { text = this.placeholder; }
var html = IPython.markdown_converter.makeHtml(text);
this.set_rendered(html);
MathJax.Hub.Queue(["Typeset",MathJax.Hub]);
@ -217,7 +217,7 @@ var IPython = (function (IPython) {
return '<code class="prettyprint">' + code + '</code>';
});
this.rendered = true;
};
}
};
@ -234,9 +234,9 @@ var IPython = (function (IPython) {
RSTCell.prototype.render = function () {
if (this.rendered === false) {
if (this.rendered === false) {
var text = this.get_source();
if (text === "") {text = this.placeholder;};
if (text === "") { text = this.placeholder; }
var settings = {
processData : false,
cache : false,
@ -249,7 +249,7 @@ var IPython = (function (IPython) {
this.element.find('div.text_cell_input').hide();
this.element.find("div.text_cell_render").show();
this.set_rendered("Rendering...");
};
}
};

View File

@ -9,7 +9,7 @@
// Utilities
//============================================================================
IPython.namespace('IPython.utils')
IPython.namespace('IPython.utils');
IPython.utils = (function (IPython) {
@ -35,7 +35,7 @@ IPython.utils = (function (IPython) {
.replace(/>/g,'&'+'gt;')
.replace(/\'/g,'&'+'apos;')
.replace(/\"/g,'&'+'quot;')
.replace(/`/g,'&'+'#96;')
.replace(/`/g,'&'+'#96;');
}
@ -45,32 +45,32 @@ IPython.utils = (function (IPython) {
"32":"ansigreen", "33":"ansiyellow",
"34":"ansiblue", "35":"ansipurple","36":"ansicyan",
"37":"ansigrey", "01":"ansibold"
}
};
// Transform ANI color escape codes into HTML <span> tags with css
// classes listed in the above ansi_colormap object. The actual color used
// are set in the css file.
function fixConsole(txt) {
txt = xmlencode(txt)
var re = /\033\[([\d;]*?)m/
var opened = false
var cmds = []
var opener = ""
var closer = ""
txt = xmlencode(txt);
var re = /\033\[([\d;]*?)m/;
var opened = false;
var cmds = [];
var opener = "";
var closer = "";
while (re.test(txt)) {
var cmds = txt.match(re)[1].split(";")
closer = opened?"</span>":""
opened = cmds.length > 1 || cmds[0] != 0
var rep = []
var cmds = txt.match(re)[1].split(";");
closer = opened?"</span>":"";
opened = cmds.length > 1 || cmds[0] != 0;
var rep = [];
for (var i in cmds)
if (typeof(ansi_colormap[cmds[i]]) != "undefined")
rep.push(ansi_colormap[cmds[i]])
opener = rep.length > 0?"<span class=\""+rep.join(" ")+"\">":""
txt = txt.replace(re, closer + opener)
rep.push(ansi_colormap[cmds[i]]);
opener = rep.length > 0?"<span class=\""+rep.join(" ")+"\">":"";
txt = txt.replace(re, closer + opener);
}
if (opened) txt += "</span>"
return txt
if (opened) txt += "</span>";
return txt;
}
@ -95,7 +95,7 @@ IPython.utils = (function (IPython) {
uuid : uuid,
fixConsole : fixConsole,
grow : grow
}
};
}(IPython));