##// END OF EJS Templates
catch if cm_config of other key undefined
Matthias Bussonnier -
Show More
@@ -1,335 +1,340 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
19
20 var utils = IPython.utils;
20 var utils = IPython.utils;
21
21
22 /**
22 /**
23 * The Base `Cell` class from which to inherit
23 * The Base `Cell` class from which to inherit
24 * @class Cell
24 * @class Cell
25 **/
25 **/
26
26
27 /*
27 /*
28 * @constructor
28 * @constructor
29 *
29 *
30 * @param {object|undefined} [options]
30 * @param {object|undefined} [options]
31 * @param [options.cm_config] {object} config to pass to CodeMirror, will extend default parameters
31 * @param [options.cm_config] {object} config to pass to CodeMirror, will extend default parameters
32 */
32 */
33 var Cell = function (options) {
33 var Cell = function (options) {
34
34
35 options = this.mergeopt(Cell, options)
35 options = this.mergeopt(Cell, options)
36 // superclass default overwrite our default
36 // superclass default overwrite our default
37
37
38 this.placeholder = options.placeholder || '';
38 this.placeholder = options.placeholder || '';
39 this.read_only = options.cm_config.readOnly;
39 this.read_only = options.cm_config.readOnly;
40 this.selected = false;
40 this.selected = false;
41 this.element = null;
41 this.element = null;
42 this.metadata = {};
42 this.metadata = {};
43 // load this from metadata later ?
43 // load this from metadata later ?
44 this.user_highlight = 'auto';
44 this.user_highlight = 'auto';
45 this.cm_config = options.cm_config;
45 this.cm_config = options.cm_config;
46 this.create_element();
46 this.create_element();
47 if (this.element !== null) {
47 if (this.element !== null) {
48 this.element.data("cell", this);
48 this.element.data("cell", this);
49 this.bind_events();
49 this.bind_events();
50 }
50 }
51 this.cell_id = utils.uuid();
51 this.cell_id = utils.uuid();
52 this._options = options;
52 this._options = options;
53 };
53 };
54
54
55 Cell.options_default = {
55 Cell.options_default = {
56 cm_config : {
56 cm_config : {
57 indentUnit : 4,
57 indentUnit : 4,
58 readOnly: false,
58 readOnly: false,
59 theme: "default"
59 theme: "default"
60 }
60 }
61 };
61 };
62
62
63 // FIXME: Workaround CM Bug #332 (Safari segfault on drag)
63 // FIXME: Workaround CM Bug #332 (Safari segfault on drag)
64 // by disabling drag/drop altogether on Safari
64 // by disabling drag/drop altogether on Safari
65 // https://github.com/marijnh/CodeMirror/issues/332
65 // https://github.com/marijnh/CodeMirror/issues/332
66
66
67 if (utils.browser[0] == "Safari") {
67 if (utils.browser[0] == "Safari") {
68 Cell.options_default.cm_config.dragDrop = false;
68 Cell.options_default.cm_config.dragDrop = false;
69 }
69 }
70
70
71 Cell.prototype.mergeopt = function(_class, options, overwrite){
71 Cell.prototype.mergeopt = function(_class, options, overwrite){
72 overwrite = overwrite || {};
72 overwrite = overwrite || {};
73 return $.extend(true, {}, _class.options_default, options, overwrite)
73 return $.extend(true, {}, _class.options_default, options, overwrite)
74
74
75 }
75 }
76
76
77
77
78
78
79 /**
79 /**
80 * Empty. Subclasses must implement create_element.
80 * Empty. Subclasses must implement create_element.
81 * This should contain all the code to create the DOM element in notebook
81 * This should contain all the code to create the DOM element in notebook
82 * and will be called by Base Class constructor.
82 * and will be called by Base Class constructor.
83 * @method create_element
83 * @method create_element
84 */
84 */
85 Cell.prototype.create_element = function () {
85 Cell.prototype.create_element = function () {
86 };
86 };
87
87
88
88
89 /**
89 /**
90 * Subclasses can implement override bind_events.
90 * Subclasses can implement override bind_events.
91 * Be carefull to call the parent method when overwriting as it fires event.
91 * Be carefull to call the parent method when overwriting as it fires event.
92 * this will be triggerd after create_element in constructor.
92 * this will be triggerd after create_element in constructor.
93 * @method bind_events
93 * @method bind_events
94 */
94 */
95 Cell.prototype.bind_events = function () {
95 Cell.prototype.bind_events = function () {
96 var that = this;
96 var that = this;
97 // We trigger events so that Cell doesn't have to depend on Notebook.
97 // We trigger events so that Cell doesn't have to depend on Notebook.
98 that.element.click(function (event) {
98 that.element.click(function (event) {
99 if (that.selected === false) {
99 if (that.selected === false) {
100 $([IPython.events]).trigger('select.Cell', {'cell':that});
100 $([IPython.events]).trigger('select.Cell', {'cell':that});
101 }
101 }
102 });
102 });
103 that.element.focusin(function (event) {
103 that.element.focusin(function (event) {
104 if (that.selected === false) {
104 if (that.selected === false) {
105 $([IPython.events]).trigger('select.Cell', {'cell':that});
105 $([IPython.events]).trigger('select.Cell', {'cell':that});
106 }
106 }
107 });
107 });
108 if (this.code_mirror) {
108 if (this.code_mirror) {
109 this.code_mirror.on("change", function(cm, change) {
109 this.code_mirror.on("change", function(cm, change) {
110 $([IPython.events]).trigger("set_dirty.Notebook", {value: true});
110 $([IPython.events]).trigger("set_dirty.Notebook", {value: true});
111 });
111 });
112 }
112 }
113 };
113 };
114
114
115 /**
115 /**
116 * Triger typsetting of math by mathjax on current cell element
116 * Triger typsetting of math by mathjax on current cell element
117 * @method typeset
117 * @method typeset
118 */
118 */
119 Cell.prototype.typeset = function () {
119 Cell.prototype.typeset = function () {
120 if (window.MathJax){
120 if (window.MathJax){
121 var cell_math = this.element.get(0);
121 var cell_math = this.element.get(0);
122 MathJax.Hub.Queue(["Typeset", MathJax.Hub, cell_math]);
122 MathJax.Hub.Queue(["Typeset", MathJax.Hub, cell_math]);
123 }
123 }
124 };
124 };
125
125
126 /**
126 /**
127 * should be triggerd when cell is selected
127 * should be triggerd when cell is selected
128 * @method select
128 * @method select
129 */
129 */
130 Cell.prototype.select = function () {
130 Cell.prototype.select = function () {
131 this.element.addClass('selected');
131 this.element.addClass('selected');
132 this.selected = true;
132 this.selected = true;
133 };
133 };
134
134
135
135
136 /**
136 /**
137 * should be triggerd when cell is unselected
137 * should be triggerd when cell is unselected
138 * @method unselect
138 * @method unselect
139 */
139 */
140 Cell.prototype.unselect = function () {
140 Cell.prototype.unselect = function () {
141 this.element.removeClass('selected');
141 this.element.removeClass('selected');
142 this.selected = false;
142 this.selected = false;
143 };
143 };
144
144
145 /**
145 /**
146 * should be overritten by subclass
146 * should be overritten by subclass
147 * @method get_text
147 * @method get_text
148 */
148 */
149 Cell.prototype.get_text = function () {
149 Cell.prototype.get_text = function () {
150 };
150 };
151
151
152 /**
152 /**
153 * should be overritten by subclass
153 * should be overritten by subclass
154 * @method set_text
154 * @method set_text
155 * @param {string} text
155 * @param {string} text
156 */
156 */
157 Cell.prototype.set_text = function (text) {
157 Cell.prototype.set_text = function (text) {
158 };
158 };
159
159
160 /**
160 /**
161 * Refresh codemirror instance
161 * Refresh codemirror instance
162 * @method refresh
162 * @method refresh
163 */
163 */
164 Cell.prototype.refresh = function () {
164 Cell.prototype.refresh = function () {
165 this.code_mirror.refresh();
165 this.code_mirror.refresh();
166 };
166 };
167
167
168
168
169 /**
169 /**
170 * should be overritten by subclass
170 * should be overritten by subclass
171 * @method edit
171 * @method edit
172 **/
172 **/
173 Cell.prototype.edit = function () {
173 Cell.prototype.edit = function () {
174 };
174 };
175
175
176
176
177 /**
177 /**
178 * should be overritten by subclass
178 * should be overritten by subclass
179 * @method render
179 * @method render
180 **/
180 **/
181 Cell.prototype.render = function () {
181 Cell.prototype.render = function () {
182 };
182 };
183
183
184 /**
184 /**
185 * should be overritten by subclass
185 * should be overritten by subclass
186 * serialise cell to json.
186 * serialise cell to json.
187 * @method toJSON
187 * @method toJSON
188 **/
188 **/
189 Cell.prototype.toJSON = function () {
189 Cell.prototype.toJSON = function () {
190 var data = {};
190 var data = {};
191 data.metadata = this.metadata;
191 data.metadata = this.metadata;
192 return data;
192 return data;
193 };
193 };
194
194
195
195
196 /**
196 /**
197 * should be overritten by subclass
197 * should be overritten by subclass
198 * @method fromJSON
198 * @method fromJSON
199 **/
199 **/
200 Cell.prototype.fromJSON = function (data) {
200 Cell.prototype.fromJSON = function (data) {
201 if (data.metadata !== undefined) {
201 if (data.metadata !== undefined) {
202 this.metadata = data.metadata;
202 this.metadata = data.metadata;
203 }
203 }
204 this.celltoolbar.rebuild();
204 this.celltoolbar.rebuild();
205 };
205 };
206
206
207
207
208 /**
208 /**
209 * can the cell be splitted in 2 cells.
209 * can the cell be splitted in 2 cells.
210 * @method is_splittable
210 * @method is_splittable
211 **/
211 **/
212 Cell.prototype.is_splittable = function () {
212 Cell.prototype.is_splittable = function () {
213 return true;
213 return true;
214 };
214 };
215
215
216
216
217 /**
217 /**
218 * @return {String} - the text before the cursor
218 * @return {String} - the text before the cursor
219 * @method get_pre_cursor
219 * @method get_pre_cursor
220 **/
220 **/
221 Cell.prototype.get_pre_cursor = function () {
221 Cell.prototype.get_pre_cursor = function () {
222 var cursor = this.code_mirror.getCursor();
222 var cursor = this.code_mirror.getCursor();
223 var text = this.code_mirror.getRange({line:0, ch:0}, cursor);
223 var text = this.code_mirror.getRange({line:0, ch:0}, cursor);
224 text = text.replace(/^\n+/, '').replace(/\n+$/, '');
224 text = text.replace(/^\n+/, '').replace(/\n+$/, '');
225 return text;
225 return text;
226 }
226 }
227
227
228
228
229 /**
229 /**
230 * @return {String} - the text after the cursor
230 * @return {String} - the text after the cursor
231 * @method get_post_cursor
231 * @method get_post_cursor
232 **/
232 **/
233 Cell.prototype.get_post_cursor = function () {
233 Cell.prototype.get_post_cursor = function () {
234 var cursor = this.code_mirror.getCursor();
234 var cursor = this.code_mirror.getCursor();
235 var last_line_num = this.code_mirror.lineCount()-1;
235 var last_line_num = this.code_mirror.lineCount()-1;
236 var last_line_len = this.code_mirror.getLine(last_line_num).length;
236 var last_line_len = this.code_mirror.getLine(last_line_num).length;
237 var end = {line:last_line_num, ch:last_line_len}
237 var end = {line:last_line_num, ch:last_line_len}
238 var text = this.code_mirror.getRange(cursor, end);
238 var text = this.code_mirror.getRange(cursor, end);
239 text = text.replace(/^\n+/, '').replace(/\n+$/, '');
239 text = text.replace(/^\n+/, '').replace(/\n+$/, '');
240 return text;
240 return text;
241 };
241 };
242
242
243 /**
243 /**
244 * Show/Hide CodeMirror LineNumber
244 * Show/Hide CodeMirror LineNumber
245 * @method show_line_numbers
245 * @method show_line_numbers
246 *
246 *
247 * @param value {Bool} show (true), or hide (false) the line number in CodeMirror
247 * @param value {Bool} show (true), or hide (false) the line number in CodeMirror
248 **/
248 **/
249 Cell.prototype.show_line_numbers = function (value) {
249 Cell.prototype.show_line_numbers = function (value) {
250 this.code_mirror.setOption('lineNumbers', value);
250 this.code_mirror.setOption('lineNumbers', value);
251 this.code_mirror.refresh();
251 this.code_mirror.refresh();
252 };
252 };
253
253
254 /**
254 /**
255 * Toggle CodeMirror LineNumber
255 * Toggle CodeMirror LineNumber
256 * @method toggle_line_numbers
256 * @method toggle_line_numbers
257 **/
257 **/
258 Cell.prototype.toggle_line_numbers = function () {
258 Cell.prototype.toggle_line_numbers = function () {
259 var val = this.code_mirror.getOption('lineNumbers');
259 var val = this.code_mirror.getOption('lineNumbers');
260 this.show_line_numbers(!val);
260 this.show_line_numbers(!val);
261 };
261 };
262
262
263 /**
263 /**
264 * Force codemirror highlight mode
264 * Force codemirror highlight mode
265 * @method force_highlight
265 * @method force_highlight
266 * @param {object} - CodeMirror mode
266 * @param {object} - CodeMirror mode
267 **/
267 **/
268 Cell.prototype.force_highlight = function(mode) {
268 Cell.prototype.force_highlight = function(mode) {
269 this.user_highlight = mode;
269 this.user_highlight = mode;
270 this.auto_highlight();
270 this.auto_highlight();
271 };
271 };
272
272
273 /**
273 /**
274 * Try to autodetect cell highlight mode, or use selected mode
274 * Try to autodetect cell highlight mode, or use selected mode
275 * @methods _auto_highlight
275 * @methods _auto_highlight
276 * @private
276 * @private
277 * @param {String|object|undefined} - CodeMirror mode | 'auto'
277 * @param {String|object|undefined} - CodeMirror mode | 'auto'
278 **/
278 **/
279 Cell.prototype._auto_highlight = function (modes) {
279 Cell.prototype._auto_highlight = function (modes) {
280 //Here we handle manually selected modes
280 //Here we handle manually selected modes
281 if( this.user_highlight != undefined && this.user_highlight != 'auto' )
281 if( this.user_highlight != undefined && this.user_highlight != 'auto' )
282 {
282 {
283 var mode = this.user_highlight;
283 var mode = this.user_highlight;
284 CodeMirror.autoLoadMode(this.code_mirror, mode);
284 CodeMirror.autoLoadMode(this.code_mirror, mode);
285 this.code_mirror.setOption('mode', mode);
285 this.code_mirror.setOption('mode', mode);
286 return;
286 return;
287 }
287 }
288 var first_line = this.code_mirror.getLine(0);
288 var first_line = this.code_mirror.getLine(0);
289 // loop on every pairs
289 // loop on every pairs
290 for( var mode in modes) {
290 for( var mode in modes) {
291 var regs = modes[mode]['reg'];
291 var regs = modes[mode]['reg'];
292 // only one key every time but regexp can't be keys...
292 // only one key every time but regexp can't be keys...
293 for(var reg in regs ) {
293 for(var reg in regs ) {
294 // here we handle non magic_modes
294 // here we handle non magic_modes
295 if(first_line.match(regs[reg]) != null) {
295 if(first_line.match(regs[reg]) != null) {
296 if (mode.search('magic_') != 0) {
296 if (mode.search('magic_') != 0) {
297 this.code_mirror.setOption('mode', mode);
297 this.code_mirror.setOption('mode', mode);
298 CodeMirror.autoLoadMode(this.code_mirror, mode);
298 CodeMirror.autoLoadMode(this.code_mirror, mode);
299 return;
299 return;
300 }
300 }
301 var open = modes[mode]['open']|| "%%";
301 var open = modes[mode]['open']|| "%%";
302 var close = modes[mode]['close']|| "%%end";
302 var close = modes[mode]['close']|| "%%end";
303 var mmode = mode;
303 var mmode = mode;
304 mode = mmode.substr(6);
304 mode = mmode.substr(6);
305 CodeMirror.autoLoadMode(this.code_mirror, mode);
305 CodeMirror.autoLoadMode(this.code_mirror, mode);
306 // create on the fly a mode that swhitch between
306 // create on the fly a mode that swhitch between
307 // plain/text and smth else otherwise `%%` is
307 // plain/text and smth else otherwise `%%` is
308 // source of some highlight issues.
308 // source of some highlight issues.
309 // we use patchedGetMode to circumvent a bug in CM
309 // we use patchedGetMode to circumvent a bug in CM
310 CodeMirror.defineMode(mmode , function(config) {
310 CodeMirror.defineMode(mmode , function(config) {
311 return CodeMirror.multiplexingMode(
311 return CodeMirror.multiplexingMode(
312 CodeMirror.patchedGetMode(config, 'text/plain'),
312 CodeMirror.patchedGetMode(config, 'text/plain'),
313 // always set someting on close
313 // always set someting on close
314 {open: open, close: close,
314 {open: open, close: close,
315 mode: CodeMirror.patchedGetMode(config, mode),
315 mode: CodeMirror.patchedGetMode(config, mode),
316 delimStyle: "delimit"
316 delimStyle: "delimit"
317 }
317 }
318 );
318 );
319 });
319 });
320 this.code_mirror.setOption('mode', mmode);
320 this.code_mirror.setOption('mode', mmode);
321 return;
321 return;
322 }
322 }
323 }
323 }
324 }
324 }
325 // fallback on default
325 // fallback on default
326 var default_mode = this._options.cm_config.mode || 'text/plain';
326 var default_mode
327 try {
328 default_mode = this._options.cm_config.mode;
329 } catch(e) {
330 default_mode = 'text/plain';
331 }
327 this.code_mirror.setOption('mode', default_mode);
332 this.code_mirror.setOption('mode', default_mode);
328 };
333 };
329
334
330 IPython.Cell = Cell;
335 IPython.Cell = Cell;
331
336
332 return IPython;
337 return IPython;
333
338
334 }(IPython));
339 }(IPython));
335
340
General Comments 0
You need to be logged in to leave comments. Login now