Show More
@@ -1,487 +1,528 | |||||
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 | */ |
|
14 | */ | |
15 | var IPython = (function (IPython) { |
|
15 | var IPython = (function (IPython) { | |
16 |
|
16 | |||
17 | // TextCell base class |
|
17 | // TextCell base class | |
18 | var key = IPython.utils.keycodes; |
|
18 | var key = IPython.utils.keycodes; | |
19 |
|
19 | |||
20 | /** |
|
20 | /** | |
|
21 | * Construct a new TextCell, codemirror mode is by default 'htmlmixed', and cell type is 'text' | |||
|
22 | * cell start as not redered. | |||
|
23 | * | |||
21 | * @class TextCell |
|
24 | * @class TextCell | |
22 |
* @construct |
|
25 | * @constructor TextCell | |
23 | */ |
|
26 | */ | |
24 | var TextCell = function () { |
|
27 | var TextCell = function () { | |
25 | this.code_mirror_mode = this.code_mirror_mode || 'htmlmixed'; |
|
28 | this.code_mirror_mode = this.code_mirror_mode || 'htmlmixed'; | |
26 | IPython.Cell.apply(this, arguments); |
|
29 | IPython.Cell.apply(this, arguments); | |
27 | this.rendered = false; |
|
30 | this.rendered = false; | |
28 | this.cell_type = this.cell_type || 'text'; |
|
31 | this.cell_type = this.cell_type || 'text'; | |
29 | }; |
|
32 | }; | |
30 |
|
33 | |||
31 | TextCell.prototype = new IPython.Cell(); |
|
34 | TextCell.prototype = new IPython.Cell(); | |
32 |
|
35 | |||
33 | /** create the DOM element of the TextCell |
|
36 | /** | |
|
37 | * Create the DOM element of the TextCell | |||
34 | * @method create_element |
|
38 | * @method create_element | |
35 | * @private |
|
39 | * @private | |
36 | */ |
|
40 | */ | |
37 | TextCell.prototype.create_element = function () { |
|
41 | TextCell.prototype.create_element = function () { | |
38 | var cell = $("<div>").addClass('cell text_cell border-box-sizing'); |
|
42 | var cell = $("<div>").addClass('cell text_cell border-box-sizing'); | |
39 | cell.attr('tabindex','2'); |
|
43 | cell.attr('tabindex','2'); | |
40 | var input_area = $('<div/>').addClass('text_cell_input border-box-sizing'); |
|
44 | var input_area = $('<div/>').addClass('text_cell_input border-box-sizing'); | |
41 | this.code_mirror = CodeMirror(input_area.get(0), { |
|
45 | this.code_mirror = CodeMirror(input_area.get(0), { | |
42 | indentUnit : 4, |
|
46 | indentUnit : 4, | |
43 | mode: this.code_mirror_mode, |
|
47 | mode: this.code_mirror_mode, | |
44 | theme: 'default', |
|
48 | theme: 'default', | |
45 | value: this.placeholder, |
|
49 | value: this.placeholder, | |
46 | readOnly: this.read_only, |
|
50 | readOnly: this.read_only, | |
47 | lineWrapping : true, |
|
51 | lineWrapping : true, | |
48 | extraKeys: {"Tab": "indentMore","Shift-Tab" : "indentLess"}, |
|
52 | extraKeys: {"Tab": "indentMore","Shift-Tab" : "indentLess"}, | |
49 | onKeyEvent: $.proxy(this.handle_codemirror_keyevent,this) |
|
53 | onKeyEvent: $.proxy(this.handle_codemirror_keyevent,this) | |
50 | }); |
|
54 | }); | |
51 | // The tabindex=-1 makes this div focusable. |
|
55 | // The tabindex=-1 makes this div focusable. | |
52 | var render_area = $('<div/>').addClass('text_cell_render border-box-sizing'). |
|
56 | var render_area = $('<div/>').addClass('text_cell_render border-box-sizing'). | |
53 | addClass('rendered_html').attr('tabindex','-1'); |
|
57 | addClass('rendered_html').attr('tabindex','-1'); | |
54 | cell.append(input_area).append(render_area); |
|
58 | cell.append(input_area).append(render_area); | |
55 | this.element = cell; |
|
59 | this.element = cell; | |
56 | }; |
|
60 | }; | |
57 |
|
61 | |||
58 |
|
62 | |||
59 | /** bind the DOM evet to cell actions |
|
63 | /** | |
|
64 | * Bind the DOM evet to cell actions | |||
60 | * Need to be called after TextCell.create_element |
|
65 | * Need to be called after TextCell.create_element | |
61 | * @private |
|
66 | * @private | |
62 | * @method bind_event |
|
67 | * @method bind_event | |
63 | */ |
|
68 | */ | |
64 | TextCell.prototype.bind_events = function () { |
|
69 | TextCell.prototype.bind_events = function () { | |
65 | IPython.Cell.prototype.bind_events.apply(this); |
|
70 | IPython.Cell.prototype.bind_events.apply(this); | |
66 | var that = this; |
|
71 | var that = this; | |
67 | this.element.keydown(function (event) { |
|
72 | this.element.keydown(function (event) { | |
68 | if (event.which === 13 && !event.shiftKey) { |
|
73 | if (event.which === 13 && !event.shiftKey) { | |
69 | if (that.rendered) { |
|
74 | if (that.rendered) { | |
70 | that.edit(); |
|
75 | that.edit(); | |
71 | return false; |
|
76 | return false; | |
72 | }; |
|
77 | }; | |
73 | }; |
|
78 | }; | |
74 | }); |
|
79 | }); | |
75 | this.element.dblclick(function () { |
|
80 | this.element.dblclick(function () { | |
76 | that.edit(); |
|
81 | that.edit(); | |
77 | }); |
|
82 | }); | |
78 | }; |
|
83 | }; | |
79 |
|
84 | |||
80 | /** This method gets called in CodeMirror's onKeyDown/onKeyPress |
|
85 | /** | |
|
86 | * This method gets called in CodeMirror's onKeyDown/onKeyPress | |||
81 | * handlers and is used to provide custom key handling. |
|
87 | * handlers and is used to provide custom key handling. | |
82 | * |
|
88 | * | |
|
89 | * Subclass should override this method to have custom handeling | |||
|
90 | * | |||
83 | * @method handle_codemirror_keyevent |
|
91 | * @method handle_codemirror_keyevent | |
84 |
* @param {CodeMirror |
|
92 | * @param {CodeMirror} editor - The codemirror instance bound to the cell | |
85 | * @param {event} event - |
|
93 | * @param {event} event - | |
86 |
* @return {Boolean} |
|
94 | * @return {Boolean} `true` if CodeMirror should ignore the event, `false` Otherwise | |
87 | */ |
|
95 | */ | |
88 | TextCell.prototype.handle_codemirror_keyevent = function (editor, event) { |
|
96 | TextCell.prototype.handle_codemirror_keyevent = function (editor, event) { | |
89 |
|
97 | |||
90 | if (event.keyCode === 13 && (event.shiftKey || event.ctrlKey)) { |
|
98 | if (event.keyCode === 13 && (event.shiftKey || event.ctrlKey)) { | |
91 | // Always ignore shift-enter in CodeMirror as we handle it. |
|
99 | // Always ignore shift-enter in CodeMirror as we handle it. | |
92 | return true; |
|
100 | return true; | |
93 | } |
|
101 | } | |
94 | return false; |
|
102 | return false; | |
95 | }; |
|
103 | }; | |
96 |
|
104 | |||
97 | /** |
|
105 | /** | |
98 | * Select the current cell |
|
106 | * Select the current cell and trigger 'focus' | |
99 | * @method select |
|
107 | * @method select | |
100 | */ |
|
108 | */ | |
101 | TextCell.prototype.select = function () { |
|
109 | TextCell.prototype.select = function () { | |
102 | IPython.Cell.prototype.select.apply(this); |
|
110 | IPython.Cell.prototype.select.apply(this); | |
103 | var output = this.element.find("div.text_cell_render"); |
|
111 | var output = this.element.find("div.text_cell_render"); | |
104 | output.trigger('focus'); |
|
112 | output.trigger('focus'); | |
105 | }; |
|
113 | }; | |
106 |
|
114 | |||
107 | /** unselect the current cell |
|
115 | /** | |
|
116 | * unselect the current cell and `render` it | |||
108 | * @method unselect |
|
117 | * @method unselect | |
109 | */ |
|
118 | */ | |
110 | TextCell.prototype.unselect = function() { |
|
119 | TextCell.prototype.unselect = function() { | |
111 | // render on selection of another cell |
|
120 | // render on selection of another cell | |
112 | this.render(); |
|
121 | this.render(); | |
113 | IPython.Cell.prototype.unselect.apply(this); |
|
122 | IPython.Cell.prototype.unselect.apply(this); | |
114 | }; |
|
123 | }; | |
115 |
|
124 | |||
116 | /** put the current cell in edition mode |
|
125 | /** | |
|
126 | * | |||
|
127 | * put the current cell in edition mode | |||
117 | * @method edit |
|
128 | * @method edit | |
118 | */ |
|
129 | */ | |
119 | TextCell.prototype.edit = function () { |
|
130 | TextCell.prototype.edit = function () { | |
120 | if ( this.read_only ) return; |
|
131 | if ( this.read_only ) return; | |
121 | if (this.rendered === true) { |
|
132 | if (this.rendered === true) { | |
122 | var text_cell = this.element; |
|
133 | var text_cell = this.element; | |
123 | var output = text_cell.find("div.text_cell_render"); |
|
134 | var output = text_cell.find("div.text_cell_render"); | |
124 | output.hide(); |
|
135 | output.hide(); | |
125 | text_cell.find('div.text_cell_input').show(); |
|
136 | text_cell.find('div.text_cell_input').show(); | |
126 | this.code_mirror.refresh(); |
|
137 | this.code_mirror.refresh(); | |
127 | this.code_mirror.focus(); |
|
138 | this.code_mirror.focus(); | |
128 | // We used to need an additional refresh() after the focus, but |
|
139 | // We used to need an additional refresh() after the focus, but | |
129 | // it appears that this has been fixed in CM. This bug would show |
|
140 | // it appears that this has been fixed in CM. This bug would show | |
130 | // up on FF when a newly loaded markdown cell was edited. |
|
141 | // up on FF when a newly loaded markdown cell was edited. | |
131 | this.rendered = false; |
|
142 | this.rendered = false; | |
132 | if (this.get_text() === this.placeholder) { |
|
143 | if (this.get_text() === this.placeholder) { | |
133 | this.set_text(''); |
|
144 | this.set_text(''); | |
134 | this.refresh(); |
|
145 | this.refresh(); | |
135 | } |
|
146 | } | |
136 | } |
|
147 | } | |
137 | }; |
|
148 | }; | |
138 |
|
149 | |||
139 |
|
150 | |||
140 | /** Subclasses must define render. |
|
151 | /** | |
|
152 | * Empty, Subclasses must define render. | |||
141 | * @method render |
|
153 | * @method render | |
142 | */ |
|
154 | */ | |
143 | TextCell.prototype.render = function () {}; |
|
155 | TextCell.prototype.render = function () {}; | |
144 |
|
156 | |||
145 |
|
157 | |||
146 | /** |
|
158 | /** | |
147 | * setter: {{#crossLink "TextCell/set_text"}}{{/crossLink}} |
|
159 | * setter: {{#crossLink "TextCell/set_text"}}{{/crossLink}} | |
148 | * @method get_text |
|
160 | * @method get_text | |
149 | * @retrun {string} CodeMirror current text value |
|
161 | * @retrun {string} CodeMirror current text value | |
150 | */ |
|
162 | */ | |
151 | TextCell.prototype.get_text = function() { |
|
163 | TextCell.prototype.get_text = function() { | |
152 | return this.code_mirror.getValue(); |
|
164 | return this.code_mirror.getValue(); | |
153 | }; |
|
165 | }; | |
154 |
|
166 | |||
155 | /** |
|
167 | /** | |
156 | * @param {string} text - Codemiror text value |
|
168 | * @param {string} text - Codemiror text value | |
157 | * @see TextCell#get_text |
|
169 | * @see TextCell#get_text | |
158 | * @method set_text |
|
170 | * @method set_text | |
159 | * */ |
|
171 | * */ | |
160 | TextCell.prototype.set_text = function(text) { |
|
172 | TextCell.prototype.set_text = function(text) { | |
161 | this.code_mirror.setValue(text); |
|
173 | this.code_mirror.setValue(text); | |
162 | this.code_mirror.refresh(); |
|
174 | this.code_mirror.refresh(); | |
163 | }; |
|
175 | }; | |
164 |
|
176 | |||
165 | /** |
|
177 | /** | |
166 | * setter :{{#crossLink "TextCell/set_rendered"}}{{/crossLink}} |
|
178 | * setter :{{#crossLink "TextCell/set_rendered"}}{{/crossLink}} | |
167 | * @method get_rendered |
|
179 | * @method get_rendered | |
168 | * @return {html} html of rendered element |
|
180 | * @return {html} html of rendered element | |
169 | * */ |
|
181 | * */ | |
170 | TextCell.prototype.get_rendered = function() { |
|
182 | TextCell.prototype.get_rendered = function() { | |
171 | return this.element.find('div.text_cell_render').html(); |
|
183 | return this.element.find('div.text_cell_render').html(); | |
172 | }; |
|
184 | }; | |
173 |
|
185 | |||
174 | /** |
|
186 | /** | |
175 | * @method set_rendered |
|
187 | * @method set_rendered | |
176 | */ |
|
188 | */ | |
177 | TextCell.prototype.set_rendered = function(text) { |
|
189 | TextCell.prototype.set_rendered = function(text) { | |
178 | this.element.find('div.text_cell_render').html(text); |
|
190 | this.element.find('div.text_cell_render').html(text); | |
179 | }; |
|
191 | }; | |
180 |
|
192 | |||
181 | /** |
|
193 | /** | |
182 | * not deprecated, but implementation wrong |
|
194 | * not deprecated, but implementation wrong | |
183 | * @method at_top |
|
195 | * @method at_top | |
184 | * @deprecated |
|
196 | * @deprecated | |
185 | * @return {Boolean} true is cell rendered, false otherwise |
|
197 | * @return {Boolean} true is cell rendered, false otherwise | |
186 | * I doubt this is what it is supposed to do |
|
198 | * I doubt this is what it is supposed to do | |
187 | * this implementation is completly false |
|
199 | * this implementation is completly false | |
188 | */ |
|
200 | */ | |
189 | TextCell.prototype.at_top = function () { |
|
201 | TextCell.prototype.at_top = function () { | |
190 | if (this.rendered) { |
|
202 | if (this.rendered) { | |
191 | return true; |
|
203 | return true; | |
192 | } else { |
|
204 | } else { | |
193 | return false; |
|
205 | return false; | |
194 | } |
|
206 | } | |
195 | }; |
|
207 | }; | |
196 |
|
208 | |||
197 |
|
209 | |||
198 | /** |
|
210 | /** | |
199 | * not deprecated, but implementation wrong |
|
211 | * not deprecated, but implementation wrong | |
200 | * @method at_bottom |
|
212 | * @method at_bottom | |
201 | * @deprecated |
|
213 | * @deprecated | |
202 | * @return {Boolean} true is cell rendered, false otherwise |
|
214 | * @return {Boolean} true is cell rendered, false otherwise | |
203 | * I doubt this is what it is supposed to do |
|
215 | * I doubt this is what it is supposed to do | |
204 | * this implementation is completly false |
|
216 | * this implementation is completly false | |
205 | * */ |
|
217 | * */ | |
206 | TextCell.prototype.at_bottom = function () { |
|
218 | TextCell.prototype.at_bottom = function () { | |
207 | if (this.rendered) { |
|
219 | if (this.rendered) { | |
208 | return true; |
|
220 | return true; | |
209 | } else { |
|
221 | } else { | |
210 | return false; |
|
222 | return false; | |
211 | } |
|
223 | } | |
212 | }; |
|
224 | }; | |
213 |
|
225 | |||
214 | /** Create Text cell from JSON |
|
226 | /** | |
|
227 | * Create Text cell from JSON | |||
215 | * @param {json} data - JSON serialized text-cell |
|
228 | * @param {json} data - JSON serialized text-cell | |
|
229 | * @method fromJSON | |||
216 | */ |
|
230 | */ | |
217 | TextCell.prototype.fromJSON = function (data) { |
|
231 | TextCell.prototype.fromJSON = function (data) { | |
218 | IPython.Cell.prototype.fromJSON.apply(this, arguments); |
|
232 | IPython.Cell.prototype.fromJSON.apply(this, arguments); | |
219 | if (data.cell_type === this.cell_type) { |
|
233 | if (data.cell_type === this.cell_type) { | |
220 | if (data.source !== undefined) { |
|
234 | if (data.source !== undefined) { | |
221 | this.set_text(data.source); |
|
235 | this.set_text(data.source); | |
222 | // make this value the starting point, so that we can only undo |
|
236 | // make this value the starting point, so that we can only undo | |
223 | // to this state, instead of a blank cell |
|
237 | // to this state, instead of a blank cell | |
224 | this.code_mirror.clearHistory(); |
|
238 | this.code_mirror.clearHistory(); | |
225 | this.set_rendered(data.rendered || ''); |
|
239 | this.set_rendered(data.rendered || ''); | |
226 | this.rendered = false; |
|
240 | this.rendered = false; | |
227 | this.render(); |
|
241 | this.render(); | |
228 | } |
|
242 | } | |
229 | } |
|
243 | } | |
230 | }; |
|
244 | }; | |
231 |
|
245 | |||
232 |
|
246 | /** Generate JSON from cell | ||
|
247 | * @return {object} cell data serialised to json | |||
|
248 | */ | |||
233 | TextCell.prototype.toJSON = function () { |
|
249 | TextCell.prototype.toJSON = function () { | |
234 | var data = IPython.Cell.prototype.toJSON.apply(this); |
|
250 | var data = IPython.Cell.prototype.toJSON.apply(this); | |
235 | data.cell_type = this.cell_type; |
|
251 | data.cell_type = this.cell_type; | |
236 | data.source = this.get_text(); |
|
252 | data.source = this.get_text(); | |
237 | return data; |
|
253 | return data; | |
238 | }; |
|
254 | }; | |
239 |
|
255 | |||
240 |
|
256 | |||
241 | /** |
|
257 | /** | |
242 |
* @construct |
|
258 | * @constructor HtmlCell | |
243 | * @class HtmlCell |
|
259 | * @class HtmlCell | |
244 | * @extends TextCell |
|
260 | * @extends TextCell | |
245 | */ |
|
261 | */ | |
246 | var HTMLCell = function () { |
|
262 | var HTMLCell = function () { | |
247 | this.placeholder = "Type <strong>HTML</strong> and LaTeX: $\\alpha^2$"; |
|
263 | this.placeholder = "Type <strong>HTML</strong> and LaTeX: $\\alpha^2$"; | |
248 | IPython.TextCell.apply(this, arguments); |
|
264 | IPython.TextCell.apply(this, arguments); | |
249 | this.cell_type = 'html'; |
|
265 | this.cell_type = 'html'; | |
250 | }; |
|
266 | }; | |
251 |
|
267 | |||
252 |
|
268 | |||
253 | HTMLCell.prototype = new TextCell(); |
|
269 | HTMLCell.prototype = new TextCell(); | |
254 |
|
270 | |||
255 |
|
271 | /** | ||
|
272 | * @method render | |||
|
273 | */ | |||
256 | HTMLCell.prototype.render = function () { |
|
274 | HTMLCell.prototype.render = function () { | |
257 | if (this.rendered === false) { |
|
275 | if (this.rendered === false) { | |
258 | var text = this.get_text(); |
|
276 | var text = this.get_text(); | |
259 | if (text === "") { text = this.placeholder; } |
|
277 | if (text === "") { text = this.placeholder; } | |
260 | this.set_rendered(text); |
|
278 | this.set_rendered(text); | |
261 | this.typeset(); |
|
279 | this.typeset(); | |
262 | this.element.find('div.text_cell_input').hide(); |
|
280 | this.element.find('div.text_cell_input').hide(); | |
263 | this.element.find("div.text_cell_render").show(); |
|
281 | this.element.find("div.text_cell_render").show(); | |
264 | this.rendered = true; |
|
282 | this.rendered = true; | |
265 | } |
|
283 | } | |
266 | }; |
|
284 | }; | |
267 |
|
285 | |||
268 |
|
286 | |||
269 | // MarkdownCell |
|
287 | /** | |
270 |
|
|
288 | * @class MarkdownCell | |
271 |
* @construct |
|
289 | * @constructor MarkdownCell | |
272 | * @extends HtmlCell |
|
290 | * @extends HtmlCell | |
273 | */ |
|
291 | */ | |
274 | var MarkdownCell = function () { |
|
292 | var MarkdownCell = function () { | |
275 | this.placeholder = "Type *Markdown* and LaTeX: $\\alpha^2$"; |
|
293 | this.placeholder = "Type *Markdown* and LaTeX: $\\alpha^2$"; | |
276 | IPython.TextCell.apply(this, arguments); |
|
294 | IPython.TextCell.apply(this, arguments); | |
277 | this.cell_type = 'markdown'; |
|
295 | this.cell_type = 'markdown'; | |
278 | }; |
|
296 | }; | |
279 |
|
297 | |||
280 |
|
298 | |||
281 | MarkdownCell.prototype = new TextCell(); |
|
299 | MarkdownCell.prototype = new TextCell(); | |
282 |
|
300 | |||
283 |
|
301 | /** | ||
|
302 | * @method render | |||
|
303 | */ | |||
284 | MarkdownCell.prototype.render = function () { |
|
304 | MarkdownCell.prototype.render = function () { | |
285 | if (this.rendered === false) { |
|
305 | if (this.rendered === false) { | |
286 | var text = this.get_text(); |
|
306 | var text = this.get_text(); | |
287 | if (text === "") { text = this.placeholder; } |
|
307 | if (text === "") { text = this.placeholder; } | |
288 | text = IPython.mathjaxutils.remove_math(text) |
|
308 | text = IPython.mathjaxutils.remove_math(text) | |
289 | var html = IPython.markdown_converter.makeHtml(text); |
|
309 | var html = IPython.markdown_converter.makeHtml(text); | |
290 | html = IPython.mathjaxutils.replace_math(html) |
|
310 | html = IPython.mathjaxutils.replace_math(html) | |
291 | try { |
|
311 | try { | |
292 | this.set_rendered(html); |
|
312 | this.set_rendered(html); | |
293 | } catch (e) { |
|
313 | } catch (e) { | |
294 | console.log("Error running Javascript in Markdown:"); |
|
314 | console.log("Error running Javascript in Markdown:"); | |
295 | console.log(e); |
|
315 | console.log(e); | |
296 | this.set_rendered($("<div/>").addClass("js-error").html( |
|
316 | this.set_rendered($("<div/>").addClass("js-error").html( | |
297 | "Error rendering Markdown!<br/>" + e.toString()) |
|
317 | "Error rendering Markdown!<br/>" + e.toString()) | |
298 | ); |
|
318 | ); | |
299 | } |
|
319 | } | |
300 | this.element.find('div.text_cell_input').hide(); |
|
320 | this.element.find('div.text_cell_input').hide(); | |
301 | this.element.find("div.text_cell_render").show(); |
|
321 | this.element.find("div.text_cell_render").show(); | |
302 | var code_snippets = this.element.find("pre > code"); |
|
322 | var code_snippets = this.element.find("pre > code"); | |
303 | code_snippets.replaceWith(function () { |
|
323 | code_snippets.replaceWith(function () { | |
304 | var code = $(this).html(); |
|
324 | var code = $(this).html(); | |
305 | /* Substitute br for newlines and for spaces |
|
325 | /* Substitute br for newlines and for spaces | |
306 | before highlighting, since prettify doesn't |
|
326 | before highlighting, since prettify doesn't | |
307 | preserve those on all browsers */ |
|
327 | preserve those on all browsers */ | |
308 | code = code.replace(/(\r\n|\n|\r)/gm, "<br/>"); |
|
328 | code = code.replace(/(\r\n|\n|\r)/gm, "<br/>"); | |
309 | code = code.replace(/ /gm, ' '); |
|
329 | code = code.replace(/ /gm, ' '); | |
310 | code = prettyPrintOne(code); |
|
330 | code = prettyPrintOne(code); | |
311 |
|
331 | |||
312 | return '<code class="prettyprint">' + code + '</code>'; |
|
332 | return '<code class="prettyprint">' + code + '</code>'; | |
313 | }); |
|
333 | }); | |
314 | this.typeset() |
|
334 | this.typeset() | |
315 | this.rendered = true; |
|
335 | this.rendered = true; | |
316 | } |
|
336 | } | |
317 | }; |
|
337 | }; | |
318 |
|
338 | |||
319 |
|
339 | |||
320 | // RawCell |
|
340 | // RawCell | |
321 |
|
341 | |||
322 | /** @construct RawCell |
|
342 | /** | |
|
343 | * @class RawCell | |||
|
344 | * @constructor RawCell | |||
323 | * @extends TextCell |
|
345 | * @extends TextCell | |
324 | */ |
|
346 | */ | |
325 | var RawCell = function () { |
|
347 | var RawCell = function () { | |
326 | this.placeholder = "Type plain text and LaTeX: $\\alpha^2$"; |
|
348 | this.placeholder = "Type plain text and LaTeX: $\\alpha^2$"; | |
327 | this.code_mirror_mode = 'rst'; |
|
349 | this.code_mirror_mode = 'rst'; | |
328 | IPython.TextCell.apply(this, arguments); |
|
350 | IPython.TextCell.apply(this, arguments); | |
329 | this.cell_type = 'raw'; |
|
351 | this.cell_type = 'raw'; | |
330 | var that = this |
|
352 | var that = this | |
331 |
|
353 | |||
332 | this.element.focusout( |
|
354 | this.element.focusout( | |
333 | function() { that.auto_highlight(); } |
|
355 | function() { that.auto_highlight(); } | |
334 | ); |
|
356 | ); | |
335 | }; |
|
357 | }; | |
336 |
|
358 | |||
337 |
|
359 | |||
338 | RawCell.prototype = new TextCell(); |
|
360 | RawCell.prototype = new TextCell(); | |
339 |
|
361 | |||
|
362 | /** | |||
|
363 | * Trigger autodetection of highlight scheme for current cell | |||
|
364 | * @method auto_highlight | |||
|
365 | */ | |||
340 | RawCell.prototype.auto_highlight = function () { |
|
366 | RawCell.prototype.auto_highlight = function () { | |
341 | this._auto_highlight(IPython.config.raw_cell_highlight); |
|
367 | this._auto_highlight(IPython.config.raw_cell_highlight); | |
342 | }; |
|
368 | }; | |
343 |
|
369 | |||
|
370 | /** @method render **/ | |||
344 | RawCell.prototype.render = function () { |
|
371 | RawCell.prototype.render = function () { | |
345 | this.rendered = true; |
|
372 | this.rendered = true; | |
346 | this.edit(); |
|
373 | this.edit(); | |
347 | }; |
|
374 | }; | |
348 |
|
375 | |||
349 |
|
376 | |||
|
377 | /** @method handle_codemirror_keyevent **/ | |||
350 | RawCell.prototype.handle_codemirror_keyevent = function (editor, event) { |
|
378 | RawCell.prototype.handle_codemirror_keyevent = function (editor, event) { | |
351 | // This method gets called in CodeMirror's onKeyDown/onKeyPress |
|
|||
352 | // handlers and is used to provide custom key handling. Its return |
|
|||
353 | // value is used to determine if CodeMirror should ignore the event: |
|
|||
354 | // true = ignore, false = don't ignore. |
|
|||
355 |
|
379 | |||
356 | var that = this; |
|
380 | var that = this; | |
357 | if (event.which === key.UPARROW && event.type === 'keydown') { |
|
381 | if (event.which === key.UPARROW && event.type === 'keydown') { | |
358 | // If we are not at the top, let CM handle the up arrow and |
|
382 | // If we are not at the top, let CM handle the up arrow and | |
359 | // prevent the global keydown handler from handling it. |
|
383 | // prevent the global keydown handler from handling it. | |
360 | if (!that.at_top()) { |
|
384 | if (!that.at_top()) { | |
361 | event.stop(); |
|
385 | event.stop(); | |
362 | return false; |
|
386 | return false; | |
363 | } else { |
|
387 | } else { | |
364 | return true; |
|
388 | return true; | |
365 | }; |
|
389 | }; | |
366 | } else if (event.which === key.DOWNARROW && event.type === 'keydown') { |
|
390 | } else if (event.which === key.DOWNARROW && event.type === 'keydown') { | |
367 | // If we are not at the bottom, let CM handle the down arrow and |
|
391 | // If we are not at the bottom, let CM handle the down arrow and | |
368 | // prevent the global keydown handler from handling it. |
|
392 | // prevent the global keydown handler from handling it. | |
369 | if (!that.at_bottom()) { |
|
393 | if (!that.at_bottom()) { | |
370 | event.stop(); |
|
394 | event.stop(); | |
371 | return false; |
|
395 | return false; | |
372 | } else { |
|
396 | } else { | |
373 | return true; |
|
397 | return true; | |
374 | }; |
|
398 | }; | |
375 | }; |
|
399 | }; | |
376 | return false; |
|
400 | return false; | |
377 | }; |
|
401 | }; | |
378 |
|
402 | |||
379 |
|
403 | /** @method select **/ | ||
380 | RawCell.prototype.select = function () { |
|
404 | RawCell.prototype.select = function () { | |
381 | IPython.Cell.prototype.select.apply(this); |
|
405 | IPython.Cell.prototype.select.apply(this); | |
382 | this.code_mirror.refresh(); |
|
406 | this.code_mirror.refresh(); | |
383 | this.code_mirror.focus(); |
|
407 | this.code_mirror.focus(); | |
384 | }; |
|
408 | }; | |
385 |
|
409 | |||
386 |
|
410 | /** @method at_top **/ | ||
387 | RawCell.prototype.at_top = function () { |
|
411 | RawCell.prototype.at_top = function () { | |
388 | var cursor = this.code_mirror.getCursor(); |
|
412 | var cursor = this.code_mirror.getCursor(); | |
389 | if (cursor.line === 0 && cursor.ch === 0) { |
|
413 | if (cursor.line === 0 && cursor.ch === 0) { | |
390 | return true; |
|
414 | return true; | |
391 | } else { |
|
415 | } else { | |
392 | return false; |
|
416 | return false; | |
393 | } |
|
417 | } | |
394 | }; |
|
418 | }; | |
395 |
|
419 | |||
396 |
|
420 | |||
|
421 | /** @method at_bottom **/ | |||
397 | RawCell.prototype.at_bottom = function () { |
|
422 | RawCell.prototype.at_bottom = function () { | |
398 | var cursor = this.code_mirror.getCursor(); |
|
423 | var cursor = this.code_mirror.getCursor(); | |
399 | if (cursor.line === (this.code_mirror.lineCount()-1) && cursor.ch === this.code_mirror.getLine(cursor.line).length) { |
|
424 | if (cursor.line === (this.code_mirror.lineCount()-1) && cursor.ch === this.code_mirror.getLine(cursor.line).length) { | |
400 | return true; |
|
425 | return true; | |
401 | } else { |
|
426 | } else { | |
402 | return false; |
|
427 | return false; | |
403 | } |
|
428 | } | |
404 | }; |
|
429 | }; | |
405 |
|
430 | |||
406 |
|
431 | |||
407 | /** @constructs HeadingCell |
|
432 | /** | |
|
433 | * @class HeadingCell | |||
|
434 | * @extends TextCell | |||
|
435 | */ | |||
|
436 | ||||
|
437 | /** | |||
|
438 | * @constructor HeadingCell | |||
408 | * @extends TextCell |
|
439 | * @extends TextCell | |
409 | */ |
|
440 | */ | |
410 | var HeadingCell = function () { |
|
441 | var HeadingCell = function () { | |
411 | this.placeholder = "Type Heading Here"; |
|
442 | this.placeholder = "Type Heading Here"; | |
412 | IPython.TextCell.apply(this, arguments); |
|
443 | IPython.TextCell.apply(this, arguments); | |
413 | this.cell_type = 'heading'; |
|
444 | /** | |
|
445 | * heading level of the cell, use getter and setter to access | |||
|
446 | * @property level | |||
|
447 | */ | |||
414 | this.level = 1; |
|
448 | this.level = 1; | |
|
449 | this.cell_type = 'heading'; | |||
415 | }; |
|
450 | }; | |
416 |
|
451 | |||
417 |
|
452 | |||
418 | HeadingCell.prototype = new TextCell(); |
|
453 | HeadingCell.prototype = new TextCell(); | |
419 |
|
454 | |||
420 |
/** |
|
455 | /** @method fromJSON */ | |
421 | HeadingCell.prototype.fromJSON = function (data) { |
|
456 | HeadingCell.prototype.fromJSON = function (data) { | |
422 | if (data.level != undefined){ |
|
457 | if (data.level != undefined){ | |
423 | this.level = data.level; |
|
458 | this.level = data.level; | |
424 | } |
|
459 | } | |
425 | IPython.TextCell.prototype.fromJSON.apply(this, arguments); |
|
460 | IPython.TextCell.prototype.fromJSON.apply(this, arguments); | |
426 | }; |
|
461 | }; | |
427 |
|
462 | |||
428 |
|
463 | |||
|
464 | /** @method toJSON */ | |||
429 | HeadingCell.prototype.toJSON = function () { |
|
465 | HeadingCell.prototype.toJSON = function () { | |
430 | var data = IPython.TextCell.prototype.toJSON.apply(this); |
|
466 | var data = IPython.TextCell.prototype.toJSON.apply(this); | |
431 | data.level = this.get_level(); |
|
467 | data.level = this.get_level(); | |
432 | return data; |
|
468 | return data; | |
433 | }; |
|
469 | }; | |
434 |
|
470 | |||
435 |
|
471 | |||
|
472 | /** | |||
|
473 | * Change heading level of cell, and re-render | |||
|
474 | * @method set_level | |||
|
475 | */ | |||
436 | HeadingCell.prototype.set_level = function (level) { |
|
476 | HeadingCell.prototype.set_level = function (level) { | |
437 | this.level = level; |
|
477 | this.level = level; | |
438 | if (this.rendered) { |
|
478 | if (this.rendered) { | |
439 | this.rendered = false; |
|
479 | this.rendered = false; | |
440 | this.render(); |
|
480 | this.render(); | |
441 | }; |
|
481 | }; | |
442 | }; |
|
482 | }; | |
443 |
|
483 | |||
444 | /** The depth of header cell, based on html (h1 to h6) |
|
484 | /** The depth of header cell, based on html (h1 to h6) | |
|
485 | * @method get_level | |||
445 | * @return {integer} level - for 1 to 6 |
|
486 | * @return {integer} level - for 1 to 6 | |
446 | */ |
|
487 | */ | |
447 | HeadingCell.prototype.get_level = function () { |
|
488 | HeadingCell.prototype.get_level = function () { | |
448 | return this.level; |
|
489 | return this.level; | |
449 | }; |
|
490 | }; | |
450 |
|
491 | |||
451 |
|
492 | |||
452 | HeadingCell.prototype.set_rendered = function (text) { |
|
493 | HeadingCell.prototype.set_rendered = function (text) { | |
453 | var r = this.element.find("div.text_cell_render"); |
|
494 | var r = this.element.find("div.text_cell_render"); | |
454 | r.empty(); |
|
495 | r.empty(); | |
455 | r.append($('<h'+this.level+'/>').html(text)); |
|
496 | r.append($('<h'+this.level+'/>').html(text)); | |
456 | }; |
|
497 | }; | |
457 |
|
498 | |||
458 |
|
499 | |||
459 | HeadingCell.prototype.get_rendered = function () { |
|
500 | HeadingCell.prototype.get_rendered = function () { | |
460 | var r = this.element.find("div.text_cell_render"); |
|
501 | var r = this.element.find("div.text_cell_render"); | |
461 | return r.children().first().html(); |
|
502 | return r.children().first().html(); | |
462 | }; |
|
503 | }; | |
463 |
|
504 | |||
464 |
|
505 | |||
465 | HeadingCell.prototype.render = function () { |
|
506 | HeadingCell.prototype.render = function () { | |
466 | if (this.rendered === false) { |
|
507 | if (this.rendered === false) { | |
467 | var text = this.get_text(); |
|
508 | var text = this.get_text(); | |
468 | if (text === "") { text = this.placeholder; } |
|
509 | if (text === "") { text = this.placeholder; } | |
469 | this.set_rendered(text); |
|
510 | this.set_rendered(text); | |
470 | this.typeset(); |
|
511 | this.typeset(); | |
471 | this.element.find('div.text_cell_input').hide(); |
|
512 | this.element.find('div.text_cell_input').hide(); | |
472 | this.element.find("div.text_cell_render").show(); |
|
513 | this.element.find("div.text_cell_render").show(); | |
473 | this.rendered = true; |
|
514 | this.rendered = true; | |
474 | }; |
|
515 | }; | |
475 | }; |
|
516 | }; | |
476 |
|
517 | |||
477 | IPython.TextCell = TextCell; |
|
518 | IPython.TextCell = TextCell; | |
478 | IPython.HTMLCell = HTMLCell; |
|
519 | IPython.HTMLCell = HTMLCell; | |
479 | IPython.MarkdownCell = MarkdownCell; |
|
520 | IPython.MarkdownCell = MarkdownCell; | |
480 | IPython.RawCell = RawCell; |
|
521 | IPython.RawCell = RawCell; | |
481 | IPython.HeadingCell = HeadingCell; |
|
522 | IPython.HeadingCell = HeadingCell; | |
482 |
|
523 | |||
483 |
|
524 | |||
484 | return IPython; |
|
525 | return IPython; | |
485 |
|
526 | |||
486 | }(IPython)); |
|
527 | }(IPython)); | |
487 |
|
528 |
General Comments 0
You need to be logged in to leave comments.
Login now