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