##// END OF EJS Templates
DRY: refactor common keyboard handling to Cell...
Paul Ivanov -
Show More
@@ -1,543 +1,596 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
23
23 /**
24 /**
24 * The Base `Cell` class from which to inherit
25 * The Base `Cell` class from which to inherit
25 * @class Cell
26 * @class Cell
26 **/
27 **/
27
28
28 /*
29 /*
29 * @constructor
30 * @constructor
30 *
31 *
31 * @param {object|undefined} [options]
32 * @param {object|undefined} [options]
32 * @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
33 */
34 */
34 var Cell = function (options) {
35 var Cell = function (options) {
35
36
36 options = this.mergeopt(Cell, options);
37 options = this.mergeopt(Cell, options);
37 // superclass default overwrite our default
38 // superclass default overwrite our default
38
39
39 this.placeholder = options.placeholder || '';
40 this.placeholder = options.placeholder || '';
40 this.read_only = options.cm_config.readOnly;
41 this.read_only = options.cm_config.readOnly;
41 this.selected = false;
42 this.selected = false;
42 this.rendered = false;
43 this.rendered = false;
43 this.mode = 'command';
44 this.mode = 'command';
44 this.metadata = {};
45 this.metadata = {};
45 // load this from metadata later ?
46 // load this from metadata later ?
46 this.user_highlight = 'auto';
47 this.user_highlight = 'auto';
47 this.cm_config = options.cm_config;
48 this.cm_config = options.cm_config;
48 this.cell_id = utils.uuid();
49 this.cell_id = utils.uuid();
49 this._options = options;
50 this._options = options;
50
51
51 // For JS VM engines optimization, attributes should be all set (even
52 // For JS VM engines optimization, attributes should be all set (even
52 // to null) in the constructor, and if possible, if different subclass
53 // to null) in the constructor, and if possible, if different subclass
53 // 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
54 // 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.
55
56
56 this.element = null;
57 this.element = null;
57 this.cell_type = this.cell_type || null;
58 this.cell_type = this.cell_type || null;
58 this.code_mirror = null;
59 this.code_mirror = null;
59
60
60 this.create_element();
61 this.create_element();
61 if (this.element !== null) {
62 if (this.element !== null) {
62 this.element.data("cell", this);
63 this.element.data("cell", this);
63 this.bind_events();
64 this.bind_events();
64 this.init_classes();
65 this.init_classes();
65 }
66 }
66 };
67 };
67
68
68 Cell.options_default = {
69 Cell.options_default = {
69 cm_config : {
70 cm_config : {
70 indentUnit : 4,
71 indentUnit : 4,
71 readOnly: false,
72 readOnly: false,
72 theme: "default"
73 theme: "default"
73 }
74 }
74 };
75 };
75
76
76 // FIXME: Workaround CM Bug #332 (Safari segfault on drag)
77 // FIXME: Workaround CM Bug #332 (Safari segfault on drag)
77 // by disabling drag/drop altogether on Safari
78 // by disabling drag/drop altogether on Safari
78 // https://github.com/marijnh/CodeMirror/issues/332
79 // https://github.com/marijnh/CodeMirror/issues/332
79 if (utils.browser[0] == "Safari") {
80 if (utils.browser[0] == "Safari") {
80 Cell.options_default.cm_config.dragDrop = false;
81 Cell.options_default.cm_config.dragDrop = false;
81 }
82 }
82
83
83 Cell.prototype.mergeopt = function(_class, options, overwrite){
84 Cell.prototype.mergeopt = function(_class, options, overwrite){
84 options = options || {};
85 options = options || {};
85 overwrite = overwrite || {};
86 overwrite = overwrite || {};
86 return $.extend(true, {}, _class.options_default, options, overwrite);
87 return $.extend(true, {}, _class.options_default, options, overwrite);
87 };
88 };
88
89
89 /**
90 /**
90 * Empty. Subclasses must implement create_element.
91 * Empty. Subclasses must implement create_element.
91 * 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
92 * and will be called by Base Class constructor.
93 * and will be called by Base Class constructor.
93 * @method create_element
94 * @method create_element
94 */
95 */
95 Cell.prototype.create_element = function () {
96 Cell.prototype.create_element = function () {
96 };
97 };
97
98
98 Cell.prototype.init_classes = function () {
99 Cell.prototype.init_classes = function () {
99 // Call after this.element exists to initialize the css classes
100 // Call after this.element exists to initialize the css classes
100 // related to selected, rendered and mode.
101 // related to selected, rendered and mode.
101 if (this.selected) {
102 if (this.selected) {
102 this.element.addClass('selected');
103 this.element.addClass('selected');
103 } else {
104 } else {
104 this.element.addClass('unselected');
105 this.element.addClass('unselected');
105 }
106 }
106 if (this.rendered) {
107 if (this.rendered) {
107 this.element.addClass('rendered');
108 this.element.addClass('rendered');
108 } else {
109 } else {
109 this.element.addClass('unrendered');
110 this.element.addClass('unrendered');
110 }
111 }
111 if (this.mode === 'edit') {
112 if (this.mode === 'edit') {
112 this.element.addClass('edit_mode');
113 this.element.addClass('edit_mode');
113 } else {
114 } else {
114 this.element.addClass('command_mode');
115 this.element.addClass('command_mode');
115 }
116 }
116 };
117 };
117
118
118 /**
119 /**
119 * Subclasses can implement override bind_events.
120 * Subclasses can implement override bind_events.
120 * 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.
121 * this will be triggerd after create_element in constructor.
122 * this will be triggerd after create_element in constructor.
122 * @method bind_events
123 * @method bind_events
123 */
124 */
124 Cell.prototype.bind_events = function () {
125 Cell.prototype.bind_events = function () {
125 var that = this;
126 var that = this;
126 // 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.
127 that.element.click(function (event) {
128 that.element.click(function (event) {
128 if (!that.selected) {
129 if (!that.selected) {
129 $([IPython.events]).trigger('select.Cell', {'cell':that});
130 $([IPython.events]).trigger('select.Cell', {'cell':that});
130 }
131 }
131 });
132 });
132 that.element.focusin(function (event) {
133 that.element.focusin(function (event) {
133 if (!that.selected) {
134 if (!that.selected) {
134 $([IPython.events]).trigger('select.Cell', {'cell':that});
135 $([IPython.events]).trigger('select.Cell', {'cell':that});
135 }
136 }
136 });
137 });
137 if (this.code_mirror) {
138 if (this.code_mirror) {
138 this.code_mirror.on("change", function(cm, change) {
139 this.code_mirror.on("change", function(cm, change) {
139 $([IPython.events]).trigger("set_dirty.Notebook", {value: true});
140 $([IPython.events]).trigger("set_dirty.Notebook", {value: true});
140 });
141 });
141 }
142 }
142 if (this.code_mirror) {
143 if (this.code_mirror) {
143 this.code_mirror.on('focus', function(cm, change) {
144 this.code_mirror.on('focus', function(cm, change) {
144 $([IPython.events]).trigger('edit_mode.Cell', {cell: that});
145 $([IPython.events]).trigger('edit_mode.Cell', {cell: that});
145 });
146 });
146 }
147 }
147 if (this.code_mirror) {
148 if (this.code_mirror) {
148 this.code_mirror.on('blur', function(cm, change) {
149 this.code_mirror.on('blur', function(cm, change) {
149 // Check if this unfocus event is legit.
150 // Check if this unfocus event is legit.
150 if (!that.should_cancel_blur()) {
151 if (!that.should_cancel_blur()) {
151 $([IPython.events]).trigger('command_mode.Cell', {cell: that});
152 $([IPython.events]).trigger('command_mode.Cell', {cell: that});
152 }
153 }
153 });
154 });
154 }
155 }
155 };
156 };
157
158 /**
159 * This method gets called in CodeMirror's onKeyDown/onKeyPress
160 * handlers and is used to provide custom key handling.
161 *
162 * To have custom handling, subclasses should override this method, but still call it
163 * in order to process the Edit mode keyboard shortcuts.
164 *
165 * @method handle_codemirror_keyevent
166 * @param {CodeMirror} editor - The codemirror instance bound to the cell
167 * @param {event} event -
168 * @return {Boolean} `true` if CodeMirror should ignore the event, `false` Otherwise
169 */
170 Cell.prototype.handle_codemirror_keyevent = function (editor, event) {
171 var that = this;
172
173 if (event.keyCode === keycodes.enter && (event.shiftKey || event.ctrlKey || event.altKey)) {
174 // Always ignore shift-enter in CodeMirror as we handle it.
175 return true;
176 } else if (event.which === keycodes.up && event.type === 'keydown') {
177 // If we are not at the top, let CM handle the up arrow and
178 // prevent the global keydown handler from handling it.
179 if (!that.at_top()) {
180 event.stop();
181 return false;
182 } else {
183 return true;
184 };
185 } else if (event.which === keycodes.down && event.type === 'keydown') {
186 // If we are not at the bottom, let CM handle the down arrow and
187 // prevent the global keydown handler from handling it.
188 if (!that.at_bottom()) {
189 event.stop();
190 return false;
191 } else {
192 return true;
193 };
194 } else if (event.which === keycodes.esc && event.type === 'keydown') {
195 if (that.code_mirror.options.keyMap === "vim-insert") {
196 // vim keyMap is active and in insert mode. In this case we leave vim
197 // insert mode, but remain in notebook edit mode.
198 // Let' CM handle this event and prevent global handling.
199 event.stop();
200 return false;
201 } else {
202 // vim keyMap is not active. Leave notebook edit mode.
203 // Don't let CM handle the event, defer to global handling.
204 return true;
205 }
206 }
207 return false;
208 };
209
156
210
157 /**
211 /**
158 * Triger typsetting of math by mathjax on current cell element
212 * Triger typsetting of math by mathjax on current cell element
159 * @method typeset
213 * @method typeset
160 */
214 */
161 Cell.prototype.typeset = function () {
215 Cell.prototype.typeset = function () {
162 if (window.MathJax) {
216 if (window.MathJax) {
163 var cell_math = this.element.get(0);
217 var cell_math = this.element.get(0);
164 MathJax.Hub.Queue(["Typeset", MathJax.Hub, cell_math]);
218 MathJax.Hub.Queue(["Typeset", MathJax.Hub, cell_math]);
165 }
219 }
166 };
220 };
167
221
168 /**
222 /**
169 * handle cell level logic when a cell is selected
223 * handle cell level logic when a cell is selected
170 * @method select
224 * @method select
171 * @return is the action being taken
225 * @return is the action being taken
172 */
226 */
173 Cell.prototype.select = function () {
227 Cell.prototype.select = function () {
174 if (!this.selected) {
228 if (!this.selected) {
175 this.element.addClass('selected');
229 this.element.addClass('selected');
176 this.element.removeClass('unselected');
230 this.element.removeClass('unselected');
177 this.selected = true;
231 this.selected = true;
178 return true;
232 return true;
179 } else {
233 } else {
180 return false;
234 return false;
181 }
235 }
182 };
236 };
183
237
184 /**
238 /**
185 * handle cell level logic when a cell is unselected
239 * handle cell level logic when a cell is unselected
186 * @method unselect
240 * @method unselect
187 * @return is the action being taken
241 * @return is the action being taken
188 */
242 */
189 Cell.prototype.unselect = function () {
243 Cell.prototype.unselect = function () {
190 if (this.selected) {
244 if (this.selected) {
191 this.element.addClass('unselected');
245 this.element.addClass('unselected');
192 this.element.removeClass('selected');
246 this.element.removeClass('selected');
193 this.selected = false;
247 this.selected = false;
194 return true;
248 return true;
195 } else {
249 } else {
196 return false;
250 return false;
197 }
251 }
198 };
252 };
199
253
200 /**
254 /**
201 * handle cell level logic when a cell is rendered
255 * handle cell level logic when a cell is rendered
202 * @method render
256 * @method render
203 * @return is the action being taken
257 * @return is the action being taken
204 */
258 */
205 Cell.prototype.render = function () {
259 Cell.prototype.render = function () {
206 if (!this.rendered) {
260 if (!this.rendered) {
207 this.element.addClass('rendered');
261 this.element.addClass('rendered');
208 this.element.removeClass('unrendered');
262 this.element.removeClass('unrendered');
209 this.rendered = true;
263 this.rendered = true;
210 return true;
264 return true;
211 } else {
265 } else {
212 return false;
266 return false;
213 }
267 }
214 };
268 };
215
269
216 /**
270 /**
217 * handle cell level logic when a cell is unrendered
271 * handle cell level logic when a cell is unrendered
218 * @method unrender
272 * @method unrender
219 * @return is the action being taken
273 * @return is the action being taken
220 */
274 */
221 Cell.prototype.unrender = function () {
275 Cell.prototype.unrender = function () {
222 if (this.rendered) {
276 if (this.rendered) {
223 this.element.addClass('unrendered');
277 this.element.addClass('unrendered');
224 this.element.removeClass('rendered');
278 this.element.removeClass('rendered');
225 this.rendered = false;
279 this.rendered = false;
226 return true;
280 return true;
227 } else {
281 } else {
228 return false;
282 return false;
229 }
283 }
230 };
284 };
231
285
232 /**
286 /**
233 * Either delegates keyboard shortcut handling to either IPython keyboard
287 * Either delegates keyboard shortcut handling to either IPython keyboard
234 * manager when in command mode, or CodeMirror when in edit mode
288 * manager when in command mode, or CodeMirror when in edit mode
235 *
289 *
236 * @method handle_keyevent
290 * @method handle_keyevent
237 * @param {CodeMirror} editor - The codemirror instance bound to the cell
291 * @param {CodeMirror} editor - The codemirror instance bound to the cell
238 * @param {event} event -
292 * @param {event} event -
239 * @return {Boolean} `true` if CodeMirror should ignore the event, `false` Otherwise
293 * @return {Boolean} `true` if CodeMirror should ignore the event, `false` Otherwise
240 */
294 */
241 Cell.prototype.handle_keyevent = function (editor, event) {
295 Cell.prototype.handle_keyevent = function (editor, event) {
242
296
243 // console.log('CM', this.mode, event.which, event.type)
297 // console.log('CM', this.mode, event.which, event.type)
244
298
245 if (this.mode === 'command') {
299 if (this.mode === 'command') {
246 return true;
300 return true;
247 } else if (this.mode === 'edit') {
301 } else if (this.mode === 'edit') {
248 return this.handle_codemirror_keyevent(editor, event);
302 return this.handle_codemirror_keyevent(editor, event);
249 }
303 }
250 };
304 };
251
305
252 /**
306 /**
253 * @method at_top
307 * @method at_top
254 * @return {Boolean}
308 * @return {Boolean}
255 */
309 */
256 Cell.prototype.at_top = function () {
310 Cell.prototype.at_top = function () {
257 var cm = this.code_mirror
311 var cm = this.code_mirror
258 var cursor = cm.getCursor();
312 var cursor = cm.getCursor();
259 if (cursor.line === 0 && cm.findPosV(cursor, -1, 'line').hitSide) {
313 if (cursor.line === 0 && cm.findPosV(cursor, -1, 'line').hitSide) {
260 console.log('at top');
261 return true;
314 return true;
262 } else {
315 } else {
263 return false;
316 return false;
264 }
317 }
265 };
318 };
266
319
267 /**
320 /**
268 * @method at_bottom
321 * @method at_bottom
269 * @return {Boolean}
322 * @return {Boolean}
270 * */
323 * */
271 Cell.prototype.at_bottom = function () {
324 Cell.prototype.at_bottom = function () {
272 var cm = this.code_mirror
325 var cm = this.code_mirror
273 var cursor = cm.getCursor();
326 var cursor = cm.getCursor();
274 if (cursor.line === (cm.lineCount()-1) && cm.findPosV(cursor, 1, 'line').hitSide) {
327 if (cursor.line === (cm.lineCount()-1) && cm.findPosV(cursor, 1, 'line').hitSide) {
275 return true;
328 return true;
276 } else {
329 } else {
277 return false;
330 return false;
278 }
331 }
279 };
332 };
280 /**
333 /**
281 * enter the command mode for the cell
334 * enter the command mode for the cell
282 * @method command_mode
335 * @method command_mode
283 * @return is the action being taken
336 * @return is the action being taken
284 */
337 */
285 Cell.prototype.command_mode = function () {
338 Cell.prototype.command_mode = function () {
286 if (this.mode !== 'command') {
339 if (this.mode !== 'command') {
287 this.element.addClass('command_mode');
340 this.element.addClass('command_mode');
288 this.element.removeClass('edit_mode');
341 this.element.removeClass('edit_mode');
289 this.mode = 'command';
342 this.mode = 'command';
290 return true;
343 return true;
291 } else {
344 } else {
292 return false;
345 return false;
293 }
346 }
294 };
347 };
295
348
296 /**
349 /**
297 * enter the edit mode for the cell
350 * enter the edit mode for the cell
298 * @method command_mode
351 * @method command_mode
299 * @return is the action being taken
352 * @return is the action being taken
300 */
353 */
301 Cell.prototype.edit_mode = function () {
354 Cell.prototype.edit_mode = function () {
302 if (this.mode !== 'edit') {
355 if (this.mode !== 'edit') {
303 this.element.addClass('edit_mode');
356 this.element.addClass('edit_mode');
304 this.element.removeClass('command_mode');
357 this.element.removeClass('command_mode');
305 this.mode = 'edit';
358 this.mode = 'edit';
306 return true;
359 return true;
307 } else {
360 } else {
308 return false;
361 return false;
309 }
362 }
310 };
363 };
311
364
312 /**
365 /**
313 * Determine whether or not the unfocus event should be aknowledged.
366 * Determine whether or not the unfocus event should be aknowledged.
314 *
367 *
315 * @method should_cancel_blur
368 * @method should_cancel_blur
316 *
369 *
317 * @return results {bool} Whether or not to ignore the cell's blur event.
370 * @return results {bool} Whether or not to ignore the cell's blur event.
318 **/
371 **/
319 Cell.prototype.should_cancel_blur = function () {
372 Cell.prototype.should_cancel_blur = function () {
320 return false;
373 return false;
321 };
374 };
322
375
323 /**
376 /**
324 * Focus the cell in the DOM sense
377 * Focus the cell in the DOM sense
325 * @method focus_cell
378 * @method focus_cell
326 */
379 */
327 Cell.prototype.focus_cell = function () {
380 Cell.prototype.focus_cell = function () {
328 this.element.focus();
381 this.element.focus();
329 };
382 };
330
383
331 /**
384 /**
332 * Focus the editor area so a user can type
385 * Focus the editor area so a user can type
333 *
386 *
334 * NOTE: If codemirror is focused via a mouse click event, you don't want to
387 * NOTE: If codemirror is focused via a mouse click event, you don't want to
335 * call this because it will cause a page jump.
388 * call this because it will cause a page jump.
336 * @method focus_editor
389 * @method focus_editor
337 */
390 */
338 Cell.prototype.focus_editor = function () {
391 Cell.prototype.focus_editor = function () {
339 this.refresh();
392 this.refresh();
340 this.code_mirror.focus();
393 this.code_mirror.focus();
341 };
394 };
342
395
343 /**
396 /**
344 * Refresh codemirror instance
397 * Refresh codemirror instance
345 * @method refresh
398 * @method refresh
346 */
399 */
347 Cell.prototype.refresh = function () {
400 Cell.prototype.refresh = function () {
348 this.code_mirror.refresh();
401 this.code_mirror.refresh();
349 };
402 };
350
403
351 /**
404 /**
352 * should be overritten by subclass
405 * should be overritten by subclass
353 * @method get_text
406 * @method get_text
354 */
407 */
355 Cell.prototype.get_text = function () {
408 Cell.prototype.get_text = function () {
356 };
409 };
357
410
358 /**
411 /**
359 * should be overritten by subclass
412 * should be overritten by subclass
360 * @method set_text
413 * @method set_text
361 * @param {string} text
414 * @param {string} text
362 */
415 */
363 Cell.prototype.set_text = function (text) {
416 Cell.prototype.set_text = function (text) {
364 };
417 };
365
418
366 /**
419 /**
367 * should be overritten by subclass
420 * should be overritten by subclass
368 * serialise cell to json.
421 * serialise cell to json.
369 * @method toJSON
422 * @method toJSON
370 **/
423 **/
371 Cell.prototype.toJSON = function () {
424 Cell.prototype.toJSON = function () {
372 var data = {};
425 var data = {};
373 data.metadata = this.metadata;
426 data.metadata = this.metadata;
374 data.cell_type = this.cell_type;
427 data.cell_type = this.cell_type;
375 return data;
428 return data;
376 };
429 };
377
430
378
431
379 /**
432 /**
380 * should be overritten by subclass
433 * should be overritten by subclass
381 * @method fromJSON
434 * @method fromJSON
382 **/
435 **/
383 Cell.prototype.fromJSON = function (data) {
436 Cell.prototype.fromJSON = function (data) {
384 if (data.metadata !== undefined) {
437 if (data.metadata !== undefined) {
385 this.metadata = data.metadata;
438 this.metadata = data.metadata;
386 }
439 }
387 this.celltoolbar.rebuild();
440 this.celltoolbar.rebuild();
388 };
441 };
389
442
390
443
391 /**
444 /**
392 * can the cell be split into two cells
445 * can the cell be split into two cells
393 * @method is_splittable
446 * @method is_splittable
394 **/
447 **/
395 Cell.prototype.is_splittable = function () {
448 Cell.prototype.is_splittable = function () {
396 return true;
449 return true;
397 };
450 };
398
451
399
452
400 /**
453 /**
401 * can the cell be merged with other cells
454 * can the cell be merged with other cells
402 * @method is_mergeable
455 * @method is_mergeable
403 **/
456 **/
404 Cell.prototype.is_mergeable = function () {
457 Cell.prototype.is_mergeable = function () {
405 return true;
458 return true;
406 };
459 };
407
460
408
461
409 /**
462 /**
410 * @return {String} - the text before the cursor
463 * @return {String} - the text before the cursor
411 * @method get_pre_cursor
464 * @method get_pre_cursor
412 **/
465 **/
413 Cell.prototype.get_pre_cursor = function () {
466 Cell.prototype.get_pre_cursor = function () {
414 var cursor = this.code_mirror.getCursor();
467 var cursor = this.code_mirror.getCursor();
415 var text = this.code_mirror.getRange({line:0, ch:0}, cursor);
468 var text = this.code_mirror.getRange({line:0, ch:0}, cursor);
416 text = text.replace(/^\n+/, '').replace(/\n+$/, '');
469 text = text.replace(/^\n+/, '').replace(/\n+$/, '');
417 return text;
470 return text;
418 };
471 };
419
472
420
473
421 /**
474 /**
422 * @return {String} - the text after the cursor
475 * @return {String} - the text after the cursor
423 * @method get_post_cursor
476 * @method get_post_cursor
424 **/
477 **/
425 Cell.prototype.get_post_cursor = function () {
478 Cell.prototype.get_post_cursor = function () {
426 var cursor = this.code_mirror.getCursor();
479 var cursor = this.code_mirror.getCursor();
427 var last_line_num = this.code_mirror.lineCount()-1;
480 var last_line_num = this.code_mirror.lineCount()-1;
428 var last_line_len = this.code_mirror.getLine(last_line_num).length;
481 var last_line_len = this.code_mirror.getLine(last_line_num).length;
429 var end = {line:last_line_num, ch:last_line_len};
482 var end = {line:last_line_num, ch:last_line_len};
430 var text = this.code_mirror.getRange(cursor, end);
483 var text = this.code_mirror.getRange(cursor, end);
431 text = text.replace(/^\n+/, '').replace(/\n+$/, '');
484 text = text.replace(/^\n+/, '').replace(/\n+$/, '');
432 return text;
485 return text;
433 };
486 };
434
487
435 /**
488 /**
436 * Show/Hide CodeMirror LineNumber
489 * Show/Hide CodeMirror LineNumber
437 * @method show_line_numbers
490 * @method show_line_numbers
438 *
491 *
439 * @param value {Bool} show (true), or hide (false) the line number in CodeMirror
492 * @param value {Bool} show (true), or hide (false) the line number in CodeMirror
440 **/
493 **/
441 Cell.prototype.show_line_numbers = function (value) {
494 Cell.prototype.show_line_numbers = function (value) {
442 this.code_mirror.setOption('lineNumbers', value);
495 this.code_mirror.setOption('lineNumbers', value);
443 this.code_mirror.refresh();
496 this.code_mirror.refresh();
444 };
497 };
445
498
446 /**
499 /**
447 * Toggle CodeMirror LineNumber
500 * Toggle CodeMirror LineNumber
448 * @method toggle_line_numbers
501 * @method toggle_line_numbers
449 **/
502 **/
450 Cell.prototype.toggle_line_numbers = function () {
503 Cell.prototype.toggle_line_numbers = function () {
451 var val = this.code_mirror.getOption('lineNumbers');
504 var val = this.code_mirror.getOption('lineNumbers');
452 this.show_line_numbers(!val);
505 this.show_line_numbers(!val);
453 };
506 };
454
507
455 /**
508 /**
456 * Force codemirror highlight mode
509 * Force codemirror highlight mode
457 * @method force_highlight
510 * @method force_highlight
458 * @param {object} - CodeMirror mode
511 * @param {object} - CodeMirror mode
459 **/
512 **/
460 Cell.prototype.force_highlight = function(mode) {
513 Cell.prototype.force_highlight = function(mode) {
461 this.user_highlight = mode;
514 this.user_highlight = mode;
462 this.auto_highlight();
515 this.auto_highlight();
463 };
516 };
464
517
465 /**
518 /**
466 * Try to autodetect cell highlight mode, or use selected mode
519 * Try to autodetect cell highlight mode, or use selected mode
467 * @methods _auto_highlight
520 * @methods _auto_highlight
468 * @private
521 * @private
469 * @param {String|object|undefined} - CodeMirror mode | 'auto'
522 * @param {String|object|undefined} - CodeMirror mode | 'auto'
470 **/
523 **/
471 Cell.prototype._auto_highlight = function (modes) {
524 Cell.prototype._auto_highlight = function (modes) {
472 //Here we handle manually selected modes
525 //Here we handle manually selected modes
473 var mode;
526 var mode;
474 if( this.user_highlight !== undefined && this.user_highlight != 'auto' )
527 if( this.user_highlight !== undefined && this.user_highlight != 'auto' )
475 {
528 {
476 mode = this.user_highlight;
529 mode = this.user_highlight;
477 CodeMirror.autoLoadMode(this.code_mirror, mode);
530 CodeMirror.autoLoadMode(this.code_mirror, mode);
478 this.code_mirror.setOption('mode', mode);
531 this.code_mirror.setOption('mode', mode);
479 return;
532 return;
480 }
533 }
481 var current_mode = this.code_mirror.getOption('mode', mode);
534 var current_mode = this.code_mirror.getOption('mode', mode);
482 var first_line = this.code_mirror.getLine(0);
535 var first_line = this.code_mirror.getLine(0);
483 // loop on every pairs
536 // loop on every pairs
484 for(mode in modes) {
537 for(mode in modes) {
485 var regs = modes[mode].reg;
538 var regs = modes[mode].reg;
486 // only one key every time but regexp can't be keys...
539 // only one key every time but regexp can't be keys...
487 for(var i=0; i<regs.length; i++) {
540 for(var i=0; i<regs.length; i++) {
488 // here we handle non magic_modes
541 // here we handle non magic_modes
489 if(first_line.match(regs[i]) !== null) {
542 if(first_line.match(regs[i]) !== null) {
490 if(current_mode == mode){
543 if(current_mode == mode){
491 return;
544 return;
492 }
545 }
493 if (mode.search('magic_') !== 0) {
546 if (mode.search('magic_') !== 0) {
494 this.code_mirror.setOption('mode', mode);
547 this.code_mirror.setOption('mode', mode);
495 CodeMirror.autoLoadMode(this.code_mirror, mode);
548 CodeMirror.autoLoadMode(this.code_mirror, mode);
496 return;
549 return;
497 }
550 }
498 var open = modes[mode].open || "%%";
551 var open = modes[mode].open || "%%";
499 var close = modes[mode].close || "%%end";
552 var close = modes[mode].close || "%%end";
500 var mmode = mode;
553 var mmode = mode;
501 mode = mmode.substr(6);
554 mode = mmode.substr(6);
502 if(current_mode == mode){
555 if(current_mode == mode){
503 return;
556 return;
504 }
557 }
505 CodeMirror.autoLoadMode(this.code_mirror, mode);
558 CodeMirror.autoLoadMode(this.code_mirror, mode);
506 // create on the fly a mode that swhitch between
559 // create on the fly a mode that swhitch between
507 // plain/text and smth else otherwise `%%` is
560 // plain/text and smth else otherwise `%%` is
508 // source of some highlight issues.
561 // source of some highlight issues.
509 // we use patchedGetMode to circumvent a bug in CM
562 // we use patchedGetMode to circumvent a bug in CM
510 CodeMirror.defineMode(mmode , function(config) {
563 CodeMirror.defineMode(mmode , function(config) {
511 return CodeMirror.multiplexingMode(
564 return CodeMirror.multiplexingMode(
512 CodeMirror.patchedGetMode(config, 'text/plain'),
565 CodeMirror.patchedGetMode(config, 'text/plain'),
513 // always set someting on close
566 // always set someting on close
514 {open: open, close: close,
567 {open: open, close: close,
515 mode: CodeMirror.patchedGetMode(config, mode),
568 mode: CodeMirror.patchedGetMode(config, mode),
516 delimStyle: "delimit"
569 delimStyle: "delimit"
517 }
570 }
518 );
571 );
519 });
572 });
520 this.code_mirror.setOption('mode', mmode);
573 this.code_mirror.setOption('mode', mmode);
521 return;
574 return;
522 }
575 }
523 }
576 }
524 }
577 }
525 // fallback on default
578 // fallback on default
526 var default_mode;
579 var default_mode;
527 try {
580 try {
528 default_mode = this._options.cm_config.mode;
581 default_mode = this._options.cm_config.mode;
529 } catch(e) {
582 } catch(e) {
530 default_mode = 'text/plain';
583 default_mode = 'text/plain';
531 }
584 }
532 if( current_mode === default_mode){
585 if( current_mode === default_mode){
533 return;
586 return;
534 }
587 }
535 this.code_mirror.setOption('mode', default_mode);
588 this.code_mirror.setOption('mode', default_mode);
536 };
589 };
537
590
538 IPython.Cell = Cell;
591 IPython.Cell = Cell;
539
592
540 return IPython;
593 return IPython;
541
594
542 }(IPython));
595 }(IPython));
543
596
@@ -1,549 +1,514 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 // CodeCell
9 // CodeCell
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 CodeCell
15 * @submodule CodeCell
16 */
16 */
17
17
18
18
19 /* local util for codemirror */
19 /* local util for codemirror */
20 var posEq = function(a, b) {return a.line == b.line && a.ch == b.ch;};
20 var posEq = function(a, b) {return a.line == b.line && a.ch == b.ch;};
21
21
22 /**
22 /**
23 *
23 *
24 * function to delete until previous non blanking space character
24 * function to delete until previous non blanking space character
25 * or first multiple of 4 tabstop.
25 * or first multiple of 4 tabstop.
26 * @private
26 * @private
27 */
27 */
28 CodeMirror.commands.delSpaceToPrevTabStop = function(cm){
28 CodeMirror.commands.delSpaceToPrevTabStop = function(cm){
29 var from = cm.getCursor(true), to = cm.getCursor(false), sel = !posEq(from, to);
29 var from = cm.getCursor(true), to = cm.getCursor(false), sel = !posEq(from, to);
30 if (!posEq(from, to)) { cm.replaceRange("", from, to); return; }
30 if (!posEq(from, to)) { cm.replaceRange("", from, to); return; }
31 var cur = cm.getCursor(), line = cm.getLine(cur.line);
31 var cur = cm.getCursor(), line = cm.getLine(cur.line);
32 var tabsize = cm.getOption('tabSize');
32 var tabsize = cm.getOption('tabSize');
33 var chToPrevTabStop = cur.ch-(Math.ceil(cur.ch/tabsize)-1)*tabsize;
33 var chToPrevTabStop = cur.ch-(Math.ceil(cur.ch/tabsize)-1)*tabsize;
34 from = {ch:cur.ch-chToPrevTabStop,line:cur.line};
34 from = {ch:cur.ch-chToPrevTabStop,line:cur.line};
35 var select = cm.getRange(from,cur);
35 var select = cm.getRange(from,cur);
36 if( select.match(/^\ +$/) !== null){
36 if( select.match(/^\ +$/) !== null){
37 cm.replaceRange("",from,cur);
37 cm.replaceRange("",from,cur);
38 } else {
38 } else {
39 cm.deleteH(-1,"char");
39 cm.deleteH(-1,"char");
40 }
40 }
41 };
41 };
42
42
43
43
44 var IPython = (function (IPython) {
44 var IPython = (function (IPython) {
45 "use strict";
45 "use strict";
46
46
47 var utils = IPython.utils;
47 var utils = IPython.utils;
48 var keycodes = IPython.keyboard.keycodes;
48 var keycodes = IPython.keyboard.keycodes;
49
49
50 /**
50 /**
51 * A Cell conceived to write code.
51 * A Cell conceived to write code.
52 *
52 *
53 * The kernel doesn't have to be set at creation time, in that case
53 * The kernel doesn't have to be set at creation time, in that case
54 * it will be null and set_kernel has to be called later.
54 * it will be null and set_kernel has to be called later.
55 * @class CodeCell
55 * @class CodeCell
56 * @extends IPython.Cell
56 * @extends IPython.Cell
57 *
57 *
58 * @constructor
58 * @constructor
59 * @param {Object|null} kernel
59 * @param {Object|null} kernel
60 * @param {object|undefined} [options]
60 * @param {object|undefined} [options]
61 * @param [options.cm_config] {object} config to pass to CodeMirror
61 * @param [options.cm_config] {object} config to pass to CodeMirror
62 */
62 */
63 var CodeCell = function (kernel, options) {
63 var CodeCell = function (kernel, options) {
64 this.kernel = kernel || null;
64 this.kernel = kernel || null;
65 this.collapsed = false;
65 this.collapsed = false;
66
66
67 // create all attributed in constructor function
67 // create all attributed in constructor function
68 // even if null for V8 VM optimisation
68 // even if null for V8 VM optimisation
69 this.input_prompt_number = null;
69 this.input_prompt_number = null;
70 this.celltoolbar = null;
70 this.celltoolbar = null;
71 this.output_area = null;
71 this.output_area = null;
72 this.last_msg_id = null;
72 this.last_msg_id = null;
73 this.completer = null;
73 this.completer = null;
74
74
75
75
76 var cm_overwrite_options = {
76 var cm_overwrite_options = {
77 onKeyEvent: $.proxy(this.handle_keyevent,this)
77 onKeyEvent: $.proxy(this.handle_keyevent,this)
78 };
78 };
79
79
80 options = this.mergeopt(CodeCell, options, {cm_config:cm_overwrite_options});
80 options = this.mergeopt(CodeCell, options, {cm_config:cm_overwrite_options});
81
81
82 IPython.Cell.apply(this,[options]);
82 IPython.Cell.apply(this,[options]);
83
83
84 // Attributes we want to override in this subclass.
84 // Attributes we want to override in this subclass.
85 this.cell_type = "code";
85 this.cell_type = "code";
86
86
87 var that = this;
87 var that = this;
88 this.element.focusout(
88 this.element.focusout(
89 function() { that.auto_highlight(); }
89 function() { that.auto_highlight(); }
90 );
90 );
91 };
91 };
92
92
93 CodeCell.options_default = {
93 CodeCell.options_default = {
94 cm_config : {
94 cm_config : {
95 extraKeys: {
95 extraKeys: {
96 "Tab" : "indentMore",
96 "Tab" : "indentMore",
97 "Shift-Tab" : "indentLess",
97 "Shift-Tab" : "indentLess",
98 "Backspace" : "delSpaceToPrevTabStop",
98 "Backspace" : "delSpaceToPrevTabStop",
99 "Cmd-/" : "toggleComment",
99 "Cmd-/" : "toggleComment",
100 "Ctrl-/" : "toggleComment"
100 "Ctrl-/" : "toggleComment"
101 },
101 },
102 mode: 'ipython',
102 mode: 'ipython',
103 theme: 'ipython',
103 theme: 'ipython',
104 matchBrackets: true,
104 matchBrackets: true,
105 autoCloseBrackets: true
105 autoCloseBrackets: true
106 }
106 }
107 };
107 };
108
108
109 CodeCell.msg_cells = {};
109 CodeCell.msg_cells = {};
110
110
111 CodeCell.prototype = new IPython.Cell();
111 CodeCell.prototype = new IPython.Cell();
112
112
113 /**
113 /**
114 * @method auto_highlight
114 * @method auto_highlight
115 */
115 */
116 CodeCell.prototype.auto_highlight = function () {
116 CodeCell.prototype.auto_highlight = function () {
117 this._auto_highlight(IPython.config.cell_magic_highlight);
117 this._auto_highlight(IPython.config.cell_magic_highlight);
118 };
118 };
119
119
120 /** @method create_element */
120 /** @method create_element */
121 CodeCell.prototype.create_element = function () {
121 CodeCell.prototype.create_element = function () {
122 IPython.Cell.prototype.create_element.apply(this, arguments);
122 IPython.Cell.prototype.create_element.apply(this, arguments);
123
123
124 var cell = $('<div></div>').addClass('cell border-box-sizing code_cell');
124 var cell = $('<div></div>').addClass('cell border-box-sizing code_cell');
125 cell.attr('tabindex','2');
125 cell.attr('tabindex','2');
126
126
127 var input = $('<div></div>').addClass('input');
127 var input = $('<div></div>').addClass('input');
128 var prompt = $('<div/>').addClass('prompt input_prompt');
128 var prompt = $('<div/>').addClass('prompt input_prompt');
129 var inner_cell = $('<div/>').addClass('inner_cell');
129 var inner_cell = $('<div/>').addClass('inner_cell');
130 this.celltoolbar = new IPython.CellToolbar(this);
130 this.celltoolbar = new IPython.CellToolbar(this);
131 inner_cell.append(this.celltoolbar.element);
131 inner_cell.append(this.celltoolbar.element);
132 var input_area = $('<div/>').addClass('input_area');
132 var input_area = $('<div/>').addClass('input_area');
133 this.code_mirror = CodeMirror(input_area.get(0), this.cm_config);
133 this.code_mirror = CodeMirror(input_area.get(0), this.cm_config);
134 $(this.code_mirror.getInputField()).attr("spellcheck", "false");
134 $(this.code_mirror.getInputField()).attr("spellcheck", "false");
135 inner_cell.append(input_area);
135 inner_cell.append(input_area);
136 input.append(prompt).append(inner_cell);
136 input.append(prompt).append(inner_cell);
137
137
138 var widget_area = $('<div/>')
138 var widget_area = $('<div/>')
139 .addClass('widget-area')
139 .addClass('widget-area')
140 .hide();
140 .hide();
141 this.widget_area = widget_area;
141 this.widget_area = widget_area;
142 var widget_prompt = $('<div/>')
142 var widget_prompt = $('<div/>')
143 .addClass('prompt')
143 .addClass('prompt')
144 .appendTo(widget_area);
144 .appendTo(widget_area);
145 var widget_subarea = $('<div/>')
145 var widget_subarea = $('<div/>')
146 .addClass('widget-subarea')
146 .addClass('widget-subarea')
147 .appendTo(widget_area);
147 .appendTo(widget_area);
148 this.widget_subarea = widget_subarea;
148 this.widget_subarea = widget_subarea;
149 var widget_clear_buton = $('<button />')
149 var widget_clear_buton = $('<button />')
150 .addClass('close')
150 .addClass('close')
151 .html('&times;')
151 .html('&times;')
152 .click(function() {
152 .click(function() {
153 widget_area.slideUp('', function(){ widget_subarea.html(''); });
153 widget_area.slideUp('', function(){ widget_subarea.html(''); });
154 })
154 })
155 .appendTo(widget_prompt);
155 .appendTo(widget_prompt);
156
156
157 var output = $('<div></div>');
157 var output = $('<div></div>');
158 cell.append(input).append(widget_area).append(output);
158 cell.append(input).append(widget_area).append(output);
159 this.element = cell;
159 this.element = cell;
160 this.output_area = new IPython.OutputArea(output, true);
160 this.output_area = new IPython.OutputArea(output, true);
161 this.completer = new IPython.Completer(this);
161 this.completer = new IPython.Completer(this);
162 };
162 };
163
163
164 /** @method bind_events */
164 /** @method bind_events */
165 CodeCell.prototype.bind_events = function () {
165 CodeCell.prototype.bind_events = function () {
166 IPython.Cell.prototype.bind_events.apply(this);
166 IPython.Cell.prototype.bind_events.apply(this);
167 var that = this;
167 var that = this;
168
168
169 this.element.focusout(
169 this.element.focusout(
170 function() { that.auto_highlight(); }
170 function() { that.auto_highlight(); }
171 );
171 );
172 };
172 };
173
173
174
174
175 /**
175 /**
176 * This method gets called in CodeMirror's onKeyDown/onKeyPress
176 * This method gets called in CodeMirror's onKeyDown/onKeyPress
177 * handlers and is used to provide custom key handling. Its return
177 * handlers and is used to provide custom key handling. Its return
178 * value is used to determine if CodeMirror should ignore the event:
178 * value is used to determine if CodeMirror should ignore the event:
179 * true = ignore, false = don't ignore.
179 * true = ignore, false = don't ignore.
180 * @method handle_codemirror_keyevent
180 * @method handle_codemirror_keyevent
181 */
181 */
182 CodeCell.prototype.handle_codemirror_keyevent = function (editor, event) {
182 CodeCell.prototype.handle_codemirror_keyevent = function (editor, event) {
183
183
184 var that = this;
184 var that = this;
185 // whatever key is pressed, first, cancel the tooltip request before
185 // whatever key is pressed, first, cancel the tooltip request before
186 // they are sent, and remove tooltip if any, except for tab again
186 // they are sent, and remove tooltip if any, except for tab again
187 var tooltip_closed = null;
187 var tooltip_closed = null;
188 if (event.type === 'keydown' && event.which != keycodes.tab ) {
188 if (event.type === 'keydown' && event.which != keycodes.tab ) {
189 tooltip_closed = IPython.tooltip.remove_and_cancel_tooltip();
189 tooltip_closed = IPython.tooltip.remove_and_cancel_tooltip();
190 }
190 }
191
191
192 var cur = editor.getCursor();
192 var cur = editor.getCursor();
193 if (event.keyCode === keycodes.enter){
193 if (event.keyCode === keycodes.enter){
194 this.auto_highlight();
194 this.auto_highlight();
195 }
195 }
196
196
197 if (event.keyCode === keycodes.enter && (event.shiftKey || event.ctrlKey || event.altKey)) {
197 if (event.which === keycodes.down && event.type === 'keypress' && IPython.tooltip.time_before_tooltip >= 0) {
198 // Always ignore shift-enter in CodeMirror as we handle it.
199 return true;
200 } else if (event.which === keycodes.down && event.type === 'keypress' && IPython.tooltip.time_before_tooltip >= 0) {
201 // triger on keypress (!) otherwise inconsistent event.which depending on plateform
198 // triger on keypress (!) otherwise inconsistent event.which depending on plateform
202 // browser and keyboard layout !
199 // browser and keyboard layout !
203 // Pressing '(' , request tooltip, don't forget to reappend it
200 // Pressing '(' , request tooltip, don't forget to reappend it
204 // The second argument says to hide the tooltip if the docstring
201 // The second argument says to hide the tooltip if the docstring
205 // is actually empty
202 // is actually empty
206 IPython.tooltip.pending(that, true);
203 IPython.tooltip.pending(that, true);
207 } else if (event.which === keycodes.up && event.type === 'keydown') {
204 } else if ( tooltip_closed && event.which === keycodes.esc && event.type === 'keydown') {
208 // If we are not at the top, let CM handle the up arrow and
205 // If tooltip is active, cancel it.
209 // prevent the global keydown handler from handling it.
206 // The call to remove_and_cancel_tooltip above in L177 doesn't pass
210 if (!that.at_top()) {
207 // force=true. Because of this it won't actually close the tooltip
211 event.stop();
208 // if it is in sticky mode. Thus, we have to check again if it is open
212 return false;
209 // and close it with force=true.
213 } else {
210 if (!IPython.tooltip._hidden) {
214 return true;
211 IPython.tooltip.remove_and_cancel_tooltip(true);
215 }
216 } else if (event.which === keycodes.esc && event.type === 'keydown') {
217 // First see if the tooltip is active and if so cancel it.
218 if (tooltip_closed) {
219 // The call to remove_and_cancel_tooltip above in L177 doesn't pass
220 // force=true. Because of this it won't actually close the tooltip
221 // if it is in sticky mode. Thus, we have to check again if it is open
222 // and close it with force=true.
223 if (!IPython.tooltip._hidden) {
224 IPython.tooltip.remove_and_cancel_tooltip(true);
225 }
226 // If we closed the tooltip, don't let CM or the global handlers
227 // handle this event.
228 event.stop();
229 return true;
230 }
231 if (that.code_mirror.options.keyMap === "vim-insert") {
232 // vim keyMap is active and in insert mode. In this case we leave vim
233 // insert mode, but remain in notebook edit mode.
234 // Let' CM handle this event and prevent global handling.
235 event.stop();
236 return false;
237 } else {
238 // vim keyMap is not active. Leave notebook edit mode.
239 // Don't let CM handle the event, defer to global handling.
240 return true;
241 }
242 } else if (event.which === keycodes.down && event.type === 'keydown') {
243 // If we are not at the bottom, let CM handle the down arrow and
244 // prevent the global keydown handler from handling it.
245 if (!that.at_bottom()) {
246 event.stop();
247 return false;
248 } else {
249 return true;
250 }
212 }
213 // If we closed the tooltip, don't let CM or the global handlers
214 // handle this event.
215 event.stop();
216 return true;
251 } else if (event.keyCode === keycodes.tab && event.type === 'keydown' && event.shiftKey) {
217 } else if (event.keyCode === keycodes.tab && event.type === 'keydown' && event.shiftKey) {
252 if (editor.somethingSelected()){
218 if (editor.somethingSelected()){
253 var anchor = editor.getCursor("anchor");
219 var anchor = editor.getCursor("anchor");
254 var head = editor.getCursor("head");
220 var head = editor.getCursor("head");
255 if( anchor.line != head.line){
221 if( anchor.line != head.line){
256 return false;
222 return false;
257 }
223 }
258 }
224 }
259 IPython.tooltip.request(that);
225 IPython.tooltip.request(that);
260 event.stop();
226 event.stop();
261 return true;
227 return true;
262 } else if (event.keyCode === keycodes.tab && event.type == 'keydown') {
228 } else if (event.keyCode === keycodes.tab && event.type == 'keydown') {
263 // Tab completion.
229 // Tab completion.
264 IPython.tooltip.remove_and_cancel_tooltip();
230 IPython.tooltip.remove_and_cancel_tooltip();
265 if (editor.somethingSelected()) {
231 if (editor.somethingSelected()) {
266 return false;
232 return false;
267 }
233 }
268 var pre_cursor = editor.getRange({line:cur.line,ch:0},cur);
234 var pre_cursor = editor.getRange({line:cur.line,ch:0},cur);
269 if (pre_cursor.trim() === "") {
235 if (pre_cursor.trim() === "") {
270 // Don't autocomplete if the part of the line before the cursor
236 // Don't autocomplete if the part of the line before the cursor
271 // is empty. In this case, let CodeMirror handle indentation.
237 // is empty. In this case, let CodeMirror handle indentation.
272 return false;
238 return false;
273 } else {
239 } else {
274 event.stop();
240 event.stop();
275 this.completer.startCompletion();
241 this.completer.startCompletion();
276 return true;
242 return true;
277 }
243 }
278 } else {
244 }
279 // keypress/keyup also trigger on TAB press, and we don't want to
245
280 // use those to disable tab completion.
246 // keyboard event wasn't one of those unique to code cells, let's see
281 return false;
247 // if it's one of the generic ones (i.e. check edit mode shortcuts)
282 }
248 return IPython.Cell.prototype.handle_codemirror_keyevent.apply(this, [editor, event])
283 return false;
284 };
249 };
285
250
286 // Kernel related calls.
251 // Kernel related calls.
287
252
288 CodeCell.prototype.set_kernel = function (kernel) {
253 CodeCell.prototype.set_kernel = function (kernel) {
289 this.kernel = kernel;
254 this.kernel = kernel;
290 };
255 };
291
256
292 /**
257 /**
293 * Execute current code cell to the kernel
258 * Execute current code cell to the kernel
294 * @method execute
259 * @method execute
295 */
260 */
296 CodeCell.prototype.execute = function () {
261 CodeCell.prototype.execute = function () {
297 this.output_area.clear_output();
262 this.output_area.clear_output();
298
263
299 // Clear widget area
264 // Clear widget area
300 this.widget_subarea.html('');
265 this.widget_subarea.html('');
301 this.widget_subarea.height('');
266 this.widget_subarea.height('');
302 this.widget_area.height('');
267 this.widget_area.height('');
303 this.widget_area.hide();
268 this.widget_area.hide();
304
269
305 this.set_input_prompt('*');
270 this.set_input_prompt('*');
306 this.element.addClass("running");
271 this.element.addClass("running");
307 if (this.last_msg_id) {
272 if (this.last_msg_id) {
308 this.kernel.clear_callbacks_for_msg(this.last_msg_id);
273 this.kernel.clear_callbacks_for_msg(this.last_msg_id);
309 }
274 }
310 var callbacks = this.get_callbacks();
275 var callbacks = this.get_callbacks();
311
276
312 var old_msg_id = this.last_msg_id;
277 var old_msg_id = this.last_msg_id;
313 this.last_msg_id = this.kernel.execute(this.get_text(), callbacks, {silent: false, store_history: true});
278 this.last_msg_id = this.kernel.execute(this.get_text(), callbacks, {silent: false, store_history: true});
314 if (old_msg_id) {
279 if (old_msg_id) {
315 delete CodeCell.msg_cells[old_msg_id];
280 delete CodeCell.msg_cells[old_msg_id];
316 }
281 }
317 CodeCell.msg_cells[this.last_msg_id] = this;
282 CodeCell.msg_cells[this.last_msg_id] = this;
318 };
283 };
319
284
320 /**
285 /**
321 * Construct the default callbacks for
286 * Construct the default callbacks for
322 * @method get_callbacks
287 * @method get_callbacks
323 */
288 */
324 CodeCell.prototype.get_callbacks = function () {
289 CodeCell.prototype.get_callbacks = function () {
325 return {
290 return {
326 shell : {
291 shell : {
327 reply : $.proxy(this._handle_execute_reply, this),
292 reply : $.proxy(this._handle_execute_reply, this),
328 payload : {
293 payload : {
329 set_next_input : $.proxy(this._handle_set_next_input, this),
294 set_next_input : $.proxy(this._handle_set_next_input, this),
330 page : $.proxy(this._open_with_pager, this)
295 page : $.proxy(this._open_with_pager, this)
331 }
296 }
332 },
297 },
333 iopub : {
298 iopub : {
334 output : $.proxy(this.output_area.handle_output, this.output_area),
299 output : $.proxy(this.output_area.handle_output, this.output_area),
335 clear_output : $.proxy(this.output_area.handle_clear_output, this.output_area),
300 clear_output : $.proxy(this.output_area.handle_clear_output, this.output_area),
336 },
301 },
337 input : $.proxy(this._handle_input_request, this)
302 input : $.proxy(this._handle_input_request, this)
338 };
303 };
339 };
304 };
340
305
341 CodeCell.prototype._open_with_pager = function (payload) {
306 CodeCell.prototype._open_with_pager = function (payload) {
342 $([IPython.events]).trigger('open_with_text.Pager', payload);
307 $([IPython.events]).trigger('open_with_text.Pager', payload);
343 };
308 };
344
309
345 /**
310 /**
346 * @method _handle_execute_reply
311 * @method _handle_execute_reply
347 * @private
312 * @private
348 */
313 */
349 CodeCell.prototype._handle_execute_reply = function (msg) {
314 CodeCell.prototype._handle_execute_reply = function (msg) {
350 this.set_input_prompt(msg.content.execution_count);
315 this.set_input_prompt(msg.content.execution_count);
351 this.element.removeClass("running");
316 this.element.removeClass("running");
352 $([IPython.events]).trigger('set_dirty.Notebook', {value: true});
317 $([IPython.events]).trigger('set_dirty.Notebook', {value: true});
353 };
318 };
354
319
355 /**
320 /**
356 * @method _handle_set_next_input
321 * @method _handle_set_next_input
357 * @private
322 * @private
358 */
323 */
359 CodeCell.prototype._handle_set_next_input = function (payload) {
324 CodeCell.prototype._handle_set_next_input = function (payload) {
360 var data = {'cell': this, 'text': payload.text};
325 var data = {'cell': this, 'text': payload.text};
361 $([IPython.events]).trigger('set_next_input.Notebook', data);
326 $([IPython.events]).trigger('set_next_input.Notebook', data);
362 };
327 };
363
328
364 /**
329 /**
365 * @method _handle_input_request
330 * @method _handle_input_request
366 * @private
331 * @private
367 */
332 */
368 CodeCell.prototype._handle_input_request = function (msg) {
333 CodeCell.prototype._handle_input_request = function (msg) {
369 this.output_area.append_raw_input(msg);
334 this.output_area.append_raw_input(msg);
370 };
335 };
371
336
372
337
373 // Basic cell manipulation.
338 // Basic cell manipulation.
374
339
375 CodeCell.prototype.select = function () {
340 CodeCell.prototype.select = function () {
376 var cont = IPython.Cell.prototype.select.apply(this);
341 var cont = IPython.Cell.prototype.select.apply(this);
377 if (cont) {
342 if (cont) {
378 this.code_mirror.refresh();
343 this.code_mirror.refresh();
379 this.auto_highlight();
344 this.auto_highlight();
380 }
345 }
381 return cont;
346 return cont;
382 };
347 };
383
348
384 CodeCell.prototype.render = function () {
349 CodeCell.prototype.render = function () {
385 var cont = IPython.Cell.prototype.render.apply(this);
350 var cont = IPython.Cell.prototype.render.apply(this);
386 // Always execute, even if we are already in the rendered state
351 // Always execute, even if we are already in the rendered state
387 return cont;
352 return cont;
388 };
353 };
389
354
390 CodeCell.prototype.unrender = function () {
355 CodeCell.prototype.unrender = function () {
391 // CodeCell is always rendered
356 // CodeCell is always rendered
392 return false;
357 return false;
393 };
358 };
394
359
395 /**
360 /**
396 * Determine whether or not the unfocus event should be aknowledged.
361 * Determine whether or not the unfocus event should be aknowledged.
397 *
362 *
398 * @method should_cancel_blur
363 * @method should_cancel_blur
399 *
364 *
400 * @return results {bool} Whether or not to ignore the cell's blur event.
365 * @return results {bool} Whether or not to ignore the cell's blur event.
401 **/
366 **/
402 CodeCell.prototype.should_cancel_blur = function () {
367 CodeCell.prototype.should_cancel_blur = function () {
403 // Cancel this unfocus event if the base wants to cancel or the cell
368 // Cancel this unfocus event if the base wants to cancel or the cell
404 // completer is open or the tooltip is open.
369 // completer is open or the tooltip is open.
405 return IPython.Cell.prototype.should_cancel_blur.apply(this) ||
370 return IPython.Cell.prototype.should_cancel_blur.apply(this) ||
406 (this.completer && this.completer.is_visible()) ||
371 (this.completer && this.completer.is_visible()) ||
407 (IPython.tooltip && IPython.tooltip.is_visible());
372 (IPython.tooltip && IPython.tooltip.is_visible());
408 };
373 };
409
374
410 CodeCell.prototype.select_all = function () {
375 CodeCell.prototype.select_all = function () {
411 var start = {line: 0, ch: 0};
376 var start = {line: 0, ch: 0};
412 var nlines = this.code_mirror.lineCount();
377 var nlines = this.code_mirror.lineCount();
413 var last_line = this.code_mirror.getLine(nlines-1);
378 var last_line = this.code_mirror.getLine(nlines-1);
414 var end = {line: nlines-1, ch: last_line.length};
379 var end = {line: nlines-1, ch: last_line.length};
415 this.code_mirror.setSelection(start, end);
380 this.code_mirror.setSelection(start, end);
416 };
381 };
417
382
418
383
419 CodeCell.prototype.collapse_output = function () {
384 CodeCell.prototype.collapse_output = function () {
420 this.collapsed = true;
385 this.collapsed = true;
421 this.output_area.collapse();
386 this.output_area.collapse();
422 };
387 };
423
388
424
389
425 CodeCell.prototype.expand_output = function () {
390 CodeCell.prototype.expand_output = function () {
426 this.collapsed = false;
391 this.collapsed = false;
427 this.output_area.expand();
392 this.output_area.expand();
428 this.output_area.unscroll_area();
393 this.output_area.unscroll_area();
429 };
394 };
430
395
431 CodeCell.prototype.scroll_output = function () {
396 CodeCell.prototype.scroll_output = function () {
432 this.output_area.expand();
397 this.output_area.expand();
433 this.output_area.scroll_if_long();
398 this.output_area.scroll_if_long();
434 };
399 };
435
400
436 CodeCell.prototype.toggle_output = function () {
401 CodeCell.prototype.toggle_output = function () {
437 this.collapsed = Boolean(1 - this.collapsed);
402 this.collapsed = Boolean(1 - this.collapsed);
438 this.output_area.toggle_output();
403 this.output_area.toggle_output();
439 };
404 };
440
405
441 CodeCell.prototype.toggle_output_scroll = function () {
406 CodeCell.prototype.toggle_output_scroll = function () {
442 this.output_area.toggle_scroll();
407 this.output_area.toggle_scroll();
443 };
408 };
444
409
445
410
446 CodeCell.input_prompt_classical = function (prompt_value, lines_number) {
411 CodeCell.input_prompt_classical = function (prompt_value, lines_number) {
447 var ns;
412 var ns;
448 if (prompt_value == undefined) {
413 if (prompt_value == undefined) {
449 ns = "&nbsp;";
414 ns = "&nbsp;";
450 } else {
415 } else {
451 ns = encodeURIComponent(prompt_value);
416 ns = encodeURIComponent(prompt_value);
452 }
417 }
453 return 'In&nbsp;[' + ns + ']:';
418 return 'In&nbsp;[' + ns + ']:';
454 };
419 };
455
420
456 CodeCell.input_prompt_continuation = function (prompt_value, lines_number) {
421 CodeCell.input_prompt_continuation = function (prompt_value, lines_number) {
457 var html = [CodeCell.input_prompt_classical(prompt_value, lines_number)];
422 var html = [CodeCell.input_prompt_classical(prompt_value, lines_number)];
458 for(var i=1; i < lines_number; i++) {
423 for(var i=1; i < lines_number; i++) {
459 html.push(['...:']);
424 html.push(['...:']);
460 }
425 }
461 return html.join('<br/>');
426 return html.join('<br/>');
462 };
427 };
463
428
464 CodeCell.input_prompt_function = CodeCell.input_prompt_classical;
429 CodeCell.input_prompt_function = CodeCell.input_prompt_classical;
465
430
466
431
467 CodeCell.prototype.set_input_prompt = function (number) {
432 CodeCell.prototype.set_input_prompt = function (number) {
468 var nline = 1;
433 var nline = 1;
469 if (this.code_mirror !== undefined) {
434 if (this.code_mirror !== undefined) {
470 nline = this.code_mirror.lineCount();
435 nline = this.code_mirror.lineCount();
471 }
436 }
472 this.input_prompt_number = number;
437 this.input_prompt_number = number;
473 var prompt_html = CodeCell.input_prompt_function(this.input_prompt_number, nline);
438 var prompt_html = CodeCell.input_prompt_function(this.input_prompt_number, nline);
474 // This HTML call is okay because the user contents are escaped.
439 // This HTML call is okay because the user contents are escaped.
475 this.element.find('div.input_prompt').html(prompt_html);
440 this.element.find('div.input_prompt').html(prompt_html);
476 };
441 };
477
442
478
443
479 CodeCell.prototype.clear_input = function () {
444 CodeCell.prototype.clear_input = function () {
480 this.code_mirror.setValue('');
445 this.code_mirror.setValue('');
481 };
446 };
482
447
483
448
484 CodeCell.prototype.get_text = function () {
449 CodeCell.prototype.get_text = function () {
485 return this.code_mirror.getValue();
450 return this.code_mirror.getValue();
486 };
451 };
487
452
488
453
489 CodeCell.prototype.set_text = function (code) {
454 CodeCell.prototype.set_text = function (code) {
490 return this.code_mirror.setValue(code);
455 return this.code_mirror.setValue(code);
491 };
456 };
492
457
493
458
494 CodeCell.prototype.clear_output = function (wait) {
459 CodeCell.prototype.clear_output = function (wait) {
495 this.output_area.clear_output(wait);
460 this.output_area.clear_output(wait);
496 this.set_input_prompt();
461 this.set_input_prompt();
497 };
462 };
498
463
499
464
500 // JSON serialization
465 // JSON serialization
501
466
502 CodeCell.prototype.fromJSON = function (data) {
467 CodeCell.prototype.fromJSON = function (data) {
503 IPython.Cell.prototype.fromJSON.apply(this, arguments);
468 IPython.Cell.prototype.fromJSON.apply(this, arguments);
504 if (data.cell_type === 'code') {
469 if (data.cell_type === 'code') {
505 if (data.input !== undefined) {
470 if (data.input !== undefined) {
506 this.set_text(data.input);
471 this.set_text(data.input);
507 // make this value the starting point, so that we can only undo
472 // make this value the starting point, so that we can only undo
508 // to this state, instead of a blank cell
473 // to this state, instead of a blank cell
509 this.code_mirror.clearHistory();
474 this.code_mirror.clearHistory();
510 this.auto_highlight();
475 this.auto_highlight();
511 }
476 }
512 if (data.prompt_number !== undefined) {
477 if (data.prompt_number !== undefined) {
513 this.set_input_prompt(data.prompt_number);
478 this.set_input_prompt(data.prompt_number);
514 } else {
479 } else {
515 this.set_input_prompt();
480 this.set_input_prompt();
516 }
481 }
517 this.output_area.trusted = data.trusted || false;
482 this.output_area.trusted = data.trusted || false;
518 this.output_area.fromJSON(data.outputs);
483 this.output_area.fromJSON(data.outputs);
519 if (data.collapsed !== undefined) {
484 if (data.collapsed !== undefined) {
520 if (data.collapsed) {
485 if (data.collapsed) {
521 this.collapse_output();
486 this.collapse_output();
522 } else {
487 } else {
523 this.expand_output();
488 this.expand_output();
524 }
489 }
525 }
490 }
526 }
491 }
527 };
492 };
528
493
529
494
530 CodeCell.prototype.toJSON = function () {
495 CodeCell.prototype.toJSON = function () {
531 var data = IPython.Cell.prototype.toJSON.apply(this);
496 var data = IPython.Cell.prototype.toJSON.apply(this);
532 data.input = this.get_text();
497 data.input = this.get_text();
533 // is finite protect against undefined and '*' value
498 // is finite protect against undefined and '*' value
534 if (isFinite(this.input_prompt_number)) {
499 if (isFinite(this.input_prompt_number)) {
535 data.prompt_number = this.input_prompt_number;
500 data.prompt_number = this.input_prompt_number;
536 }
501 }
537 var outputs = this.output_area.toJSON();
502 var outputs = this.output_area.toJSON();
538 data.outputs = outputs;
503 data.outputs = outputs;
539 data.language = 'python';
504 data.language = 'python';
540 data.trusted = this.output_area.trusted;
505 data.trusted = this.output_area.trusted;
541 data.collapsed = this.collapsed;
506 data.collapsed = this.collapsed;
542 return data;
507 return data;
543 };
508 };
544
509
545
510
546 IPython.CodeCell = CodeCell;
511 IPython.CodeCell = CodeCell;
547
512
548 return IPython;
513 return IPython;
549 }(IPython));
514 }(IPython));
@@ -1,506 +1,455 b''
1 //----------------------------------------------------------------------------
1 //----------------------------------------------------------------------------
2 // Copyright (C) 2008-2012 The IPython Development Team
2 // Copyright (C) 2008-2012 The IPython Development Team
3 //
3 //
4 // Distributed under the terms of the BSD License. The full license is in
4 // Distributed under the terms of the BSD License. The full license is in
5 // the file COPYING, distributed as part of this software.
5 // the file COPYING, distributed as part of this software.
6 //----------------------------------------------------------------------------
6 //----------------------------------------------------------------------------
7
7
8 //============================================================================
8 //============================================================================
9 // TextCell
9 // TextCell
10 //============================================================================
10 //============================================================================
11
11
12
12
13
13
14 /**
14 /**
15 A module that allow to create different type of Text Cell
15 A module that allow to create different type of Text Cell
16 @module IPython
16 @module IPython
17 @namespace IPython
17 @namespace IPython
18 */
18 */
19 var IPython = (function (IPython) {
19 var IPython = (function (IPython) {
20 "use strict";
20 "use strict";
21
21
22 // TextCell base class
22 // TextCell base class
23 var keycodes = IPython.keyboard.keycodes;
23 var keycodes = IPython.keyboard.keycodes;
24 var security = IPython.security;
24 var security = IPython.security;
25
25
26 /**
26 /**
27 * Construct a new TextCell, codemirror mode is by default 'htmlmixed', and cell type is 'text'
27 * Construct a new TextCell, codemirror mode is by default 'htmlmixed', and cell type is 'text'
28 * cell start as not redered.
28 * cell start as not redered.
29 *
29 *
30 * @class TextCell
30 * @class TextCell
31 * @constructor TextCell
31 * @constructor TextCell
32 * @extend IPython.Cell
32 * @extend IPython.Cell
33 * @param {object|undefined} [options]
33 * @param {object|undefined} [options]
34 * @param [options.cm_config] {object} config to pass to CodeMirror, will extend/overwrite default config
34 * @param [options.cm_config] {object} config to pass to CodeMirror, will extend/overwrite default config
35 * @param [options.placeholder] {string} default string to use when souce in empty for rendering (only use in some TextCell subclass)
35 * @param [options.placeholder] {string} default string to use when souce in empty for rendering (only use in some TextCell subclass)
36 */
36 */
37 var TextCell = function (options) {
37 var TextCell = function (options) {
38 // in all TextCell/Cell subclasses
38 // in all TextCell/Cell subclasses
39 // do not assign most of members here, just pass it down
39 // do not assign most of members here, just pass it down
40 // in the options dict potentially overwriting what you wish.
40 // in the options dict potentially overwriting what you wish.
41 // they will be assigned in the base class.
41 // they will be assigned in the base class.
42
42
43 // we cannot put this as a class key as it has handle to "this".
43 // we cannot put this as a class key as it has handle to "this".
44 var cm_overwrite_options = {
44 var cm_overwrite_options = {
45 onKeyEvent: $.proxy(this.handle_keyevent,this)
45 onKeyEvent: $.proxy(this.handle_keyevent,this)
46 };
46 };
47
47
48 options = this.mergeopt(TextCell,options,{cm_config:cm_overwrite_options});
48 options = this.mergeopt(TextCell,options,{cm_config:cm_overwrite_options});
49
49
50 this.cell_type = this.cell_type || 'text';
50 this.cell_type = this.cell_type || 'text';
51
51
52 IPython.Cell.apply(this, [options]);
52 IPython.Cell.apply(this, [options]);
53
53
54 this.rendered = false;
54 this.rendered = false;
55 };
55 };
56
56
57 TextCell.prototype = new IPython.Cell();
57 TextCell.prototype = new IPython.Cell();
58
58
59 TextCell.options_default = {
59 TextCell.options_default = {
60 cm_config : {
60 cm_config : {
61 extraKeys: {"Tab": "indentMore","Shift-Tab" : "indentLess"},
61 extraKeys: {"Tab": "indentMore","Shift-Tab" : "indentLess"},
62 mode: 'htmlmixed',
62 mode: 'htmlmixed',
63 lineWrapping : true,
63 lineWrapping : true,
64 }
64 }
65 };
65 };
66
66
67
67
68 /**
68 /**
69 * Create the DOM element of the TextCell
69 * Create the DOM element of the TextCell
70 * @method create_element
70 * @method create_element
71 * @private
71 * @private
72 */
72 */
73 TextCell.prototype.create_element = function () {
73 TextCell.prototype.create_element = function () {
74 IPython.Cell.prototype.create_element.apply(this, arguments);
74 IPython.Cell.prototype.create_element.apply(this, arguments);
75
75
76 var cell = $("<div>").addClass('cell text_cell border-box-sizing');
76 var cell = $("<div>").addClass('cell text_cell border-box-sizing');
77 cell.attr('tabindex','2');
77 cell.attr('tabindex','2');
78
78
79 var prompt = $('<div/>').addClass('prompt input_prompt');
79 var prompt = $('<div/>').addClass('prompt input_prompt');
80 cell.append(prompt);
80 cell.append(prompt);
81 var inner_cell = $('<div/>').addClass('inner_cell');
81 var inner_cell = $('<div/>').addClass('inner_cell');
82 this.celltoolbar = new IPython.CellToolbar(this);
82 this.celltoolbar = new IPython.CellToolbar(this);
83 inner_cell.append(this.celltoolbar.element);
83 inner_cell.append(this.celltoolbar.element);
84 var input_area = $('<div/>').addClass('input_area');
84 var input_area = $('<div/>').addClass('input_area');
85 this.code_mirror = new CodeMirror(input_area.get(0), this.cm_config);
85 this.code_mirror = new CodeMirror(input_area.get(0), this.cm_config);
86 // The tabindex=-1 makes this div focusable.
86 // The tabindex=-1 makes this div focusable.
87 var render_area = $('<div/>').addClass('text_cell_render border-box-sizing').
87 var render_area = $('<div/>').addClass('text_cell_render border-box-sizing').
88 addClass('rendered_html').attr('tabindex','-1');
88 addClass('rendered_html').attr('tabindex','-1');
89 inner_cell.append(input_area).append(render_area);
89 inner_cell.append(input_area).append(render_area);
90 cell.append(inner_cell);
90 cell.append(inner_cell);
91 this.element = cell;
91 this.element = cell;
92 };
92 };
93
93
94
94
95 /**
95 /**
96 * Bind the DOM evet to cell actions
96 * Bind the DOM evet to cell actions
97 * Need to be called after TextCell.create_element
97 * Need to be called after TextCell.create_element
98 * @private
98 * @private
99 * @method bind_event
99 * @method bind_event
100 */
100 */
101 TextCell.prototype.bind_events = function () {
101 TextCell.prototype.bind_events = function () {
102 IPython.Cell.prototype.bind_events.apply(this);
102 IPython.Cell.prototype.bind_events.apply(this);
103 var that = this;
103 var that = this;
104
104
105 this.element.dblclick(function () {
105 this.element.dblclick(function () {
106 if (that.selected === false) {
106 if (that.selected === false) {
107 $([IPython.events]).trigger('select.Cell', {'cell':that});
107 $([IPython.events]).trigger('select.Cell', {'cell':that});
108 }
108 }
109 var cont = that.unrender();
109 var cont = that.unrender();
110 if (cont) {
110 if (cont) {
111 that.focus_editor();
111 that.focus_editor();
112 }
112 }
113 });
113 });
114 };
114 };
115
115
116 /**
117 * This method gets called in CodeMirror's onKeyDown/onKeyPress
118 * handlers and is used to provide custom key handling.
119 *
120 * Subclass should override this method to have custom handling
121 *
122 * @method handle_codemirror_keyevent
123 * @param {CodeMirror} editor - The codemirror instance bound to the cell
124 * @param {event} event -
125 * @return {Boolean} `true` if CodeMirror should ignore the event, `false` Otherwise
126 */
127 TextCell.prototype.handle_codemirror_keyevent = function (editor, event) {
128 var that = this;
129
130 if (event.keyCode === 13 && (event.shiftKey || event.ctrlKey || event.altKey)) {
131 // Always ignore shift-enter in CodeMirror as we handle it.
132 return true;
133 } else if (event.which === keycodes.up && event.type === 'keydown') {
134 // If we are not at the top, let CM handle the up arrow and
135 // prevent the global keydown handler from handling it.
136 if (!that.at_top()) {
137 event.stop();
138 return false;
139 } else {
140 return true;
141 };
142 } else if (event.which === keycodes.down && event.type === 'keydown') {
143 // If we are not at the bottom, let CM handle the down arrow and
144 // prevent the global keydown handler from handling it.
145 if (!that.at_bottom()) {
146 event.stop();
147 return false;
148 } else {
149 return true;
150 };
151 } else if (event.which === keycodes.esc && event.type === 'keydown') {
152 if (that.code_mirror.options.keyMap === "vim-insert") {
153 // vim keyMap is active and in insert mode. In this case we leave vim
154 // insert mode, but remain in notebook edit mode.
155 // Let' CM handle this event and prevent global handling.
156 event.stop();
157 return false;
158 } else {
159 // vim keyMap is not active. Leave notebook edit mode.
160 // Don't let CM handle the event, defer to global handling.
161 return true;
162 }
163 }
164 return false;
165 };
166
167 // Cell level actions
116 // Cell level actions
168
117
169 TextCell.prototype.select = function () {
118 TextCell.prototype.select = function () {
170 var cont = IPython.Cell.prototype.select.apply(this);
119 var cont = IPython.Cell.prototype.select.apply(this);
171 if (cont) {
120 if (cont) {
172 if (this.mode === 'edit') {
121 if (this.mode === 'edit') {
173 this.code_mirror.refresh();
122 this.code_mirror.refresh();
174 }
123 }
175 }
124 }
176 return cont;
125 return cont;
177 };
126 };
178
127
179 TextCell.prototype.unrender = function () {
128 TextCell.prototype.unrender = function () {
180 if (this.read_only) return;
129 if (this.read_only) return;
181 var cont = IPython.Cell.prototype.unrender.apply(this);
130 var cont = IPython.Cell.prototype.unrender.apply(this);
182 if (cont) {
131 if (cont) {
183 var text_cell = this.element;
132 var text_cell = this.element;
184 var output = text_cell.find("div.text_cell_render");
133 var output = text_cell.find("div.text_cell_render");
185 output.hide();
134 output.hide();
186 text_cell.find('div.input_area').show();
135 text_cell.find('div.input_area').show();
187 if (this.get_text() === this.placeholder) {
136 if (this.get_text() === this.placeholder) {
188 this.set_text('');
137 this.set_text('');
189 }
138 }
190 this.refresh();
139 this.refresh();
191 }
140 }
192 return cont;
141 return cont;
193 };
142 };
194
143
195 TextCell.prototype.execute = function () {
144 TextCell.prototype.execute = function () {
196 this.render();
145 this.render();
197 };
146 };
198
147
199 /**
148 /**
200 * setter: {{#crossLink "TextCell/set_text"}}{{/crossLink}}
149 * setter: {{#crossLink "TextCell/set_text"}}{{/crossLink}}
201 * @method get_text
150 * @method get_text
202 * @retrun {string} CodeMirror current text value
151 * @retrun {string} CodeMirror current text value
203 */
152 */
204 TextCell.prototype.get_text = function() {
153 TextCell.prototype.get_text = function() {
205 return this.code_mirror.getValue();
154 return this.code_mirror.getValue();
206 };
155 };
207
156
208 /**
157 /**
209 * @param {string} text - Codemiror text value
158 * @param {string} text - Codemiror text value
210 * @see TextCell#get_text
159 * @see TextCell#get_text
211 * @method set_text
160 * @method set_text
212 * */
161 * */
213 TextCell.prototype.set_text = function(text) {
162 TextCell.prototype.set_text = function(text) {
214 this.code_mirror.setValue(text);
163 this.code_mirror.setValue(text);
215 this.code_mirror.refresh();
164 this.code_mirror.refresh();
216 };
165 };
217
166
218 /**
167 /**
219 * setter :{{#crossLink "TextCell/set_rendered"}}{{/crossLink}}
168 * setter :{{#crossLink "TextCell/set_rendered"}}{{/crossLink}}
220 * @method get_rendered
169 * @method get_rendered
221 * @return {html} html of rendered element
170 * @return {html} html of rendered element
222 * */
171 * */
223 TextCell.prototype.get_rendered = function() {
172 TextCell.prototype.get_rendered = function() {
224 return this.element.find('div.text_cell_render').html();
173 return this.element.find('div.text_cell_render').html();
225 };
174 };
226
175
227 /**
176 /**
228 * @method set_rendered
177 * @method set_rendered
229 */
178 */
230 TextCell.prototype.set_rendered = function(text) {
179 TextCell.prototype.set_rendered = function(text) {
231 this.element.find('div.text_cell_render').html(text);
180 this.element.find('div.text_cell_render').html(text);
232 };
181 };
233
182
234
183
235 /**
184 /**
236 * Create Text cell from JSON
185 * Create Text cell from JSON
237 * @param {json} data - JSON serialized text-cell
186 * @param {json} data - JSON serialized text-cell
238 * @method fromJSON
187 * @method fromJSON
239 */
188 */
240 TextCell.prototype.fromJSON = function (data) {
189 TextCell.prototype.fromJSON = function (data) {
241 IPython.Cell.prototype.fromJSON.apply(this, arguments);
190 IPython.Cell.prototype.fromJSON.apply(this, arguments);
242 if (data.cell_type === this.cell_type) {
191 if (data.cell_type === this.cell_type) {
243 if (data.source !== undefined) {
192 if (data.source !== undefined) {
244 this.set_text(data.source);
193 this.set_text(data.source);
245 // make this value the starting point, so that we can only undo
194 // make this value the starting point, so that we can only undo
246 // to this state, instead of a blank cell
195 // to this state, instead of a blank cell
247 this.code_mirror.clearHistory();
196 this.code_mirror.clearHistory();
248 // TODO: This HTML needs to be treated as potentially dangerous
197 // TODO: This HTML needs to be treated as potentially dangerous
249 // user input and should be handled before set_rendered.
198 // user input and should be handled before set_rendered.
250 this.set_rendered(data.rendered || '');
199 this.set_rendered(data.rendered || '');
251 this.rendered = false;
200 this.rendered = false;
252 this.render();
201 this.render();
253 }
202 }
254 }
203 }
255 };
204 };
256
205
257 /** Generate JSON from cell
206 /** Generate JSON from cell
258 * @return {object} cell data serialised to json
207 * @return {object} cell data serialised to json
259 */
208 */
260 TextCell.prototype.toJSON = function () {
209 TextCell.prototype.toJSON = function () {
261 var data = IPython.Cell.prototype.toJSON.apply(this);
210 var data = IPython.Cell.prototype.toJSON.apply(this);
262 data.source = this.get_text();
211 data.source = this.get_text();
263 if (data.source == this.placeholder) {
212 if (data.source == this.placeholder) {
264 data.source = "";
213 data.source = "";
265 }
214 }
266 return data;
215 return data;
267 };
216 };
268
217
269
218
270 /**
219 /**
271 * @class MarkdownCell
220 * @class MarkdownCell
272 * @constructor MarkdownCell
221 * @constructor MarkdownCell
273 * @extends IPython.HTMLCell
222 * @extends IPython.HTMLCell
274 */
223 */
275 var MarkdownCell = function (options) {
224 var MarkdownCell = function (options) {
276 options = this.mergeopt(MarkdownCell, options);
225 options = this.mergeopt(MarkdownCell, options);
277
226
278 this.cell_type = 'markdown';
227 this.cell_type = 'markdown';
279 TextCell.apply(this, [options]);
228 TextCell.apply(this, [options]);
280 };
229 };
281
230
282 MarkdownCell.options_default = {
231 MarkdownCell.options_default = {
283 cm_config: {
232 cm_config: {
284 mode: 'gfm'
233 mode: 'gfm'
285 },
234 },
286 placeholder: "Type *Markdown* and LaTeX: $\\alpha^2$"
235 placeholder: "Type *Markdown* and LaTeX: $\\alpha^2$"
287 };
236 };
288
237
289 MarkdownCell.prototype = new TextCell();
238 MarkdownCell.prototype = new TextCell();
290
239
291 /**
240 /**
292 * @method render
241 * @method render
293 */
242 */
294 MarkdownCell.prototype.render = function () {
243 MarkdownCell.prototype.render = function () {
295 var cont = IPython.TextCell.prototype.render.apply(this);
244 var cont = IPython.TextCell.prototype.render.apply(this);
296 if (cont) {
245 if (cont) {
297 var text = this.get_text();
246 var text = this.get_text();
298 var math = null;
247 var math = null;
299 if (text === "") { text = this.placeholder; }
248 if (text === "") { text = this.placeholder; }
300 var text_and_math = IPython.mathjaxutils.remove_math(text);
249 var text_and_math = IPython.mathjaxutils.remove_math(text);
301 text = text_and_math[0];
250 text = text_and_math[0];
302 math = text_and_math[1];
251 math = text_and_math[1];
303 var html = marked.parser(marked.lexer(text));
252 var html = marked.parser(marked.lexer(text));
304 html = IPython.mathjaxutils.replace_math(html, math);
253 html = IPython.mathjaxutils.replace_math(html, math);
305 html = security.sanitize_html(html);
254 html = security.sanitize_html(html);
306 html = $(html);
255 html = $(html);
307 // links in markdown cells should open in new tabs
256 // links in markdown cells should open in new tabs
308 html.find("a[href]").not('[href^="#"]').attr("target", "_blank");
257 html.find("a[href]").not('[href^="#"]').attr("target", "_blank");
309 this.set_rendered(html);
258 this.set_rendered(html);
310 this.element.find('div.input_area').hide();
259 this.element.find('div.input_area').hide();
311 this.element.find("div.text_cell_render").show();
260 this.element.find("div.text_cell_render").show();
312 this.typeset();
261 this.typeset();
313 }
262 }
314 return cont;
263 return cont;
315 };
264 };
316
265
317
266
318 // RawCell
267 // RawCell
319
268
320 /**
269 /**
321 * @class RawCell
270 * @class RawCell
322 * @constructor RawCell
271 * @constructor RawCell
323 * @extends IPython.TextCell
272 * @extends IPython.TextCell
324 */
273 */
325 var RawCell = function (options) {
274 var RawCell = function (options) {
326
275
327 options = this.mergeopt(RawCell,options);
276 options = this.mergeopt(RawCell,options);
328 TextCell.apply(this, [options]);
277 TextCell.apply(this, [options]);
329 this.cell_type = 'raw';
278 this.cell_type = 'raw';
330 // RawCell should always hide its rendered div
279 // RawCell should always hide its rendered div
331 this.element.find('div.text_cell_render').hide();
280 this.element.find('div.text_cell_render').hide();
332 };
281 };
333
282
334 RawCell.options_default = {
283 RawCell.options_default = {
335 placeholder : "Write raw LaTeX or other formats here, for use with nbconvert.\n" +
284 placeholder : "Write raw LaTeX or other formats here, for use with nbconvert.\n" +
336 "It will not be rendered in the notebook.\n" +
285 "It will not be rendered in the notebook.\n" +
337 "When passing through nbconvert, a Raw Cell's content is added to the output unmodified."
286 "When passing through nbconvert, a Raw Cell's content is added to the output unmodified."
338 };
287 };
339
288
340 RawCell.prototype = new TextCell();
289 RawCell.prototype = new TextCell();
341
290
342 /** @method bind_events **/
291 /** @method bind_events **/
343 RawCell.prototype.bind_events = function () {
292 RawCell.prototype.bind_events = function () {
344 TextCell.prototype.bind_events.apply(this);
293 TextCell.prototype.bind_events.apply(this);
345 var that = this;
294 var that = this;
346 this.element.focusout(function() {
295 this.element.focusout(function() {
347 that.auto_highlight();
296 that.auto_highlight();
348 });
297 });
349 };
298 };
350
299
351 /**
300 /**
352 * Trigger autodetection of highlight scheme for current cell
301 * Trigger autodetection of highlight scheme for current cell
353 * @method auto_highlight
302 * @method auto_highlight
354 */
303 */
355 RawCell.prototype.auto_highlight = function () {
304 RawCell.prototype.auto_highlight = function () {
356 this._auto_highlight(IPython.config.raw_cell_highlight);
305 this._auto_highlight(IPython.config.raw_cell_highlight);
357 };
306 };
358
307
359 /** @method render **/
308 /** @method render **/
360 RawCell.prototype.render = function () {
309 RawCell.prototype.render = function () {
361 // Make sure that this cell type can never be rendered
310 // Make sure that this cell type can never be rendered
362 if (this.rendered) {
311 if (this.rendered) {
363 this.unrender();
312 this.unrender();
364 }
313 }
365 var text = this.get_text();
314 var text = this.get_text();
366 if (text === "") { text = this.placeholder; }
315 if (text === "") { text = this.placeholder; }
367 this.set_text(text);
316 this.set_text(text);
368 };
317 };
369
318
370
319
371 /**
320 /**
372 * @class HeadingCell
321 * @class HeadingCell
373 * @extends IPython.TextCell
322 * @extends IPython.TextCell
374 */
323 */
375
324
376 /**
325 /**
377 * @constructor HeadingCell
326 * @constructor HeadingCell
378 * @extends IPython.TextCell
327 * @extends IPython.TextCell
379 */
328 */
380 var HeadingCell = function (options) {
329 var HeadingCell = function (options) {
381 options = this.mergeopt(HeadingCell, options);
330 options = this.mergeopt(HeadingCell, options);
382
331
383 this.level = 1;
332 this.level = 1;
384 this.cell_type = 'heading';
333 this.cell_type = 'heading';
385 TextCell.apply(this, [options]);
334 TextCell.apply(this, [options]);
386
335
387 /**
336 /**
388 * heading level of the cell, use getter and setter to access
337 * heading level of the cell, use getter and setter to access
389 * @property level
338 * @property level
390 */
339 */
391 };
340 };
392
341
393 HeadingCell.options_default = {
342 HeadingCell.options_default = {
394 placeholder: "Type Heading Here"
343 placeholder: "Type Heading Here"
395 };
344 };
396
345
397 HeadingCell.prototype = new TextCell();
346 HeadingCell.prototype = new TextCell();
398
347
399 /** @method fromJSON */
348 /** @method fromJSON */
400 HeadingCell.prototype.fromJSON = function (data) {
349 HeadingCell.prototype.fromJSON = function (data) {
401 if (data.level !== undefined){
350 if (data.level !== undefined){
402 this.level = data.level;
351 this.level = data.level;
403 }
352 }
404 TextCell.prototype.fromJSON.apply(this, arguments);
353 TextCell.prototype.fromJSON.apply(this, arguments);
405 };
354 };
406
355
407
356
408 /** @method toJSON */
357 /** @method toJSON */
409 HeadingCell.prototype.toJSON = function () {
358 HeadingCell.prototype.toJSON = function () {
410 var data = TextCell.prototype.toJSON.apply(this);
359 var data = TextCell.prototype.toJSON.apply(this);
411 data.level = this.get_level();
360 data.level = this.get_level();
412 return data;
361 return data;
413 };
362 };
414
363
415 /**
364 /**
416 * can the cell be split into two cells
365 * can the cell be split into two cells
417 * @method is_splittable
366 * @method is_splittable
418 **/
367 **/
419 HeadingCell.prototype.is_splittable = function () {
368 HeadingCell.prototype.is_splittable = function () {
420 return false;
369 return false;
421 };
370 };
422
371
423
372
424 /**
373 /**
425 * can the cell be merged with other cells
374 * can the cell be merged with other cells
426 * @method is_mergeable
375 * @method is_mergeable
427 **/
376 **/
428 HeadingCell.prototype.is_mergeable = function () {
377 HeadingCell.prototype.is_mergeable = function () {
429 return false;
378 return false;
430 };
379 };
431
380
432 /**
381 /**
433 * Change heading level of cell, and re-render
382 * Change heading level of cell, and re-render
434 * @method set_level
383 * @method set_level
435 */
384 */
436 HeadingCell.prototype.set_level = function (level) {
385 HeadingCell.prototype.set_level = function (level) {
437 this.level = level;
386 this.level = level;
438 if (this.rendered) {
387 if (this.rendered) {
439 this.rendered = false;
388 this.rendered = false;
440 this.render();
389 this.render();
441 }
390 }
442 };
391 };
443
392
444 /** The depth of header cell, based on html (h1 to h6)
393 /** The depth of header cell, based on html (h1 to h6)
445 * @method get_level
394 * @method get_level
446 * @return {integer} level - for 1 to 6
395 * @return {integer} level - for 1 to 6
447 */
396 */
448 HeadingCell.prototype.get_level = function () {
397 HeadingCell.prototype.get_level = function () {
449 return this.level;
398 return this.level;
450 };
399 };
451
400
452
401
453 HeadingCell.prototype.set_rendered = function (html) {
402 HeadingCell.prototype.set_rendered = function (html) {
454 this.element.find("div.text_cell_render").html(html);
403 this.element.find("div.text_cell_render").html(html);
455 };
404 };
456
405
457
406
458 HeadingCell.prototype.get_rendered = function () {
407 HeadingCell.prototype.get_rendered = function () {
459 var r = this.element.find("div.text_cell_render");
408 var r = this.element.find("div.text_cell_render");
460 return r.children().first().html();
409 return r.children().first().html();
461 };
410 };
462
411
463
412
464 HeadingCell.prototype.render = function () {
413 HeadingCell.prototype.render = function () {
465 var cont = IPython.TextCell.prototype.render.apply(this);
414 var cont = IPython.TextCell.prototype.render.apply(this);
466 if (cont) {
415 if (cont) {
467 var text = this.get_text();
416 var text = this.get_text();
468 var math = null;
417 var math = null;
469 // Markdown headings must be a single line
418 // Markdown headings must be a single line
470 text = text.replace(/\n/g, ' ');
419 text = text.replace(/\n/g, ' ');
471 if (text === "") { text = this.placeholder; }
420 if (text === "") { text = this.placeholder; }
472 text = Array(this.level + 1).join("#") + " " + text;
421 text = Array(this.level + 1).join("#") + " " + text;
473 var text_and_math = IPython.mathjaxutils.remove_math(text);
422 var text_and_math = IPython.mathjaxutils.remove_math(text);
474 text = text_and_math[0];
423 text = text_and_math[0];
475 math = text_and_math[1];
424 math = text_and_math[1];
476 var html = marked.parser(marked.lexer(text));
425 var html = marked.parser(marked.lexer(text));
477 html = IPython.mathjaxutils.replace_math(html, math);
426 html = IPython.mathjaxutils.replace_math(html, math);
478 html = security.sanitize_html(html);
427 html = security.sanitize_html(html);
479 var h = $(html);
428 var h = $(html);
480 // add id and linkback anchor
429 // add id and linkback anchor
481 var hash = h.text().replace(/ /g, '-');
430 var hash = h.text().replace(/ /g, '-');
482 h.attr('id', hash);
431 h.attr('id', hash);
483 h.append(
432 h.append(
484 $('<a/>')
433 $('<a/>')
485 .addClass('anchor-link')
434 .addClass('anchor-link')
486 .attr('href', '#' + hash)
435 .attr('href', '#' + hash)
487 .text('¶')
436 .text('¶')
488 );
437 );
489 this.set_rendered(h);
438 this.set_rendered(h);
490 this.element.find('div.input_area').hide();
439 this.element.find('div.input_area').hide();
491 this.element.find("div.text_cell_render").show();
440 this.element.find("div.text_cell_render").show();
492 this.typeset();
441 this.typeset();
493 }
442 }
494 return cont;
443 return cont;
495 };
444 };
496
445
497 IPython.TextCell = TextCell;
446 IPython.TextCell = TextCell;
498 IPython.MarkdownCell = MarkdownCell;
447 IPython.MarkdownCell = MarkdownCell;
499 IPython.RawCell = RawCell;
448 IPython.RawCell = RawCell;
500 IPython.HeadingCell = HeadingCell;
449 IPython.HeadingCell = HeadingCell;
501
450
502
451
503 return IPython;
452 return IPython;
504
453
505 }(IPython));
454 }(IPython));
506
455
General Comments 0
You need to be logged in to leave comments. Login now