##// END OF EJS Templates
Implemented module and namespace pattern in js notebook.
Brian E. Granger -
Show More
@@ -3,6 +3,9
3 3 // Cell
4 4 //============================================================================
5 5
6 var IPython = (function (IPython) {
7
8 var utils = IPython.utils;
6 9
7 10 var Cell = function (notebook) {
8 11 this.notebook = notebook;
@@ -13,7 +16,7 var Cell = function (notebook) {
13 16 this.element.data("cell", this);
14 17 this.bind_events();
15 18 }
16 this.cell_id = uuid();
19 this.cell_id = utils.uuid();
17 20 };
18 21
19 22
@@ -69,4 +72,9 Cell.prototype.bind_events = function () {
69 72 // Subclasses must implement create_element.
70 73 Cell.prototype.create_element = function () {};
71 74
75 IPython.Cell = Cell;
76
77 return IPython;
78
79 }(IPython));
72 80
@@ -3,15 +3,18
3 3 // CodeCell
4 4 //============================================================================
5 5
6 var IPython = (function (IPython) {
7
8 var utils = IPython.utils;
6 9
7 10 var CodeCell = function (notebook) {
8 11 this.code_mirror = null;
9 12 this.input_prompt_number = ' ';
10 Cell.apply(this, arguments);
13 IPython.Cell.apply(this, arguments);
11 14 };
12 15
13 16
14 CodeCell.prototype = new Cell();
17 CodeCell.prototype = new IPython.Cell();
15 18
16 19
17 20 CodeCell.prototype.create_element = function () {
@@ -33,7 +36,7 CodeCell.prototype.create_element = function () {
33 36
34 37
35 38 CodeCell.prototype.select = function () {
36 Cell.prototype.select.apply(this);
39 IPython.Cell.prototype.select.apply(this);
37 40 this.code_mirror.focus();
38 41 };
39 42
@@ -66,7 +69,6 CodeCell.prototype.append_pyerr = function (ename, evalue, tb) {
66 69
67 70
68 71 CodeCell.prototype.append_display_data = function (data, element) {
69 console.log(data);
70 72 if (data["text/latex"] !== undefined) {
71 73 this.append_latex(data["text/latex"], element);
72 74 // If it is undefined, then we just appended to div.output, which
@@ -89,7 +91,7 CodeCell.prototype.append_display_data = function (data, element) {
89 91 CodeCell.prototype.append_stream = function (data, element) {
90 92 element = element || this.element.find("div.output");
91 93 var toinsert = $("<div/>").addClass("output_area output_stream monospace-font");
92 toinsert.append($("<pre/>").addClass("monospace-font").html(fixConsole(data)));
94 toinsert.append($("<pre/>").addClass("monospace-font").html(utils.fixConsole(data)));
93 95 element.append(toinsert);
94 96 return element;
95 97 };
@@ -192,4 +194,8 CodeCell.prototype.toJSON = function () {
192 194 };
193 195 };
194 196
197 IPython.CodeCell = CodeCell;
198
199 return IPython;
200 }(IPython));
195 201
@@ -3,6 +3,9
3 3 // Kernel
4 4 //============================================================================
5 5
6 var IPython = (function (IPython) {
7
8 var utils = IPython.utils;
6 9
7 10 var Kernel = function () {
8 11 this.kernel_id = null;
@@ -14,7 +17,7 var Kernel = function () {
14 17 Kernel.prototype.get_msg = function (msg_type, content) {
15 18 var msg = {
16 19 header : {
17 msg_id : uuid(),
20 msg_id : utils.uuid(),
18 21 username : "bgranger",
19 22 session: this.session_id,
20 23 msg_type : msg_type
@@ -104,4 +107,9 Kernel.prototype.status_restarting = function () {
104 107 $("#kernel_status").text("Restarting");
105 108 };
106 109
110 IPython.Kernel = Kernel;
111
112 return IPython;
113
114 }(IPython));
107 115
@@ -1,4 +1,23
1 var IPYTHON = {};
1 var IPython = IPython || {};
2
3 IPython.namespace = function (ns_string) {
4 var parts = ns_string.split('.'),
5 parent = IPython,
6 i;
7
8 // String redundant leading global
9 if (parts[0] === "IPython") {
10 parts = parts.slice(1);
11 }
12
13 for (i=0; i<parts.length; i+=1) {
14 // Create property if it doesn't exist
15 if (typeof parent[parts[i]] === "undefined") {
16 parent[parts[i]] == {};
17 }
18 }
19 return parent;
20 };
2 21
3 22
4 23
@@ -3,6 +3,7
3 3 // Notebook
4 4 //============================================================================
5 5
6 var IPython = (function (IPython) {
6 7
7 8 var Notebook = function (selector) {
8 9 this.element = $(selector);
@@ -42,7 +43,7 Notebook.prototype.bind_events = function () {
42 43 var cell_index = that.find_cell_index(cell);
43 44 // TODO: the logic here needs to be moved into appropriate
44 45 // methods of Notebook.
45 if (cell instanceof CodeCell) {
46 if (cell instanceof IPython.CodeCell) {
46 47 event.preventDefault();
47 48 cell.clear_output();
48 49 var code = cell.get_code();
@@ -62,7 +63,7 Notebook.prototype.bind_events = function () {
62 63 var msg_id = that.kernel.execute(cell.get_code());
63 64 that.msg_cell_map[msg_id] = cell.cell_id;
64 65 };
65 } else if (cell instanceof TextCell) {
66 } else if (cell instanceof IPython.TextCell) {
66 67 event.preventDefault();
67 68 cell.render();
68 69 }
@@ -274,7 +275,7 Notebook.prototype.sort_cells = function () {
274 275 Notebook.prototype.insert_code_cell_before = function (index) {
275 276 // TODO: Bounds check for i
276 277 var i = this.index_or_selected(index);
277 var cell = new CodeCell(this);
278 var cell = new IPython.CodeCell(this);
278 279 cell.set_input_prompt(this.next_prompt_number);
279 280 this.next_prompt_number = this.next_prompt_number + 1;
280 281 this.insert_cell_before(cell, i);
@@ -286,7 +287,7 Notebook.prototype.insert_code_cell_before = function (index) {
286 287 Notebook.prototype.insert_code_cell_after = function (index) {
287 288 // TODO: Bounds check for i
288 289 var i = this.index_or_selected(index);
289 var cell = new CodeCell(this);
290 var cell = new IPython.CodeCell(this);
290 291 cell.set_input_prompt(this.next_prompt_number);
291 292 this.next_prompt_number = this.next_prompt_number + 1;
292 293 this.insert_cell_after(cell, i);
@@ -298,7 +299,7 Notebook.prototype.insert_code_cell_after = function (index) {
298 299 Notebook.prototype.insert_text_cell_before = function (index) {
299 300 // TODO: Bounds check for i
300 301 var i = this.index_or_selected(index);
301 var cell = new TextCell(this);
302 var cell = new IPython.TextCell(this);
302 303 cell.config_mathjax();
303 304 this.insert_cell_before(cell, i);
304 305 this.select(this.find_cell_index(cell));
@@ -309,7 +310,7 Notebook.prototype.insert_text_cell_before = function (index) {
309 310 Notebook.prototype.insert_text_cell_after = function (index) {
310 311 // TODO: Bounds check for i
311 312 var i = this.index_or_selected(index);
312 var cell = new TextCell(this);
313 var cell = new IPython.TextCell(this);
313 314 cell.config_mathjax();
314 315 this.insert_cell_after(cell, i);
315 316 this.select(this.find_cell_index(cell));
@@ -322,7 +323,7 Notebook.prototype.text_to_code = function (index) {
322 323 var i = this.index_or_selected(index);
323 324 var source_element = this.cell_elements().eq(i);
324 325 var source_cell = source_element.data("cell");
325 if (source_cell instanceof TextCell) {
326 if (source_cell instanceof IPython.TextCell) {
326 327 this.insert_code_cell_after(i);
327 328 var target_cell = this.cells()[i+1];
328 329 target_cell.set_code(source_cell.get_text());
@@ -336,7 +337,7 Notebook.prototype.code_to_text = function (index) {
336 337 var i = this.index_or_selected(index);
337 338 var source_element = this.cell_elements().eq(i);
338 339 var source_cell = source_element.data("cell");
339 if (source_cell instanceof CodeCell) {
340 if (source_cell instanceof IPython.CodeCell) {
340 341 this.insert_text_cell_after(i);
341 342 var target_cell = this.cells()[i+1];
342 343 var text = source_cell.get_code();
@@ -365,7 +366,7 Notebook.prototype.expand = function (index) {
365 366 // Kernel related things
366 367
367 368 Notebook.prototype.start_kernel = function () {
368 this.kernel = new Kernel();
369 this.kernel = new IPython.Kernel();
369 370 this.kernel.start_kernel(this._kernel_started, this);
370 371 };
371 372
@@ -387,7 +388,7 Notebook.prototype._kernel_started = function () {
387 388 this.kernel.iopub_channel.onmessage = function (e) {
388 389 reply = $.parseJSON(e.data);
389 390 var content = reply.content;
390 console.log(reply);
391 // console.log(reply);
391 392 var msg_type = reply.header.msg_type;
392 393 var cell = that.cell_for_msg(reply.parent_header.msg_id);
393 394 if (msg_type === "stream") {
@@ -506,3 +507,9 Notebook.prototype.load_notebook = function (filename) {
506 507 $.ajax("/notebooks/" + filename, settings);
507 508 }
508 509
510 IPython.Notebook = Notebook;
511
512 return IPython;
513
514 }(IPython));
515
@@ -20,43 +20,43 $(document).ready(function () {
20 20 }
21 21 });
22 22
23 IPYTHON.notebook = new Notebook('div.notebook');
24 IPYTHON.notebook.insert_code_cell_after();
23 IPython.notebook = new IPython.Notebook('div.notebook');
24 IPython.notebook.insert_code_cell_after();
25 25
26 26 $("#menu_tabs").tabs();
27 27
28 28 $("#help_toolbar").buttonset();
29 29
30 30 $("#kernel_toolbar").buttonset();
31 $("#interrupt_kernel").click(function () {IPYTHON.notebook.kernel.interrupt();});
32 $("#restart_kernel").click(function () {IPYTHON.notebook.kernel.restart();});
31 $("#interrupt_kernel").click(function () {IPython.notebook.kernel.interrupt();});
32 $("#restart_kernel").click(function () {IPython.notebook.kernel.restart();});
33 33 $("#kernel_status").addClass("status_idle");
34 34
35 35 $("#move_cell").buttonset();
36 36 $("#move_up").button("option", "icons", {primary:"ui-icon-arrowthick-1-n"});
37 37 $("#move_up").button("option", "text", false);
38 $("#move_up").click(function () {IPYTHON.notebook.move_cell_up();});
38 $("#move_up").click(function () {IPython.notebook.move_cell_up();});
39 39 $("#move_down").button("option", "icons", {primary:"ui-icon-arrowthick-1-s"});
40 40 $("#move_down").button("option", "text", false);
41 $("#move_down").click(function () {IPYTHON.notebook.move_cell_down();});
41 $("#move_down").click(function () {IPython.notebook.move_cell_down();});
42 42
43 43 $("#insert_delete").buttonset();
44 $("#insert_cell_before").click(function () {IPYTHON.notebook.insert_code_cell_before();});
45 $("#insert_cell_after").click(function () {IPYTHON.notebook.insert_code_cell_after();});
44 $("#insert_cell_before").click(function () {IPython.notebook.insert_code_cell_before();});
45 $("#insert_cell_after").click(function () {IPython.notebook.insert_code_cell_after();});
46 46 $("#delete_cell").button("option", "icons", {primary:"ui-icon-closethick"});
47 47 $("#delete_cell").button("option", "text", false);
48 $("#delete_cell").click(function () {IPYTHON.notebook.delete_cell();});
48 $("#delete_cell").click(function () {IPython.notebook.delete_cell();});
49 49
50 50 $("#cell_type").buttonset();
51 $("#to_code").click(function () {IPYTHON.notebook.text_to_code();});
52 $("#to_text").click(function () {IPYTHON.notebook.code_to_text();});
51 $("#to_code").click(function () {IPython.notebook.text_to_code();});
52 $("#to_text").click(function () {IPython.notebook.code_to_text();});
53 53
54 54 $("#sort").buttonset();
55 $("#sort_cells").click(function () {IPYTHON.notebook.sort_cells();});
55 $("#sort_cells").click(function () {IPython.notebook.sort_cells();});
56 56
57 57 $("#toggle").buttonset();
58 $("#collapse").click(function () {IPYTHON.notebook.collapse();});
59 $("#expand").click(function () {IPYTHON.notebook.expand();});
58 $("#collapse").click(function () {IPython.notebook.collapse();});
59 $("#expand").click(function () {IPython.notebook.expand();});
60 60
61 61 });
62 62
@@ -3,15 +3,16
3 3 // TextCell
4 4 //============================================================================
5 5
6 var IPython = (function (IPython) {
6 7
7 8 var TextCell = function (notebook) {
8 Cell.apply(this, arguments);
9 IPython.Cell.apply(this, arguments);
9 10 this.placeholder = "Type <strong>HTML</strong> and LaTeX: $\\alpha^2$"
10 11 this.rendered = false;
11 12 };
12 13
13 14
14 TextCell.prototype = new Cell();
15 TextCell.prototype = new IPython.Cell();
15 16
16 17
17 18 TextCell.prototype.create_element = function () {
@@ -31,7 +32,7 TextCell.prototype.create_element = function () {
31 32
32 33
33 34 TextCell.prototype.bind_events = function () {
34 Cell.prototype.bind_events.apply(this);
35 IPython.Cell.prototype.bind_events.apply(this);
35 36 var that = this;
36 37 this.element.keydown(function (event) {
37 38 if (event.which === 13) {
@@ -45,7 +46,7 TextCell.prototype.bind_events = function () {
45 46
46 47
47 48 TextCell.prototype.select = function () {
48 Cell.prototype.select.apply(this);
49 IPython.Cell.prototype.select.apply(this);
49 50 var output = this.element.find("div.text_cell_render");
50 51 output.trigger('focus');
51 52 };
@@ -141,4 +142,9 TextCell.prototype.toJSON = function () {
141 142 };
142 143 };
143 144
145 IPython.TextCell = TextCell;
146
147 return IPython;
148
149 }(IPython));
144 150
@@ -3,6 +3,9
3 3 // Utilities
4 4 //============================================================================
5 5
6 IPython.namespace('IPython.utils')
7
8 IPython.utils = (function (IPython) {
6 9
7 10 var uuid = function () {
8 11 // http://www.ietf.org/rfc/rfc4122.txt
@@ -60,3 +63,10 function fixConsole(txt) {
60 63 return txt.trim()
61 64 }
62 65
66 return {
67 uuid : uuid,
68 fixConsole : fixConsole
69 }
70
71 }(IPython));
72
General Comments 0
You need to be logged in to leave comments. Login now