Show More
@@ -1,579 +1,566 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 - |
|
167 | * @param {event} event - | |
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, we've already handled it. |
|
174 | // if this is an edit_shortcuts shortcut, we've already handled it. | |
175 | if (shortcuts.use_shortcut(event)) { return true; } |
|
175 | if (shortcuts.use_shortcut(event)) { return true; } | |
176 |
|
176 | |||
177 | if (event.which === keycodes.esc && event.type === 'keydown') { |
|
|||
178 | if (that.code_mirror.options.keyMap === "vim-insert") { |
|
|||
179 | // vim keyMap is active and in insert mode. In this case we leave vim |
|
|||
180 | // insert mode, but remain in notebook edit mode. |
|
|||
181 | // Let' CM handle this event and prevent global handling. |
|
|||
182 | event.stop(); |
|
|||
183 | return false; |
|
|||
184 | } else { |
|
|||
185 | // vim keyMap is not active. Leave notebook edit mode. |
|
|||
186 | // Don't let CM handle the event, defer to global handling. |
|
|||
187 | return true; |
|
|||
188 | } |
|
|||
189 | } |
|
|||
190 | return false; |
|
177 | return false; | |
191 | }; |
|
178 | }; | |
192 |
|
179 | |||
193 |
|
180 | |||
194 | /** |
|
181 | /** | |
195 | * Triger typsetting of math by mathjax on current cell element |
|
182 | * Triger typsetting of math by mathjax on current cell element | |
196 | * @method typeset |
|
183 | * @method typeset | |
197 | */ |
|
184 | */ | |
198 | Cell.prototype.typeset = function () { |
|
185 | Cell.prototype.typeset = function () { | |
199 | if (window.MathJax) { |
|
186 | if (window.MathJax) { | |
200 | var cell_math = this.element.get(0); |
|
187 | var cell_math = this.element.get(0); | |
201 | MathJax.Hub.Queue(["Typeset", MathJax.Hub, cell_math]); |
|
188 | MathJax.Hub.Queue(["Typeset", MathJax.Hub, cell_math]); | |
202 | } |
|
189 | } | |
203 | }; |
|
190 | }; | |
204 |
|
191 | |||
205 | /** |
|
192 | /** | |
206 | * handle cell level logic when a cell is selected |
|
193 | * handle cell level logic when a cell is selected | |
207 | * @method select |
|
194 | * @method select | |
208 | * @return is the action being taken |
|
195 | * @return is the action being taken | |
209 | */ |
|
196 | */ | |
210 | Cell.prototype.select = function () { |
|
197 | Cell.prototype.select = function () { | |
211 | if (!this.selected) { |
|
198 | if (!this.selected) { | |
212 | this.element.addClass('selected'); |
|
199 | this.element.addClass('selected'); | |
213 | this.element.removeClass('unselected'); |
|
200 | this.element.removeClass('unselected'); | |
214 | this.selected = true; |
|
201 | this.selected = true; | |
215 | return true; |
|
202 | return true; | |
216 | } else { |
|
203 | } else { | |
217 | return false; |
|
204 | return false; | |
218 | } |
|
205 | } | |
219 | }; |
|
206 | }; | |
220 |
|
207 | |||
221 | /** |
|
208 | /** | |
222 | * handle cell level logic when a cell is unselected |
|
209 | * handle cell level logic when a cell is unselected | |
223 | * @method unselect |
|
210 | * @method unselect | |
224 | * @return is the action being taken |
|
211 | * @return is the action being taken | |
225 | */ |
|
212 | */ | |
226 | Cell.prototype.unselect = function () { |
|
213 | Cell.prototype.unselect = function () { | |
227 | if (this.selected) { |
|
214 | if (this.selected) { | |
228 | this.element.addClass('unselected'); |
|
215 | this.element.addClass('unselected'); | |
229 | this.element.removeClass('selected'); |
|
216 | this.element.removeClass('selected'); | |
230 | this.selected = false; |
|
217 | this.selected = false; | |
231 | return true; |
|
218 | return true; | |
232 | } else { |
|
219 | } else { | |
233 | return false; |
|
220 | return false; | |
234 | } |
|
221 | } | |
235 | }; |
|
222 | }; | |
236 |
|
223 | |||
237 | /** |
|
224 | /** | |
238 | * handle cell level logic when a cell is rendered |
|
225 | * handle cell level logic when a cell is rendered | |
239 | * @method render |
|
226 | * @method render | |
240 | * @return is the action being taken |
|
227 | * @return is the action being taken | |
241 | */ |
|
228 | */ | |
242 | Cell.prototype.render = function () { |
|
229 | Cell.prototype.render = function () { | |
243 | if (!this.rendered) { |
|
230 | if (!this.rendered) { | |
244 | this.element.addClass('rendered'); |
|
231 | this.element.addClass('rendered'); | |
245 | this.element.removeClass('unrendered'); |
|
232 | this.element.removeClass('unrendered'); | |
246 | this.rendered = true; |
|
233 | this.rendered = true; | |
247 | return true; |
|
234 | return true; | |
248 | } else { |
|
235 | } else { | |
249 | return false; |
|
236 | return false; | |
250 | } |
|
237 | } | |
251 | }; |
|
238 | }; | |
252 |
|
239 | |||
253 | /** |
|
240 | /** | |
254 | * handle cell level logic when a cell is unrendered |
|
241 | * handle cell level logic when a cell is unrendered | |
255 | * @method unrender |
|
242 | * @method unrender | |
256 | * @return is the action being taken |
|
243 | * @return is the action being taken | |
257 | */ |
|
244 | */ | |
258 | Cell.prototype.unrender = function () { |
|
245 | Cell.prototype.unrender = function () { | |
259 | if (this.rendered) { |
|
246 | if (this.rendered) { | |
260 | this.element.addClass('unrendered'); |
|
247 | this.element.addClass('unrendered'); | |
261 | this.element.removeClass('rendered'); |
|
248 | this.element.removeClass('rendered'); | |
262 | this.rendered = false; |
|
249 | this.rendered = false; | |
263 | return true; |
|
250 | return true; | |
264 | } else { |
|
251 | } else { | |
265 | return false; |
|
252 | return false; | |
266 | } |
|
253 | } | |
267 | }; |
|
254 | }; | |
268 |
|
255 | |||
269 | /** |
|
256 | /** | |
270 | * Either delegates keyboard shortcut handling to either IPython keyboard |
|
257 | * Either delegates keyboard shortcut handling to either IPython keyboard | |
271 | * manager when in command mode, or CodeMirror when in edit mode |
|
258 | * manager when in command mode, or CodeMirror when in edit mode | |
272 | * |
|
259 | * | |
273 | * @method handle_keyevent |
|
260 | * @method handle_keyevent | |
274 | * @param {CodeMirror} editor - The codemirror instance bound to the cell |
|
261 | * @param {CodeMirror} editor - The codemirror instance bound to the cell | |
275 | * @param {event} event - |
|
262 | * @param {event} event - | |
276 | * @return {Boolean} `true` if CodeMirror should ignore the event, `false` Otherwise |
|
263 | * @return {Boolean} `true` if CodeMirror should ignore the event, `false` Otherwise | |
277 | */ |
|
264 | */ | |
278 | Cell.prototype.handle_keyevent = function (editor, event) { |
|
265 | Cell.prototype.handle_keyevent = function (editor, event) { | |
279 |
|
266 | |||
280 | // console.log('CM', this.mode, event.which, event.type) |
|
267 | // console.log('CM', this.mode, event.which, event.type) | |
281 |
|
268 | |||
282 | if (this.mode === 'command') { |
|
269 | if (this.mode === 'command') { | |
283 | return true; |
|
270 | return true; | |
284 | } else if (this.mode === 'edit') { |
|
271 | } else if (this.mode === 'edit') { | |
285 | return this.handle_codemirror_keyevent(editor, event); |
|
272 | return this.handle_codemirror_keyevent(editor, event); | |
286 | } |
|
273 | } | |
287 | }; |
|
274 | }; | |
288 |
|
275 | |||
289 | /** |
|
276 | /** | |
290 | * @method at_top |
|
277 | * @method at_top | |
291 | * @return {Boolean} |
|
278 | * @return {Boolean} | |
292 | */ |
|
279 | */ | |
293 | Cell.prototype.at_top = function () { |
|
280 | Cell.prototype.at_top = function () { | |
294 | var cm = this.code_mirror |
|
281 | var cm = this.code_mirror | |
295 | var cursor = cm.getCursor(); |
|
282 | var cursor = cm.getCursor(); | |
296 | if (cursor.line === 0 && cm.findPosV(cursor, -1, 'line').hitSide) { |
|
283 | if (cursor.line === 0 && cm.findPosV(cursor, -1, 'line').hitSide) { | |
297 | return true; |
|
284 | return true; | |
298 | } else { |
|
285 | } else { | |
299 | return false; |
|
286 | return false; | |
300 | } |
|
287 | } | |
301 | }; |
|
288 | }; | |
302 |
|
289 | |||
303 | /** |
|
290 | /** | |
304 | * @method at_bottom |
|
291 | * @method at_bottom | |
305 | * @return {Boolean} |
|
292 | * @return {Boolean} | |
306 | * */ |
|
293 | * */ | |
307 | Cell.prototype.at_bottom = function () { |
|
294 | Cell.prototype.at_bottom = function () { | |
308 | var cm = this.code_mirror |
|
295 | var cm = this.code_mirror | |
309 | var cursor = cm.getCursor(); |
|
296 | var cursor = cm.getCursor(); | |
310 | if (cursor.line === (cm.lineCount()-1) && cm.findPosV(cursor, 1, 'line').hitSide) { |
|
297 | if (cursor.line === (cm.lineCount()-1) && cm.findPosV(cursor, 1, 'line').hitSide) { | |
311 | return true; |
|
298 | return true; | |
312 | } else { |
|
299 | } else { | |
313 | return false; |
|
300 | return false; | |
314 | } |
|
301 | } | |
315 | }; |
|
302 | }; | |
316 | /** |
|
303 | /** | |
317 | * enter the command mode for the cell |
|
304 | * enter the command mode for the cell | |
318 | * @method command_mode |
|
305 | * @method command_mode | |
319 | * @return is the action being taken |
|
306 | * @return is the action being taken | |
320 | */ |
|
307 | */ | |
321 | Cell.prototype.command_mode = function () { |
|
308 | Cell.prototype.command_mode = function () { | |
322 | if (this.mode !== 'command') { |
|
309 | if (this.mode !== 'command') { | |
323 | this.element.addClass('command_mode'); |
|
310 | this.element.addClass('command_mode'); | |
324 | this.element.removeClass('edit_mode'); |
|
311 | this.element.removeClass('edit_mode'); | |
325 | this.mode = 'command'; |
|
312 | this.mode = 'command'; | |
326 | return true; |
|
313 | return true; | |
327 | } else { |
|
314 | } else { | |
328 | return false; |
|
315 | return false; | |
329 | } |
|
316 | } | |
330 | }; |
|
317 | }; | |
331 |
|
318 | |||
332 | /** |
|
319 | /** | |
333 | * enter the edit mode for the cell |
|
320 | * enter the edit mode for the cell | |
334 | * @method command_mode |
|
321 | * @method command_mode | |
335 | * @return is the action being taken |
|
322 | * @return is the action being taken | |
336 | */ |
|
323 | */ | |
337 | Cell.prototype.edit_mode = function () { |
|
324 | Cell.prototype.edit_mode = function () { | |
338 | if (this.mode !== 'edit') { |
|
325 | if (this.mode !== 'edit') { | |
339 | this.element.addClass('edit_mode'); |
|
326 | this.element.addClass('edit_mode'); | |
340 | this.element.removeClass('command_mode'); |
|
327 | this.element.removeClass('command_mode'); | |
341 | this.mode = 'edit'; |
|
328 | this.mode = 'edit'; | |
342 | return true; |
|
329 | return true; | |
343 | } else { |
|
330 | } else { | |
344 | return false; |
|
331 | return false; | |
345 | } |
|
332 | } | |
346 | }; |
|
333 | }; | |
347 |
|
334 | |||
348 | /** |
|
335 | /** | |
349 | * Determine whether or not the unfocus event should be aknowledged. |
|
336 | * Determine whether or not the unfocus event should be aknowledged. | |
350 | * |
|
337 | * | |
351 | * @method should_cancel_blur |
|
338 | * @method should_cancel_blur | |
352 | * |
|
339 | * | |
353 | * @return results {bool} Whether or not to ignore the cell's blur event. |
|
340 | * @return results {bool} Whether or not to ignore the cell's blur event. | |
354 | **/ |
|
341 | **/ | |
355 | Cell.prototype.should_cancel_blur = function () { |
|
342 | Cell.prototype.should_cancel_blur = function () { | |
356 | return false; |
|
343 | return false; | |
357 | }; |
|
344 | }; | |
358 |
|
345 | |||
359 | /** |
|
346 | /** | |
360 | * Focus the cell in the DOM sense |
|
347 | * Focus the cell in the DOM sense | |
361 | * @method focus_cell |
|
348 | * @method focus_cell | |
362 | */ |
|
349 | */ | |
363 | Cell.prototype.focus_cell = function () { |
|
350 | Cell.prototype.focus_cell = function () { | |
364 | this.element.focus(); |
|
351 | this.element.focus(); | |
365 | }; |
|
352 | }; | |
366 |
|
353 | |||
367 | /** |
|
354 | /** | |
368 | * Focus the editor area so a user can type |
|
355 | * Focus the editor area so a user can type | |
369 | * |
|
356 | * | |
370 | * NOTE: If codemirror is focused via a mouse click event, you don't want to |
|
357 | * NOTE: If codemirror is focused via a mouse click event, you don't want to | |
371 | * call this because it will cause a page jump. |
|
358 | * call this because it will cause a page jump. | |
372 | * @method focus_editor |
|
359 | * @method focus_editor | |
373 | */ |
|
360 | */ | |
374 | Cell.prototype.focus_editor = function () { |
|
361 | Cell.prototype.focus_editor = function () { | |
375 | this.refresh(); |
|
362 | this.refresh(); | |
376 | this.code_mirror.focus(); |
|
363 | this.code_mirror.focus(); | |
377 | }; |
|
364 | }; | |
378 |
|
365 | |||
379 | /** |
|
366 | /** | |
380 | * Refresh codemirror instance |
|
367 | * Refresh codemirror instance | |
381 | * @method refresh |
|
368 | * @method refresh | |
382 | */ |
|
369 | */ | |
383 | Cell.prototype.refresh = function () { |
|
370 | Cell.prototype.refresh = function () { | |
384 | this.code_mirror.refresh(); |
|
371 | this.code_mirror.refresh(); | |
385 | }; |
|
372 | }; | |
386 |
|
373 | |||
387 | /** |
|
374 | /** | |
388 | * should be overritten by subclass |
|
375 | * should be overritten by subclass | |
389 | * @method get_text |
|
376 | * @method get_text | |
390 | */ |
|
377 | */ | |
391 | Cell.prototype.get_text = function () { |
|
378 | Cell.prototype.get_text = function () { | |
392 | }; |
|
379 | }; | |
393 |
|
380 | |||
394 | /** |
|
381 | /** | |
395 | * should be overritten by subclass |
|
382 | * should be overritten by subclass | |
396 | * @method set_text |
|
383 | * @method set_text | |
397 | * @param {string} text |
|
384 | * @param {string} text | |
398 | */ |
|
385 | */ | |
399 | Cell.prototype.set_text = function (text) { |
|
386 | Cell.prototype.set_text = function (text) { | |
400 | }; |
|
387 | }; | |
401 |
|
388 | |||
402 | /** |
|
389 | /** | |
403 | * should be overritten by subclass |
|
390 | * should be overritten by subclass | |
404 | * serialise cell to json. |
|
391 | * serialise cell to json. | |
405 | * @method toJSON |
|
392 | * @method toJSON | |
406 | **/ |
|
393 | **/ | |
407 | Cell.prototype.toJSON = function () { |
|
394 | Cell.prototype.toJSON = function () { | |
408 | var data = {}; |
|
395 | var data = {}; | |
409 | data.metadata = this.metadata; |
|
396 | data.metadata = this.metadata; | |
410 | data.cell_type = this.cell_type; |
|
397 | data.cell_type = this.cell_type; | |
411 | return data; |
|
398 | return data; | |
412 | }; |
|
399 | }; | |
413 |
|
400 | |||
414 |
|
401 | |||
415 | /** |
|
402 | /** | |
416 | * should be overritten by subclass |
|
403 | * should be overritten by subclass | |
417 | * @method fromJSON |
|
404 | * @method fromJSON | |
418 | **/ |
|
405 | **/ | |
419 | Cell.prototype.fromJSON = function (data) { |
|
406 | Cell.prototype.fromJSON = function (data) { | |
420 | if (data.metadata !== undefined) { |
|
407 | if (data.metadata !== undefined) { | |
421 | this.metadata = data.metadata; |
|
408 | this.metadata = data.metadata; | |
422 | } |
|
409 | } | |
423 | this.celltoolbar.rebuild(); |
|
410 | this.celltoolbar.rebuild(); | |
424 | }; |
|
411 | }; | |
425 |
|
412 | |||
426 |
|
413 | |||
427 | /** |
|
414 | /** | |
428 | * can the cell be split into two cells |
|
415 | * can the cell be split into two cells | |
429 | * @method is_splittable |
|
416 | * @method is_splittable | |
430 | **/ |
|
417 | **/ | |
431 | Cell.prototype.is_splittable = function () { |
|
418 | Cell.prototype.is_splittable = function () { | |
432 | return true; |
|
419 | return true; | |
433 | }; |
|
420 | }; | |
434 |
|
421 | |||
435 |
|
422 | |||
436 | /** |
|
423 | /** | |
437 | * can the cell be merged with other cells |
|
424 | * can the cell be merged with other cells | |
438 | * @method is_mergeable |
|
425 | * @method is_mergeable | |
439 | **/ |
|
426 | **/ | |
440 | Cell.prototype.is_mergeable = function () { |
|
427 | Cell.prototype.is_mergeable = function () { | |
441 | return true; |
|
428 | return true; | |
442 | }; |
|
429 | }; | |
443 |
|
430 | |||
444 |
|
431 | |||
445 | /** |
|
432 | /** | |
446 | * @return {String} - the text before the cursor |
|
433 | * @return {String} - the text before the cursor | |
447 | * @method get_pre_cursor |
|
434 | * @method get_pre_cursor | |
448 | **/ |
|
435 | **/ | |
449 | Cell.prototype.get_pre_cursor = function () { |
|
436 | Cell.prototype.get_pre_cursor = function () { | |
450 | var cursor = this.code_mirror.getCursor(); |
|
437 | var cursor = this.code_mirror.getCursor(); | |
451 | var text = this.code_mirror.getRange({line:0, ch:0}, cursor); |
|
438 | var text = this.code_mirror.getRange({line:0, ch:0}, cursor); | |
452 | text = text.replace(/^\n+/, '').replace(/\n+$/, ''); |
|
439 | text = text.replace(/^\n+/, '').replace(/\n+$/, ''); | |
453 | return text; |
|
440 | return text; | |
454 | }; |
|
441 | }; | |
455 |
|
442 | |||
456 |
|
443 | |||
457 | /** |
|
444 | /** | |
458 | * @return {String} - the text after the cursor |
|
445 | * @return {String} - the text after the cursor | |
459 | * @method get_post_cursor |
|
446 | * @method get_post_cursor | |
460 | **/ |
|
447 | **/ | |
461 | Cell.prototype.get_post_cursor = function () { |
|
448 | Cell.prototype.get_post_cursor = function () { | |
462 | var cursor = this.code_mirror.getCursor(); |
|
449 | var cursor = this.code_mirror.getCursor(); | |
463 | var last_line_num = this.code_mirror.lineCount()-1; |
|
450 | var last_line_num = this.code_mirror.lineCount()-1; | |
464 | var last_line_len = this.code_mirror.getLine(last_line_num).length; |
|
451 | var last_line_len = this.code_mirror.getLine(last_line_num).length; | |
465 | var end = {line:last_line_num, ch:last_line_len}; |
|
452 | var end = {line:last_line_num, ch:last_line_len}; | |
466 | var text = this.code_mirror.getRange(cursor, end); |
|
453 | var text = this.code_mirror.getRange(cursor, end); | |
467 | text = text.replace(/^\n+/, '').replace(/\n+$/, ''); |
|
454 | text = text.replace(/^\n+/, '').replace(/\n+$/, ''); | |
468 | return text; |
|
455 | return text; | |
469 | }; |
|
456 | }; | |
470 |
|
457 | |||
471 | /** |
|
458 | /** | |
472 | * Show/Hide CodeMirror LineNumber |
|
459 | * Show/Hide CodeMirror LineNumber | |
473 | * @method show_line_numbers |
|
460 | * @method show_line_numbers | |
474 | * |
|
461 | * | |
475 | * @param value {Bool} show (true), or hide (false) the line number in CodeMirror |
|
462 | * @param value {Bool} show (true), or hide (false) the line number in CodeMirror | |
476 | **/ |
|
463 | **/ | |
477 | Cell.prototype.show_line_numbers = function (value) { |
|
464 | Cell.prototype.show_line_numbers = function (value) { | |
478 | this.code_mirror.setOption('lineNumbers', value); |
|
465 | this.code_mirror.setOption('lineNumbers', value); | |
479 | this.code_mirror.refresh(); |
|
466 | this.code_mirror.refresh(); | |
480 | }; |
|
467 | }; | |
481 |
|
468 | |||
482 | /** |
|
469 | /** | |
483 | * Toggle CodeMirror LineNumber |
|
470 | * Toggle CodeMirror LineNumber | |
484 | * @method toggle_line_numbers |
|
471 | * @method toggle_line_numbers | |
485 | **/ |
|
472 | **/ | |
486 | Cell.prototype.toggle_line_numbers = function () { |
|
473 | Cell.prototype.toggle_line_numbers = function () { | |
487 | var val = this.code_mirror.getOption('lineNumbers'); |
|
474 | var val = this.code_mirror.getOption('lineNumbers'); | |
488 | this.show_line_numbers(!val); |
|
475 | this.show_line_numbers(!val); | |
489 | }; |
|
476 | }; | |
490 |
|
477 | |||
491 | /** |
|
478 | /** | |
492 | * Force codemirror highlight mode |
|
479 | * Force codemirror highlight mode | |
493 | * @method force_highlight |
|
480 | * @method force_highlight | |
494 | * @param {object} - CodeMirror mode |
|
481 | * @param {object} - CodeMirror mode | |
495 | **/ |
|
482 | **/ | |
496 | Cell.prototype.force_highlight = function(mode) { |
|
483 | Cell.prototype.force_highlight = function(mode) { | |
497 | this.user_highlight = mode; |
|
484 | this.user_highlight = mode; | |
498 | this.auto_highlight(); |
|
485 | this.auto_highlight(); | |
499 | }; |
|
486 | }; | |
500 |
|
487 | |||
501 | /** |
|
488 | /** | |
502 | * Try to autodetect cell highlight mode, or use selected mode |
|
489 | * Try to autodetect cell highlight mode, or use selected mode | |
503 | * @methods _auto_highlight |
|
490 | * @methods _auto_highlight | |
504 | * @private |
|
491 | * @private | |
505 | * @param {String|object|undefined} - CodeMirror mode | 'auto' |
|
492 | * @param {String|object|undefined} - CodeMirror mode | 'auto' | |
506 | **/ |
|
493 | **/ | |
507 | Cell.prototype._auto_highlight = function (modes) { |
|
494 | Cell.prototype._auto_highlight = function (modes) { | |
508 | //Here we handle manually selected modes |
|
495 | //Here we handle manually selected modes | |
509 | var mode; |
|
496 | var mode; | |
510 | if( this.user_highlight !== undefined && this.user_highlight != 'auto' ) |
|
497 | if( this.user_highlight !== undefined && this.user_highlight != 'auto' ) | |
511 | { |
|
498 | { | |
512 | mode = this.user_highlight; |
|
499 | mode = this.user_highlight; | |
513 | CodeMirror.autoLoadMode(this.code_mirror, mode); |
|
500 | CodeMirror.autoLoadMode(this.code_mirror, mode); | |
514 | this.code_mirror.setOption('mode', mode); |
|
501 | this.code_mirror.setOption('mode', mode); | |
515 | return; |
|
502 | return; | |
516 | } |
|
503 | } | |
517 | var current_mode = this.code_mirror.getOption('mode', mode); |
|
504 | var current_mode = this.code_mirror.getOption('mode', mode); | |
518 | var first_line = this.code_mirror.getLine(0); |
|
505 | var first_line = this.code_mirror.getLine(0); | |
519 | // loop on every pairs |
|
506 | // loop on every pairs | |
520 | for(mode in modes) { |
|
507 | for(mode in modes) { | |
521 | var regs = modes[mode].reg; |
|
508 | var regs = modes[mode].reg; | |
522 | // only one key every time but regexp can't be keys... |
|
509 | // only one key every time but regexp can't be keys... | |
523 | for(var i=0; i<regs.length; i++) { |
|
510 | for(var i=0; i<regs.length; i++) { | |
524 | // here we handle non magic_modes |
|
511 | // here we handle non magic_modes | |
525 | if(first_line.match(regs[i]) !== null) { |
|
512 | if(first_line.match(regs[i]) !== null) { | |
526 | if(current_mode == mode){ |
|
513 | if(current_mode == mode){ | |
527 | return; |
|
514 | return; | |
528 | } |
|
515 | } | |
529 | if (mode.search('magic_') !== 0) { |
|
516 | if (mode.search('magic_') !== 0) { | |
530 | this.code_mirror.setOption('mode', mode); |
|
517 | this.code_mirror.setOption('mode', mode); | |
531 | CodeMirror.autoLoadMode(this.code_mirror, mode); |
|
518 | CodeMirror.autoLoadMode(this.code_mirror, mode); | |
532 | return; |
|
519 | return; | |
533 | } |
|
520 | } | |
534 | var open = modes[mode].open || "%%"; |
|
521 | var open = modes[mode].open || "%%"; | |
535 | var close = modes[mode].close || "%%end"; |
|
522 | var close = modes[mode].close || "%%end"; | |
536 | var mmode = mode; |
|
523 | var mmode = mode; | |
537 | mode = mmode.substr(6); |
|
524 | mode = mmode.substr(6); | |
538 | if(current_mode == mode){ |
|
525 | if(current_mode == mode){ | |
539 | return; |
|
526 | return; | |
540 | } |
|
527 | } | |
541 | CodeMirror.autoLoadMode(this.code_mirror, mode); |
|
528 | CodeMirror.autoLoadMode(this.code_mirror, mode); | |
542 | // create on the fly a mode that swhitch between |
|
529 | // create on the fly a mode that swhitch between | |
543 | // plain/text and smth else otherwise `%%` is |
|
530 | // plain/text and smth else otherwise `%%` is | |
544 | // source of some highlight issues. |
|
531 | // source of some highlight issues. | |
545 | // we use patchedGetMode to circumvent a bug in CM |
|
532 | // we use patchedGetMode to circumvent a bug in CM | |
546 | CodeMirror.defineMode(mmode , function(config) { |
|
533 | CodeMirror.defineMode(mmode , function(config) { | |
547 | return CodeMirror.multiplexingMode( |
|
534 | return CodeMirror.multiplexingMode( | |
548 | CodeMirror.patchedGetMode(config, 'text/plain'), |
|
535 | CodeMirror.patchedGetMode(config, 'text/plain'), | |
549 | // always set someting on close |
|
536 | // always set someting on close | |
550 | {open: open, close: close, |
|
537 | {open: open, close: close, | |
551 | mode: CodeMirror.patchedGetMode(config, mode), |
|
538 | mode: CodeMirror.patchedGetMode(config, mode), | |
552 | delimStyle: "delimit" |
|
539 | delimStyle: "delimit" | |
553 | } |
|
540 | } | |
554 | ); |
|
541 | ); | |
555 | }); |
|
542 | }); | |
556 | this.code_mirror.setOption('mode', mmode); |
|
543 | this.code_mirror.setOption('mode', mmode); | |
557 | return; |
|
544 | return; | |
558 | } |
|
545 | } | |
559 | } |
|
546 | } | |
560 | } |
|
547 | } | |
561 | // fallback on default |
|
548 | // fallback on default | |
562 | var default_mode; |
|
549 | var default_mode; | |
563 | try { |
|
550 | try { | |
564 | default_mode = this._options.cm_config.mode; |
|
551 | default_mode = this._options.cm_config.mode; | |
565 | } catch(e) { |
|
552 | } catch(e) { | |
566 | default_mode = 'text/plain'; |
|
553 | default_mode = 'text/plain'; | |
567 | } |
|
554 | } | |
568 | if( current_mode === default_mode){ |
|
555 | if( current_mode === default_mode){ | |
569 | return; |
|
556 | return; | |
570 | } |
|
557 | } | |
571 | this.code_mirror.setOption('mode', default_mode); |
|
558 | this.code_mirror.setOption('mode', default_mode); | |
572 | }; |
|
559 | }; | |
573 |
|
560 | |||
574 | IPython.Cell = Cell; |
|
561 | IPython.Cell = Cell; | |
575 |
|
562 | |||
576 | return IPython; |
|
563 | return IPython; | |
577 |
|
564 | |||
578 | }(IPython)); |
|
565 | }(IPython)); | |
579 |
|
566 |
General Comments 0
You need to be logged in to leave comments.
Login now