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