Show More
@@ -1,501 +1,504 b'' | |||||
1 | var IPYTHON = {}; |
|
1 | var IPYTHON = {}; | |
2 |
|
2 | |||
3 |
|
3 | |||
4 | //============================================================================ |
|
4 | //============================================================================ | |
5 | // Notebook |
|
5 | // Notebook | |
6 | //============================================================================ |
|
6 | //============================================================================ | |
7 |
|
7 | |||
8 |
|
8 | |||
9 | var Notebook = function (selector) { |
|
9 | var Notebook = function (selector) { | |
10 | this.element = $(selector); |
|
10 | this.element = $(selector); | |
11 | this.element.data("notebook", this); |
|
11 | this.element.data("notebook", this); | |
12 | this.next_prompt_number = 1; |
|
12 | this.next_prompt_number = 1; | |
13 | this.bind_events(); |
|
13 | this.bind_events(); | |
14 | } |
|
14 | } | |
15 |
|
15 | |||
16 |
|
16 | |||
17 | Notebook.prototype.bind_events = function () { |
|
17 | Notebook.prototype.bind_events = function () { | |
18 | var that = this; |
|
18 | var that = this; | |
19 | that.element.keydown(function (event) { |
|
19 | that.element.keydown(function (event) { | |
20 |
|
|
20 | console.log(event); | |
21 | if (event.which == 38 && event.shiftKey) { |
|
21 | if (event.which == 38 && event.shiftKey) { | |
22 | that.select_prev(); |
|
22 | that.select_prev(); | |
23 | } else if (event.which == 40 && event.shiftKey) { |
|
23 | } else if (event.which == 40 && event.shiftKey) { | |
24 | that.select_next(); |
|
24 | that.select_next(); | |
|
25 | } else if (event.which == 13 && event.shiftKey) { | |||
|
26 | // The focus is not quite working here. | |||
|
27 | that.insert_code_cell_after(); | |||
25 | } |
|
28 | } | |
26 | }); |
|
29 | }); | |
27 | }; |
|
30 | }; | |
28 |
|
31 | |||
29 |
|
32 | |||
30 | // Cell indexing, retrieval, etc. |
|
33 | // Cell indexing, retrieval, etc. | |
31 |
|
34 | |||
32 |
|
35 | |||
33 | Notebook.prototype.cell_elements = function () { |
|
36 | Notebook.prototype.cell_elements = function () { | |
34 | return this.element.children("div.cell"); |
|
37 | return this.element.children("div.cell"); | |
35 | } |
|
38 | } | |
36 |
|
39 | |||
37 |
|
40 | |||
38 | Notebook.prototype.ncells = function (cell) { |
|
41 | Notebook.prototype.ncells = function (cell) { | |
39 | return this.cell_elements().length; |
|
42 | return this.cell_elements().length; | |
40 | } |
|
43 | } | |
41 |
|
44 | |||
42 |
|
45 | |||
43 | // TODO: we are often calling cells as cells()[i], which we should optimize |
|
46 | // TODO: we are often calling cells as cells()[i], which we should optimize | |
44 | // to cells(i) or a new method. |
|
47 | // to cells(i) or a new method. | |
45 | Notebook.prototype.cells = function () { |
|
48 | Notebook.prototype.cells = function () { | |
46 | return this.cell_elements().toArray().map(function (e) { |
|
49 | return this.cell_elements().toArray().map(function (e) { | |
47 | return $(e).data("cell"); |
|
50 | return $(e).data("cell"); | |
48 | }); |
|
51 | }); | |
49 | } |
|
52 | } | |
50 |
|
53 | |||
51 |
|
54 | |||
52 | Notebook.prototype.find_cell_index = function (cell) { |
|
55 | Notebook.prototype.find_cell_index = function (cell) { | |
53 | var result = null; |
|
56 | var result = null; | |
54 | this.cell_elements().filter(function (index) { |
|
57 | this.cell_elements().filter(function (index) { | |
55 | if ($(this).data("cell") === cell) { |
|
58 | if ($(this).data("cell") === cell) { | |
56 | result = index; |
|
59 | result = index; | |
57 | }; |
|
60 | }; | |
58 | }); |
|
61 | }); | |
59 | return result; |
|
62 | return result; | |
60 | }; |
|
63 | }; | |
61 |
|
64 | |||
62 |
|
65 | |||
63 | Notebook.prototype.index_or_selected = function (index) { |
|
66 | Notebook.prototype.index_or_selected = function (index) { | |
64 | return index || this.selected_index() || 0; |
|
67 | return index || this.selected_index() || 0; | |
65 | } |
|
68 | } | |
66 |
|
69 | |||
67 |
|
70 | |||
68 | Notebook.prototype.select = function (index) { |
|
71 | Notebook.prototype.select = function (index) { | |
69 | if (index !== undefined && index >= 0 && index < this.ncells()) { |
|
72 | if (index !== undefined && index >= 0 && index < this.ncells()) { | |
70 | if (this.selected_index() !== null) { |
|
73 | if (this.selected_index() !== null) { | |
71 | this.selected_cell().unselect(); |
|
74 | this.selected_cell().unselect(); | |
72 | }; |
|
75 | }; | |
73 | this.cells()[index].select(); |
|
76 | this.cells()[index].select(); | |
74 | }; |
|
77 | }; | |
75 | return this; |
|
78 | return this; | |
76 | }; |
|
79 | }; | |
77 |
|
80 | |||
78 |
|
81 | |||
79 | Notebook.prototype.select_next = function () { |
|
82 | Notebook.prototype.select_next = function () { | |
80 | var index = this.selected_index(); |
|
83 | var index = this.selected_index(); | |
81 | if (index !== null && index >= 0 && (index+1) < this.ncells()) { |
|
84 | if (index !== null && index >= 0 && (index+1) < this.ncells()) { | |
82 | this.select(index+1); |
|
85 | this.select(index+1); | |
83 | }; |
|
86 | }; | |
84 | return this; |
|
87 | return this; | |
85 | }; |
|
88 | }; | |
86 |
|
89 | |||
87 |
|
90 | |||
88 | Notebook.prototype.select_prev = function () { |
|
91 | Notebook.prototype.select_prev = function () { | |
89 | var index = this.selected_index(); |
|
92 | var index = this.selected_index(); | |
90 | if (index !== null && index >= 0 && (index-1) < this.ncells()) { |
|
93 | if (index !== null && index >= 0 && (index-1) < this.ncells()) { | |
91 | this.select(index-1); |
|
94 | this.select(index-1); | |
92 | }; |
|
95 | }; | |
93 | return this; |
|
96 | return this; | |
94 | }; |
|
97 | }; | |
95 |
|
98 | |||
96 |
|
99 | |||
97 | Notebook.prototype.selected_index = function () { |
|
100 | Notebook.prototype.selected_index = function () { | |
98 | var result = null; |
|
101 | var result = null; | |
99 | this.cell_elements().filter(function (index) { |
|
102 | this.cell_elements().filter(function (index) { | |
100 | if ($(this).data("cell").selected === true) { |
|
103 | if ($(this).data("cell").selected === true) { | |
101 | result = index; |
|
104 | result = index; | |
102 | }; |
|
105 | }; | |
103 | }); |
|
106 | }); | |
104 | return result; |
|
107 | return result; | |
105 | }; |
|
108 | }; | |
106 |
|
109 | |||
107 |
|
110 | |||
108 | Notebook.prototype.selected_cell = function () { |
|
111 | Notebook.prototype.selected_cell = function () { | |
109 | return this.cell_elements().eq(this.selected_index()).data("cell"); |
|
112 | return this.cell_elements().eq(this.selected_index()).data("cell"); | |
110 | } |
|
113 | } | |
111 |
|
114 | |||
112 |
|
115 | |||
113 | // Cell insertion, deletion and moving. |
|
116 | // Cell insertion, deletion and moving. | |
114 |
|
117 | |||
115 |
|
118 | |||
116 | Notebook.prototype.delete_cell = function (index) { |
|
119 | Notebook.prototype.delete_cell = function (index) { | |
117 | var i = index || this.selected_index(); |
|
120 | var i = index || this.selected_index(); | |
118 | if (i !== null && i >= 0 && i < this.ncells()) { |
|
121 | if (i !== null && i >= 0 && i < this.ncells()) { | |
119 | this.cell_elements().eq(i).remove(); |
|
122 | this.cell_elements().eq(i).remove(); | |
120 | if (i === (this.ncells())) { |
|
123 | if (i === (this.ncells())) { | |
121 | this.select(i-1); |
|
124 | this.select(i-1); | |
122 | } else { |
|
125 | } else { | |
123 | this.select(i); |
|
126 | this.select(i); | |
124 | }; |
|
127 | }; | |
125 | }; |
|
128 | }; | |
126 | return this; |
|
129 | return this; | |
127 | }; |
|
130 | }; | |
128 |
|
131 | |||
129 |
|
132 | |||
130 | Notebook.prototype.append_cell = function (cell) { |
|
133 | Notebook.prototype.append_cell = function (cell) { | |
131 | this.element.append(cell.element); |
|
134 | this.element.append(cell.element); | |
132 | return this; |
|
135 | return this; | |
133 | }; |
|
136 | }; | |
134 |
|
137 | |||
135 |
|
138 | |||
136 | Notebook.prototype.insert_cell_after = function (cell, index) { |
|
139 | Notebook.prototype.insert_cell_after = function (cell, index) { | |
137 | var ncells = this.ncells(); |
|
140 | var ncells = this.ncells(); | |
138 | if (ncells === 0) { |
|
141 | if (ncells === 0) { | |
139 | this.append_cell(cell); |
|
142 | this.append_cell(cell); | |
140 | return this; |
|
143 | return this; | |
141 | }; |
|
144 | }; | |
142 | if (index >= 0 && index < ncells) { |
|
145 | if (index >= 0 && index < ncells) { | |
143 | this.cell_elements().eq(index).after(cell.element); |
|
146 | this.cell_elements().eq(index).after(cell.element); | |
144 | }; |
|
147 | }; | |
145 | return this |
|
148 | return this | |
146 | }; |
|
149 | }; | |
147 |
|
150 | |||
148 |
|
151 | |||
149 | Notebook.prototype.insert_cell_before = function (cell, index) { |
|
152 | Notebook.prototype.insert_cell_before = function (cell, index) { | |
150 | var ncells = this.ncells(); |
|
153 | var ncells = this.ncells(); | |
151 | if (ncells === 0) { |
|
154 | if (ncells === 0) { | |
152 | this.append_cell(cell); |
|
155 | this.append_cell(cell); | |
153 | return this; |
|
156 | return this; | |
154 | }; |
|
157 | }; | |
155 | if (index > 0 && index < ncells) { |
|
158 | if (index > 0 && index < ncells) { | |
156 | this.cell_elements().eq(index).before(cell.element); |
|
159 | this.cell_elements().eq(index).before(cell.element); | |
157 | }; |
|
160 | }; | |
158 | return this; |
|
161 | return this; | |
159 | }; |
|
162 | }; | |
160 |
|
163 | |||
161 |
|
164 | |||
162 | Notebook.prototype.move_cell_up = function (index) { |
|
165 | Notebook.prototype.move_cell_up = function (index) { | |
163 | var i = index || this.selected_index(); |
|
166 | var i = index || this.selected_index(); | |
164 | if (i !== null && i < this.ncells() && i > 0) { |
|
167 | if (i !== null && i < this.ncells() && i > 0) { | |
165 | var pivot = this.cell_elements().eq(i-1); |
|
168 | var pivot = this.cell_elements().eq(i-1); | |
166 | var tomove = this.cell_elements().eq(i); |
|
169 | var tomove = this.cell_elements().eq(i); | |
167 | if (pivot !== null && tomove !== null) { |
|
170 | if (pivot !== null && tomove !== null) { | |
168 | tomove.detach(); |
|
171 | tomove.detach(); | |
169 | pivot.before(tomove); |
|
172 | pivot.before(tomove); | |
170 | }; |
|
173 | }; | |
171 | }; |
|
174 | }; | |
172 | return this; |
|
175 | return this; | |
173 | } |
|
176 | } | |
174 |
|
177 | |||
175 |
|
178 | |||
176 | Notebook.prototype.move_cell_down = function (index) { |
|
179 | Notebook.prototype.move_cell_down = function (index) { | |
177 | var i = index || this.selected_index(); |
|
180 | var i = index || this.selected_index(); | |
178 | if (i !== null && i < (this.ncells()-1) && i >= 0) { |
|
181 | if (i !== null && i < (this.ncells()-1) && i >= 0) { | |
179 | var pivot = this.cell_elements().eq(i+1) |
|
182 | var pivot = this.cell_elements().eq(i+1) | |
180 | var tomove = this.cell_elements().eq(i) |
|
183 | var tomove = this.cell_elements().eq(i) | |
181 | if (pivot !== null && tomove !== null) { |
|
184 | if (pivot !== null && tomove !== null) { | |
182 | tomove.detach(); |
|
185 | tomove.detach(); | |
183 | pivot.after(tomove); |
|
186 | pivot.after(tomove); | |
184 | }; |
|
187 | }; | |
185 | }; |
|
188 | }; | |
186 | return this; |
|
189 | return this; | |
187 | } |
|
190 | } | |
188 |
|
191 | |||
189 |
|
192 | |||
190 | Notebook.prototype.sort_cells = function () { |
|
193 | Notebook.prototype.sort_cells = function () { | |
191 | var ncells = this.ncells(); |
|
194 | var ncells = this.ncells(); | |
192 | var swapped; |
|
195 | var swapped; | |
193 | do { |
|
196 | do { | |
194 | swapped = false |
|
197 | swapped = false | |
195 | for (var i=1; i<ncells; i++) { |
|
198 | for (var i=1; i<ncells; i++) { | |
196 | current = this.cell_elements().eq(i).data("cell"); |
|
199 | current = this.cell_elements().eq(i).data("cell"); | |
197 | previous = this.cell_elements().eq(i-1).data("cell"); |
|
200 | previous = this.cell_elements().eq(i-1).data("cell"); | |
198 | if (previous.input_prompt_number > current.input_prompt_number) { |
|
201 | if (previous.input_prompt_number > current.input_prompt_number) { | |
199 | this.move_cell_up(i); |
|
202 | this.move_cell_up(i); | |
200 | swapped = true; |
|
203 | swapped = true; | |
201 | }; |
|
204 | }; | |
202 | }; |
|
205 | }; | |
203 | } while (swapped); |
|
206 | } while (swapped); | |
204 | return this; |
|
207 | return this; | |
205 | }; |
|
208 | }; | |
206 |
|
209 | |||
207 |
|
210 | |||
208 | Notebook.prototype.insert_code_cell_before = function (index) { |
|
211 | Notebook.prototype.insert_code_cell_before = function (index) { | |
209 | // TODO: Bounds check for i |
|
212 | // TODO: Bounds check for i | |
210 | var i = this.index_or_selected(index); |
|
213 | var i = this.index_or_selected(index); | |
211 | var cell = new CodeCell(this); |
|
214 | var cell = new CodeCell(this); | |
212 | cell.set_input_prompt(this.next_prompt_number); |
|
215 | cell.set_input_prompt(this.next_prompt_number); | |
213 | this.next_prompt_number = this.next_prompt_number + 1; |
|
216 | this.next_prompt_number = this.next_prompt_number + 1; | |
214 | this.insert_cell_before(cell, i); |
|
217 | this.insert_cell_before(cell, i); | |
215 | this.select(this.find_cell_index(cell)); |
|
218 | this.select(this.find_cell_index(cell)); | |
216 | return this; |
|
219 | return this; | |
217 | } |
|
220 | } | |
218 |
|
221 | |||
219 |
|
222 | |||
220 | Notebook.prototype.insert_code_cell_after = function (index) { |
|
223 | Notebook.prototype.insert_code_cell_after = function (index) { | |
221 | // TODO: Bounds check for i |
|
224 | // TODO: Bounds check for i | |
222 | var i = this.index_or_selected(index); |
|
225 | var i = this.index_or_selected(index); | |
223 | var cell = new CodeCell(this); |
|
226 | var cell = new CodeCell(this); | |
224 | cell.set_input_prompt(this.next_prompt_number); |
|
227 | cell.set_input_prompt(this.next_prompt_number); | |
225 | this.next_prompt_number = this.next_prompt_number + 1; |
|
228 | this.next_prompt_number = this.next_prompt_number + 1; | |
226 | this.insert_cell_after(cell, i); |
|
229 | this.insert_cell_after(cell, i); | |
227 | this.select(this.find_cell_index(cell)); |
|
230 | this.select(this.find_cell_index(cell)); | |
228 | return this; |
|
231 | return this; | |
229 | } |
|
232 | } | |
230 |
|
233 | |||
231 |
|
234 | |||
232 | Notebook.prototype.insert_text_cell_before = function (index) { |
|
235 | Notebook.prototype.insert_text_cell_before = function (index) { | |
233 | // TODO: Bounds check for i |
|
236 | // TODO: Bounds check for i | |
234 | var i = this.index_or_selected(index); |
|
237 | var i = this.index_or_selected(index); | |
235 | var cell = new TextCell(this); |
|
238 | var cell = new TextCell(this); | |
236 | cell.config_mathjax(); |
|
239 | cell.config_mathjax(); | |
237 | this.insert_cell_before(cell, i); |
|
240 | this.insert_cell_before(cell, i); | |
238 | this.select(this.find_cell_index(cell)); |
|
241 | this.select(this.find_cell_index(cell)); | |
239 | return this; |
|
242 | return this; | |
240 | } |
|
243 | } | |
241 |
|
244 | |||
242 |
|
245 | |||
243 | Notebook.prototype.insert_text_cell_after = function (index) { |
|
246 | Notebook.prototype.insert_text_cell_after = function (index) { | |
244 | // TODO: Bounds check for i |
|
247 | // TODO: Bounds check for i | |
245 | var i = this.index_or_selected(index); |
|
248 | var i = this.index_or_selected(index); | |
246 | var cell = new TextCell(this); |
|
249 | var cell = new TextCell(this); | |
247 | cell.config_mathjax(); |
|
250 | cell.config_mathjax(); | |
248 | this.insert_cell_after(cell, i); |
|
251 | this.insert_cell_after(cell, i); | |
249 | this.select(this.find_cell_index(cell)); |
|
252 | this.select(this.find_cell_index(cell)); | |
250 | return this; |
|
253 | return this; | |
251 | } |
|
254 | } | |
252 |
|
255 | |||
253 |
|
256 | |||
254 | Notebook.prototype.text_to_code = function (index) { |
|
257 | Notebook.prototype.text_to_code = function (index) { | |
255 | // TODO: Bounds check for i |
|
258 | // TODO: Bounds check for i | |
256 | var i = this.index_or_selected(index); |
|
259 | var i = this.index_or_selected(index); | |
257 | var source_element = this.cell_elements().eq(i); |
|
260 | var source_element = this.cell_elements().eq(i); | |
258 | var source_cell = source_element.data("cell"); |
|
261 | var source_cell = source_element.data("cell"); | |
259 | if (source_cell instanceof TextCell) { |
|
262 | if (source_cell instanceof TextCell) { | |
260 | this.insert_code_cell_after(i); |
|
263 | this.insert_code_cell_after(i); | |
261 | var target_cell = this.cells()[i+1]; |
|
264 | var target_cell = this.cells()[i+1]; | |
262 | var text = source_element.find("textarea.text_cell_input").val(); |
|
265 | var text = source_element.find("textarea.text_cell_input").val(); | |
263 | target_cell.element.find("textarea.input_area").val(text); |
|
266 | target_cell.element.find("textarea.input_area").val(text); | |
264 | source_element.remove(); |
|
267 | source_element.remove(); | |
265 | }; |
|
268 | }; | |
266 | }; |
|
269 | }; | |
267 |
|
270 | |||
268 |
|
271 | |||
269 | Notebook.prototype.code_to_text = function (index) { |
|
272 | Notebook.prototype.code_to_text = function (index) { | |
270 | // TODO: Bounds check for i |
|
273 | // TODO: Bounds check for i | |
271 | var i = this.index_or_selected(index); |
|
274 | var i = this.index_or_selected(index); | |
272 | var source_element = this.cell_elements().eq(i); |
|
275 | var source_element = this.cell_elements().eq(i); | |
273 | var source_cell = source_element.data("cell"); |
|
276 | var source_cell = source_element.data("cell"); | |
274 | if (source_cell instanceof CodeCell) { |
|
277 | if (source_cell instanceof CodeCell) { | |
275 | this.insert_text_cell_after(i); |
|
278 | this.insert_text_cell_after(i); | |
276 | var target_cell = this.cells()[i+1]; |
|
279 | var target_cell = this.cells()[i+1]; | |
277 | var text = source_element.find("textarea.input_area").val(); |
|
280 | var text = source_element.find("textarea.input_area").val(); | |
278 | target_cell.element.find("textarea.text_cell_input").val(text); |
|
281 | target_cell.element.find("textarea.text_cell_input").val(text); | |
279 | target_cell.element.find("textarea.text_cell_input").html(text); |
|
282 | target_cell.element.find("textarea.text_cell_input").html(text); | |
280 | target_cell.element.find("div.text_cell_render").html(text); |
|
283 | target_cell.element.find("div.text_cell_render").html(text); | |
281 |
|
284 | |||
282 | source_element.remove(); |
|
285 | source_element.remove(); | |
283 | }; |
|
286 | }; | |
284 | }; |
|
287 | }; | |
285 |
|
288 | |||
286 |
|
289 | |||
287 | // Cell collapsing |
|
290 | // Cell collapsing | |
288 |
|
291 | |||
289 | Notebook.prototype.collapse = function (index) { |
|
292 | Notebook.prototype.collapse = function (index) { | |
290 | var i = this.index_or_selected(index); |
|
293 | var i = this.index_or_selected(index); | |
291 | this.cells()[i].collapse(); |
|
294 | this.cells()[i].collapse(); | |
292 | } |
|
295 | } | |
293 |
|
296 | |||
294 |
|
297 | |||
295 | Notebook.prototype.expand = function (index) { |
|
298 | Notebook.prototype.expand = function (index) { | |
296 | var i = this.index_or_selected(index); |
|
299 | var i = this.index_or_selected(index); | |
297 | this.cells()[i].expand(); |
|
300 | this.cells()[i].expand(); | |
298 | } |
|
301 | } | |
299 |
|
302 | |||
300 |
|
303 | |||
301 | //============================================================================ |
|
304 | //============================================================================ | |
302 | // Cell |
|
305 | // Cell | |
303 | //============================================================================ |
|
306 | //============================================================================ | |
304 |
|
307 | |||
305 |
|
308 | |||
306 | var Cell = function (notebook) { |
|
309 | var Cell = function (notebook) { | |
307 | this.notebook = notebook; |
|
310 | this.notebook = notebook; | |
308 | this.selected = false; |
|
311 | this.selected = false; | |
309 | this.element; |
|
312 | this.element; | |
310 | this.create_element(); |
|
313 | this.create_element(); | |
311 | if (this.element !== undefined) { |
|
314 | if (this.element !== undefined) { | |
312 | this.element.data("cell", this); |
|
315 | this.element.data("cell", this); | |
313 | this.bind_events(); |
|
316 | this.bind_events(); | |
314 | } |
|
317 | } | |
315 | }; |
|
318 | }; | |
316 |
|
319 | |||
317 |
|
320 | |||
318 | Cell.prototype.select = function () { |
|
321 | Cell.prototype.select = function () { | |
319 | this.element.addClass('ui-widget-content ui-corner-all'); |
|
322 | this.element.addClass('ui-widget-content ui-corner-all'); | |
320 | this.selected = true; |
|
323 | this.selected = true; | |
321 | this.element.find('textarea').trigger('focusin'); |
|
324 | this.element.find('textarea').trigger('focusin'); | |
322 | }; |
|
325 | }; | |
323 |
|
326 | |||
324 |
|
327 | |||
325 | Cell.prototype.unselect = function () { |
|
328 | Cell.prototype.unselect = function () { | |
326 | this.element.removeClass('ui-widget-content ui-corner-all'); |
|
329 | this.element.removeClass('ui-widget-content ui-corner-all'); | |
327 | this.selected = false; |
|
330 | this.selected = false; | |
328 | }; |
|
331 | }; | |
329 |
|
332 | |||
330 |
|
333 | |||
331 | Cell.prototype.bind_events = function () { |
|
334 | Cell.prototype.bind_events = function () { | |
332 | var that = this; |
|
335 | var that = this; | |
333 | var nb = that.notebook |
|
336 | var nb = that.notebook | |
334 | that.element.click(function (event) { |
|
337 | that.element.click(function (event) { | |
335 | if (that.selected === false) { |
|
338 | if (that.selected === false) { | |
336 | nb.select(nb.find_cell_index(that)); |
|
339 | nb.select(nb.find_cell_index(that)); | |
337 | }; |
|
340 | }; | |
338 | }); |
|
341 | }); | |
339 | that.element.focusin(function (event) { |
|
342 | that.element.focusin(function (event) { | |
340 | if (that.selected === false) { |
|
343 | if (that.selected === false) { | |
341 | nb.select(nb.find_cell_index(that)); |
|
344 | nb.select(nb.find_cell_index(that)); | |
342 | }; |
|
345 | }; | |
343 | }); |
|
346 | }); | |
344 | }; |
|
347 | }; | |
345 |
|
348 | |||
346 |
|
349 | |||
347 | // Subclasses must implement create_element. |
|
350 | // Subclasses must implement create_element. | |
348 | Cell.prototype.create_element = function () {}; |
|
351 | Cell.prototype.create_element = function () {}; | |
349 |
|
352 | |||
350 |
|
353 | |||
351 | //============================================================================ |
|
354 | //============================================================================ | |
352 | // CodeCell |
|
355 | // CodeCell | |
353 | //============================================================================ |
|
356 | //============================================================================ | |
354 |
|
357 | |||
355 |
|
358 | |||
356 | var CodeCell = function (notebook) { |
|
359 | var CodeCell = function (notebook) { | |
357 | Cell.apply(this, arguments); |
|
360 | Cell.apply(this, arguments); | |
358 | this.input_prompt_number = ' '; |
|
361 | this.input_prompt_number = ' '; | |
359 | this.output_prompt_number = ' '; |
|
362 | this.output_prompt_number = ' '; | |
360 | }; |
|
363 | }; | |
361 |
|
364 | |||
362 |
|
365 | |||
363 | CodeCell.prototype = new Cell(); |
|
366 | CodeCell.prototype = new Cell(); | |
364 |
|
367 | |||
365 |
|
368 | |||
366 | CodeCell.prototype.create_element = function () { |
|
369 | CodeCell.prototype.create_element = function () { | |
367 | var cell = $('<div></div>').addClass('cell code_cell') |
|
370 | var cell = $('<div></div>').addClass('cell code_cell') | |
368 | var input = $('<div></div>').addClass('input').append( |
|
371 | var input = $('<div></div>').addClass('input').append( | |
369 | $('<div/>').addClass('prompt input_prompt') |
|
372 | $('<div/>').addClass('prompt input_prompt') | |
370 | ).append( |
|
373 | ).append( | |
371 | $('<textarea/>').addClass('input_area'). |
|
374 | $('<textarea/>').addClass('input_area'). | |
372 | attr('rows',1). |
|
375 | attr('rows',1). | |
373 | attr('cols',80). |
|
376 | attr('cols',80). | |
374 | attr('wrap','hard'). |
|
377 | attr('wrap','hard'). | |
375 | autoGrow() |
|
378 | autoGrow() | |
376 | ); |
|
379 | ); | |
377 | var output = $('<div></div>').addClass('output').append( |
|
380 | var output = $('<div></div>').addClass('output').append( | |
378 | $('<div/>').addClass('prompt output_prompt') |
|
381 | $('<div/>').addClass('prompt output_prompt') | |
379 | ).append( |
|
382 | ).append( | |
380 | $('<div/>').addClass('output_area') |
|
383 | $('<div/>').addClass('output_area') | |
381 | ); |
|
384 | ); | |
382 | output.hide(); |
|
385 | output.hide(); | |
383 | cell.append(input).append(output); |
|
386 | cell.append(input).append(output); | |
384 | this.element = cell; |
|
387 | this.element = cell; | |
385 | }; |
|
388 | }; | |
386 |
|
389 | |||
387 |
|
390 | |||
388 | CodeCell.prototype.collapse = function () { |
|
391 | CodeCell.prototype.collapse = function () { | |
389 | this.element.find('div.output').hide(); |
|
392 | this.element.find('div.output').hide(); | |
390 | }; |
|
393 | }; | |
391 |
|
394 | |||
392 |
|
395 | |||
393 | CodeCell.prototype.expand = function () { |
|
396 | CodeCell.prototype.expand = function () { | |
394 | this.element.find('div.output').show(); |
|
397 | this.element.find('div.output').show(); | |
395 | }; |
|
398 | }; | |
396 |
|
399 | |||
397 |
|
400 | |||
398 | CodeCell.prototype.set_prompt = function (number) { |
|
401 | CodeCell.prototype.set_prompt = function (number) { | |
399 | this.set_input_prompt(number); |
|
402 | this.set_input_prompt(number); | |
400 | this.set_output_prompt(number); |
|
403 | this.set_output_prompt(number); | |
401 | }; |
|
404 | }; | |
402 |
|
405 | |||
403 | CodeCell.prototype.set_input_prompt = function (number) { |
|
406 | CodeCell.prototype.set_input_prompt = function (number) { | |
404 | var n = number || ' '; |
|
407 | var n = number || ' '; | |
405 | this.input_prompt_number = n |
|
408 | this.input_prompt_number = n | |
406 | this.element.find('div.input_prompt').html('In [' + n + ']:'); |
|
409 | this.element.find('div.input_prompt').html('In [' + n + ']:'); | |
407 | }; |
|
410 | }; | |
408 |
|
411 | |||
409 |
|
412 | |||
410 | CodeCell.prototype.set_output_prompt = function (number) { |
|
413 | CodeCell.prototype.set_output_prompt = function (number) { | |
411 | var n = number || ' '; |
|
414 | var n = number || ' '; | |
412 | this.output_prompt_number = n |
|
415 | this.output_prompt_number = n | |
413 | this.element.find('div.output_prompt').html('Out[' + n + ']:'); |
|
416 | this.element.find('div.output_prompt').html('Out[' + n + ']:'); | |
414 | }; |
|
417 | }; | |
415 |
|
418 | |||
416 |
|
419 | |||
417 | //============================================================================ |
|
420 | //============================================================================ | |
418 | // TextCell |
|
421 | // TextCell | |
419 | //============================================================================ |
|
422 | //============================================================================ | |
420 |
|
423 | |||
421 |
|
424 | |||
422 | var TextCell = function (notebook) { |
|
425 | var TextCell = function (notebook) { | |
423 | Cell.apply(this, arguments); |
|
426 | Cell.apply(this, arguments); | |
424 | }; |
|
427 | }; | |
425 |
|
428 | |||
426 |
|
429 | |||
427 | TextCell.prototype = new Cell(); |
|
430 | TextCell.prototype = new Cell(); | |
428 |
|
431 | |||
429 |
|
432 | |||
430 | TextCell.prototype.create_element = function () { |
|
433 | TextCell.prototype.create_element = function () { | |
431 | var cell = $('<div></div').addClass('cell text_cell'). |
|
434 | var cell = $('<div></div').addClass('cell text_cell'). | |
432 | append( |
|
435 | append( | |
433 | $('<textarea>Type HTML/LaTex content here</textarea>'). |
|
436 | $('<textarea>Type HTML/LaTex content here</textarea>'). | |
434 | addClass('text_cell_input'). |
|
437 | addClass('text_cell_input'). | |
435 | attr('rows',1). |
|
438 | attr('rows',1). | |
436 | attr('cols',80). |
|
439 | attr('cols',80). | |
437 | autoGrow() |
|
440 | autoGrow() | |
438 | ).append( |
|
441 | ).append( | |
439 | $('<div></div>').addClass('text_cell_render') |
|
442 | $('<div></div>').addClass('text_cell_render') | |
440 | ) |
|
443 | ) | |
441 | this.element = cell; |
|
444 | this.element = cell; | |
442 | }; |
|
445 | }; | |
443 |
|
446 | |||
444 |
|
447 | |||
445 | TextCell.prototype.config_mathjax = function () { |
|
448 | TextCell.prototype.config_mathjax = function () { | |
446 | var text_cell = this.element; |
|
449 | var text_cell = this.element; | |
447 | var input = text_cell.find("textarea.text_cell_input"); |
|
450 | var input = text_cell.find("textarea.text_cell_input"); | |
448 | var output = text_cell.find("div.text_cell_render"); |
|
451 | var output = text_cell.find("div.text_cell_render"); | |
449 |
|
452 | |||
450 | text_cell.click(function () { |
|
453 | text_cell.click(function () { | |
451 | output.hide(); |
|
454 | output.hide(); | |
452 | input.show().trigger('focus'); |
|
455 | input.show().trigger('focus'); | |
453 | }).focusout(function () { |
|
456 | }).focusout(function () { | |
454 | var text = input.val(); |
|
457 | var text = input.val(); | |
455 | output.html(text) |
|
458 | output.html(text) | |
456 | input.html(text); |
|
459 | input.html(text); | |
457 | MathJax.Hub.Queue(["Typeset",MathJax.Hub]); |
|
460 | MathJax.Hub.Queue(["Typeset",MathJax.Hub]); | |
458 | input.hide(); |
|
461 | input.hide(); | |
459 | output.show(); |
|
462 | output.show(); | |
460 | }); |
|
463 | }); | |
461 |
|
464 | |||
462 | text_cell.trigger("focusout"); |
|
465 | text_cell.trigger("focusout"); | |
463 | }; |
|
466 | }; | |
464 |
|
467 | |||
465 |
|
468 | |||
466 | $(document).ready(function () { |
|
469 | $(document).ready(function () { | |
467 |
|
470 | |||
468 | MathJax.Hub.Config({ |
|
471 | MathJax.Hub.Config({ | |
469 | tex2jax: { |
|
472 | tex2jax: { | |
470 | inlineMath: [ ['$','$'], ["\\(","\\)"] ], |
|
473 | inlineMath: [ ['$','$'], ["\\(","\\)"] ], | |
471 | displayMath: [ ['$$','$$'], ["\\[","\\]"] ], |
|
474 | displayMath: [ ['$$','$$'], ["\\[","\\]"] ], | |
472 | } |
|
475 | } | |
473 | }); |
|
476 | }); | |
474 |
|
477 | |||
475 | $("ul#main_menu").wijmenu({animation:{animated: "slide", duration: 100, easing: null}}); |
|
478 | $("ul#main_menu").wijmenu({animation:{animated: "slide", duration: 100, easing: null}}); | |
476 | IPYTHON.notebook = new Notebook('div.notebook'); |
|
479 | IPYTHON.notebook = new Notebook('div.notebook'); | |
477 | IPYTHON.notebook.insert_code_cell_after(); |
|
480 | IPYTHON.notebook.insert_code_cell_after(); | |
478 |
|
481 | |||
479 | $("#move_cell").buttonset(); |
|
482 | $("#move_cell").buttonset(); | |
480 | $("#move_up").button("option", "icons", {primary:"ui-icon-arrowthick-1-n"}); |
|
483 | $("#move_up").button("option", "icons", {primary:"ui-icon-arrowthick-1-n"}); | |
481 | $("#move_up").button("option", "text", false); |
|
484 | $("#move_up").button("option", "text", false); | |
482 | $("#move_up").click(function () {IPYTHON.notebook.move_cell_up();}); |
|
485 | $("#move_up").click(function () {IPYTHON.notebook.move_cell_up();}); | |
483 | $("#move_down").button("option", "icons", {primary:"ui-icon-arrowthick-1-s"}); |
|
486 | $("#move_down").button("option", "icons", {primary:"ui-icon-arrowthick-1-s"}); | |
484 | $("#move_down").button("option", "text", false); |
|
487 | $("#move_down").button("option", "text", false); | |
485 | $("#move_down").click(function () {IPYTHON.notebook.move_cell_down();}); |
|
488 | $("#move_down").click(function () {IPYTHON.notebook.move_cell_down();}); | |
486 |
|
489 | |||
487 | $("#insert_delete").buttonset(); |
|
490 | $("#insert_delete").buttonset(); | |
488 | $("#insert_cell_before").click(function () {IPYTHON.notebook.insert_code_cell_before();}); |
|
491 | $("#insert_cell_before").click(function () {IPYTHON.notebook.insert_code_cell_before();}); | |
489 | $("#insert_cell_after").click(function () {IPYTHON.notebook.insert_code_cell_after();}); |
|
492 | $("#insert_cell_after").click(function () {IPYTHON.notebook.insert_code_cell_after();}); | |
490 | $("#delete_cell").button("option", "icons", {primary:"ui-icon-closethick"}); |
|
493 | $("#delete_cell").button("option", "icons", {primary:"ui-icon-closethick"}); | |
491 | $("#delete_cell").button("option", "text", false); |
|
494 | $("#delete_cell").button("option", "text", false); | |
492 | $("#delete_cell").click(function () {IPYTHON.notebook.delete_cell();}); |
|
495 | $("#delete_cell").click(function () {IPYTHON.notebook.delete_cell();}); | |
493 |
|
496 | |||
494 | $("#cell_type").buttonset(); |
|
497 | $("#cell_type").buttonset(); | |
495 | $("#to_code").click(function () {IPYTHON.notebook.text_to_code();}); |
|
498 | $("#to_code").click(function () {IPYTHON.notebook.text_to_code();}); | |
496 | $("#to_text").click(function () {IPYTHON.notebook.code_to_text();}); |
|
499 | $("#to_text").click(function () {IPYTHON.notebook.code_to_text();}); | |
497 |
|
500 | |||
498 | $("#sort").buttonset(); |
|
501 | $("#sort").buttonset(); | |
499 | $("#sort_cells").click(function () {IPYTHON.notebook.sort_cells();}); |
|
502 | $("#sort_cells").click(function () {IPYTHON.notebook.sort_cells();}); | |
500 |
|
503 | |||
501 | }); No newline at end of file |
|
504 | }); |
General Comments 0
You need to be logged in to leave comments.
Login now