Show More
@@ -1,567 +1,567 b'' | |||||
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 | "use strict"; |
|
19 | "use strict"; | |
20 |
|
20 | |||
21 | var utils = IPython.utils; |
|
21 | var utils = IPython.utils; | |
22 | var keycodes = IPython.keyboard.keycodes; |
|
22 | var keycodes = IPython.keyboard.keycodes; | |
23 |
|
23 | |||
24 | /** |
|
24 | /** | |
25 | * The Base `Cell` class from which to inherit |
|
25 | * The Base `Cell` class from which to inherit | |
26 | * @class Cell |
|
26 | * @class Cell | |
27 | **/ |
|
27 | **/ | |
28 |
|
28 | |||
29 | /* |
|
29 | /* | |
30 | * @constructor |
|
30 | * @constructor | |
31 | * |
|
31 | * | |
32 | * @param {object|undefined} [options] |
|
32 | * @param {object|undefined} [options] | |
33 | * @param [options.cm_config] {object} config to pass to CodeMirror, will extend default parameters |
|
33 | * @param [options.cm_config] {object} config to pass to CodeMirror, will extend default parameters | |
34 | */ |
|
34 | */ | |
35 | var Cell = function (options) { |
|
35 | var Cell = function (options) { | |
36 |
|
36 | |||
37 | options = this.mergeopt(Cell, options); |
|
37 | options = this.mergeopt(Cell, options); | |
38 | // superclass default overwrite our default |
|
38 | // superclass default overwrite our default | |
39 |
|
39 | |||
40 | this.placeholder = options.placeholder || ''; |
|
40 | this.placeholder = options.placeholder || ''; | |
41 | this.read_only = options.cm_config.readOnly; |
|
41 | this.read_only = options.cm_config.readOnly; | |
42 | this.selected = false; |
|
42 | this.selected = false; | |
43 | this.rendered = false; |
|
43 | this.rendered = false; | |
44 | this.mode = 'command'; |
|
44 | this.mode = 'command'; | |
45 | this.metadata = {}; |
|
45 | this.metadata = {}; | |
46 | // load this from metadata later ? |
|
46 | // load this from metadata later ? | |
47 | this.user_highlight = 'auto'; |
|
47 | this.user_highlight = 'auto'; | |
48 | this.cm_config = options.cm_config; |
|
48 | this.cm_config = options.cm_config; | |
49 | this.cell_id = utils.uuid(); |
|
49 | this.cell_id = utils.uuid(); | |
50 | this._options = options; |
|
50 | this._options = options; | |
51 |
|
51 | |||
52 | // For JS VM engines optimization, attributes should be all set (even |
|
52 | // For JS VM engines optimization, attributes should be all set (even | |
53 | // to null) in the constructor, and if possible, if different subclass |
|
53 | // to null) in the constructor, and if possible, if different subclass | |
54 | // have new attributes with same name, they should be created in the |
|
54 | // have new attributes with same name, they should be created in the | |
55 | // same order. Easiest is to create and set to null in parent class. |
|
55 | // same order. Easiest is to create and set to null in parent class. | |
56 |
|
56 | |||
57 | this.element = null; |
|
57 | this.element = null; | |
58 | this.cell_type = this.cell_type || null; |
|
58 | this.cell_type = this.cell_type || null; | |
59 | this.code_mirror = null; |
|
59 | this.code_mirror = null; | |
60 |
|
60 | |||
61 | this.create_element(); |
|
61 | this.create_element(); | |
62 | if (this.element !== null) { |
|
62 | if (this.element !== null) { | |
63 | this.element.data("cell", this); |
|
63 | this.element.data("cell", this); | |
64 | this.bind_events(); |
|
64 | this.bind_events(); | |
65 | this.init_classes(); |
|
65 | this.init_classes(); | |
66 | } |
|
66 | } | |
67 | }; |
|
67 | }; | |
68 |
|
68 | |||
69 | Cell.options_default = { |
|
69 | Cell.options_default = { | |
70 | cm_config : { |
|
70 | cm_config : { | |
71 | indentUnit : 4, |
|
71 | indentUnit : 4, | |
72 | readOnly: false, |
|
72 | readOnly: false, | |
73 | theme: "default" |
|
73 | theme: "default" | |
74 | } |
|
74 | } | |
75 | }; |
|
75 | }; | |
76 |
|
76 | |||
77 | // FIXME: Workaround CM Bug #332 (Safari segfault on drag) |
|
77 | // FIXME: Workaround CM Bug #332 (Safari segfault on drag) | |
78 | // by disabling drag/drop altogether on Safari |
|
78 | // by disabling drag/drop altogether on Safari | |
79 | // https://github.com/marijnh/CodeMirror/issues/332 |
|
79 | // https://github.com/marijnh/CodeMirror/issues/332 | |
80 | if (utils.browser[0] == "Safari") { |
|
80 | if (utils.browser[0] == "Safari") { | |
81 | Cell.options_default.cm_config.dragDrop = false; |
|
81 | Cell.options_default.cm_config.dragDrop = false; | |
82 | } |
|
82 | } | |
83 |
|
83 | |||
84 | Cell.prototype.mergeopt = function(_class, options, overwrite){ |
|
84 | Cell.prototype.mergeopt = function(_class, options, overwrite){ | |
85 | options = options || {}; |
|
85 | options = options || {}; | |
86 | overwrite = overwrite || {}; |
|
86 | overwrite = overwrite || {}; | |
87 | return $.extend(true, {}, _class.options_default, options, overwrite); |
|
87 | return $.extend(true, {}, _class.options_default, options, overwrite); | |
88 | }; |
|
88 | }; | |
89 |
|
89 | |||
90 | /** |
|
90 | /** | |
91 | * Empty. Subclasses must implement create_element. |
|
91 | * Empty. Subclasses must implement create_element. | |
92 | * This should contain all the code to create the DOM element in notebook |
|
92 | * This should contain all the code to create the DOM element in notebook | |
93 | * and will be called by Base Class constructor. |
|
93 | * and will be called by Base Class constructor. | |
94 | * @method create_element |
|
94 | * @method create_element | |
95 | */ |
|
95 | */ | |
96 | Cell.prototype.create_element = function () { |
|
96 | Cell.prototype.create_element = function () { | |
97 | }; |
|
97 | }; | |
98 |
|
98 | |||
99 | Cell.prototype.init_classes = function () { |
|
99 | Cell.prototype.init_classes = function () { | |
100 | // Call after this.element exists to initialize the css classes |
|
100 | // Call after this.element exists to initialize the css classes | |
101 | // related to selected, rendered and mode. |
|
101 | // related to selected, rendered and mode. | |
102 | if (this.selected) { |
|
102 | if (this.selected) { | |
103 | this.element.addClass('selected'); |
|
103 | this.element.addClass('selected'); | |
104 | } else { |
|
104 | } else { | |
105 | this.element.addClass('unselected'); |
|
105 | this.element.addClass('unselected'); | |
106 | } |
|
106 | } | |
107 | if (this.rendered) { |
|
107 | if (this.rendered) { | |
108 | this.element.addClass('rendered'); |
|
108 | this.element.addClass('rendered'); | |
109 | } else { |
|
109 | } else { | |
110 | this.element.addClass('unrendered'); |
|
110 | this.element.addClass('unrendered'); | |
111 | } |
|
111 | } | |
112 | if (this.mode === 'edit') { |
|
112 | if (this.mode === 'edit') { | |
113 | this.element.addClass('edit_mode'); |
|
113 | this.element.addClass('edit_mode'); | |
114 | } else { |
|
114 | } else { | |
115 | this.element.addClass('command_mode'); |
|
115 | this.element.addClass('command_mode'); | |
116 | } |
|
116 | } | |
117 | }; |
|
117 | }; | |
118 |
|
118 | |||
119 | /** |
|
119 | /** | |
120 | * Subclasses can implement override bind_events. |
|
120 | * Subclasses can implement override bind_events. | |
121 | * Be carefull to call the parent method when overwriting as it fires event. |
|
121 | * Be carefull to call the parent method when overwriting as it fires event. | |
122 | * this will be triggerd after create_element in constructor. |
|
122 | * this will be triggerd after create_element in constructor. | |
123 | * @method bind_events |
|
123 | * @method bind_events | |
124 | */ |
|
124 | */ | |
125 | Cell.prototype.bind_events = function () { |
|
125 | Cell.prototype.bind_events = function () { | |
126 | var that = this; |
|
126 | var that = this; | |
127 | // We trigger events so that Cell doesn't have to depend on Notebook. |
|
127 | // We trigger events so that Cell doesn't have to depend on Notebook. | |
128 | that.element.click(function (event) { |
|
128 | that.element.click(function (event) { | |
129 | if (!that.selected) { |
|
129 | if (!that.selected) { | |
130 | $([IPython.events]).trigger('select.Cell', {'cell':that}); |
|
130 | $([IPython.events]).trigger('select.Cell', {'cell':that}); | |
131 | } |
|
131 | } | |
132 | }); |
|
132 | }); | |
133 | that.element.focusin(function (event) { |
|
133 | that.element.focusin(function (event) { | |
134 | if (!that.selected) { |
|
134 | if (!that.selected) { | |
135 | $([IPython.events]).trigger('select.Cell', {'cell':that}); |
|
135 | $([IPython.events]).trigger('select.Cell', {'cell':that}); | |
136 | } |
|
136 | } | |
137 | }); |
|
137 | }); | |
138 | if (this.code_mirror) { |
|
138 | if (this.code_mirror) { | |
139 | this.code_mirror.on("change", function(cm, change) { |
|
139 | this.code_mirror.on("change", function(cm, change) { | |
140 | $([IPython.events]).trigger("set_dirty.Notebook", {value: true}); |
|
140 | $([IPython.events]).trigger("set_dirty.Notebook", {value: true}); | |
141 | }); |
|
141 | }); | |
142 | } |
|
142 | } | |
143 | if (this.code_mirror) { |
|
143 | if (this.code_mirror) { | |
144 | this.code_mirror.on('focus', function(cm, change) { |
|
144 | this.code_mirror.on('focus', function(cm, change) { | |
145 | $([IPython.events]).trigger('edit_mode.Cell', {cell: that}); |
|
145 | $([IPython.events]).trigger('edit_mode.Cell', {cell: that}); | |
146 | }); |
|
146 | }); | |
147 | } |
|
147 | } | |
148 | if (this.code_mirror) { |
|
148 | if (this.code_mirror) { | |
149 | this.code_mirror.on('blur', function(cm, change) { |
|
149 | this.code_mirror.on('blur', function(cm, change) { | |
150 | // Check if this unfocus event is legit. |
|
150 | // Check if this unfocus event is legit. | |
151 | if (!that.should_cancel_blur()) { |
|
151 | if (!that.should_cancel_blur()) { | |
152 | $([IPython.events]).trigger('command_mode.Cell', {cell: that}); |
|
152 | $([IPython.events]).trigger('command_mode.Cell', {cell: that}); | |
153 | } |
|
153 | } | |
154 | }); |
|
154 | }); | |
155 | } |
|
155 | } | |
156 | }; |
|
156 | }; | |
157 |
|
157 | |||
158 | /** |
|
158 | /** | |
159 | * This method gets called in CodeMirror's onKeyDown/onKeyPress |
|
159 | * This method gets called in CodeMirror's onKeyDown/onKeyPress | |
160 | * handlers and is used to provide custom key handling. |
|
160 | * handlers and is used to provide custom key handling. | |
161 | * |
|
161 | * | |
162 | * To have custom handling, subclasses should override this method, but still call it |
|
162 | * To have custom handling, subclasses should override this method, but still call it | |
163 | * in order to process the Edit mode keyboard shortcuts. |
|
163 | * in order to process the Edit mode keyboard shortcuts. | |
164 | * |
|
164 | * | |
165 | * @method handle_codemirror_keyevent |
|
165 | * @method handle_codemirror_keyevent | |
166 | * @param {CodeMirror} editor - The codemirror instance bound to the cell |
|
166 | * @param {CodeMirror} editor - The codemirror instance bound to the cell | |
167 | * @param {event} event - key press event which either should or should not be handled by CodeMirror |
|
167 | * @param {event} event - key press event which either should or should not be handled by CodeMirror | |
168 | * @return {Boolean} `true` if CodeMirror should ignore the event, `false` Otherwise |
|
168 | * @return {Boolean} `true` if CodeMirror should ignore the event, `false` Otherwise | |
169 | */ |
|
169 | */ | |
170 | Cell.prototype.handle_codemirror_keyevent = function (editor, event) { |
|
170 | Cell.prototype.handle_codemirror_keyevent = function (editor, event) { | |
171 | var that = this; |
|
171 | var that = this; | |
172 | var shortcuts = IPython.keyboard_manager.edit_shortcuts; |
|
172 | var shortcuts = IPython.keyboard_manager.edit_shortcuts; | |
173 |
|
173 | |||
174 | // if this is an edit_shortcuts shortcut, the global keyboard/shortcut |
|
174 | // if this is an edit_shortcuts shortcut, the global keyboard/shortcut | |
175 | // manager will handle it |
|
175 | // manager will handle it | |
176 | if (shortcuts.handles(event)) { return true; } |
|
176 | if (shortcuts.handles(event)) { return true; } | |
177 |
|
177 | |||
178 | return false; |
|
178 | return false; | |
179 | }; |
|
179 | }; | |
180 |
|
180 | |||
181 |
|
181 | |||
182 | /** |
|
182 | /** | |
183 | * Triger typsetting of math by mathjax on current cell element |
|
183 | * Triger typsetting of math by mathjax on current cell element | |
184 | * @method typeset |
|
184 | * @method typeset | |
185 | */ |
|
185 | */ | |
186 | Cell.prototype.typeset = function () { |
|
186 | Cell.prototype.typeset = function () { | |
187 | if (window.MathJax) { |
|
187 | if (window.MathJax) { | |
188 | var cell_math = this.element.get(0); |
|
188 | var cell_math = this.element.get(0); | |
189 | MathJax.Hub.Queue(["Typeset", MathJax.Hub, cell_math]); |
|
189 | MathJax.Hub.Queue(["Typeset", MathJax.Hub, cell_math]); | |
190 | } |
|
190 | } | |
191 | }; |
|
191 | }; | |
192 |
|
192 | |||
193 | /** |
|
193 | /** | |
194 | * handle cell level logic when a cell is selected |
|
194 | * handle cell level logic when a cell is selected | |
195 | * @method select |
|
195 | * @method select | |
196 | * @return is the action being taken |
|
196 | * @return is the action being taken | |
197 | */ |
|
197 | */ | |
198 | Cell.prototype.select = function () { |
|
198 | Cell.prototype.select = function () { | |
199 | if (!this.selected) { |
|
199 | if (!this.selected) { | |
200 | this.element.addClass('selected'); |
|
200 | this.element.addClass('selected'); | |
201 | this.element.removeClass('unselected'); |
|
201 | this.element.removeClass('unselected'); | |
202 | this.selected = true; |
|
202 | this.selected = true; | |
203 | return true; |
|
203 | return true; | |
204 | } else { |
|
204 | } else { | |
205 | return false; |
|
205 | return false; | |
206 | } |
|
206 | } | |
207 | }; |
|
207 | }; | |
208 |
|
208 | |||
209 | /** |
|
209 | /** | |
210 | * handle cell level logic when a cell is unselected |
|
210 | * handle cell level logic when a cell is unselected | |
211 | * @method unselect |
|
211 | * @method unselect | |
212 | * @return is the action being taken |
|
212 | * @return is the action being taken | |
213 | */ |
|
213 | */ | |
214 | Cell.prototype.unselect = function () { |
|
214 | Cell.prototype.unselect = function () { | |
215 | if (this.selected) { |
|
215 | if (this.selected) { | |
216 | this.element.addClass('unselected'); |
|
216 | this.element.addClass('unselected'); | |
217 | this.element.removeClass('selected'); |
|
217 | this.element.removeClass('selected'); | |
218 | this.selected = false; |
|
218 | this.selected = false; | |
219 | return true; |
|
219 | return true; | |
220 | } else { |
|
220 | } else { | |
221 | return false; |
|
221 | return false; | |
222 | } |
|
222 | } | |
223 | }; |
|
223 | }; | |
224 |
|
224 | |||
225 | /** |
|
225 | /** | |
226 | * handle cell level logic when a cell is rendered |
|
226 | * handle cell level logic when a cell is rendered | |
227 | * @method render |
|
227 | * @method render | |
228 | * @return is the action being taken |
|
228 | * @return is the action being taken | |
229 | */ |
|
229 | */ | |
230 | Cell.prototype.render = function () { |
|
230 | Cell.prototype.render = function () { | |
231 | if (!this.rendered) { |
|
231 | if (!this.rendered) { | |
232 | this.element.addClass('rendered'); |
|
232 | this.element.addClass('rendered'); | |
233 | this.element.removeClass('unrendered'); |
|
233 | this.element.removeClass('unrendered'); | |
234 | this.rendered = true; |
|
234 | this.rendered = true; | |
235 | return true; |
|
235 | return true; | |
236 | } else { |
|
236 | } else { | |
237 | return false; |
|
237 | return false; | |
238 | } |
|
238 | } | |
239 | }; |
|
239 | }; | |
240 |
|
240 | |||
241 | /** |
|
241 | /** | |
242 | * handle cell level logic when a cell is unrendered |
|
242 | * handle cell level logic when a cell is unrendered | |
243 | * @method unrender |
|
243 | * @method unrender | |
244 | * @return is the action being taken |
|
244 | * @return is the action being taken | |
245 | */ |
|
245 | */ | |
246 | Cell.prototype.unrender = function () { |
|
246 | Cell.prototype.unrender = function () { | |
247 | if (this.rendered) { |
|
247 | if (this.rendered) { | |
248 | this.element.addClass('unrendered'); |
|
248 | this.element.addClass('unrendered'); | |
249 | this.element.removeClass('rendered'); |
|
249 | this.element.removeClass('rendered'); | |
250 | this.rendered = false; |
|
250 | this.rendered = false; | |
251 | return true; |
|
251 | return true; | |
252 | } else { |
|
252 | } else { | |
253 | return false; |
|
253 | return false; | |
254 | } |
|
254 | } | |
255 | }; |
|
255 | }; | |
256 |
|
256 | |||
257 | /** |
|
257 | /** | |
258 | * Delegates keyboard shortcut handling to either IPython keyboard |
|
258 | * Delegates keyboard shortcut handling to either IPython keyboard | |
259 | * manager when in command mode, or CodeMirror when in edit mode |
|
259 | * manager when in command mode, or CodeMirror when in edit mode | |
260 | * |
|
260 | * | |
261 | * @method handle_keyevent |
|
261 | * @method handle_keyevent | |
262 | * @param {CodeMirror} editor - The codemirror instance bound to the cell |
|
262 | * @param {CodeMirror} editor - The codemirror instance bound to the cell | |
263 | * @param {event} event - |
|
263 | * @param {event} event - | |
264 | * @return {Boolean} `true` if CodeMirror should ignore the event, `false` Otherwise |
|
264 | * @return {Boolean} `true` if CodeMirror should ignore the event, `false` Otherwise | |
265 | */ |
|
265 | */ | |
266 | Cell.prototype.handle_keyevent = function (editor, event) { |
|
266 | Cell.prototype.handle_keyevent = function (editor, event) { | |
267 |
|
267 | |||
268 | // console.log('CM', this.mode, event.which, event.type) |
|
268 | // console.log('CM', this.mode, event.which, event.type) | |
269 |
|
269 | |||
270 | if (this.mode === 'command') { |
|
270 | if (this.mode === 'command') { | |
271 | return true; |
|
271 | return true; | |
272 | } else if (this.mode === 'edit') { |
|
272 | } else if (this.mode === 'edit') { | |
273 | return this.handle_codemirror_keyevent(editor, event); |
|
273 | return this.handle_codemirror_keyevent(editor, event); | |
274 | } |
|
274 | } | |
275 | }; |
|
275 | }; | |
276 |
|
276 | |||
277 | /** |
|
277 | /** | |
278 | * @method at_top |
|
278 | * @method at_top | |
279 | * @return {Boolean} |
|
279 | * @return {Boolean} | |
280 | */ |
|
280 | */ | |
281 | Cell.prototype.at_top = function () { |
|
281 | Cell.prototype.at_top = function () { | |
282 | var cm = this.code_mirror; |
|
282 | var cm = this.code_mirror; | |
283 | var cursor = cm.getCursor(); |
|
283 | var cursor = cm.getCursor(); | |
284 |
if (cursor.line === 0 && c |
|
284 | if (cursor.line === 0 && cursor.ch === 0) { | |
285 | return true; |
|
285 | return true; | |
286 | } else { |
|
286 | } else { | |
287 | return false; |
|
287 | return false; | |
288 | } |
|
288 | } | |
289 | }; |
|
289 | }; | |
290 |
|
290 | |||
291 | /** |
|
291 | /** | |
292 | * @method at_bottom |
|
292 | * @method at_bottom | |
293 | * @return {Boolean} |
|
293 | * @return {Boolean} | |
294 | * */ |
|
294 | * */ | |
295 | Cell.prototype.at_bottom = function () { |
|
295 | Cell.prototype.at_bottom = function () { | |
296 | var cm = this.code_mirror; |
|
296 | var cm = this.code_mirror; | |
297 | var cursor = cm.getCursor(); |
|
297 | var cursor = cm.getCursor(); | |
298 |
if (cursor.line === (cm.lineCount()-1) && cm. |
|
298 | if (cursor.line === (cm.lineCount()-1) && cursor.ch === cm.getLine(cursor.line).length) { | |
299 | return true; |
|
299 | return true; | |
300 | } else { |
|
300 | } else { | |
301 | return false; |
|
301 | return false; | |
302 | } |
|
302 | } | |
303 | }; |
|
303 | }; | |
304 | /** |
|
304 | /** | |
305 | * enter the command mode for the cell |
|
305 | * enter the command mode for the cell | |
306 | * @method command_mode |
|
306 | * @method command_mode | |
307 | * @return is the action being taken |
|
307 | * @return is the action being taken | |
308 | */ |
|
308 | */ | |
309 | Cell.prototype.command_mode = function () { |
|
309 | Cell.prototype.command_mode = function () { | |
310 | if (this.mode !== 'command') { |
|
310 | if (this.mode !== 'command') { | |
311 | this.element.addClass('command_mode'); |
|
311 | this.element.addClass('command_mode'); | |
312 | this.element.removeClass('edit_mode'); |
|
312 | this.element.removeClass('edit_mode'); | |
313 | this.mode = 'command'; |
|
313 | this.mode = 'command'; | |
314 | return true; |
|
314 | return true; | |
315 | } else { |
|
315 | } else { | |
316 | return false; |
|
316 | return false; | |
317 | } |
|
317 | } | |
318 | }; |
|
318 | }; | |
319 |
|
319 | |||
320 | /** |
|
320 | /** | |
321 | * enter the edit mode for the cell |
|
321 | * enter the edit mode for the cell | |
322 | * @method command_mode |
|
322 | * @method command_mode | |
323 | * @return is the action being taken |
|
323 | * @return is the action being taken | |
324 | */ |
|
324 | */ | |
325 | Cell.prototype.edit_mode = function () { |
|
325 | Cell.prototype.edit_mode = function () { | |
326 | if (this.mode !== 'edit') { |
|
326 | if (this.mode !== 'edit') { | |
327 | this.element.addClass('edit_mode'); |
|
327 | this.element.addClass('edit_mode'); | |
328 | this.element.removeClass('command_mode'); |
|
328 | this.element.removeClass('command_mode'); | |
329 | this.mode = 'edit'; |
|
329 | this.mode = 'edit'; | |
330 | return true; |
|
330 | return true; | |
331 | } else { |
|
331 | } else { | |
332 | return false; |
|
332 | return false; | |
333 | } |
|
333 | } | |
334 | }; |
|
334 | }; | |
335 |
|
335 | |||
336 | /** |
|
336 | /** | |
337 | * Determine whether or not the unfocus event should be aknowledged. |
|
337 | * Determine whether or not the unfocus event should be aknowledged. | |
338 | * |
|
338 | * | |
339 | * @method should_cancel_blur |
|
339 | * @method should_cancel_blur | |
340 | * |
|
340 | * | |
341 | * @return results {bool} Whether or not to ignore the cell's blur event. |
|
341 | * @return results {bool} Whether or not to ignore the cell's blur event. | |
342 | **/ |
|
342 | **/ | |
343 | Cell.prototype.should_cancel_blur = function () { |
|
343 | Cell.prototype.should_cancel_blur = function () { | |
344 | return false; |
|
344 | return false; | |
345 | }; |
|
345 | }; | |
346 |
|
346 | |||
347 | /** |
|
347 | /** | |
348 | * Focus the cell in the DOM sense |
|
348 | * Focus the cell in the DOM sense | |
349 | * @method focus_cell |
|
349 | * @method focus_cell | |
350 | */ |
|
350 | */ | |
351 | Cell.prototype.focus_cell = function () { |
|
351 | Cell.prototype.focus_cell = function () { | |
352 | this.element.focus(); |
|
352 | this.element.focus(); | |
353 | }; |
|
353 | }; | |
354 |
|
354 | |||
355 | /** |
|
355 | /** | |
356 | * Focus the editor area so a user can type |
|
356 | * Focus the editor area so a user can type | |
357 | * |
|
357 | * | |
358 | * NOTE: If codemirror is focused via a mouse click event, you don't want to |
|
358 | * NOTE: If codemirror is focused via a mouse click event, you don't want to | |
359 | * call this because it will cause a page jump. |
|
359 | * call this because it will cause a page jump. | |
360 | * @method focus_editor |
|
360 | * @method focus_editor | |
361 | */ |
|
361 | */ | |
362 | Cell.prototype.focus_editor = function () { |
|
362 | Cell.prototype.focus_editor = function () { | |
363 | this.refresh(); |
|
363 | this.refresh(); | |
364 | this.code_mirror.focus(); |
|
364 | this.code_mirror.focus(); | |
365 | }; |
|
365 | }; | |
366 |
|
366 | |||
367 | /** |
|
367 | /** | |
368 | * Refresh codemirror instance |
|
368 | * Refresh codemirror instance | |
369 | * @method refresh |
|
369 | * @method refresh | |
370 | */ |
|
370 | */ | |
371 | Cell.prototype.refresh = function () { |
|
371 | Cell.prototype.refresh = function () { | |
372 | this.code_mirror.refresh(); |
|
372 | this.code_mirror.refresh(); | |
373 | }; |
|
373 | }; | |
374 |
|
374 | |||
375 | /** |
|
375 | /** | |
376 | * should be overritten by subclass |
|
376 | * should be overritten by subclass | |
377 | * @method get_text |
|
377 | * @method get_text | |
378 | */ |
|
378 | */ | |
379 | Cell.prototype.get_text = function () { |
|
379 | Cell.prototype.get_text = function () { | |
380 | }; |
|
380 | }; | |
381 |
|
381 | |||
382 | /** |
|
382 | /** | |
383 | * should be overritten by subclass |
|
383 | * should be overritten by subclass | |
384 | * @method set_text |
|
384 | * @method set_text | |
385 | * @param {string} text |
|
385 | * @param {string} text | |
386 | */ |
|
386 | */ | |
387 | Cell.prototype.set_text = function (text) { |
|
387 | Cell.prototype.set_text = function (text) { | |
388 | }; |
|
388 | }; | |
389 |
|
389 | |||
390 | /** |
|
390 | /** | |
391 | * should be overritten by subclass |
|
391 | * should be overritten by subclass | |
392 | * serialise cell to json. |
|
392 | * serialise cell to json. | |
393 | * @method toJSON |
|
393 | * @method toJSON | |
394 | **/ |
|
394 | **/ | |
395 | Cell.prototype.toJSON = function () { |
|
395 | Cell.prototype.toJSON = function () { | |
396 | var data = {}; |
|
396 | var data = {}; | |
397 | data.metadata = this.metadata; |
|
397 | data.metadata = this.metadata; | |
398 | data.cell_type = this.cell_type; |
|
398 | data.cell_type = this.cell_type; | |
399 | return data; |
|
399 | return data; | |
400 | }; |
|
400 | }; | |
401 |
|
401 | |||
402 |
|
402 | |||
403 | /** |
|
403 | /** | |
404 | * should be overritten by subclass |
|
404 | * should be overritten by subclass | |
405 | * @method fromJSON |
|
405 | * @method fromJSON | |
406 | **/ |
|
406 | **/ | |
407 | Cell.prototype.fromJSON = function (data) { |
|
407 | Cell.prototype.fromJSON = function (data) { | |
408 | if (data.metadata !== undefined) { |
|
408 | if (data.metadata !== undefined) { | |
409 | this.metadata = data.metadata; |
|
409 | this.metadata = data.metadata; | |
410 | } |
|
410 | } | |
411 | this.celltoolbar.rebuild(); |
|
411 | this.celltoolbar.rebuild(); | |
412 | }; |
|
412 | }; | |
413 |
|
413 | |||
414 |
|
414 | |||
415 | /** |
|
415 | /** | |
416 | * can the cell be split into two cells |
|
416 | * can the cell be split into two cells | |
417 | * @method is_splittable |
|
417 | * @method is_splittable | |
418 | **/ |
|
418 | **/ | |
419 | Cell.prototype.is_splittable = function () { |
|
419 | Cell.prototype.is_splittable = function () { | |
420 | return true; |
|
420 | return true; | |
421 | }; |
|
421 | }; | |
422 |
|
422 | |||
423 |
|
423 | |||
424 | /** |
|
424 | /** | |
425 | * can the cell be merged with other cells |
|
425 | * can the cell be merged with other cells | |
426 | * @method is_mergeable |
|
426 | * @method is_mergeable | |
427 | **/ |
|
427 | **/ | |
428 | Cell.prototype.is_mergeable = function () { |
|
428 | Cell.prototype.is_mergeable = function () { | |
429 | return true; |
|
429 | return true; | |
430 | }; |
|
430 | }; | |
431 |
|
431 | |||
432 |
|
432 | |||
433 | /** |
|
433 | /** | |
434 | * @return {String} - the text before the cursor |
|
434 | * @return {String} - the text before the cursor | |
435 | * @method get_pre_cursor |
|
435 | * @method get_pre_cursor | |
436 | **/ |
|
436 | **/ | |
437 | Cell.prototype.get_pre_cursor = function () { |
|
437 | Cell.prototype.get_pre_cursor = function () { | |
438 | var cursor = this.code_mirror.getCursor(); |
|
438 | var cursor = this.code_mirror.getCursor(); | |
439 | var text = this.code_mirror.getRange({line:0, ch:0}, cursor); |
|
439 | var text = this.code_mirror.getRange({line:0, ch:0}, cursor); | |
440 | text = text.replace(/^\n+/, '').replace(/\n+$/, ''); |
|
440 | text = text.replace(/^\n+/, '').replace(/\n+$/, ''); | |
441 | return text; |
|
441 | return text; | |
442 | }; |
|
442 | }; | |
443 |
|
443 | |||
444 |
|
444 | |||
445 | /** |
|
445 | /** | |
446 | * @return {String} - the text after the cursor |
|
446 | * @return {String} - the text after the cursor | |
447 | * @method get_post_cursor |
|
447 | * @method get_post_cursor | |
448 | **/ |
|
448 | **/ | |
449 | Cell.prototype.get_post_cursor = function () { |
|
449 | Cell.prototype.get_post_cursor = function () { | |
450 | var cursor = this.code_mirror.getCursor(); |
|
450 | var cursor = this.code_mirror.getCursor(); | |
451 | var last_line_num = this.code_mirror.lineCount()-1; |
|
451 | var last_line_num = this.code_mirror.lineCount()-1; | |
452 | var last_line_len = this.code_mirror.getLine(last_line_num).length; |
|
452 | var last_line_len = this.code_mirror.getLine(last_line_num).length; | |
453 | var end = {line:last_line_num, ch:last_line_len}; |
|
453 | var end = {line:last_line_num, ch:last_line_len}; | |
454 | var text = this.code_mirror.getRange(cursor, end); |
|
454 | var text = this.code_mirror.getRange(cursor, end); | |
455 | text = text.replace(/^\n+/, '').replace(/\n+$/, ''); |
|
455 | text = text.replace(/^\n+/, '').replace(/\n+$/, ''); | |
456 | return text; |
|
456 | return text; | |
457 | }; |
|
457 | }; | |
458 |
|
458 | |||
459 | /** |
|
459 | /** | |
460 | * Show/Hide CodeMirror LineNumber |
|
460 | * Show/Hide CodeMirror LineNumber | |
461 | * @method show_line_numbers |
|
461 | * @method show_line_numbers | |
462 | * |
|
462 | * | |
463 | * @param value {Bool} show (true), or hide (false) the line number in CodeMirror |
|
463 | * @param value {Bool} show (true), or hide (false) the line number in CodeMirror | |
464 | **/ |
|
464 | **/ | |
465 | Cell.prototype.show_line_numbers = function (value) { |
|
465 | Cell.prototype.show_line_numbers = function (value) { | |
466 | this.code_mirror.setOption('lineNumbers', value); |
|
466 | this.code_mirror.setOption('lineNumbers', value); | |
467 | this.code_mirror.refresh(); |
|
467 | this.code_mirror.refresh(); | |
468 | }; |
|
468 | }; | |
469 |
|
469 | |||
470 | /** |
|
470 | /** | |
471 | * Toggle CodeMirror LineNumber |
|
471 | * Toggle CodeMirror LineNumber | |
472 | * @method toggle_line_numbers |
|
472 | * @method toggle_line_numbers | |
473 | **/ |
|
473 | **/ | |
474 | Cell.prototype.toggle_line_numbers = function () { |
|
474 | Cell.prototype.toggle_line_numbers = function () { | |
475 | var val = this.code_mirror.getOption('lineNumbers'); |
|
475 | var val = this.code_mirror.getOption('lineNumbers'); | |
476 | this.show_line_numbers(!val); |
|
476 | this.show_line_numbers(!val); | |
477 | }; |
|
477 | }; | |
478 |
|
478 | |||
479 | /** |
|
479 | /** | |
480 | * Force codemirror highlight mode |
|
480 | * Force codemirror highlight mode | |
481 | * @method force_highlight |
|
481 | * @method force_highlight | |
482 | * @param {object} - CodeMirror mode |
|
482 | * @param {object} - CodeMirror mode | |
483 | **/ |
|
483 | **/ | |
484 | Cell.prototype.force_highlight = function(mode) { |
|
484 | Cell.prototype.force_highlight = function(mode) { | |
485 | this.user_highlight = mode; |
|
485 | this.user_highlight = mode; | |
486 | this.auto_highlight(); |
|
486 | this.auto_highlight(); | |
487 | }; |
|
487 | }; | |
488 |
|
488 | |||
489 | /** |
|
489 | /** | |
490 | * Try to autodetect cell highlight mode, or use selected mode |
|
490 | * Try to autodetect cell highlight mode, or use selected mode | |
491 | * @methods _auto_highlight |
|
491 | * @methods _auto_highlight | |
492 | * @private |
|
492 | * @private | |
493 | * @param {String|object|undefined} - CodeMirror mode | 'auto' |
|
493 | * @param {String|object|undefined} - CodeMirror mode | 'auto' | |
494 | **/ |
|
494 | **/ | |
495 | Cell.prototype._auto_highlight = function (modes) { |
|
495 | Cell.prototype._auto_highlight = function (modes) { | |
496 | //Here we handle manually selected modes |
|
496 | //Here we handle manually selected modes | |
497 | var mode; |
|
497 | var mode; | |
498 | if( this.user_highlight !== undefined && this.user_highlight != 'auto' ) |
|
498 | if( this.user_highlight !== undefined && this.user_highlight != 'auto' ) | |
499 | { |
|
499 | { | |
500 | mode = this.user_highlight; |
|
500 | mode = this.user_highlight; | |
501 | CodeMirror.autoLoadMode(this.code_mirror, mode); |
|
501 | CodeMirror.autoLoadMode(this.code_mirror, mode); | |
502 | this.code_mirror.setOption('mode', mode); |
|
502 | this.code_mirror.setOption('mode', mode); | |
503 | return; |
|
503 | return; | |
504 | } |
|
504 | } | |
505 | var current_mode = this.code_mirror.getOption('mode', mode); |
|
505 | var current_mode = this.code_mirror.getOption('mode', mode); | |
506 | var first_line = this.code_mirror.getLine(0); |
|
506 | var first_line = this.code_mirror.getLine(0); | |
507 | // loop on every pairs |
|
507 | // loop on every pairs | |
508 | for(mode in modes) { |
|
508 | for(mode in modes) { | |
509 | var regs = modes[mode].reg; |
|
509 | var regs = modes[mode].reg; | |
510 | // only one key every time but regexp can't be keys... |
|
510 | // only one key every time but regexp can't be keys... | |
511 | for(var i=0; i<regs.length; i++) { |
|
511 | for(var i=0; i<regs.length; i++) { | |
512 | // here we handle non magic_modes |
|
512 | // here we handle non magic_modes | |
513 | if(first_line.match(regs[i]) !== null) { |
|
513 | if(first_line.match(regs[i]) !== null) { | |
514 | if(current_mode == mode){ |
|
514 | if(current_mode == mode){ | |
515 | return; |
|
515 | return; | |
516 | } |
|
516 | } | |
517 | if (mode.search('magic_') !== 0) { |
|
517 | if (mode.search('magic_') !== 0) { | |
518 | this.code_mirror.setOption('mode', mode); |
|
518 | this.code_mirror.setOption('mode', mode); | |
519 | CodeMirror.autoLoadMode(this.code_mirror, mode); |
|
519 | CodeMirror.autoLoadMode(this.code_mirror, mode); | |
520 | return; |
|
520 | return; | |
521 | } |
|
521 | } | |
522 | var open = modes[mode].open || "%%"; |
|
522 | var open = modes[mode].open || "%%"; | |
523 | var close = modes[mode].close || "%%end"; |
|
523 | var close = modes[mode].close || "%%end"; | |
524 | var mmode = mode; |
|
524 | var mmode = mode; | |
525 | mode = mmode.substr(6); |
|
525 | mode = mmode.substr(6); | |
526 | if(current_mode == mode){ |
|
526 | if(current_mode == mode){ | |
527 | return; |
|
527 | return; | |
528 | } |
|
528 | } | |
529 | CodeMirror.autoLoadMode(this.code_mirror, mode); |
|
529 | CodeMirror.autoLoadMode(this.code_mirror, mode); | |
530 | // create on the fly a mode that swhitch between |
|
530 | // create on the fly a mode that swhitch between | |
531 | // plain/text and smth else otherwise `%%` is |
|
531 | // plain/text and smth else otherwise `%%` is | |
532 | // source of some highlight issues. |
|
532 | // source of some highlight issues. | |
533 | // we use patchedGetMode to circumvent a bug in CM |
|
533 | // we use patchedGetMode to circumvent a bug in CM | |
534 | CodeMirror.defineMode(mmode , function(config) { |
|
534 | CodeMirror.defineMode(mmode , function(config) { | |
535 | return CodeMirror.multiplexingMode( |
|
535 | return CodeMirror.multiplexingMode( | |
536 | CodeMirror.patchedGetMode(config, 'text/plain'), |
|
536 | CodeMirror.patchedGetMode(config, 'text/plain'), | |
537 | // always set someting on close |
|
537 | // always set someting on close | |
538 | {open: open, close: close, |
|
538 | {open: open, close: close, | |
539 | mode: CodeMirror.patchedGetMode(config, mode), |
|
539 | mode: CodeMirror.patchedGetMode(config, mode), | |
540 | delimStyle: "delimit" |
|
540 | delimStyle: "delimit" | |
541 | } |
|
541 | } | |
542 | ); |
|
542 | ); | |
543 | }); |
|
543 | }); | |
544 | this.code_mirror.setOption('mode', mmode); |
|
544 | this.code_mirror.setOption('mode', mmode); | |
545 | return; |
|
545 | return; | |
546 | } |
|
546 | } | |
547 | } |
|
547 | } | |
548 | } |
|
548 | } | |
549 | // fallback on default |
|
549 | // fallback on default | |
550 | var default_mode; |
|
550 | var default_mode; | |
551 | try { |
|
551 | try { | |
552 | default_mode = this._options.cm_config.mode; |
|
552 | default_mode = this._options.cm_config.mode; | |
553 | } catch(e) { |
|
553 | } catch(e) { | |
554 | default_mode = 'text/plain'; |
|
554 | default_mode = 'text/plain'; | |
555 | } |
|
555 | } | |
556 | if( current_mode === default_mode){ |
|
556 | if( current_mode === default_mode){ | |
557 | return; |
|
557 | return; | |
558 | } |
|
558 | } | |
559 | this.code_mirror.setOption('mode', default_mode); |
|
559 | this.code_mirror.setOption('mode', default_mode); | |
560 | }; |
|
560 | }; | |
561 |
|
561 | |||
562 | IPython.Cell = Cell; |
|
562 | IPython.Cell = Cell; | |
563 |
|
563 | |||
564 | return IPython; |
|
564 | return IPython; | |
565 |
|
565 | |||
566 | }(IPython)); |
|
566 | }(IPython)); | |
567 |
|
567 |
General Comments 0
You need to be logged in to leave comments.
Login now