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