Show More
@@ -1,314 +1,327 | |||||
1 | //---------------------------------------------------------------------------- |
|
1 | //---------------------------------------------------------------------------- | |
2 | // Copyright (C) 2008-2011 The IPython Development Team |
|
2 | // Copyright (C) 2008-2011 The IPython Development Team | |
3 | // |
|
3 | // | |
4 | // Distributed under the terms of the BSD License. The full license is in |
|
4 | // Distributed under the terms of the BSD License. The full license is in | |
5 | // the file COPYING, distributed as part of this software. |
|
5 | // the file COPYING, distributed as part of this software. | |
6 | //---------------------------------------------------------------------------- |
|
6 | //---------------------------------------------------------------------------- | |
7 |
|
7 | |||
8 | //============================================================================ |
|
8 | //============================================================================ | |
9 | // Cell |
|
9 | // Cell | |
10 | //============================================================================ |
|
10 | //============================================================================ | |
11 | /** |
|
11 | /** | |
12 | * An extendable module that provide base functionnality to create cell for notebook. |
|
12 | * An extendable module that provide base functionnality to create cell for notebook. | |
13 | * @module IPython |
|
13 | * @module IPython | |
14 | * @namespace IPython |
|
14 | * @namespace IPython | |
15 | * @submodule Cell |
|
15 | * @submodule Cell | |
16 | */ |
|
16 | */ | |
17 |
|
17 | |||
18 | var IPython = (function (IPython) { |
|
18 | var IPython = (function (IPython) { | |
19 |
|
19 | |||
20 | var utils = IPython.utils; |
|
20 | var utils = IPython.utils; | |
21 |
|
21 | |||
22 | /** |
|
22 | /** | |
23 | * The Base `Cell` class from which to inherit |
|
23 | * The Base `Cell` class from which to inherit | |
24 | * @class Cell |
|
24 | * @class Cell | |
25 | */ |
|
25 | **/ | |
26 |
|
26 | |||
27 | /* |
|
27 | /* | |
28 | * @constructor |
|
28 | * @constructor | |
|
29 | * | |||
|
30 | * @param {object|undefined} [options] | |||
|
31 | * @param [options.cm_config] {object} config to pass to CodeMirror, will extend default parameters | |||
29 | */ |
|
32 | */ | |
30 | var Cell = function () { |
|
33 | var Cell = function (options) { | |
|
34 | ||||
|
35 | options = options || {}; | |||
|
36 | // superclass default overwrite our default | |||
|
37 | this.cm_config = $.extend({},Cell.cm_default,options.cm_config); | |||
|
38 | ||||
31 | this.placeholder = this.placeholder || ''; |
|
39 | this.placeholder = this.placeholder || ''; | |
32 | this.read_only = false; |
|
40 | this.read_only = false; | |
33 | this.selected = false; |
|
41 | this.selected = false; | |
34 | this.element = null; |
|
42 | this.element = null; | |
35 | this.metadata = {}; |
|
43 | this.metadata = {}; | |
36 | // load this from metadata later ? |
|
44 | // load this from metadata later ? | |
37 | this.user_highlight = 'auto'; |
|
45 | this.user_highlight = 'auto'; | |
38 | this.create_element(); |
|
46 | this.create_element(); | |
39 | if (this.element !== null) { |
|
47 | if (this.element !== null) { | |
40 | this.element.data("cell", this); |
|
48 | this.element.data("cell", this); | |
41 | this.bind_events(); |
|
49 | this.bind_events(); | |
42 | } |
|
50 | } | |
43 | this.cell_id = utils.uuid(); |
|
51 | this.cell_id = utils.uuid(); | |
44 | }; |
|
52 | }; | |
45 |
|
53 | |||
|
54 | Cell.cm_default = { | |||
|
55 | indentUnit : 4, | |||
|
56 | readOnly: this.read_only, | |||
|
57 | }; | |||
|
58 | ||||
46 |
|
59 | |||
47 | /** |
|
60 | /** | |
48 | * Empty. Subclasses must implement create_element. |
|
61 | * Empty. Subclasses must implement create_element. | |
49 | * This should contain all the code to create the DOM element in notebook |
|
62 | * This should contain all the code to create the DOM element in notebook | |
50 | * and will be called by Base Class constructor. |
|
63 | * and will be called by Base Class constructor. | |
51 | * @method create_element |
|
64 | * @method create_element | |
52 | */ |
|
65 | */ | |
53 | Cell.prototype.create_element = function () { |
|
66 | Cell.prototype.create_element = function () { | |
54 | }; |
|
67 | }; | |
55 |
|
68 | |||
56 |
|
69 | |||
57 | /** |
|
70 | /** | |
58 | * Subclasses can implement override bind_events. |
|
71 | * Subclasses can implement override bind_events. | |
59 | * Be carefull to call the parent method when overwriting as it fires event. |
|
72 | * Be carefull to call the parent method when overwriting as it fires event. | |
60 | * this will be triggerd after create_element in constructor. |
|
73 | * this will be triggerd after create_element in constructor. | |
61 | * @method bind_events |
|
74 | * @method bind_events | |
62 | */ |
|
75 | */ | |
63 | Cell.prototype.bind_events = function () { |
|
76 | Cell.prototype.bind_events = function () { | |
64 | var that = this; |
|
77 | var that = this; | |
65 | // We trigger events so that Cell doesn't have to depend on Notebook. |
|
78 | // We trigger events so that Cell doesn't have to depend on Notebook. | |
66 | that.element.click(function (event) { |
|
79 | that.element.click(function (event) { | |
67 | if (that.selected === false) { |
|
80 | if (that.selected === false) { | |
68 | $([IPython.events]).trigger('select.Cell', {'cell':that}); |
|
81 | $([IPython.events]).trigger('select.Cell', {'cell':that}); | |
69 | } |
|
82 | } | |
70 | }); |
|
83 | }); | |
71 | that.element.focusin(function (event) { |
|
84 | that.element.focusin(function (event) { | |
72 | if (that.selected === false) { |
|
85 | if (that.selected === false) { | |
73 | $([IPython.events]).trigger('select.Cell', {'cell':that}); |
|
86 | $([IPython.events]).trigger('select.Cell', {'cell':that}); | |
74 | } |
|
87 | } | |
75 | }); |
|
88 | }); | |
76 | }; |
|
89 | }; | |
77 |
|
90 | |||
78 | /** |
|
91 | /** | |
79 | * Triger typsetting of math by mathjax on current cell element |
|
92 | * Triger typsetting of math by mathjax on current cell element | |
80 | * @method typeset |
|
93 | * @method typeset | |
81 | */ |
|
94 | */ | |
82 | Cell.prototype.typeset = function () { |
|
95 | Cell.prototype.typeset = function () { | |
83 | if (window.MathJax){ |
|
96 | if (window.MathJax){ | |
84 | var cell_math = this.element.get(0); |
|
97 | var cell_math = this.element.get(0); | |
85 | MathJax.Hub.Queue(["Typeset",MathJax.Hub,cell_math]); |
|
98 | MathJax.Hub.Queue(["Typeset",MathJax.Hub,cell_math]); | |
86 | } |
|
99 | } | |
87 | }; |
|
100 | }; | |
88 |
|
101 | |||
89 | /** |
|
102 | /** | |
90 | * should be triggerd when cell is selected |
|
103 | * should be triggerd when cell is selected | |
91 | * @method select |
|
104 | * @method select | |
92 | */ |
|
105 | */ | |
93 | Cell.prototype.select = function () { |
|
106 | Cell.prototype.select = function () { | |
94 | this.element.addClass('selected'); |
|
107 | this.element.addClass('selected'); | |
95 | this.selected = true; |
|
108 | this.selected = true; | |
96 | }; |
|
109 | }; | |
97 |
|
110 | |||
98 |
|
111 | |||
99 | /** |
|
112 | /** | |
100 | * should be triggerd when cell is unselected |
|
113 | * should be triggerd when cell is unselected | |
101 | * @method unselect |
|
114 | * @method unselect | |
102 | */ |
|
115 | */ | |
103 | Cell.prototype.unselect = function () { |
|
116 | Cell.prototype.unselect = function () { | |
104 | this.element.removeClass('selected'); |
|
117 | this.element.removeClass('selected'); | |
105 | this.selected = false; |
|
118 | this.selected = false; | |
106 | }; |
|
119 | }; | |
107 |
|
120 | |||
108 | /** |
|
121 | /** | |
109 | * should be overritten by subclass |
|
122 | * should be overritten by subclass | |
110 | * @method get_text |
|
123 | * @method get_text | |
111 | */ |
|
124 | */ | |
112 | Cell.prototype.get_text = function () { |
|
125 | Cell.prototype.get_text = function () { | |
113 | }; |
|
126 | }; | |
114 |
|
127 | |||
115 | /** |
|
128 | /** | |
116 | * should be overritten by subclass |
|
129 | * should be overritten by subclass | |
117 | * @method set_text |
|
130 | * @method set_text | |
118 | * @param {string} text |
|
131 | * @param {string} text | |
119 | */ |
|
132 | */ | |
120 | Cell.prototype.set_text = function (text) { |
|
133 | Cell.prototype.set_text = function (text) { | |
121 | }; |
|
134 | }; | |
122 |
|
135 | |||
123 | /** |
|
136 | /** | |
124 | * Refresh codemirror instance |
|
137 | * Refresh codemirror instance | |
125 | * @method refresh |
|
138 | * @method refresh | |
126 | */ |
|
139 | */ | |
127 | Cell.prototype.refresh = function () { |
|
140 | Cell.prototype.refresh = function () { | |
128 | this.code_mirror.refresh(); |
|
141 | this.code_mirror.refresh(); | |
129 | }; |
|
142 | }; | |
130 |
|
143 | |||
131 |
|
144 | |||
132 | /** |
|
145 | /** | |
133 | * should be overritten by subclass |
|
146 | * should be overritten by subclass | |
134 | * @method edit |
|
147 | * @method edit | |
135 | **/ |
|
148 | **/ | |
136 | Cell.prototype.edit = function () { |
|
149 | Cell.prototype.edit = function () { | |
137 | }; |
|
150 | }; | |
138 |
|
151 | |||
139 |
|
152 | |||
140 | /** |
|
153 | /** | |
141 | * should be overritten by subclass |
|
154 | * should be overritten by subclass | |
142 | * @method render |
|
155 | * @method render | |
143 | **/ |
|
156 | **/ | |
144 | Cell.prototype.render = function () { |
|
157 | Cell.prototype.render = function () { | |
145 | }; |
|
158 | }; | |
146 |
|
159 | |||
147 | /** |
|
160 | /** | |
148 | * should be overritten by subclass |
|
161 | * should be overritten by subclass | |
149 | * serialise cell to json. |
|
162 | * serialise cell to json. | |
150 | * @method toJSON |
|
163 | * @method toJSON | |
151 | **/ |
|
164 | **/ | |
152 | Cell.prototype.toJSON = function () { |
|
165 | Cell.prototype.toJSON = function () { | |
153 | var data = {}; |
|
166 | var data = {}; | |
154 | data.metadata = this.metadata; |
|
167 | data.metadata = this.metadata; | |
155 | return data; |
|
168 | return data; | |
156 | }; |
|
169 | }; | |
157 |
|
170 | |||
158 |
|
171 | |||
159 | /** |
|
172 | /** | |
160 | * should be overritten by subclass |
|
173 | * should be overritten by subclass | |
161 | * @method fromJSON |
|
174 | * @method fromJSON | |
162 | **/ |
|
175 | **/ | |
163 | Cell.prototype.fromJSON = function (data) { |
|
176 | Cell.prototype.fromJSON = function (data) { | |
164 | if (data.metadata !== undefined) { |
|
177 | if (data.metadata !== undefined) { | |
165 | this.metadata = data.metadata; |
|
178 | this.metadata = data.metadata; | |
166 | } |
|
179 | } | |
167 | this.celltoolbar.rebuild(); |
|
180 | this.celltoolbar.rebuild(); | |
168 | }; |
|
181 | }; | |
169 |
|
182 | |||
170 |
|
183 | |||
171 | /** |
|
184 | /** | |
172 | * can the cell be splitted in 2 cells. |
|
185 | * can the cell be splitted in 2 cells. | |
173 | * @method is_splittable |
|
186 | * @method is_splittable | |
174 | **/ |
|
187 | **/ | |
175 | Cell.prototype.is_splittable = function () { |
|
188 | Cell.prototype.is_splittable = function () { | |
176 | return true; |
|
189 | return true; | |
177 | }; |
|
190 | }; | |
178 |
|
191 | |||
179 |
|
192 | |||
180 | /** |
|
193 | /** | |
181 | * @return {String} - the text before the cursor |
|
194 | * @return {String} - the text before the cursor | |
182 | * @method get_pre_cursor |
|
195 | * @method get_pre_cursor | |
183 | **/ |
|
196 | **/ | |
184 | Cell.prototype.get_pre_cursor = function () { |
|
197 | Cell.prototype.get_pre_cursor = function () { | |
185 | var cursor = this.code_mirror.getCursor(); |
|
198 | var cursor = this.code_mirror.getCursor(); | |
186 | var text = this.code_mirror.getRange({line:0,ch:0}, cursor); |
|
199 | var text = this.code_mirror.getRange({line:0,ch:0}, cursor); | |
187 | text = text.replace(/^\n+/, '').replace(/\n+$/, ''); |
|
200 | text = text.replace(/^\n+/, '').replace(/\n+$/, ''); | |
188 | return text; |
|
201 | return text; | |
189 | } |
|
202 | } | |
190 |
|
203 | |||
191 |
|
204 | |||
192 | /** |
|
205 | /** | |
193 | * @return {String} - the text after the cursor |
|
206 | * @return {String} - the text after the cursor | |
194 | * @method get_post_cursor |
|
207 | * @method get_post_cursor | |
195 | **/ |
|
208 | **/ | |
196 | Cell.prototype.get_post_cursor = function () { |
|
209 | Cell.prototype.get_post_cursor = function () { | |
197 | var cursor = this.code_mirror.getCursor(); |
|
210 | var cursor = this.code_mirror.getCursor(); | |
198 | var last_line_num = this.code_mirror.lineCount()-1; |
|
211 | var last_line_num = this.code_mirror.lineCount()-1; | |
199 | var last_line_len = this.code_mirror.getLine(last_line_num).length; |
|
212 | var last_line_len = this.code_mirror.getLine(last_line_num).length; | |
200 | var end = {line:last_line_num, ch:last_line_len} |
|
213 | var end = {line:last_line_num, ch:last_line_len} | |
201 | var text = this.code_mirror.getRange(cursor, end); |
|
214 | var text = this.code_mirror.getRange(cursor, end); | |
202 | text = text.replace(/^\n+/, '').replace(/\n+$/, ''); |
|
215 | text = text.replace(/^\n+/, '').replace(/\n+$/, ''); | |
203 | return text; |
|
216 | return text; | |
204 | }; |
|
217 | }; | |
205 |
|
218 | |||
206 |
|
219 | |||
207 | /** Grow the cell by hand. This is used upon reloading from JSON, when the |
|
220 | /** Grow the cell by hand. This is used upon reloading from JSON, when the | |
208 | * autogrow handler is not called. |
|
221 | * autogrow handler is not called. | |
209 | * |
|
222 | * | |
210 | * could be made static |
|
223 | * could be made static | |
211 | * |
|
224 | * | |
212 | * @param {Dom element} - element |
|
225 | * @param {Dom element} - element | |
213 | * @method grow |
|
226 | * @method grow | |
214 | **/ |
|
227 | **/ | |
215 | Cell.prototype.grow = function(element) { |
|
228 | Cell.prototype.grow = function(element) { | |
216 | var dom = element.get(0); |
|
229 | var dom = element.get(0); | |
217 | var lines_count = 0; |
|
230 | var lines_count = 0; | |
218 | // modified split rule from |
|
231 | // modified split rule from | |
219 | // http://stackoverflow.com/questions/2035910/how-to-get-the-number-of-lines-in-a-textarea/2036424#2036424 |
|
232 | // http://stackoverflow.com/questions/2035910/how-to-get-the-number-of-lines-in-a-textarea/2036424#2036424 | |
220 | var lines = dom.value.split(/\r|\r\n|\n/); |
|
233 | var lines = dom.value.split(/\r|\r\n|\n/); | |
221 | lines_count = lines.length; |
|
234 | lines_count = lines.length; | |
222 | if (lines_count >= 1) { |
|
235 | if (lines_count >= 1) { | |
223 | dom.rows = lines_count; |
|
236 | dom.rows = lines_count; | |
224 | } else { |
|
237 | } else { | |
225 | dom.rows = 1; |
|
238 | dom.rows = 1; | |
226 | } |
|
239 | } | |
227 | }; |
|
240 | }; | |
228 |
|
241 | |||
229 | /** |
|
242 | /** | |
230 | * Toggle CodeMirror LineNumber |
|
243 | * Toggle CodeMirror LineNumber | |
231 | * @method toggle_line_numbers |
|
244 | * @method toggle_line_numbers | |
232 | **/ |
|
245 | **/ | |
233 | Cell.prototype.toggle_line_numbers = function () { |
|
246 | Cell.prototype.toggle_line_numbers = function () { | |
234 | if (this.code_mirror.getOption('lineNumbers') == false) { |
|
247 | if (this.code_mirror.getOption('lineNumbers') == false) { | |
235 | this.code_mirror.setOption('lineNumbers', true); |
|
248 | this.code_mirror.setOption('lineNumbers', true); | |
236 | } else { |
|
249 | } else { | |
237 | this.code_mirror.setOption('lineNumbers', false); |
|
250 | this.code_mirror.setOption('lineNumbers', false); | |
238 | } |
|
251 | } | |
239 | this.code_mirror.refresh(); |
|
252 | this.code_mirror.refresh(); | |
240 | }; |
|
253 | }; | |
241 |
|
254 | |||
242 | /** |
|
255 | /** | |
243 |
* |
|
256 | * Force codemirror highlight mode | |
244 | * @method force_highlight |
|
257 | * @method force_highlight | |
245 | * @param {object} - CodeMirror mode |
|
258 | * @param {object} - CodeMirror mode | |
246 | **/ |
|
259 | **/ | |
247 | Cell.prototype.force_highlight = function(mode) { |
|
260 | Cell.prototype.force_highlight = function(mode) { | |
248 | this.user_highlight = mode; |
|
261 | this.user_highlight = mode; | |
249 | this.auto_highlight(); |
|
262 | this.auto_highlight(); | |
250 | }; |
|
263 | }; | |
251 |
|
264 | |||
252 | /** |
|
265 | /** | |
253 | * Try to autodetect cell highlight mode, or use selected mode |
|
266 | * Try to autodetect cell highlight mode, or use selected mode | |
254 | * @methods _auto_highlight |
|
267 | * @methods _auto_highlight | |
255 | * @private |
|
268 | * @private | |
256 | * @param {String|object|undefined} - CodeMirror mode | 'auto' |
|
269 | * @param {String|object|undefined} - CodeMirror mode | 'auto' | |
257 | **/ |
|
270 | **/ | |
258 | Cell.prototype._auto_highlight = function (modes) { |
|
271 | Cell.prototype._auto_highlight = function (modes) { | |
259 | //Here we handle manually selected modes |
|
272 | //Here we handle manually selected modes | |
260 | if( this.user_highlight != undefined && this.user_highlight != 'auto' ) |
|
273 | if( this.user_highlight != undefined && this.user_highlight != 'auto' ) | |
261 | { |
|
274 | { | |
262 | var mode = this.user_highlight; |
|
275 | var mode = this.user_highlight; | |
263 | CodeMirror.autoLoadMode(this.code_mirror, mode); |
|
276 | CodeMirror.autoLoadMode(this.code_mirror, mode); | |
264 | this.code_mirror.setOption('mode', mode); |
|
277 | this.code_mirror.setOption('mode', mode); | |
265 | return; |
|
278 | return; | |
266 | } |
|
279 | } | |
267 | var first_line = this.code_mirror.getLine(0); |
|
280 | var first_line = this.code_mirror.getLine(0); | |
268 | // loop on every pairs |
|
281 | // loop on every pairs | |
269 | for( var mode in modes) { |
|
282 | for( var mode in modes) { | |
270 | var regs = modes[mode]['reg']; |
|
283 | var regs = modes[mode]['reg']; | |
271 | // only one key every time but regexp can't be keys... |
|
284 | // only one key every time but regexp can't be keys... | |
272 | for(var reg in regs ) { |
|
285 | for(var reg in regs ) { | |
273 | // here we handle non magic_modes |
|
286 | // here we handle non magic_modes | |
274 | if(first_line.match(regs[reg]) != null) { |
|
287 | if(first_line.match(regs[reg]) != null) { | |
275 | if (mode.search('magic_') != 0) { |
|
288 | if (mode.search('magic_') != 0) { | |
276 | this.code_mirror.setOption('mode',mode); |
|
289 | this.code_mirror.setOption('mode',mode); | |
277 | CodeMirror.autoLoadMode(this.code_mirror, mode); |
|
290 | CodeMirror.autoLoadMode(this.code_mirror, mode); | |
278 | return; |
|
291 | return; | |
279 | } |
|
292 | } | |
280 | var open = modes[mode]['open']|| "%%"; |
|
293 | var open = modes[mode]['open']|| "%%"; | |
281 | var close = modes[mode]['close']|| "%%end"; |
|
294 | var close = modes[mode]['close']|| "%%end"; | |
282 | var mmode = mode; |
|
295 | var mmode = mode; | |
283 | mode = mmode.substr(6); |
|
296 | mode = mmode.substr(6); | |
284 | CodeMirror.autoLoadMode(this.code_mirror, mode); |
|
297 | CodeMirror.autoLoadMode(this.code_mirror, mode); | |
285 | // create on the fly a mode that swhitch between |
|
298 | // create on the fly a mode that swhitch between | |
286 | // plain/text and smth else otherwise `%%` is |
|
299 | // plain/text and smth else otherwise `%%` is | |
287 | // source of some highlight issues. |
|
300 | // source of some highlight issues. | |
288 | // we use patchedGetMode to circumvent a bug in CM |
|
301 | // we use patchedGetMode to circumvent a bug in CM | |
289 | CodeMirror.defineMode(mmode , function(config) { |
|
302 | CodeMirror.defineMode(mmode , function(config) { | |
290 | return CodeMirror.multiplexingMode( |
|
303 | return CodeMirror.multiplexingMode( | |
291 | CodeMirror.patchedGetMode(config, 'text/plain'), |
|
304 | CodeMirror.patchedGetMode(config, 'text/plain'), | |
292 | // always set someting on close |
|
305 | // always set someting on close | |
293 | {open: open, close: close, |
|
306 | {open: open, close: close, | |
294 | mode: CodeMirror.patchedGetMode(config, mode), |
|
307 | mode: CodeMirror.patchedGetMode(config, mode), | |
295 | delimStyle: "delimit" |
|
308 | delimStyle: "delimit" | |
296 | } |
|
309 | } | |
297 | ); |
|
310 | ); | |
298 | }); |
|
311 | }); | |
299 | this.code_mirror.setOption('mode', mmode); |
|
312 | this.code_mirror.setOption('mode', mmode); | |
300 | return; |
|
313 | return; | |
301 | } |
|
314 | } | |
302 | } |
|
315 | } | |
303 | } |
|
316 | } | |
304 | // fallback on default (python) |
|
317 | // fallback on default (python) | |
305 | var default_mode = this.default_mode || 'text/plain'; |
|
318 | var default_mode = this.default_mode || 'text/plain'; | |
306 | this.code_mirror.setOption('mode', default_mode); |
|
319 | this.code_mirror.setOption('mode', default_mode); | |
307 | }; |
|
320 | }; | |
308 |
|
321 | |||
309 | IPython.Cell = Cell; |
|
322 | IPython.Cell = Cell; | |
310 |
|
323 | |||
311 | return IPython; |
|
324 | return IPython; | |
312 |
|
325 | |||
313 | }(IPython)); |
|
326 | }(IPython)); | |
314 |
|
327 |
@@ -1,412 +1,427 | |||||
1 | //---------------------------------------------------------------------------- |
|
1 | //---------------------------------------------------------------------------- | |
2 | // Copyright (C) 2008-2011 The IPython Development Team |
|
2 | // Copyright (C) 2008-2011 The IPython Development Team | |
3 | // |
|
3 | // | |
4 | // Distributed under the terms of the BSD License. The full license is in |
|
4 | // Distributed under the terms of the BSD License. The full license is in | |
5 | // the file COPYING, distributed as part of this software. |
|
5 | // the file COPYING, distributed as part of this software. | |
6 | //---------------------------------------------------------------------------- |
|
6 | //---------------------------------------------------------------------------- | |
7 |
|
7 | |||
8 | //============================================================================ |
|
8 | //============================================================================ | |
9 | // CodeCell |
|
9 | // CodeCell | |
10 | //============================================================================ |
|
10 | //============================================================================ | |
11 | /** |
|
11 | /** | |
12 | * An extendable module that provide base functionnality to create cell for notebook. |
|
12 | * An extendable module that provide base functionnality to create cell for notebook. | |
13 | * @module IPython |
|
13 | * @module IPython | |
14 | * @namespace IPython |
|
14 | * @namespace IPython | |
15 | * @submodule CodeCell |
|
15 | * @submodule CodeCell | |
16 | */ |
|
16 | */ | |
17 |
|
17 | |||
18 |
|
18 | |||
19 | /* local util for codemirror */ |
|
19 | /* local util for codemirror */ | |
20 | var posEq = function(a, b) {return a.line == b.line && a.ch == b.ch;} |
|
20 | var posEq = function(a, b) {return a.line == b.line && a.ch == b.ch;} | |
21 |
|
21 | |||
22 | /** |
|
22 | /** | |
23 | * |
|
23 | * | |
24 | * function to delete until previous non blanking space character |
|
24 | * function to delete until previous non blanking space character | |
25 | * or first multiple of 4 tabstop. |
|
25 | * or first multiple of 4 tabstop. | |
26 | * @private |
|
26 | * @private | |
27 | */ |
|
27 | */ | |
28 | CodeMirror.commands.delSpaceToPrevTabStop = function(cm){ |
|
28 | CodeMirror.commands.delSpaceToPrevTabStop = function(cm){ | |
29 | var from = cm.getCursor(true), to = cm.getCursor(false), sel = !posEq(from, to); |
|
29 | var from = cm.getCursor(true), to = cm.getCursor(false), sel = !posEq(from, to); | |
30 | if (!posEq(from, to)) {cm.replaceRange("", from, to); return} |
|
30 | if (!posEq(from, to)) {cm.replaceRange("", from, to); return} | |
31 | var cur = cm.getCursor(), line = cm.getLine(cur.line); |
|
31 | var cur = cm.getCursor(), line = cm.getLine(cur.line); | |
32 | var tabsize = cm.getOption('tabSize'); |
|
32 | var tabsize = cm.getOption('tabSize'); | |
33 | var chToPrevTabStop = cur.ch-(Math.ceil(cur.ch/tabsize)-1)*tabsize; |
|
33 | var chToPrevTabStop = cur.ch-(Math.ceil(cur.ch/tabsize)-1)*tabsize; | |
34 | var from = {ch:cur.ch-chToPrevTabStop,line:cur.line} |
|
34 | var from = {ch:cur.ch-chToPrevTabStop,line:cur.line} | |
35 | var select = cm.getRange(from,cur) |
|
35 | var select = cm.getRange(from,cur) | |
36 | if( select.match(/^\ +$/) != null){ |
|
36 | if( select.match(/^\ +$/) != null){ | |
37 | cm.replaceRange("",from,cur) |
|
37 | cm.replaceRange("",from,cur) | |
38 | } else { |
|
38 | } else { | |
39 | cm.deleteH(-1,"char") |
|
39 | cm.deleteH(-1,"char") | |
40 | } |
|
40 | } | |
41 | }; |
|
41 | }; | |
42 |
|
42 | |||
43 |
|
43 | |||
44 | var IPython = (function (IPython) { |
|
44 | var IPython = (function (IPython) { | |
45 | "use strict"; |
|
45 | "use strict"; | |
46 |
|
46 | |||
47 | var utils = IPython.utils; |
|
47 | var utils = IPython.utils; | |
48 | var key = IPython.utils.keycodes; |
|
48 | var key = IPython.utils.keycodes; | |
49 | CodeMirror.modeURL = "/static/codemirror/mode/%N/%N.js"; |
|
49 | CodeMirror.modeURL = "/static/codemirror/mode/%N/%N.js"; | |
50 |
|
50 | |||
51 | /** |
|
51 | /** | |
52 | * A Cell conceived to write code. |
|
52 | * A Cell conceived to write code. | |
53 | * |
|
53 | * | |
54 | * The kernel doesn't have to be set at creation time, in that case |
|
54 | * The kernel doesn't have to be set at creation time, in that case | |
55 | * it will be null and set_kernel has to be called later. |
|
55 | * it will be null and set_kernel has to be called later. | |
56 | * @class CodeCell |
|
56 | * @class CodeCell | |
57 | * @extends IPython.Cell |
|
57 | * @extends IPython.Cell | |
58 | * |
|
58 | * | |
59 | * @constructor |
|
59 | * @constructor | |
60 | * @param {Object|null} kernel |
|
60 | * @param {Object|null} kernel | |
|
61 | * @param {object|undefined} [options] | |||
|
62 | * @param [options.cm_config] {object} config to pass to CodeMirror | |||
61 | */ |
|
63 | */ | |
62 | var CodeCell = function (kernel) { |
|
64 | var CodeCell = function (kernel, options) { | |
|
65 | var options = options || {} | |||
63 | this.kernel = kernel || null; |
|
66 | this.kernel = kernel || null; | |
64 | this.code_mirror = null; |
|
67 | this.code_mirror = null; | |
65 | this.input_prompt_number = null; |
|
68 | this.input_prompt_number = null; | |
66 | this.collapsed = false; |
|
69 | this.collapsed = false; | |
67 | this.default_mode = 'python'; |
|
70 | this.default_mode = 'python'; | |
68 | IPython.Cell.apply(this, arguments); |
|
71 | ||
|
72 | ||||
|
73 | var cm_overwrite_options = { | |||
|
74 | extraKeys: {"Tab": "indentMore","Shift-Tab" : "indentLess",'Backspace':"delSpaceToPrevTabStop"}, | |||
|
75 | onKeyEvent: $.proxy(this.handle_codemirror_keyevent,this) | |||
|
76 | }; | |||
|
77 | ||||
|
78 | var arg_cm_options = options.cm_options || {}; | |||
|
79 | var cm_config = $.extend({},CodeCell.cm_default, arg_cm_options, cm_overwrite_options); | |||
|
80 | ||||
|
81 | var options = {}; | |||
|
82 | options.cm_config = cm_config; | |||
|
83 | ||||
|
84 | IPython.Cell.apply(this,[options]); | |||
69 |
|
85 | |||
70 | var that = this; |
|
86 | var that = this; | |
71 | this.element.focusout( |
|
87 | this.element.focusout( | |
72 | function() { that.auto_highlight(); } |
|
88 | function() { that.auto_highlight(); } | |
73 | ); |
|
89 | ); | |
74 | }; |
|
90 | }; | |
75 |
|
91 | |||
|
92 | CodeCell.cm_default = { | |||
|
93 | mode: 'python', | |||
|
94 | theme: 'ipython', | |||
|
95 | matchBrackets: true | |||
|
96 | }; | |||
|
97 | ||||
|
98 | ||||
76 | CodeCell.prototype = new IPython.Cell(); |
|
99 | CodeCell.prototype = new IPython.Cell(); | |
77 |
|
100 | |||
78 | /** |
|
101 | /** | |
79 | * @method auto_highlight |
|
102 | * @method auto_highlight | |
80 | */ |
|
103 | */ | |
81 | CodeCell.prototype.auto_highlight = function () { |
|
104 | CodeCell.prototype.auto_highlight = function () { | |
82 | this._auto_highlight(IPython.config.cell_magic_highlight) |
|
105 | this._auto_highlight(IPython.config.cell_magic_highlight) | |
83 | }; |
|
106 | }; | |
84 |
|
107 | |||
85 | /** @method create_element */ |
|
108 | /** @method create_element */ | |
86 | CodeCell.prototype.create_element = function () { |
|
109 | CodeCell.prototype.create_element = function () { | |
87 | IPython.Cell.prototype.create_element.apply(this, arguments); |
|
110 | IPython.Cell.prototype.create_element.apply(this, arguments); | |
88 |
|
111 | |||
89 | var cell = $('<div></div>').addClass('cell border-box-sizing code_cell vbox'); |
|
112 | var cell = $('<div></div>').addClass('cell border-box-sizing code_cell vbox'); | |
90 | cell.attr('tabindex','2'); |
|
113 | cell.attr('tabindex','2'); | |
91 |
|
114 | |||
92 | this.celltoolbar = new IPython.CellToolbar(this); |
|
115 | this.celltoolbar = new IPython.CellToolbar(this); | |
93 |
|
116 | |||
94 | var input = $('<div></div>').addClass('input hbox'); |
|
117 | var input = $('<div></div>').addClass('input hbox'); | |
95 | var vbox = $('<div/>').addClass('vbox box-flex1') |
|
118 | var vbox = $('<div/>').addClass('vbox box-flex1') | |
96 | input.append($('<div/>').addClass('prompt input_prompt')); |
|
119 | input.append($('<div/>').addClass('prompt input_prompt')); | |
97 | vbox.append(this.celltoolbar.element); |
|
120 | vbox.append(this.celltoolbar.element); | |
98 | var input_area = $('<div/>').addClass('input_area'); |
|
121 | var input_area = $('<div/>').addClass('input_area'); | |
99 |
this.code_mirror = CodeMirror(input_area.get(0), |
|
122 | this.code_mirror = CodeMirror(input_area.get(0), this.cm_config); | |
100 | indentUnit : 4, |
|
|||
101 | mode: 'python', |
|
|||
102 | theme: 'ipython', |
|
|||
103 | readOnly: this.read_only, |
|
|||
104 | extraKeys: {"Tab": "indentMore","Shift-Tab" : "indentLess",'Backspace':"delSpaceToPrevTabStop"}, |
|
|||
105 | onKeyEvent: $.proxy(this.handle_codemirror_keyevent,this), |
|
|||
106 | matchBrackets: true |
|
|||
107 | }); |
|
|||
108 | vbox.append(input_area); |
|
123 | vbox.append(input_area); | |
109 | input.append(vbox); |
|
124 | input.append(vbox); | |
110 | var output = $('<div></div>'); |
|
125 | var output = $('<div></div>'); | |
111 | cell.append(input).append(output); |
|
126 | cell.append(input).append(output); | |
112 | this.element = cell; |
|
127 | this.element = cell; | |
113 | this.output_area = new IPython.OutputArea(output, true); |
|
128 | this.output_area = new IPython.OutputArea(output, true); | |
114 |
|
129 | |||
115 | // construct a completer only if class exist |
|
130 | // construct a completer only if class exist | |
116 | // otherwise no print view |
|
131 | // otherwise no print view | |
117 | if (IPython.Completer !== undefined) |
|
132 | if (IPython.Completer !== undefined) | |
118 | { |
|
133 | { | |
119 | this.completer = new IPython.Completer(this); |
|
134 | this.completer = new IPython.Completer(this); | |
120 | } |
|
135 | } | |
121 | }; |
|
136 | }; | |
122 |
|
137 | |||
123 | /** |
|
138 | /** | |
124 | * This method gets called in CodeMirror's onKeyDown/onKeyPress |
|
139 | * This method gets called in CodeMirror's onKeyDown/onKeyPress | |
125 | * handlers and is used to provide custom key handling. Its return |
|
140 | * handlers and is used to provide custom key handling. Its return | |
126 | * value is used to determine if CodeMirror should ignore the event: |
|
141 | * value is used to determine if CodeMirror should ignore the event: | |
127 | * true = ignore, false = don't ignore. |
|
142 | * true = ignore, false = don't ignore. | |
128 | * @method handle_codemirror_keyevent |
|
143 | * @method handle_codemirror_keyevent | |
129 | */ |
|
144 | */ | |
130 | CodeCell.prototype.handle_codemirror_keyevent = function (editor, event) { |
|
145 | CodeCell.prototype.handle_codemirror_keyevent = function (editor, event) { | |
131 |
|
146 | |||
132 | if (this.read_only){ |
|
147 | if (this.read_only){ | |
133 | return false; |
|
148 | return false; | |
134 | } |
|
149 | } | |
135 |
|
150 | |||
136 | var that = this; |
|
151 | var that = this; | |
137 | // whatever key is pressed, first, cancel the tooltip request before |
|
152 | // whatever key is pressed, first, cancel the tooltip request before | |
138 | // they are sent, and remove tooltip if any, except for tab again |
|
153 | // they are sent, and remove tooltip if any, except for tab again | |
139 | if (event.type === 'keydown' && event.which != key.TAB ) { |
|
154 | if (event.type === 'keydown' && event.which != key.TAB ) { | |
140 | IPython.tooltip.remove_and_cancel_tooltip(); |
|
155 | IPython.tooltip.remove_and_cancel_tooltip(); | |
141 | }; |
|
156 | }; | |
142 |
|
157 | |||
143 | var cur = editor.getCursor(); |
|
158 | var cur = editor.getCursor(); | |
144 | if (event.keyCode === key.ENTER){ |
|
159 | if (event.keyCode === key.ENTER){ | |
145 | this.auto_highlight(); |
|
160 | this.auto_highlight(); | |
146 | } |
|
161 | } | |
147 |
|
162 | |||
148 | if (event.keyCode === key.ENTER && (event.shiftKey || event.ctrlKey)) { |
|
163 | if (event.keyCode === key.ENTER && (event.shiftKey || event.ctrlKey)) { | |
149 | // Always ignore shift-enter in CodeMirror as we handle it. |
|
164 | // Always ignore shift-enter in CodeMirror as we handle it. | |
150 | return true; |
|
165 | return true; | |
151 | } else if (event.which === 40 && event.type === 'keypress' && IPython.tooltip.time_before_tooltip >= 0) { |
|
166 | } else if (event.which === 40 && event.type === 'keypress' && IPython.tooltip.time_before_tooltip >= 0) { | |
152 | // triger on keypress (!) otherwise inconsistent event.which depending on plateform |
|
167 | // triger on keypress (!) otherwise inconsistent event.which depending on plateform | |
153 | // browser and keyboard layout ! |
|
168 | // browser and keyboard layout ! | |
154 | // Pressing '(' , request tooltip, don't forget to reappend it |
|
169 | // Pressing '(' , request tooltip, don't forget to reappend it | |
155 | IPython.tooltip.pending(that); |
|
170 | IPython.tooltip.pending(that); | |
156 | } else if (event.which === key.UPARROW && event.type === 'keydown') { |
|
171 | } else if (event.which === key.UPARROW && event.type === 'keydown') { | |
157 | // If we are not at the top, let CM handle the up arrow and |
|
172 | // If we are not at the top, let CM handle the up arrow and | |
158 | // prevent the global keydown handler from handling it. |
|
173 | // prevent the global keydown handler from handling it. | |
159 | if (!that.at_top()) { |
|
174 | if (!that.at_top()) { | |
160 | event.stop(); |
|
175 | event.stop(); | |
161 | return false; |
|
176 | return false; | |
162 | } else { |
|
177 | } else { | |
163 | return true; |
|
178 | return true; | |
164 | }; |
|
179 | }; | |
165 | } else if (event.which === key.ESC) { |
|
180 | } else if (event.which === key.ESC) { | |
166 | IPython.tooltip.remove_and_cancel_tooltip(true); |
|
181 | IPython.tooltip.remove_and_cancel_tooltip(true); | |
167 | return true; |
|
182 | return true; | |
168 | } else if (event.which === key.DOWNARROW && event.type === 'keydown') { |
|
183 | } else if (event.which === key.DOWNARROW && event.type === 'keydown') { | |
169 | // If we are not at the bottom, let CM handle the down arrow and |
|
184 | // If we are not at the bottom, let CM handle the down arrow and | |
170 | // prevent the global keydown handler from handling it. |
|
185 | // prevent the global keydown handler from handling it. | |
171 | if (!that.at_bottom()) { |
|
186 | if (!that.at_bottom()) { | |
172 | event.stop(); |
|
187 | event.stop(); | |
173 | return false; |
|
188 | return false; | |
174 | } else { |
|
189 | } else { | |
175 | return true; |
|
190 | return true; | |
176 | }; |
|
191 | }; | |
177 | } else if (event.keyCode === key.TAB && event.type == 'keydown' && event.shiftKey) { |
|
192 | } else if (event.keyCode === key.TAB && event.type == 'keydown' && event.shiftKey) { | |
178 | if (editor.somethingSelected()){ |
|
193 | if (editor.somethingSelected()){ | |
179 | var anchor = editor.getCursor("anchor"); |
|
194 | var anchor = editor.getCursor("anchor"); | |
180 | var head = editor.getCursor("head"); |
|
195 | var head = editor.getCursor("head"); | |
181 | if( anchor.line != head.line){ |
|
196 | if( anchor.line != head.line){ | |
182 | return false; |
|
197 | return false; | |
183 | } |
|
198 | } | |
184 | } |
|
199 | } | |
185 | IPython.tooltip.request(that); |
|
200 | IPython.tooltip.request(that); | |
186 | event.stop(); |
|
201 | event.stop(); | |
187 | return true; |
|
202 | return true; | |
188 | } else if (event.keyCode === key.TAB && event.type == 'keydown') { |
|
203 | } else if (event.keyCode === key.TAB && event.type == 'keydown') { | |
189 | // Tab completion. |
|
204 | // Tab completion. | |
190 | //Do not trim here because of tooltip |
|
205 | //Do not trim here because of tooltip | |
191 | if (editor.somethingSelected()){return false} |
|
206 | if (editor.somethingSelected()){return false} | |
192 | var pre_cursor = editor.getRange({line:cur.line,ch:0},cur); |
|
207 | var pre_cursor = editor.getRange({line:cur.line,ch:0},cur); | |
193 | if (pre_cursor.trim() === "") { |
|
208 | if (pre_cursor.trim() === "") { | |
194 | // Don't autocomplete if the part of the line before the cursor |
|
209 | // Don't autocomplete if the part of the line before the cursor | |
195 | // is empty. In this case, let CodeMirror handle indentation. |
|
210 | // is empty. In this case, let CodeMirror handle indentation. | |
196 | return false; |
|
211 | return false; | |
197 | } else if ((pre_cursor.substr(-1) === "("|| pre_cursor.substr(-1) === " ") && IPython.config.tooltip_on_tab ) { |
|
212 | } else if ((pre_cursor.substr(-1) === "("|| pre_cursor.substr(-1) === " ") && IPython.config.tooltip_on_tab ) { | |
198 | IPython.tooltip.request(that); |
|
213 | IPython.tooltip.request(that); | |
199 | // Prevent the event from bubbling up. |
|
214 | // Prevent the event from bubbling up. | |
200 | event.stop(); |
|
215 | event.stop(); | |
201 | // Prevent CodeMirror from handling the tab. |
|
216 | // Prevent CodeMirror from handling the tab. | |
202 | return true; |
|
217 | return true; | |
203 | } else { |
|
218 | } else { | |
204 | event.stop(); |
|
219 | event.stop(); | |
205 | this.completer.startCompletion(); |
|
220 | this.completer.startCompletion(); | |
206 | return true; |
|
221 | return true; | |
207 | }; |
|
222 | }; | |
208 | } else { |
|
223 | } else { | |
209 | // keypress/keyup also trigger on TAB press, and we don't want to |
|
224 | // keypress/keyup also trigger on TAB press, and we don't want to | |
210 | // use those to disable tab completion. |
|
225 | // use those to disable tab completion. | |
211 | return false; |
|
226 | return false; | |
212 | }; |
|
227 | }; | |
213 | return false; |
|
228 | return false; | |
214 | }; |
|
229 | }; | |
215 |
|
230 | |||
216 |
|
231 | |||
217 | // Kernel related calls. |
|
232 | // Kernel related calls. | |
218 |
|
233 | |||
219 | CodeCell.prototype.set_kernel = function (kernel) { |
|
234 | CodeCell.prototype.set_kernel = function (kernel) { | |
220 | this.kernel = kernel; |
|
235 | this.kernel = kernel; | |
221 | } |
|
236 | } | |
222 |
|
237 | |||
223 | /** |
|
238 | /** | |
224 | * Execute current code cell to the kernel |
|
239 | * Execute current code cell to the kernel | |
225 | * @method execute |
|
240 | * @method execute | |
226 | */ |
|
241 | */ | |
227 | CodeCell.prototype.execute = function () { |
|
242 | CodeCell.prototype.execute = function () { | |
228 | this.output_area.clear_output(true, true, true); |
|
243 | this.output_area.clear_output(true, true, true); | |
229 | this.set_input_prompt('*'); |
|
244 | this.set_input_prompt('*'); | |
230 | this.element.addClass("running"); |
|
245 | this.element.addClass("running"); | |
231 | var callbacks = { |
|
246 | var callbacks = { | |
232 | 'execute_reply': $.proxy(this._handle_execute_reply, this), |
|
247 | 'execute_reply': $.proxy(this._handle_execute_reply, this), | |
233 | 'output': $.proxy(this.output_area.handle_output, this.output_area), |
|
248 | 'output': $.proxy(this.output_area.handle_output, this.output_area), | |
234 | 'clear_output': $.proxy(this.output_area.handle_clear_output, this.output_area), |
|
249 | 'clear_output': $.proxy(this.output_area.handle_clear_output, this.output_area), | |
235 | 'set_next_input': $.proxy(this._handle_set_next_input, this) |
|
250 | 'set_next_input': $.proxy(this._handle_set_next_input, this) | |
236 | }; |
|
251 | }; | |
237 | var msg_id = this.kernel.execute(this.get_text(), callbacks, {silent: false}); |
|
252 | var msg_id = this.kernel.execute(this.get_text(), callbacks, {silent: false}); | |
238 | }; |
|
253 | }; | |
239 |
|
254 | |||
240 | /** |
|
255 | /** | |
241 | * @method _handle_execute_reply |
|
256 | * @method _handle_execute_reply | |
242 | * @private |
|
257 | * @private | |
243 | */ |
|
258 | */ | |
244 | CodeCell.prototype._handle_execute_reply = function (content) { |
|
259 | CodeCell.prototype._handle_execute_reply = function (content) { | |
245 | this.set_input_prompt(content.execution_count); |
|
260 | this.set_input_prompt(content.execution_count); | |
246 | this.element.removeClass("running"); |
|
261 | this.element.removeClass("running"); | |
247 | $([IPython.events]).trigger('set_dirty.Notebook', {'value': true}); |
|
262 | $([IPython.events]).trigger('set_dirty.Notebook', {'value': true}); | |
248 | } |
|
263 | } | |
249 |
|
264 | |||
250 | CodeCell.prototype._handle_set_next_input = function (text) { |
|
265 | CodeCell.prototype._handle_set_next_input = function (text) { | |
251 | var data = {'cell': this, 'text': text} |
|
266 | var data = {'cell': this, 'text': text} | |
252 | $([IPython.events]).trigger('set_next_input.Notebook', data); |
|
267 | $([IPython.events]).trigger('set_next_input.Notebook', data); | |
253 | } |
|
268 | } | |
254 |
|
269 | |||
255 | // Basic cell manipulation. |
|
270 | // Basic cell manipulation. | |
256 |
|
271 | |||
257 | CodeCell.prototype.select = function () { |
|
272 | CodeCell.prototype.select = function () { | |
258 | IPython.Cell.prototype.select.apply(this); |
|
273 | IPython.Cell.prototype.select.apply(this); | |
259 | this.code_mirror.refresh(); |
|
274 | this.code_mirror.refresh(); | |
260 | this.code_mirror.focus(); |
|
275 | this.code_mirror.focus(); | |
261 | this.auto_highlight(); |
|
276 | this.auto_highlight(); | |
262 | // We used to need an additional refresh() after the focus, but |
|
277 | // We used to need an additional refresh() after the focus, but | |
263 | // it appears that this has been fixed in CM. This bug would show |
|
278 | // it appears that this has been fixed in CM. This bug would show | |
264 | // up on FF when a newly loaded markdown cell was edited. |
|
279 | // up on FF when a newly loaded markdown cell was edited. | |
265 | }; |
|
280 | }; | |
266 |
|
281 | |||
267 |
|
282 | |||
268 | CodeCell.prototype.select_all = function () { |
|
283 | CodeCell.prototype.select_all = function () { | |
269 | var start = {line: 0, ch: 0}; |
|
284 | var start = {line: 0, ch: 0}; | |
270 | var nlines = this.code_mirror.lineCount(); |
|
285 | var nlines = this.code_mirror.lineCount(); | |
271 | var last_line = this.code_mirror.getLine(nlines-1); |
|
286 | var last_line = this.code_mirror.getLine(nlines-1); | |
272 | var end = {line: nlines-1, ch: last_line.length}; |
|
287 | var end = {line: nlines-1, ch: last_line.length}; | |
273 | this.code_mirror.setSelection(start, end); |
|
288 | this.code_mirror.setSelection(start, end); | |
274 | }; |
|
289 | }; | |
275 |
|
290 | |||
276 |
|
291 | |||
277 | CodeCell.prototype.collapse = function () { |
|
292 | CodeCell.prototype.collapse = function () { | |
278 | this.collapsed = true; |
|
293 | this.collapsed = true; | |
279 | this.output_area.collapse(); |
|
294 | this.output_area.collapse(); | |
280 | }; |
|
295 | }; | |
281 |
|
296 | |||
282 |
|
297 | |||
283 | CodeCell.prototype.expand = function () { |
|
298 | CodeCell.prototype.expand = function () { | |
284 | this.collapsed = false; |
|
299 | this.collapsed = false; | |
285 | this.output_area.expand(); |
|
300 | this.output_area.expand(); | |
286 | }; |
|
301 | }; | |
287 |
|
302 | |||
288 |
|
303 | |||
289 | CodeCell.prototype.toggle_output = function () { |
|
304 | CodeCell.prototype.toggle_output = function () { | |
290 | this.collapsed = Boolean(1 - this.collapsed); |
|
305 | this.collapsed = Boolean(1 - this.collapsed); | |
291 | this.output_area.toggle_output(); |
|
306 | this.output_area.toggle_output(); | |
292 | }; |
|
307 | }; | |
293 |
|
308 | |||
294 |
|
309 | |||
295 | CodeCell.prototype.toggle_output_scroll = function () { |
|
310 | CodeCell.prototype.toggle_output_scroll = function () { | |
296 | this.output_area.toggle_scroll(); |
|
311 | this.output_area.toggle_scroll(); | |
297 | }; |
|
312 | }; | |
298 |
|
313 | |||
299 |
|
314 | |||
300 | CodeCell.input_prompt_classical = function (prompt_value, lines_number) { |
|
315 | CodeCell.input_prompt_classical = function (prompt_value, lines_number) { | |
301 | var ns = prompt_value || " "; |
|
316 | var ns = prompt_value || " "; | |
302 | return 'In [' + ns + ']:' |
|
317 | return 'In [' + ns + ']:' | |
303 | }; |
|
318 | }; | |
304 |
|
319 | |||
305 | CodeCell.input_prompt_continuation = function (prompt_value, lines_number) { |
|
320 | CodeCell.input_prompt_continuation = function (prompt_value, lines_number) { | |
306 | var html = [CodeCell.input_prompt_classical(prompt_value, lines_number)]; |
|
321 | var html = [CodeCell.input_prompt_classical(prompt_value, lines_number)]; | |
307 | for(var i=1; i < lines_number; i++){html.push(['...:'])}; |
|
322 | for(var i=1; i < lines_number; i++){html.push(['...:'])}; | |
308 | return html.join('</br>') |
|
323 | return html.join('</br>') | |
309 | }; |
|
324 | }; | |
310 |
|
325 | |||
311 | CodeCell.input_prompt_function = CodeCell.input_prompt_classical; |
|
326 | CodeCell.input_prompt_function = CodeCell.input_prompt_classical; | |
312 |
|
327 | |||
313 |
|
328 | |||
314 | CodeCell.prototype.set_input_prompt = function (number) { |
|
329 | CodeCell.prototype.set_input_prompt = function (number) { | |
315 | var nline = 1 |
|
330 | var nline = 1 | |
316 | if( this.code_mirror != undefined) { |
|
331 | if( this.code_mirror != undefined) { | |
317 | nline = this.code_mirror.lineCount(); |
|
332 | nline = this.code_mirror.lineCount(); | |
318 | } |
|
333 | } | |
319 | this.input_prompt_number = number; |
|
334 | this.input_prompt_number = number; | |
320 | var prompt_html = CodeCell.input_prompt_function(this.input_prompt_number, nline); |
|
335 | var prompt_html = CodeCell.input_prompt_function(this.input_prompt_number, nline); | |
321 | this.element.find('div.input_prompt').html(prompt_html); |
|
336 | this.element.find('div.input_prompt').html(prompt_html); | |
322 | }; |
|
337 | }; | |
323 |
|
338 | |||
324 |
|
339 | |||
325 | CodeCell.prototype.clear_input = function () { |
|
340 | CodeCell.prototype.clear_input = function () { | |
326 | this.code_mirror.setValue(''); |
|
341 | this.code_mirror.setValue(''); | |
327 | }; |
|
342 | }; | |
328 |
|
343 | |||
329 |
|
344 | |||
330 | CodeCell.prototype.get_text = function () { |
|
345 | CodeCell.prototype.get_text = function () { | |
331 | return this.code_mirror.getValue(); |
|
346 | return this.code_mirror.getValue(); | |
332 | }; |
|
347 | }; | |
333 |
|
348 | |||
334 |
|
349 | |||
335 | CodeCell.prototype.set_text = function (code) { |
|
350 | CodeCell.prototype.set_text = function (code) { | |
336 | return this.code_mirror.setValue(code); |
|
351 | return this.code_mirror.setValue(code); | |
337 | }; |
|
352 | }; | |
338 |
|
353 | |||
339 |
|
354 | |||
340 | CodeCell.prototype.at_top = function () { |
|
355 | CodeCell.prototype.at_top = function () { | |
341 | var cursor = this.code_mirror.getCursor(); |
|
356 | var cursor = this.code_mirror.getCursor(); | |
342 | if (cursor.line === 0 && cursor.ch === 0) { |
|
357 | if (cursor.line === 0 && cursor.ch === 0) { | |
343 | return true; |
|
358 | return true; | |
344 | } else { |
|
359 | } else { | |
345 | return false; |
|
360 | return false; | |
346 | } |
|
361 | } | |
347 | }; |
|
362 | }; | |
348 |
|
363 | |||
349 |
|
364 | |||
350 | CodeCell.prototype.at_bottom = function () { |
|
365 | CodeCell.prototype.at_bottom = function () { | |
351 | var cursor = this.code_mirror.getCursor(); |
|
366 | var cursor = this.code_mirror.getCursor(); | |
352 | if (cursor.line === (this.code_mirror.lineCount()-1) && cursor.ch === this.code_mirror.getLine(cursor.line).length) { |
|
367 | if (cursor.line === (this.code_mirror.lineCount()-1) && cursor.ch === this.code_mirror.getLine(cursor.line).length) { | |
353 | return true; |
|
368 | return true; | |
354 | } else { |
|
369 | } else { | |
355 | return false; |
|
370 | return false; | |
356 | } |
|
371 | } | |
357 | }; |
|
372 | }; | |
358 |
|
373 | |||
359 |
|
374 | |||
360 | CodeCell.prototype.clear_output = function (stdout, stderr, other) { |
|
375 | CodeCell.prototype.clear_output = function (stdout, stderr, other) { | |
361 | this.output_area.clear_output(stdout, stderr, other); |
|
376 | this.output_area.clear_output(stdout, stderr, other); | |
362 | }; |
|
377 | }; | |
363 |
|
378 | |||
364 |
|
379 | |||
365 | // JSON serialization |
|
380 | // JSON serialization | |
366 |
|
381 | |||
367 | CodeCell.prototype.fromJSON = function (data) { |
|
382 | CodeCell.prototype.fromJSON = function (data) { | |
368 | IPython.Cell.prototype.fromJSON.apply(this, arguments); |
|
383 | IPython.Cell.prototype.fromJSON.apply(this, arguments); | |
369 | if (data.cell_type === 'code') { |
|
384 | if (data.cell_type === 'code') { | |
370 | if (data.input !== undefined) { |
|
385 | if (data.input !== undefined) { | |
371 | this.set_text(data.input); |
|
386 | this.set_text(data.input); | |
372 | // make this value the starting point, so that we can only undo |
|
387 | // make this value the starting point, so that we can only undo | |
373 | // to this state, instead of a blank cell |
|
388 | // to this state, instead of a blank cell | |
374 | this.code_mirror.clearHistory(); |
|
389 | this.code_mirror.clearHistory(); | |
375 | this.auto_highlight(); |
|
390 | this.auto_highlight(); | |
376 | } |
|
391 | } | |
377 | if (data.prompt_number !== undefined) { |
|
392 | if (data.prompt_number !== undefined) { | |
378 | this.set_input_prompt(data.prompt_number); |
|
393 | this.set_input_prompt(data.prompt_number); | |
379 | } else { |
|
394 | } else { | |
380 | this.set_input_prompt(); |
|
395 | this.set_input_prompt(); | |
381 | }; |
|
396 | }; | |
382 | this.output_area.fromJSON(data.outputs); |
|
397 | this.output_area.fromJSON(data.outputs); | |
383 | if (data.collapsed !== undefined) { |
|
398 | if (data.collapsed !== undefined) { | |
384 | if (data.collapsed) { |
|
399 | if (data.collapsed) { | |
385 | this.collapse(); |
|
400 | this.collapse(); | |
386 | } else { |
|
401 | } else { | |
387 | this.expand(); |
|
402 | this.expand(); | |
388 | }; |
|
403 | }; | |
389 | }; |
|
404 | }; | |
390 | }; |
|
405 | }; | |
391 | }; |
|
406 | }; | |
392 |
|
407 | |||
393 |
|
408 | |||
394 | CodeCell.prototype.toJSON = function () { |
|
409 | CodeCell.prototype.toJSON = function () { | |
395 | var data = IPython.Cell.prototype.toJSON.apply(this); |
|
410 | var data = IPython.Cell.prototype.toJSON.apply(this); | |
396 | data.input = this.get_text(); |
|
411 | data.input = this.get_text(); | |
397 | data.cell_type = 'code'; |
|
412 | data.cell_type = 'code'; | |
398 | if (this.input_prompt_number) { |
|
413 | if (this.input_prompt_number) { | |
399 | data.prompt_number = this.input_prompt_number; |
|
414 | data.prompt_number = this.input_prompt_number; | |
400 | }; |
|
415 | }; | |
401 | var outputs = this.output_area.toJSON(); |
|
416 | var outputs = this.output_area.toJSON(); | |
402 | data.outputs = outputs; |
|
417 | data.outputs = outputs; | |
403 | data.language = 'python'; |
|
418 | data.language = 'python'; | |
404 | data.collapsed = this.collapsed; |
|
419 | data.collapsed = this.collapsed; | |
405 | return data; |
|
420 | return data; | |
406 | }; |
|
421 | }; | |
407 |
|
422 | |||
408 |
|
423 | |||
409 | IPython.CodeCell = CodeCell; |
|
424 | IPython.CodeCell = CodeCell; | |
410 |
|
425 | |||
411 | return IPython; |
|
426 | return IPython; | |
412 | }(IPython)); |
|
427 | }(IPython)); |
@@ -1,536 +1,551 | |||||
1 | //---------------------------------------------------------------------------- |
|
1 | //---------------------------------------------------------------------------- | |
2 | // Copyright (C) 2008-2012 The IPython Development Team |
|
2 | // Copyright (C) 2008-2012 The IPython Development Team | |
3 | // |
|
3 | // | |
4 | // Distributed under the terms of the BSD License. The full license is in |
|
4 | // Distributed under the terms of the BSD License. The full license is in | |
5 | // the file COPYING, distributed as part of this software. |
|
5 | // the file COPYING, distributed as part of this software. | |
6 | //---------------------------------------------------------------------------- |
|
6 | //---------------------------------------------------------------------------- | |
7 |
|
7 | |||
8 | //============================================================================ |
|
8 | //============================================================================ | |
9 | // TextCell |
|
9 | // TextCell | |
10 | //============================================================================ |
|
10 | //============================================================================ | |
11 |
|
11 | |||
12 | /** |
|
12 | /** | |
13 | A module that allow to create different type of Text Cell |
|
13 | A module that allow to create different type of Text Cell | |
14 | @module IPython |
|
14 | @module IPython | |
15 | @namespace IPython |
|
15 | @namespace IPython | |
16 | */ |
|
16 | */ | |
17 | var IPython = (function (IPython) { |
|
17 | var IPython = (function (IPython) { | |
18 |
|
18 | |||
19 | // TextCell base class |
|
19 | // TextCell base class | |
20 | var key = IPython.utils.keycodes; |
|
20 | var key = IPython.utils.keycodes; | |
21 |
|
21 | |||
22 | /** |
|
22 | /** | |
23 | * Construct a new TextCell, codemirror mode is by default 'htmlmixed', and cell type is 'text' |
|
23 | * Construct a new TextCell, codemirror mode is by default 'htmlmixed', and cell type is 'text' | |
24 | * cell start as not redered. |
|
24 | * cell start as not redered. | |
25 | * |
|
25 | * | |
26 | * @class TextCell |
|
26 | * @class TextCell | |
27 | * @constructor TextCell |
|
27 | * @constructor TextCell | |
28 | * @extend Ipython.Cell |
|
28 | * @extend Ipython.Cell | |
|
29 | * @param {object|undefined} [options] | |||
|
30 | * @param [options.cm_config] {object} config to pass to CodeMirror, will extend/overwrite default config | |||
29 | */ |
|
31 | */ | |
30 | var TextCell = function () { |
|
32 | var TextCell = function (options) { | |
31 | this.code_mirror_mode = this.code_mirror_mode || 'htmlmixed'; |
|
33 | this.code_mirror_mode = this.code_mirror_mode || 'htmlmixed'; | |
32 | IPython.Cell.apply(this, arguments); |
|
34 | var options = options || {}; | |
|
35 | ||||
|
36 | var cm_overwrite_options = { | |||
|
37 | extraKeys: {"Tab": "indentMore","Shift-Tab" : "indentLess"}, | |||
|
38 | onKeyEvent: $.proxy(this.handle_codemirror_keyevent,this) | |||
|
39 | }; | |||
|
40 | ||||
|
41 | var arg_cm_options = options.cm_options || {}; | |||
|
42 | var cm_config = $.extend({},TextCell.cm_default, arg_cm_options, cm_overwrite_options); | |||
|
43 | ||||
|
44 | var options = {}; | |||
|
45 | options.cm_config = cm_config; | |||
|
46 | ||||
|
47 | ||||
|
48 | IPython.Cell.apply(this, [options]); | |||
33 | this.rendered = false; |
|
49 | this.rendered = false; | |
34 | this.cell_type = this.cell_type || 'text'; |
|
50 | this.cell_type = this.cell_type || 'text'; | |
35 | }; |
|
51 | }; | |
36 |
|
52 | |||
|
53 | TextCell.cm_default = { | |||
|
54 | mode: this.code_mirror_mode, | |||
|
55 | theme: 'default', | |||
|
56 | value: this.placeholder, | |||
|
57 | lineWrapping : true, | |||
|
58 | } | |||
|
59 | ||||
|
60 | ||||
37 | TextCell.prototype = new IPython.Cell(); |
|
61 | TextCell.prototype = new IPython.Cell(); | |
38 |
|
62 | |||
39 | /** |
|
63 | /** | |
40 | * Create the DOM element of the TextCell |
|
64 | * Create the DOM element of the TextCell | |
41 | * @method create_element |
|
65 | * @method create_element | |
42 | * @private |
|
66 | * @private | |
43 | */ |
|
67 | */ | |
44 | TextCell.prototype.create_element = function () { |
|
68 | TextCell.prototype.create_element = function () { | |
45 | IPython.Cell.prototype.create_element.apply(this, arguments); |
|
69 | IPython.Cell.prototype.create_element.apply(this, arguments); | |
46 | var cell = $("<div>").addClass('cell text_cell border-box-sizing vbox'); |
|
70 | var cell = $("<div>").addClass('cell text_cell border-box-sizing vbox'); | |
47 | cell.attr('tabindex','2'); |
|
71 | cell.attr('tabindex','2'); | |
48 |
|
72 | |||
49 | this.celltoolbar = new IPython.CellToolbar(this); |
|
73 | this.celltoolbar = new IPython.CellToolbar(this); | |
50 | cell.append(this.celltoolbar.element); |
|
74 | cell.append(this.celltoolbar.element); | |
51 |
|
75 | |||
52 | var input_area = $('<div/>').addClass('text_cell_input border-box-sizing'); |
|
76 | var input_area = $('<div/>').addClass('text_cell_input border-box-sizing'); | |
53 |
this.code_mirror = CodeMirror(input_area.get(0), |
|
77 | this.code_mirror = CodeMirror(input_area.get(0), this.cm_config); | |
54 | indentUnit : 4, |
|
|||
55 | mode: this.code_mirror_mode, |
|
|||
56 | theme: 'default', |
|
|||
57 | value: this.placeholder, |
|
|||
58 | readOnly: this.read_only, |
|
|||
59 | lineWrapping : true, |
|
|||
60 | extraKeys: {"Tab": "indentMore","Shift-Tab" : "indentLess"}, |
|
|||
61 | onKeyEvent: $.proxy(this.handle_codemirror_keyevent,this) |
|
|||
62 | }); |
|
|||
63 | // The tabindex=-1 makes this div focusable. |
|
78 | // The tabindex=-1 makes this div focusable. | |
64 | var render_area = $('<div/>').addClass('text_cell_render border-box-sizing'). |
|
79 | var render_area = $('<div/>').addClass('text_cell_render border-box-sizing'). | |
65 | addClass('rendered_html').attr('tabindex','-1'); |
|
80 | addClass('rendered_html').attr('tabindex','-1'); | |
66 | cell.append(input_area).append(render_area); |
|
81 | cell.append(input_area).append(render_area); | |
67 | this.element = cell; |
|
82 | this.element = cell; | |
68 | }; |
|
83 | }; | |
69 |
|
84 | |||
70 |
|
85 | |||
71 | /** |
|
86 | /** | |
72 | * Bind the DOM evet to cell actions |
|
87 | * Bind the DOM evet to cell actions | |
73 | * Need to be called after TextCell.create_element |
|
88 | * Need to be called after TextCell.create_element | |
74 | * @private |
|
89 | * @private | |
75 | * @method bind_event |
|
90 | * @method bind_event | |
76 | */ |
|
91 | */ | |
77 | TextCell.prototype.bind_events = function () { |
|
92 | TextCell.prototype.bind_events = function () { | |
78 | IPython.Cell.prototype.bind_events.apply(this); |
|
93 | IPython.Cell.prototype.bind_events.apply(this); | |
79 | var that = this; |
|
94 | var that = this; | |
80 | this.element.keydown(function (event) { |
|
95 | this.element.keydown(function (event) { | |
81 | if (event.which === 13 && !event.shiftKey) { |
|
96 | if (event.which === 13 && !event.shiftKey) { | |
82 | if (that.rendered) { |
|
97 | if (that.rendered) { | |
83 | that.edit(); |
|
98 | that.edit(); | |
84 | return false; |
|
99 | return false; | |
85 | }; |
|
100 | }; | |
86 | }; |
|
101 | }; | |
87 | }); |
|
102 | }); | |
88 | this.element.dblclick(function () { |
|
103 | this.element.dblclick(function () { | |
89 | that.edit(); |
|
104 | that.edit(); | |
90 | }); |
|
105 | }); | |
91 | }; |
|
106 | }; | |
92 |
|
107 | |||
93 | /** |
|
108 | /** | |
94 | * This method gets called in CodeMirror's onKeyDown/onKeyPress |
|
109 | * This method gets called in CodeMirror's onKeyDown/onKeyPress | |
95 | * handlers and is used to provide custom key handling. |
|
110 | * handlers and is used to provide custom key handling. | |
96 | * |
|
111 | * | |
97 | * Subclass should override this method to have custom handeling |
|
112 | * Subclass should override this method to have custom handeling | |
98 | * |
|
113 | * | |
99 | * @method handle_codemirror_keyevent |
|
114 | * @method handle_codemirror_keyevent | |
100 | * @param {CodeMirror} editor - The codemirror instance bound to the cell |
|
115 | * @param {CodeMirror} editor - The codemirror instance bound to the cell | |
101 | * @param {event} event - |
|
116 | * @param {event} event - | |
102 | * @return {Boolean} `true` if CodeMirror should ignore the event, `false` Otherwise |
|
117 | * @return {Boolean} `true` if CodeMirror should ignore the event, `false` Otherwise | |
103 | */ |
|
118 | */ | |
104 | TextCell.prototype.handle_codemirror_keyevent = function (editor, event) { |
|
119 | TextCell.prototype.handle_codemirror_keyevent = function (editor, event) { | |
105 |
|
120 | |||
106 | if (event.keyCode === 13 && (event.shiftKey || event.ctrlKey)) { |
|
121 | if (event.keyCode === 13 && (event.shiftKey || event.ctrlKey)) { | |
107 | // Always ignore shift-enter in CodeMirror as we handle it. |
|
122 | // Always ignore shift-enter in CodeMirror as we handle it. | |
108 | return true; |
|
123 | return true; | |
109 | } |
|
124 | } | |
110 | return false; |
|
125 | return false; | |
111 | }; |
|
126 | }; | |
112 |
|
127 | |||
113 | /** |
|
128 | /** | |
114 | * Select the current cell and trigger 'focus' |
|
129 | * Select the current cell and trigger 'focus' | |
115 | * @method select |
|
130 | * @method select | |
116 | */ |
|
131 | */ | |
117 | TextCell.prototype.select = function () { |
|
132 | TextCell.prototype.select = function () { | |
118 | IPython.Cell.prototype.select.apply(this); |
|
133 | IPython.Cell.prototype.select.apply(this); | |
119 | var output = this.element.find("div.text_cell_render"); |
|
134 | var output = this.element.find("div.text_cell_render"); | |
120 | output.trigger('focus'); |
|
135 | output.trigger('focus'); | |
121 | }; |
|
136 | }; | |
122 |
|
137 | |||
123 | /** |
|
138 | /** | |
124 | * unselect the current cell and `render` it |
|
139 | * unselect the current cell and `render` it | |
125 | * @method unselect |
|
140 | * @method unselect | |
126 | */ |
|
141 | */ | |
127 | TextCell.prototype.unselect = function() { |
|
142 | TextCell.prototype.unselect = function() { | |
128 | // render on selection of another cell |
|
143 | // render on selection of another cell | |
129 | this.render(); |
|
144 | this.render(); | |
130 | IPython.Cell.prototype.unselect.apply(this); |
|
145 | IPython.Cell.prototype.unselect.apply(this); | |
131 | }; |
|
146 | }; | |
132 |
|
147 | |||
133 | /** |
|
148 | /** | |
134 | * |
|
149 | * | |
135 | * put the current cell in edition mode |
|
150 | * put the current cell in edition mode | |
136 | * @method edit |
|
151 | * @method edit | |
137 | */ |
|
152 | */ | |
138 | TextCell.prototype.edit = function () { |
|
153 | TextCell.prototype.edit = function () { | |
139 | if ( this.read_only ) return; |
|
154 | if ( this.read_only ) return; | |
140 | if (this.rendered === true) { |
|
155 | if (this.rendered === true) { | |
141 | var text_cell = this.element; |
|
156 | var text_cell = this.element; | |
142 | var output = text_cell.find("div.text_cell_render"); |
|
157 | var output = text_cell.find("div.text_cell_render"); | |
143 | output.hide(); |
|
158 | output.hide(); | |
144 | text_cell.find('div.text_cell_input').show(); |
|
159 | text_cell.find('div.text_cell_input').show(); | |
145 | this.code_mirror.refresh(); |
|
160 | this.code_mirror.refresh(); | |
146 | this.code_mirror.focus(); |
|
161 | this.code_mirror.focus(); | |
147 | // We used to need an additional refresh() after the focus, but |
|
162 | // We used to need an additional refresh() after the focus, but | |
148 | // it appears that this has been fixed in CM. This bug would show |
|
163 | // it appears that this has been fixed in CM. This bug would show | |
149 | // up on FF when a newly loaded markdown cell was edited. |
|
164 | // up on FF when a newly loaded markdown cell was edited. | |
150 | this.rendered = false; |
|
165 | this.rendered = false; | |
151 | if (this.get_text() === this.placeholder) { |
|
166 | if (this.get_text() === this.placeholder) { | |
152 | this.set_text(''); |
|
167 | this.set_text(''); | |
153 | this.refresh(); |
|
168 | this.refresh(); | |
154 | } |
|
169 | } | |
155 | } |
|
170 | } | |
156 | }; |
|
171 | }; | |
157 |
|
172 | |||
158 |
|
173 | |||
159 | /** |
|
174 | /** | |
160 | * Empty, Subclasses must define render. |
|
175 | * Empty, Subclasses must define render. | |
161 | * @method render |
|
176 | * @method render | |
162 | */ |
|
177 | */ | |
163 | TextCell.prototype.render = function () {}; |
|
178 | TextCell.prototype.render = function () {}; | |
164 |
|
179 | |||
165 |
|
180 | |||
166 | /** |
|
181 | /** | |
167 | * setter: {{#crossLink "TextCell/set_text"}}{{/crossLink}} |
|
182 | * setter: {{#crossLink "TextCell/set_text"}}{{/crossLink}} | |
168 | * @method get_text |
|
183 | * @method get_text | |
169 | * @retrun {string} CodeMirror current text value |
|
184 | * @retrun {string} CodeMirror current text value | |
170 | */ |
|
185 | */ | |
171 | TextCell.prototype.get_text = function() { |
|
186 | TextCell.prototype.get_text = function() { | |
172 | return this.code_mirror.getValue(); |
|
187 | return this.code_mirror.getValue(); | |
173 | }; |
|
188 | }; | |
174 |
|
189 | |||
175 | /** |
|
190 | /** | |
176 | * @param {string} text - Codemiror text value |
|
191 | * @param {string} text - Codemiror text value | |
177 | * @see TextCell#get_text |
|
192 | * @see TextCell#get_text | |
178 | * @method set_text |
|
193 | * @method set_text | |
179 | * */ |
|
194 | * */ | |
180 | TextCell.prototype.set_text = function(text) { |
|
195 | TextCell.prototype.set_text = function(text) { | |
181 | this.code_mirror.setValue(text); |
|
196 | this.code_mirror.setValue(text); | |
182 | this.code_mirror.refresh(); |
|
197 | this.code_mirror.refresh(); | |
183 | }; |
|
198 | }; | |
184 |
|
199 | |||
185 | /** |
|
200 | /** | |
186 | * setter :{{#crossLink "TextCell/set_rendered"}}{{/crossLink}} |
|
201 | * setter :{{#crossLink "TextCell/set_rendered"}}{{/crossLink}} | |
187 | * @method get_rendered |
|
202 | * @method get_rendered | |
188 | * @return {html} html of rendered element |
|
203 | * @return {html} html of rendered element | |
189 | * */ |
|
204 | * */ | |
190 | TextCell.prototype.get_rendered = function() { |
|
205 | TextCell.prototype.get_rendered = function() { | |
191 | return this.element.find('div.text_cell_render').html(); |
|
206 | return this.element.find('div.text_cell_render').html(); | |
192 | }; |
|
207 | }; | |
193 |
|
208 | |||
194 | /** |
|
209 | /** | |
195 | * @method set_rendered |
|
210 | * @method set_rendered | |
196 | */ |
|
211 | */ | |
197 | TextCell.prototype.set_rendered = function(text) { |
|
212 | TextCell.prototype.set_rendered = function(text) { | |
198 | this.element.find('div.text_cell_render').html(text); |
|
213 | this.element.find('div.text_cell_render').html(text); | |
199 | }; |
|
214 | }; | |
200 |
|
215 | |||
201 | /** |
|
216 | /** | |
202 | * not deprecated, but implementation wrong |
|
217 | * not deprecated, but implementation wrong | |
203 | * @method at_top |
|
218 | * @method at_top | |
204 | * @deprecated |
|
219 | * @deprecated | |
205 | * @return {Boolean} true is cell rendered, false otherwise |
|
220 | * @return {Boolean} true is cell rendered, false otherwise | |
206 | * I doubt this is what it is supposed to do |
|
221 | * I doubt this is what it is supposed to do | |
207 | * this implementation is completly false |
|
222 | * this implementation is completly false | |
208 | */ |
|
223 | */ | |
209 | TextCell.prototype.at_top = function () { |
|
224 | TextCell.prototype.at_top = function () { | |
210 | if (this.rendered) { |
|
225 | if (this.rendered) { | |
211 | return true; |
|
226 | return true; | |
212 | } else { |
|
227 | } else { | |
213 | return false; |
|
228 | return false; | |
214 | } |
|
229 | } | |
215 | }; |
|
230 | }; | |
216 |
|
231 | |||
217 |
|
232 | |||
218 | /** |
|
233 | /** | |
219 | * not deprecated, but implementation wrong |
|
234 | * not deprecated, but implementation wrong | |
220 | * @method at_bottom |
|
235 | * @method at_bottom | |
221 | * @deprecated |
|
236 | * @deprecated | |
222 | * @return {Boolean} true is cell rendered, false otherwise |
|
237 | * @return {Boolean} true is cell rendered, false otherwise | |
223 | * I doubt this is what it is supposed to do |
|
238 | * I doubt this is what it is supposed to do | |
224 | * this implementation is completly false |
|
239 | * this implementation is completly false | |
225 | * */ |
|
240 | * */ | |
226 | TextCell.prototype.at_bottom = function () { |
|
241 | TextCell.prototype.at_bottom = function () { | |
227 | if (this.rendered) { |
|
242 | if (this.rendered) { | |
228 | return true; |
|
243 | return true; | |
229 | } else { |
|
244 | } else { | |
230 | return false; |
|
245 | return false; | |
231 | } |
|
246 | } | |
232 | }; |
|
247 | }; | |
233 |
|
248 | |||
234 | /** |
|
249 | /** | |
235 | * Create Text cell from JSON |
|
250 | * Create Text cell from JSON | |
236 | * @param {json} data - JSON serialized text-cell |
|
251 | * @param {json} data - JSON serialized text-cell | |
237 | * @method fromJSON |
|
252 | * @method fromJSON | |
238 | */ |
|
253 | */ | |
239 | TextCell.prototype.fromJSON = function (data) { |
|
254 | TextCell.prototype.fromJSON = function (data) { | |
240 | IPython.Cell.prototype.fromJSON.apply(this, arguments); |
|
255 | IPython.Cell.prototype.fromJSON.apply(this, arguments); | |
241 | if (data.cell_type === this.cell_type) { |
|
256 | if (data.cell_type === this.cell_type) { | |
242 | if (data.source !== undefined) { |
|
257 | if (data.source !== undefined) { | |
243 | this.set_text(data.source); |
|
258 | this.set_text(data.source); | |
244 | // make this value the starting point, so that we can only undo |
|
259 | // make this value the starting point, so that we can only undo | |
245 | // to this state, instead of a blank cell |
|
260 | // to this state, instead of a blank cell | |
246 | this.code_mirror.clearHistory(); |
|
261 | this.code_mirror.clearHistory(); | |
247 | this.set_rendered(data.rendered || ''); |
|
262 | this.set_rendered(data.rendered || ''); | |
248 | this.rendered = false; |
|
263 | this.rendered = false; | |
249 | this.render(); |
|
264 | this.render(); | |
250 | } |
|
265 | } | |
251 | } |
|
266 | } | |
252 | }; |
|
267 | }; | |
253 |
|
268 | |||
254 | /** Generate JSON from cell |
|
269 | /** Generate JSON from cell | |
255 | * @return {object} cell data serialised to json |
|
270 | * @return {object} cell data serialised to json | |
256 | */ |
|
271 | */ | |
257 | TextCell.prototype.toJSON = function () { |
|
272 | TextCell.prototype.toJSON = function () { | |
258 | var data = IPython.Cell.prototype.toJSON.apply(this); |
|
273 | var data = IPython.Cell.prototype.toJSON.apply(this); | |
259 | data.cell_type = this.cell_type; |
|
274 | data.cell_type = this.cell_type; | |
260 | data.source = this.get_text(); |
|
275 | data.source = this.get_text(); | |
261 | return data; |
|
276 | return data; | |
262 | }; |
|
277 | }; | |
263 |
|
278 | |||
264 |
|
279 | |||
265 | /** |
|
280 | /** | |
266 | * @constructor HtmlCell |
|
281 | * @constructor HtmlCell | |
267 | * @class HtmlCell |
|
282 | * @class HtmlCell | |
268 | * @extends Ipython.TextCell |
|
283 | * @extends Ipython.TextCell | |
269 | */ |
|
284 | */ | |
270 | var HTMLCell = function () { |
|
285 | var HTMLCell = function () { | |
271 | this.placeholder = "Type <strong>HTML</strong> and LaTeX: $\\alpha^2$"; |
|
286 | this.placeholder = "Type <strong>HTML</strong> and LaTeX: $\\alpha^2$"; | |
272 | IPython.TextCell.apply(this, arguments); |
|
287 | IPython.TextCell.apply(this, arguments); | |
273 | this.cell_type = 'html'; |
|
288 | this.cell_type = 'html'; | |
274 | }; |
|
289 | }; | |
275 |
|
290 | |||
276 |
|
291 | |||
277 | HTMLCell.prototype = new TextCell(); |
|
292 | HTMLCell.prototype = new TextCell(); | |
278 |
|
293 | |||
279 | /** |
|
294 | /** | |
280 | * @method render |
|
295 | * @method render | |
281 | */ |
|
296 | */ | |
282 | HTMLCell.prototype.render = function () { |
|
297 | HTMLCell.prototype.render = function () { | |
283 | if (this.rendered === false) { |
|
298 | if (this.rendered === false) { | |
284 | var text = this.get_text(); |
|
299 | var text = this.get_text(); | |
285 | if (text === "") { text = this.placeholder; } |
|
300 | if (text === "") { text = this.placeholder; } | |
286 | this.set_rendered(text); |
|
301 | this.set_rendered(text); | |
287 | this.typeset(); |
|
302 | this.typeset(); | |
288 | this.element.find('div.text_cell_input').hide(); |
|
303 | this.element.find('div.text_cell_input').hide(); | |
289 | this.element.find("div.text_cell_render").show(); |
|
304 | this.element.find("div.text_cell_render").show(); | |
290 | this.rendered = true; |
|
305 | this.rendered = true; | |
291 | } |
|
306 | } | |
292 | }; |
|
307 | }; | |
293 |
|
308 | |||
294 |
|
309 | |||
295 | /** |
|
310 | /** | |
296 | * @class MarkdownCell |
|
311 | * @class MarkdownCell | |
297 | * @constructor MarkdownCell |
|
312 | * @constructor MarkdownCell | |
298 | * @extends Ipython.HtmlCell |
|
313 | * @extends Ipython.HtmlCell | |
299 | */ |
|
314 | */ | |
300 | var MarkdownCell = function () { |
|
315 | var MarkdownCell = function () { | |
301 | this.placeholder = "Type *Markdown* and LaTeX: $\\alpha^2$"; |
|
316 | this.placeholder = "Type *Markdown* and LaTeX: $\\alpha^2$"; | |
302 | IPython.TextCell.apply(this, arguments); |
|
317 | IPython.TextCell.apply(this, arguments); | |
303 | this.cell_type = 'markdown'; |
|
318 | this.cell_type = 'markdown'; | |
304 | }; |
|
319 | }; | |
305 |
|
320 | |||
306 |
|
321 | |||
307 | MarkdownCell.prototype = new TextCell(); |
|
322 | MarkdownCell.prototype = new TextCell(); | |
308 |
|
323 | |||
309 | /** |
|
324 | /** | |
310 | * @method render |
|
325 | * @method render | |
311 | */ |
|
326 | */ | |
312 | MarkdownCell.prototype.render = function () { |
|
327 | MarkdownCell.prototype.render = function () { | |
313 | if (this.rendered === false) { |
|
328 | if (this.rendered === false) { | |
314 | var text = this.get_text(); |
|
329 | var text = this.get_text(); | |
315 | if (text === "") { text = this.placeholder; } |
|
330 | if (text === "") { text = this.placeholder; } | |
316 | text = IPython.mathjaxutils.remove_math(text) |
|
331 | text = IPython.mathjaxutils.remove_math(text) | |
317 | var html = IPython.markdown_converter.makeHtml(text); |
|
332 | var html = IPython.markdown_converter.makeHtml(text); | |
318 | html = IPython.mathjaxutils.replace_math(html) |
|
333 | html = IPython.mathjaxutils.replace_math(html) | |
319 | try { |
|
334 | try { | |
320 | this.set_rendered(html); |
|
335 | this.set_rendered(html); | |
321 | } catch (e) { |
|
336 | } catch (e) { | |
322 | console.log("Error running Javascript in Markdown:"); |
|
337 | console.log("Error running Javascript in Markdown:"); | |
323 | console.log(e); |
|
338 | console.log(e); | |
324 | this.set_rendered($("<div/>").addClass("js-error").html( |
|
339 | this.set_rendered($("<div/>").addClass("js-error").html( | |
325 | "Error rendering Markdown!<br/>" + e.toString()) |
|
340 | "Error rendering Markdown!<br/>" + e.toString()) | |
326 | ); |
|
341 | ); | |
327 | } |
|
342 | } | |
328 | this.element.find('div.text_cell_input').hide(); |
|
343 | this.element.find('div.text_cell_input').hide(); | |
329 | this.element.find("div.text_cell_render").show(); |
|
344 | this.element.find("div.text_cell_render").show(); | |
330 | var code_snippets = this.element.find("pre > code"); |
|
345 | var code_snippets = this.element.find("pre > code"); | |
331 | code_snippets.replaceWith(function () { |
|
346 | code_snippets.replaceWith(function () { | |
332 | var code = $(this).html(); |
|
347 | var code = $(this).html(); | |
333 | /* Substitute br for newlines and for spaces |
|
348 | /* Substitute br for newlines and for spaces | |
334 | before highlighting, since prettify doesn't |
|
349 | before highlighting, since prettify doesn't | |
335 | preserve those on all browsers */ |
|
350 | preserve those on all browsers */ | |
336 | code = code.replace(/(\r\n|\n|\r)/gm, "<br/>"); |
|
351 | code = code.replace(/(\r\n|\n|\r)/gm, "<br/>"); | |
337 | code = code.replace(/ /gm, ' '); |
|
352 | code = code.replace(/ /gm, ' '); | |
338 | code = prettyPrintOne(code); |
|
353 | code = prettyPrintOne(code); | |
339 |
|
354 | |||
340 | return '<code class="prettyprint">' + code + '</code>'; |
|
355 | return '<code class="prettyprint">' + code + '</code>'; | |
341 | }); |
|
356 | }); | |
342 | this.typeset() |
|
357 | this.typeset() | |
343 | this.rendered = true; |
|
358 | this.rendered = true; | |
344 | } |
|
359 | } | |
345 | }; |
|
360 | }; | |
346 |
|
361 | |||
347 |
|
362 | |||
348 | // RawCell |
|
363 | // RawCell | |
349 |
|
364 | |||
350 | /** |
|
365 | /** | |
351 | * @class RawCell |
|
366 | * @class RawCell | |
352 | * @constructor RawCell |
|
367 | * @constructor RawCell | |
353 | * @extends Ipython.TextCell |
|
368 | * @extends Ipython.TextCell | |
354 | */ |
|
369 | */ | |
355 | var RawCell = function () { |
|
370 | var RawCell = function () { | |
356 | this.placeholder = "Type plain text and LaTeX: $\\alpha^2$"; |
|
371 | this.placeholder = "Type plain text and LaTeX: $\\alpha^2$"; | |
357 | this.code_mirror_mode = 'rst'; |
|
372 | this.code_mirror_mode = 'rst'; | |
358 | IPython.TextCell.apply(this, arguments); |
|
373 | IPython.TextCell.apply(this, arguments); | |
359 | this.cell_type = 'raw'; |
|
374 | this.cell_type = 'raw'; | |
360 | var that = this |
|
375 | var that = this | |
361 |
|
376 | |||
362 | this.element.focusout( |
|
377 | this.element.focusout( | |
363 | function() { that.auto_highlight(); } |
|
378 | function() { that.auto_highlight(); } | |
364 | ); |
|
379 | ); | |
365 | }; |
|
380 | }; | |
366 |
|
381 | |||
367 |
|
382 | |||
368 | RawCell.prototype = new TextCell(); |
|
383 | RawCell.prototype = new TextCell(); | |
369 |
|
384 | |||
370 | /** |
|
385 | /** | |
371 | * Trigger autodetection of highlight scheme for current cell |
|
386 | * Trigger autodetection of highlight scheme for current cell | |
372 | * @method auto_highlight |
|
387 | * @method auto_highlight | |
373 | */ |
|
388 | */ | |
374 | RawCell.prototype.auto_highlight = function () { |
|
389 | RawCell.prototype.auto_highlight = function () { | |
375 | this._auto_highlight(IPython.config.raw_cell_highlight); |
|
390 | this._auto_highlight(IPython.config.raw_cell_highlight); | |
376 | }; |
|
391 | }; | |
377 |
|
392 | |||
378 | /** @method render **/ |
|
393 | /** @method render **/ | |
379 | RawCell.prototype.render = function () { |
|
394 | RawCell.prototype.render = function () { | |
380 | this.rendered = true; |
|
395 | this.rendered = true; | |
381 | this.edit(); |
|
396 | this.edit(); | |
382 | }; |
|
397 | }; | |
383 |
|
398 | |||
384 |
|
399 | |||
385 | /** @method handle_codemirror_keyevent **/ |
|
400 | /** @method handle_codemirror_keyevent **/ | |
386 | RawCell.prototype.handle_codemirror_keyevent = function (editor, event) { |
|
401 | RawCell.prototype.handle_codemirror_keyevent = function (editor, event) { | |
387 |
|
402 | |||
388 | var that = this; |
|
403 | var that = this; | |
389 | if (event.which === key.UPARROW && event.type === 'keydown') { |
|
404 | if (event.which === key.UPARROW && event.type === 'keydown') { | |
390 | // If we are not at the top, let CM handle the up arrow and |
|
405 | // If we are not at the top, let CM handle the up arrow and | |
391 | // prevent the global keydown handler from handling it. |
|
406 | // prevent the global keydown handler from handling it. | |
392 | if (!that.at_top()) { |
|
407 | if (!that.at_top()) { | |
393 | event.stop(); |
|
408 | event.stop(); | |
394 | return false; |
|
409 | return false; | |
395 | } else { |
|
410 | } else { | |
396 | return true; |
|
411 | return true; | |
397 | }; |
|
412 | }; | |
398 | } else if (event.which === key.DOWNARROW && event.type === 'keydown') { |
|
413 | } else if (event.which === key.DOWNARROW && event.type === 'keydown') { | |
399 | // If we are not at the bottom, let CM handle the down arrow and |
|
414 | // If we are not at the bottom, let CM handle the down arrow and | |
400 | // prevent the global keydown handler from handling it. |
|
415 | // prevent the global keydown handler from handling it. | |
401 | if (!that.at_bottom()) { |
|
416 | if (!that.at_bottom()) { | |
402 | event.stop(); |
|
417 | event.stop(); | |
403 | return false; |
|
418 | return false; | |
404 | } else { |
|
419 | } else { | |
405 | return true; |
|
420 | return true; | |
406 | }; |
|
421 | }; | |
407 | }; |
|
422 | }; | |
408 | return false; |
|
423 | return false; | |
409 | }; |
|
424 | }; | |
410 |
|
425 | |||
411 | /** @method select **/ |
|
426 | /** @method select **/ | |
412 | RawCell.prototype.select = function () { |
|
427 | RawCell.prototype.select = function () { | |
413 | IPython.Cell.prototype.select.apply(this); |
|
428 | IPython.Cell.prototype.select.apply(this); | |
414 | this.code_mirror.refresh(); |
|
429 | this.code_mirror.refresh(); | |
415 | this.code_mirror.focus(); |
|
430 | this.code_mirror.focus(); | |
416 | }; |
|
431 | }; | |
417 |
|
432 | |||
418 | /** @method at_top **/ |
|
433 | /** @method at_top **/ | |
419 | RawCell.prototype.at_top = function () { |
|
434 | RawCell.prototype.at_top = function () { | |
420 | var cursor = this.code_mirror.getCursor(); |
|
435 | var cursor = this.code_mirror.getCursor(); | |
421 | if (cursor.line === 0 && cursor.ch === 0) { |
|
436 | if (cursor.line === 0 && cursor.ch === 0) { | |
422 | return true; |
|
437 | return true; | |
423 | } else { |
|
438 | } else { | |
424 | return false; |
|
439 | return false; | |
425 | } |
|
440 | } | |
426 | }; |
|
441 | }; | |
427 |
|
442 | |||
428 |
|
443 | |||
429 | /** @method at_bottom **/ |
|
444 | /** @method at_bottom **/ | |
430 | RawCell.prototype.at_bottom = function () { |
|
445 | RawCell.prototype.at_bottom = function () { | |
431 | var cursor = this.code_mirror.getCursor(); |
|
446 | var cursor = this.code_mirror.getCursor(); | |
432 | if (cursor.line === (this.code_mirror.lineCount()-1) && cursor.ch === this.code_mirror.getLine(cursor.line).length) { |
|
447 | if (cursor.line === (this.code_mirror.lineCount()-1) && cursor.ch === this.code_mirror.getLine(cursor.line).length) { | |
433 | return true; |
|
448 | return true; | |
434 | } else { |
|
449 | } else { | |
435 | return false; |
|
450 | return false; | |
436 | } |
|
451 | } | |
437 | }; |
|
452 | }; | |
438 |
|
453 | |||
439 |
|
454 | |||
440 | /** |
|
455 | /** | |
441 | * @class HeadingCell |
|
456 | * @class HeadingCell | |
442 | * @extends Ipython.TextCell |
|
457 | * @extends Ipython.TextCell | |
443 | */ |
|
458 | */ | |
444 |
|
459 | |||
445 | /** |
|
460 | /** | |
446 | * @constructor HeadingCell |
|
461 | * @constructor HeadingCell | |
447 | * @extends Ipython.TextCell |
|
462 | * @extends Ipython.TextCell | |
448 | */ |
|
463 | */ | |
449 | var HeadingCell = function () { |
|
464 | var HeadingCell = function () { | |
450 | this.placeholder = "Type Heading Here"; |
|
465 | this.placeholder = "Type Heading Here"; | |
451 | IPython.TextCell.apply(this, arguments); |
|
466 | IPython.TextCell.apply(this, arguments); | |
452 | /** |
|
467 | /** | |
453 | * heading level of the cell, use getter and setter to access |
|
468 | * heading level of the cell, use getter and setter to access | |
454 | * @property level |
|
469 | * @property level | |
455 | */ |
|
470 | */ | |
456 | this.level = 1; |
|
471 | this.level = 1; | |
457 | this.cell_type = 'heading'; |
|
472 | this.cell_type = 'heading'; | |
458 | }; |
|
473 | }; | |
459 |
|
474 | |||
460 |
|
475 | |||
461 | HeadingCell.prototype = new TextCell(); |
|
476 | HeadingCell.prototype = new TextCell(); | |
462 |
|
477 | |||
463 | /** @method fromJSON */ |
|
478 | /** @method fromJSON */ | |
464 | HeadingCell.prototype.fromJSON = function (data) { |
|
479 | HeadingCell.prototype.fromJSON = function (data) { | |
465 | if (data.level != undefined){ |
|
480 | if (data.level != undefined){ | |
466 | this.level = data.level; |
|
481 | this.level = data.level; | |
467 | } |
|
482 | } | |
468 | IPython.TextCell.prototype.fromJSON.apply(this, arguments); |
|
483 | IPython.TextCell.prototype.fromJSON.apply(this, arguments); | |
469 | }; |
|
484 | }; | |
470 |
|
485 | |||
471 |
|
486 | |||
472 | /** @method toJSON */ |
|
487 | /** @method toJSON */ | |
473 | HeadingCell.prototype.toJSON = function () { |
|
488 | HeadingCell.prototype.toJSON = function () { | |
474 | var data = IPython.TextCell.prototype.toJSON.apply(this); |
|
489 | var data = IPython.TextCell.prototype.toJSON.apply(this); | |
475 | data.level = this.get_level(); |
|
490 | data.level = this.get_level(); | |
476 | return data; |
|
491 | return data; | |
477 | }; |
|
492 | }; | |
478 |
|
493 | |||
479 |
|
494 | |||
480 | /** |
|
495 | /** | |
481 | * Change heading level of cell, and re-render |
|
496 | * Change heading level of cell, and re-render | |
482 | * @method set_level |
|
497 | * @method set_level | |
483 | */ |
|
498 | */ | |
484 | HeadingCell.prototype.set_level = function (level) { |
|
499 | HeadingCell.prototype.set_level = function (level) { | |
485 | this.level = level; |
|
500 | this.level = level; | |
486 | if (this.rendered) { |
|
501 | if (this.rendered) { | |
487 | this.rendered = false; |
|
502 | this.rendered = false; | |
488 | this.render(); |
|
503 | this.render(); | |
489 | }; |
|
504 | }; | |
490 | }; |
|
505 | }; | |
491 |
|
506 | |||
492 | /** The depth of header cell, based on html (h1 to h6) |
|
507 | /** The depth of header cell, based on html (h1 to h6) | |
493 | * @method get_level |
|
508 | * @method get_level | |
494 | * @return {integer} level - for 1 to 6 |
|
509 | * @return {integer} level - for 1 to 6 | |
495 | */ |
|
510 | */ | |
496 | HeadingCell.prototype.get_level = function () { |
|
511 | HeadingCell.prototype.get_level = function () { | |
497 | return this.level; |
|
512 | return this.level; | |
498 | }; |
|
513 | }; | |
499 |
|
514 | |||
500 |
|
515 | |||
501 | HeadingCell.prototype.set_rendered = function (text) { |
|
516 | HeadingCell.prototype.set_rendered = function (text) { | |
502 | var r = this.element.find("div.text_cell_render"); |
|
517 | var r = this.element.find("div.text_cell_render"); | |
503 | r.empty(); |
|
518 | r.empty(); | |
504 | r.append($('<h'+this.level+'/>').html(text)); |
|
519 | r.append($('<h'+this.level+'/>').html(text)); | |
505 | }; |
|
520 | }; | |
506 |
|
521 | |||
507 |
|
522 | |||
508 | HeadingCell.prototype.get_rendered = function () { |
|
523 | HeadingCell.prototype.get_rendered = function () { | |
509 | var r = this.element.find("div.text_cell_render"); |
|
524 | var r = this.element.find("div.text_cell_render"); | |
510 | return r.children().first().html(); |
|
525 | return r.children().first().html(); | |
511 | }; |
|
526 | }; | |
512 |
|
527 | |||
513 |
|
528 | |||
514 | HeadingCell.prototype.render = function () { |
|
529 | HeadingCell.prototype.render = function () { | |
515 | if (this.rendered === false) { |
|
530 | if (this.rendered === false) { | |
516 | var text = this.get_text(); |
|
531 | var text = this.get_text(); | |
517 | if (text === "") { text = this.placeholder; } |
|
532 | if (text === "") { text = this.placeholder; } | |
518 | this.set_rendered(text); |
|
533 | this.set_rendered(text); | |
519 | this.typeset(); |
|
534 | this.typeset(); | |
520 | this.element.find('div.text_cell_input').hide(); |
|
535 | this.element.find('div.text_cell_input').hide(); | |
521 | this.element.find("div.text_cell_render").show(); |
|
536 | this.element.find("div.text_cell_render").show(); | |
522 | this.rendered = true; |
|
537 | this.rendered = true; | |
523 | }; |
|
538 | }; | |
524 | }; |
|
539 | }; | |
525 |
|
540 | |||
526 | IPython.TextCell = TextCell; |
|
541 | IPython.TextCell = TextCell; | |
527 | IPython.HTMLCell = HTMLCell; |
|
542 | IPython.HTMLCell = HTMLCell; | |
528 | IPython.MarkdownCell = MarkdownCell; |
|
543 | IPython.MarkdownCell = MarkdownCell; | |
529 | IPython.RawCell = RawCell; |
|
544 | IPython.RawCell = RawCell; | |
530 | IPython.HeadingCell = HeadingCell; |
|
545 | IPython.HeadingCell = HeadingCell; | |
531 |
|
546 | |||
532 |
|
547 | |||
533 | return IPython; |
|
548 | return IPython; | |
534 |
|
549 | |||
535 | }(IPython)); |
|
550 | }(IPython)); | |
536 |
|
551 |
General Comments 0
You need to be logged in to leave comments.
Login now