##// END OF EJS Templates
on("destroy",...) -> once("destroy",...) so we don't keep a reference to it, preventing gc...
on("destroy",...) -> once("destroy",...) so we don't keep a reference to it, preventing gc Thanks to Sylvain Corlay for the suggestion.

File last commit:

r17940:1a62abc5
r18058:c7253b21
Show More
cell.js
557 lines | 17.1 KiB | application/javascript | JavascriptLexer
Jonathan Frederic
Progress...
r17196 // Copyright (c) IPython Development Team.
// Distributed under the terms of the Modified BSD License.
define([
'base/js/namespace',
Jonathan Frederic
MWE,...
r17200 'jquery',
Jonathan Frederic
Progress...
r17196 'base/js/utils',
Jonathan Frederic
MWE,...
r17200 ], function(IPython, $, utils) {
Matthias BUSSONNIER
a few todo
r17421 // TODO: remove IPython dependency here
Matthias BUSSONNIER
"use strict" in most (if not all) our javascript...
r12103 "use strict";
Brian E. Granger
Splitting notebook.js into muliple files for development ease.
r4349
Jonathan Frederic
Progress...
r17196 // monkey patch CM to be able to syntax highlight cell magics
// bug reported upstream,
Matthias Bussonnier
codemirror rempo moved, update links in comments
r17940 // see https://github.com/codemirror/CodeMirror/issues/670
Jonathan Frederic
Progress...
r17196 if(CodeMirror.getMode(1,'text/plain').indent === undefined ){
CodeMirror.modes.null = function() {
return {token: function(stream) {stream.skipToEnd();},indent : function(){return 0;}};
};
}
CodeMirror.patchedGetMode = function(config, mode){
var cmmode = CodeMirror.getMode(config, mode);
if(cmmode.indent === null) {
console.log('patch mode "' , mode, '" on the fly');
cmmode.indent = function(){return 0;};
}
return cmmode;
};
// end monkey patching CodeMirror
Brian E. Granger
Splitting notebook.js into muliple files for development ease.
r4349
jon
In person review with @ellisonbg
r17210 var Cell = function (options) {
jon
Added some nice comments,...
r17211 // Constructor
//
// The Base `Cell` class from which to inherit.
//
// Parameters:
// options: dictionary
// Dictionary of keyword arguments.
// events: $(Events) instance
// config: dictionary
// keyboard_manager: KeyboardManager instance
jon
In person review with @ellisonbg
r17210 options = options || {};
this.keyboard_manager = options.keyboard_manager;
this.events = options.events;
MinRK
move mergeopt to utils...
r17764 var config = utils.mergeopt(Cell, options.config);
Matthias BUSSONNIER
Make CodeMirror configurable...
r9537 // superclass default overwrite our default
MinRK
give Raw Cells a placeholder...
r13668
jon
In person review with @ellisonbg
r17210 this.placeholder = config.placeholder || '';
this.read_only = config.cm_config.readOnly;
Brian E. Granger
Implemented module and namespace pattern in js notebook.
r4352 this.selected = false;
Brian E. Granger
Adding new logic to cells.
r14014 this.rendered = false;
this.mode = 'command';
MinRK
add empty metadata field on cells/worksheets...
r7523 this.metadata = {};
Matthias BUSSONNIER
autochange highlight with cell magics...
r8202 // load this from metadata later ?
Mikhail Korobov
Some bugs in js (mostly scoping bugs) are fixed
r8839 this.user_highlight = 'auto';
jon
In person review with @ellisonbg
r17210 this.cm_config = config.cm_config;
Jonathan Frederic
Almost done!...
r17198 this.cell_id = utils.uuid();
jon
In person review with @ellisonbg
r17210 this._options = config;
Matthias BUSSONNIER
some optimisation and code cleaning...
r13574
Jonathan Frederic
Added cell unfocus event canceller API
r15495 // For JS VM engines optimization, attributes should be all set (even
Matthias BUSSONNIER
some optimisation and code cleaning...
r13574 // to null) in the constructor, and if possible, if different subclass
// have new attributes with same name, they should be created in the
// same order. Easiest is to create and set to null in parent class.
this.element = null;
MinRK
set cell_type properly first-class in the Cell object...
r13676 this.cell_type = this.cell_type || null;
Matthias BUSSONNIER
some optimisation and code cleaning...
r13574 this.code_mirror = null;
Brian E. Granger
Implemented module and namespace pattern in js notebook.
r4352 this.create_element();
Brian E. Granger
Autoindentation fixed and enabled by default.
r4529 if (this.element !== null) {
Brian E. Granger
Implemented module and namespace pattern in js notebook.
r4352 this.element.data("cell", this);
this.bind_events();
Brian E. Granger
Fixing css class on cell related to selected, rendered, mode.
r14049 this.init_classes();
Brian E. Granger
Implemented module and namespace pattern in js notebook.
r4352 }
};
Matthias BUSSONNIER
JS Configurablity Take 2...
r10165 Cell.options_default = {
cm_config : {
Matthias BUSSONNIER
Make CodeMirror configurable...
r9537 indentUnit : 4,
Matthias BUSSONNIER
JS Configurablity Take 2...
r10165 readOnly: false,
Matthias BUSSONNIER
unify visual line handling
r16596 theme: "default",
extraKeys: {
"Cmd-Right":"goLineRight",
"End":"goLineRight",
"Cmd-Left":"goLineLeft"
}
Matthias BUSSONNIER
JS Configurablity Take 2...
r10165 }
Matthias BUSSONNIER
Make CodeMirror configurable...
r9537 };
MinRK
disable CodeMirror drag/drop on Safari...
r10791
// FIXME: Workaround CM Bug #332 (Safari segfault on drag)
// by disabling drag/drop altogether on Safari
Matthias Bussonnier
codemirror rempo moved, update links in comments
r17940 // https://github.com/codemirror/CodeMirror/issues/332
Jonathan Frederic
Almost done!...
r17198 if (utils.browser[0] == "Safari") {
MinRK
disable CodeMirror drag/drop on Safari...
r10791 Cell.options_default.cm_config.dragDrop = false;
}
Matthias BUSSONNIER
Make CodeMirror configurable...
r9537
Matthias BUSSONNIER
more docs in cell
r8713 /**
* Empty. Subclasses must implement create_element.
Matthias BUSSONNIER
add doc to base Cell
r8714 * This should contain all the code to create the DOM element in notebook
Matthias BUSSONNIER
more docs in cell
r8713 * and will be called by Base Class constructor.
* @method create_element
*/
Matthias BUSSONNIER
create celltoolbar in cell.js and inherit
r9073 Cell.prototype.create_element = function () {
};
Brian E. Granger
Implemented module and namespace pattern in js notebook.
r4352
Brian E. Granger
Fixing css class on cell related to selected, rendered, mode.
r14049 Cell.prototype.init_classes = function () {
// Call after this.element exists to initialize the css classes
// related to selected, rendered and mode.
if (this.selected) {
this.element.addClass('selected');
} else {
this.element.addClass('unselected');
}
if (this.rendered) {
this.element.addClass('rendered');
} else {
this.element.addClass('unrendered');
}
if (this.mode === 'edit') {
this.element.addClass('edit_mode');
} else {
this.element.addClass('command_mode');
}
Jonathan Frederic
Fixed lots of bugs...
r15493 };
Brian E. Granger
Fixing css class on cell related to selected, rendered, mode.
r14049
Matthias BUSSONNIER
more docs in cell
r8713 /**
Matthias BUSSONNIER
add doc to base Cell
r8714 * Subclasses can implement override bind_events.
* Be carefull to call the parent method when overwriting as it fires event.
* this will be triggerd after create_element in constructor.
Matthias BUSSONNIER
more docs in cell
r8713 * @method bind_events
*/
Brian E. Granger
Implemented module and namespace pattern in js notebook.
r4352 Cell.prototype.bind_events = function () {
var that = this;
Brian Granger
Major refactoring of the Notebook, Kernel and CodeCell JavaScript....
r7168 // We trigger events so that Cell doesn't have to depend on Notebook.
Brian E. Granger
Implemented module and namespace pattern in js notebook.
r4352 that.element.click(function (event) {
Brian E. Granger
Using a more specific approach for managing CM focus....
r14033 if (!that.selected) {
Jonathan Frederic
Progress...
r17196 that.events.trigger('select.Cell', {'cell':that});
Jonathan Frederic
Fixed lots of bugs...
r15493 }
Brian E. Granger
Implemented module and namespace pattern in js notebook.
r4352 });
that.element.focusin(function (event) {
Brian E. Granger
Using a more specific approach for managing CM focus....
r14033 if (!that.selected) {
Jonathan Frederic
Progress...
r17196 that.events.trigger('select.Cell', {'cell':that});
Jonathan Frederic
Fixed lots of bugs...
r15493 }
Brian E. Granger
Semi working version of basic dual mode UX....
r14016 });
MinRK
trigger dirty on CM change
r10785 if (this.code_mirror) {
this.code_mirror.on("change", function(cm, change) {
Jonathan Frederic
Progress...
r17196 that.events.trigger("set_dirty.Notebook", {value: true});
MinRK
trigger dirty on CM change
r10785 });
Brian E. Granger
Semicolon cleanup.
r14092 }
Brian E. Granger
More work on the dual mode UX.
r14015 if (this.code_mirror) {
this.code_mirror.on('focus', function(cm, change) {
Jonathan Frederic
Progress...
r17196 that.events.trigger('edit_mode.Cell', {cell: that});
Brian E. Granger
More work on the dual mode UX.
r14015 });
Brian E. Granger
Semicolon cleanup.
r14092 }
Brian E. Granger
Using a more specific approach for managing CM focus....
r14033 if (this.code_mirror) {
this.code_mirror.on('blur', function(cm, change) {
Jonathan Frederic
Progress...
r17196 that.events.trigger('command_mode.Cell', {cell: that});
Brian E. Granger
Using a more specific approach for managing CM focus....
r14033 });
Brian E. Granger
Semicolon cleanup.
r14092 }
Brian E. Granger
Implemented module and namespace pattern in js notebook.
r4352 };
Paul Ivanov
DRY: refactor common keyboard handling to Cell...
r15757
/**
* This method gets called in CodeMirror's onKeyDown/onKeyPress
* handlers and is used to provide custom key handling.
*
* To have custom handling, subclasses should override this method, but still call it
* in order to process the Edit mode keyboard shortcuts.
*
* @method handle_codemirror_keyevent
* @param {CodeMirror} editor - The codemirror instance bound to the cell
Paul Ivanov
minor fixes to docs to address PR feedback
r15820 * @param {event} event - key press event which either should or should not be handled by CodeMirror
Paul Ivanov
DRY: refactor common keyboard handling to Cell...
r15757 * @return {Boolean} `true` if CodeMirror should ignore the event, `false` Otherwise
*/
Cell.prototype.handle_codemirror_keyevent = function (editor, event) {
Jonathan Frederic
Progress...
r17196 var shortcuts = this.keyboard_manager.edit_shortcuts;
Paul Ivanov
DRY: refactor common keyboard handling to Cell...
r15757
Paul Ivanov
another doc fix
r15822 // if this is an edit_shortcuts shortcut, the global keyboard/shortcut
// manager will handle it
Paul Ivanov
renamed 'use_shortcut' method to 'handles'
r15823 if (shortcuts.handles(event)) { return true; }
Paul Ivanov
added new use_shortcut method to shortcuts...
r15758
Paul Ivanov
DRY: refactor common keyboard handling to Cell...
r15757 return false;
};
Brian E. Granger
Implemented module and namespace pattern in js notebook.
r4352
Matthias BUSSONNIER
add doc to base Cell
r8714 /**
* Triger typsetting of math by mathjax on current cell element
* @method typeset
*/
Aron Ahmadia
Remove Equation References/Numbering, Fix Bugs...
r8660 Cell.prototype.typeset = function () {
Brian E. Granger
Adding new logic to cells.
r14014 if (window.MathJax) {
Aron Ahmadia
Finalizing fixes to MathJax enhancements...
r8662 var cell_math = this.element.get(0);
Matthias BUSSONNIER
JS Configurablity Take 2...
r10165 MathJax.Hub.Queue(["Typeset", MathJax.Hub, cell_math]);
Brian E. Granger
Semicolon cleanup.
r14092 }
Aron Ahmadia
Remove Equation References/Numbering, Fix Bugs...
r8660 };
Brian Granger
Fixing auto-indent issues in CodeMirror config....
r5959
Matthias BUSSONNIER
add doc to base Cell
r8714 /**
Brian E. Granger
Starting work on select/focus logic.
r14013 * handle cell level logic when a cell is selected
Matthias BUSSONNIER
add doc to base Cell
r8714 * @method select
Brian E. Granger
Adding new logic to cells.
r14014 * @return is the action being taken
Matthias BUSSONNIER
add doc to base Cell
r8714 */
Brian Granger
Work on the base Cell API....
r5943 Cell.prototype.select = function () {
Brian E. Granger
Adding new logic to cells.
r14014 if (!this.selected) {
this.element.addClass('selected');
this.element.removeClass('unselected');
this.selected = true;
return true;
} else {
return false;
Brian E. Granger
Semicolon cleanup.
r14092 }
Brian Granger
Work on the base Cell API....
r5943 };
Matthias BUSSONNIER
add doc to base Cell
r8714 /**
Brian E. Granger
Starting work on select/focus logic.
r14013 * handle cell level logic when a cell is unselected
Matthias BUSSONNIER
add doc to base Cell
r8714 * @method unselect
Brian E. Granger
Adding new logic to cells.
r14014 * @return is the action being taken
Matthias BUSSONNIER
add doc to base Cell
r8714 */
Brian Granger
Work on the base Cell API....
r5943 Cell.prototype.unselect = function () {
Brian E. Granger
Adding new logic to cells.
r14014 if (this.selected) {
this.element.addClass('unselected');
this.element.removeClass('selected');
this.selected = false;
return true;
} else {
return false;
Brian E. Granger
Semicolon cleanup.
r14092 }
Brian Granger
Work on the base Cell API....
r5943 };
Matthias BUSSONNIER
more docs, organize in namespace
r8739 /**
Brian E. Granger
Adding new logic to cells.
r14014 * handle cell level logic when a cell is rendered
* @method render
* @return is the action being taken
Brian E. Granger
Starting work on select/focus logic.
r14013 */
Brian E. Granger
Adding new logic to cells.
r14014 Cell.prototype.render = function () {
if (!this.rendered) {
this.element.addClass('rendered');
this.element.removeClass('unrendered');
this.rendered = true;
return true;
} else {
return false;
Brian E. Granger
Semicolon cleanup.
r14092 }
Brian E. Granger
Starting work on select/focus logic.
r14013 };
/**
Brian E. Granger
Adding new logic to cells.
r14014 * handle cell level logic when a cell is unrendered
* @method unrender
* @return is the action being taken
Brian E. Granger
Starting work on select/focus logic.
r14013 */
Brian E. Granger
Adding new logic to cells.
r14014 Cell.prototype.unrender = function () {
if (this.rendered) {
this.element.addClass('unrendered');
this.element.removeClass('rendered');
this.rendered = false;
return true;
} else {
return false;
Brian E. Granger
Semicolon cleanup.
r14092 }
Brian E. Granger
Starting work on select/focus logic.
r14013 };
/**
Paul Ivanov
another doc fix
r15822 * Delegates keyboard shortcut handling to either IPython keyboard
Paul Ivanov
DRY: factor out common handle_keyevent method...
r15755 * manager when in command mode, or CodeMirror when in edit mode
*
* @method handle_keyevent
* @param {CodeMirror} editor - The codemirror instance bound to the cell
Paul Ivanov
brief key event desription
r15838 * @param {event} - key event to be handled
Paul Ivanov
DRY: factor out common handle_keyevent method...
r15755 * @return {Boolean} `true` if CodeMirror should ignore the event, `false` Otherwise
*/
Paul Ivanov
typo, use keycodes object instead of magic numbers
r15756 Cell.prototype.handle_keyevent = function (editor, event) {
Paul Ivanov
DRY: factor out common handle_keyevent method...
r15755
// console.log('CM', this.mode, event.which, event.type)
if (this.mode === 'command') {
return true;
} else if (this.mode === 'edit') {
return this.handle_codemirror_keyevent(editor, event);
}
};
/**
Paul Ivanov
refactor to improve cell switching in edit mode...
r15754 * @method at_top
* @return {Boolean}
*/
Cell.prototype.at_top = function () {
Paul Ivanov
more semicolons
r15819 var cm = this.code_mirror;
Paul Ivanov
refactor to improve cell switching in edit mode...
r15754 var cursor = cm.getCursor();
Paul Ivanov
restore master behavior...
r15835 if (cursor.line === 0 && cursor.ch === 0) {
Paul Ivanov
refactor to improve cell switching in edit mode...
r15754 return true;
}
Paul Ivanov
go to the beginning of the line when entering cell
r15836 return false;
Paul Ivanov
refactor to improve cell switching in edit mode...
r15754 };
/**
* @method at_bottom
* @return {Boolean}
* */
Cell.prototype.at_bottom = function () {
Paul Ivanov
more semicolons
r15819 var cm = this.code_mirror;
Paul Ivanov
refactor to improve cell switching in edit mode...
r15754 var cursor = cm.getCursor();
Paul Ivanov
restore master behavior...
r15835 if (cursor.line === (cm.lineCount()-1) && cursor.ch === cm.getLine(cursor.line).length) {
Paul Ivanov
refactor to improve cell switching in edit mode...
r15754 return true;
}
Paul Ivanov
go to the beginning of the line when entering cell
r15836 return false;
Paul Ivanov
refactor to improve cell switching in edit mode...
r15754 };
Paul Ivanov
go to the beginning of the line when entering cell
r15836
Paul Ivanov
refactor to improve cell switching in edit mode...
r15754 /**
Brian E. Granger
Adding new logic to cells.
r14014 * enter the command mode for the cell
* @method command_mode
* @return is the action being taken
*/
Cell.prototype.command_mode = function () {
if (this.mode !== 'command') {
this.element.addClass('command_mode');
this.element.removeClass('edit_mode');
this.mode = 'command';
return true;
} else {
return false;
Brian E. Granger
Semicolon cleanup.
r14092 }
Brian E. Granger
Adding new logic to cells.
r14014 };
/**
* enter the edit mode for the cell
* @method command_mode
* @return is the action being taken
*/
Cell.prototype.edit_mode = function () {
if (this.mode !== 'edit') {
this.element.addClass('edit_mode');
this.element.removeClass('command_mode');
this.mode = 'edit';
return true;
} else {
return false;
Brian E. Granger
Semicolon cleanup.
r14092 }
Jonathan Frederic
Fixed lots of bugs...
r15493 };
Jonathan Frederic
Remove all should cancel blur logic.
r15777
Jonathan Frederic
Added cell unfocus event canceller API
r15495 /**
Brian E. Granger
Adding new logic to cells.
r14014 * Focus the cell in the DOM sense
* @method focus_cell
*/
Cell.prototype.focus_cell = function () {
this.element.focus();
Jonathan Frederic
Fixed lots of bugs...
r15493 };
Brian E. Granger
Adding new logic to cells.
r14014
/**
Jonathan Frederic
Post in person review...
r15497 * Focus the editor area so a user can type
*
* NOTE: If codemirror is focused via a mouse click event, you don't want to
* call this because it will cause a page jump.
* @method focus_editor
*/
Cell.prototype.focus_editor = function () {
Jonathan Frederic
Re-added removed refresh
r15540 this.refresh();
Jonathan Frederic
Post in person review...
r15497 this.code_mirror.focus();
};
/**
Matthias BUSSONNIER
more docs, organize in namespace
r8739 * Refresh codemirror instance
* @method refresh
*/
Brian Granger
Work on the base Cell API....
r5943 Cell.prototype.refresh = function () {
this.code_mirror.refresh();
};
Matthias BUSSONNIER
more docs, organize in namespace
r8739 /**
* should be overritten by subclass
Brian E. Granger
More work on the dual mode UX.
r14015 * @method get_text
*/
Cell.prototype.get_text = function () {
Brian Granger
Work on the base Cell API....
r5943 };
Matthias BUSSONNIER
more docs, organize in namespace
r8739 /**
* should be overritten by subclass
Brian E. Granger
More work on the dual mode UX.
r14015 * @method set_text
* @param {string} text
*/
Cell.prototype.set_text = function (text) {
Brian Granger
Work on the base Cell API....
r5943 };
Matthias BUSSONNIER
more docs, organize in namespace
r8739 /**
* should be overritten by subclass
* serialise cell to json.
* @method toJSON
**/
Brian Granger
Work on the base Cell API....
r5943 Cell.prototype.toJSON = function () {
MinRK
add empty metadata field on cells/worksheets...
r7523 var data = {};
data.metadata = this.metadata;
MinRK
set cell_type properly first-class in the Cell object...
r13676 data.cell_type = this.cell_type;
MinRK
add empty metadata field on cells/worksheets...
r7523 return data;
Brian Granger
Work on the base Cell API....
r5943 };
Matthias BUSSONNIER
more docs, organize in namespace
r8739 /**
* should be overritten by subclass
* @method fromJSON
**/
Brian Granger
Work on the base Cell API....
r5943 Cell.prototype.fromJSON = function (data) {
MinRK
add empty metadata field on cells/worksheets...
r7523 if (data.metadata !== undefined) {
this.metadata = data.metadata;
}
Matthias BUSSONNIER
rename metaui -> celltoolbar
r9064 this.celltoolbar.rebuild();
Brian Granger
Work on the base Cell API....
r5943 };
Matthias BUSSONNIER
more docs, organize in namespace
r8739 /**
MinRK
add Cell.is_mergeable method...
r12509 * can the cell be split into two cells
Matthias BUSSONNIER
more docs, organize in namespace
r8739 * @method is_splittable
**/
Brian Granger
Lots of small notebook improvements....
r5946 Cell.prototype.is_splittable = function () {
return true;
};
Matthias BUSSONNIER
more docs, organize in namespace
r8739 /**
MinRK
add Cell.is_mergeable method...
r12509 * can the cell be merged with other cells
* @method is_mergeable
**/
Cell.prototype.is_mergeable = function () {
return true;
};
/**
Matthias BUSSONNIER
more docs, organize in namespace
r8739 * @return {String} - the text before the cursor
* @method get_pre_cursor
**/
Brian Granger
Lots of small notebook improvements....
r5946 Cell.prototype.get_pre_cursor = function () {
var cursor = this.code_mirror.getCursor();
Matthias BUSSONNIER
JS Configurablity Take 2...
r10165 var text = this.code_mirror.getRange({line:0, ch:0}, cursor);
Brian Granger
Lots of small notebook improvements....
r5946 text = text.replace(/^\n+/, '').replace(/\n+$/, '');
return text;
Jonathan Frederic
Post in person review...
r15497 };
Brian Granger
Lots of small notebook improvements....
r5946
Matthias BUSSONNIER
more docs, organize in namespace
r8739 /**
* @return {String} - the text after the cursor
* @method get_post_cursor
**/
Brian Granger
Lots of small notebook improvements....
r5946 Cell.prototype.get_post_cursor = function () {
var cursor = this.code_mirror.getCursor();
var last_line_num = this.code_mirror.lineCount()-1;
var last_line_len = this.code_mirror.getLine(last_line_num).length;
Jonathan Frederic
Post in person review...
r15497 var end = {line:last_line_num, ch:last_line_len};
Brian Granger
Lots of small notebook improvements....
r5946 var text = this.code_mirror.getRange(cursor, end);
text = text.replace(/^\n+/, '').replace(/\n+$/, '');
return text;
};
Matthias BUSSONNIER
more docs, organize in namespace
r8739 /**
Matthias BUSSONNIER
Method to show hide linenumber of cell...
r9542 * Show/Hide CodeMirror LineNumber
Matthias BUSSONNIER
use show method in toggle method
r9690 * @method show_line_numbers
Matthias BUSSONNIER
Method to show hide linenumber of cell...
r9542 *
* @param value {Bool} show (true), or hide (false) the line number in CodeMirror
**/
Cell.prototype.show_line_numbers = function (value) {
this.code_mirror.setOption('lineNumbers', value);
this.code_mirror.refresh();
};
/**
Matthias BUSSONNIER
more docs, organize in namespace
r8739 * Toggle CodeMirror LineNumber
* @method toggle_line_numbers
**/
Brian Granger
Making keyboard shortcut for showing line numbers consistent.
r6059 Cell.prototype.toggle_line_numbers = function () {
Matthias BUSSONNIER
use show method in toggle method
r9690 var val = this.code_mirror.getOption('lineNumbers');
this.show_line_numbers(!val);
Brian Granger
Making keyboard shortcut for showing line numbers consistent.
r6059 };
Matthias BUSSONNIER
more docs, organize in namespace
r8739 /**
Matthias BUSSONNIER
doc fixes
r9548 * Force codemirror highlight mode
Matthias BUSSONNIER
more docs, organize in namespace
r8739 * @method force_highlight
* @param {object} - CodeMirror mode
**/
Matthias BUSSONNIER
autochange highlight with cell magics...
r8202 Cell.prototype.force_highlight = function(mode) {
this.user_highlight = mode;
this.auto_highlight();
};
Matthias BUSSONNIER
more docs, organize in namespace
r8739 /**
* Try to autodetect cell highlight mode, or use selected mode
* @methods _auto_highlight
* @private
* @param {String|object|undefined} - CodeMirror mode | 'auto'
**/
Matthias BUSSONNIER
autochange highlight with cell magics...
r8202 Cell.prototype._auto_highlight = function (modes) {
//Here we handle manually selected modes
Jonathan Frederic
Post in person review...
r15497 var mode;
if( this.user_highlight !== undefined && this.user_highlight != 'auto' )
Matthias BUSSONNIER
autochange highlight with cell magics...
r8202 {
Jonathan Frederic
Post in person review...
r15497 mode = this.user_highlight;
Matthias BUSSONNIER
autochange highlight with cell magics...
r8202 CodeMirror.autoLoadMode(this.code_mirror, mode);
this.code_mirror.setOption('mode', mode);
return;
}
Matthias BUSSONNIER
get rid of most slowdown at notebook loading....
r13573 var current_mode = this.code_mirror.getOption('mode', mode);
Matthias BUSSONNIER
autochange highlight with cell magics...
r8202 var first_line = this.code_mirror.getLine(0);
// loop on every pairs
Jonathan Frederic
Post in person review...
r15497 for(mode in modes) {
var regs = modes[mode].reg;
Matthias BUSSONNIER
autochange highlight with cell magics...
r8202 // only one key every time but regexp can't be keys...
Sylvain Corlay
Replacing a for-in loop by an index loop on an array, to avoid enumerating inherited properties
r14106 for(var i=0; i<regs.length; i++) {
Matthias BUSSONNIER
autochange highlight with cell magics...
r8202 // here we handle non magic_modes
Jonathan Frederic
Post in person review...
r15497 if(first_line.match(regs[i]) !== null) {
Matthias BUSSONNIER
get rid of most slowdown at notebook loading....
r13573 if(current_mode == mode){
return;
}
Jonathan Frederic
Post in person review...
r15497 if (mode.search('magic_') !== 0) {
Matthias BUSSONNIER
JS Configurablity Take 2...
r10165 this.code_mirror.setOption('mode', mode);
Matthias BUSSONNIER
autochange highlight with cell magics...
r8202 CodeMirror.autoLoadMode(this.code_mirror, mode);
return;
}
Jonathan Frederic
Post in person review...
r15497 var open = modes[mode].open || "%%";
var close = modes[mode].close || "%%end";
Matthias BUSSONNIER
autochange highlight with cell magics...
r8202 var mmode = mode;
mode = mmode.substr(6);
Matthias BUSSONNIER
get rid of most slowdown at notebook loading....
r13573 if(current_mode == mode){
return;
}
Matthias BUSSONNIER
autochange highlight with cell magics...
r8202 CodeMirror.autoLoadMode(this.code_mirror, mode);
// create on the fly a mode that swhitch between
// plain/text and smth else otherwise `%%` is
// source of some highlight issues.
// we use patchedGetMode to circumvent a bug in CM
CodeMirror.defineMode(mmode , function(config) {
return CodeMirror.multiplexingMode(
CodeMirror.patchedGetMode(config, 'text/plain'),
// always set someting on close
{open: open, close: close,
mode: CodeMirror.patchedGetMode(config, mode),
delimStyle: "delimit"
}
);
});
this.code_mirror.setOption('mode', mmode);
return;
}
}
}
Matthias BUSSONNIER
store default codemirror mode in only 1 place
r12233 // fallback on default
Jonathan Frederic
Post in person review...
r15497 var default_mode;
Matthias Bussonnier
catch if cm_config of other key undefined
r12237 try {
default_mode = this._options.cm_config.mode;
} catch(e) {
default_mode = 'text/plain';
}
Matthias BUSSONNIER
get rid of most slowdown at notebook loading....
r13573 if( current_mode === default_mode){
Jonathan Frederic
Post in person review...
r15497 return;
Matthias BUSSONNIER
get rid of most slowdown at notebook loading....
r13573 }
Matthias BUSSONNIER
autochange highlight with cell magics...
r8202 this.code_mirror.setOption('mode', default_mode);
};
Brian Granger
Making keyboard shortcut for showing line numbers consistent.
r6059
Matthias BUSSONNIER
a few todo
r17421 // Backwards compatibility.
Brian E. Granger
Implemented module and namespace pattern in js notebook.
r4352 IPython.Cell = Cell;
Jonathan Frederic
Return dicts instead of classes,...
r17201 return {'Cell': Cell};
Jonathan Frederic
Progress...
r17196 });