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