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