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