//---------------------------------------------------------------------------- // Copyright (C) 2008-2012 The IPython Development Team // // Distributed under the terms of the BSD License. The full license is in // the file COPYING, distributed as part of this software. //---------------------------------------------------------------------------- //============================================================================ // TextCell //============================================================================ /** A module that allow to create different type of Text Cell @module IPython @namespace IPython */ var IPython = (function (IPython) { // TextCell base class var key = IPython.utils.keycodes; /** * Construct a new TextCell, codemirror mode is by default 'htmlmixed', and cell type is 'text' * cell start as not redered. * * @class TextCell * @constructor TextCell * @extend Ipython.Cell * @param {object|undefined} [options] * @param [options.cm_config] {object} config to pass to CodeMirror, will extend/overwrite default config * @param [options.placeholder] {string} default string to use when souce in empty for rendering (only use in some TextCell subclass) */ var TextCell = function (options) { // in all TextCell/Cell subclasses // do not assign most of members here, just pass it down // in the options dict potentially overwriting what you wish. // they will be assigned in the base class. // we cannot put this as a class key as it has handle to "this". var cm_overwrite_options = { onKeyEvent: $.proxy(this.handle_codemirror_keyevent,this) }; options = this.mergeopt(TextCell,options,{cm_config:cm_overwrite_options}); IPython.Cell.apply(this, [options]); this.rendered = false; this.cell_type = this.cell_type || 'text'; }; TextCell.prototype = new IPython.Cell(); TextCell.options_default = { cm_config : { extraKeys: {"Tab": "indentMore","Shift-Tab" : "indentLess"}, mode: 'htmlmixed', lineWrapping : true, } }; /** * Create the DOM element of the TextCell * @method create_element * @private */ TextCell.prototype.create_element = function () { IPython.Cell.prototype.create_element.apply(this, arguments); var cell = $("
' + code + '
';
});
this.typeset()
this.rendered = true;
}
};
// RawCell
/**
* @class RawCell
* @constructor RawCell
* @extends Ipython.TextCell
*/
var RawCell = function (options) {
options = this.mergeopt(RawCell,options)
TextCell.apply(this, [options]);
this.cell_type = 'raw';
var that = this
this.element.focusout(
function() { that.auto_highlight(); }
);
};
RawCell.options_default = {
placeholder : "Type plain text and LaTeX: $\\alpha^2$"
};
RawCell.prototype = new TextCell();
/**
* Trigger autodetection of highlight scheme for current cell
* @method auto_highlight
*/
RawCell.prototype.auto_highlight = function () {
this._auto_highlight(IPython.config.raw_cell_highlight);
};
/** @method render **/
RawCell.prototype.render = function () {
this.rendered = true;
this.edit();
};
/** @method handle_codemirror_keyevent **/
RawCell.prototype.handle_codemirror_keyevent = function (editor, event) {
var that = this;
if (event.which === key.UPARROW && event.type === 'keydown') {
// If we are not at the top, let CM handle the up arrow and
// prevent the global keydown handler from handling it.
if (!that.at_top()) {
event.stop();
return false;
} else {
return true;
};
} else if (event.which === key.DOWNARROW && event.type === 'keydown') {
// If we are not at the bottom, let CM handle the down arrow and
// prevent the global keydown handler from handling it.
if (!that.at_bottom()) {
event.stop();
return false;
} else {
return true;
};
};
return false;
};
/** @method select **/
RawCell.prototype.select = function () {
IPython.Cell.prototype.select.apply(this);
this.code_mirror.refresh();
this.code_mirror.focus();
};
/** @method at_top **/
RawCell.prototype.at_top = function () {
var cursor = this.code_mirror.getCursor();
if (cursor.line === 0 && cursor.ch === 0) {
return true;
} else {
return false;
}
};
/** @method at_bottom **/
RawCell.prototype.at_bottom = function () {
var cursor = this.code_mirror.getCursor();
if (cursor.line === (this.code_mirror.lineCount()-1) && cursor.ch === this.code_mirror.getLine(cursor.line).length) {
return true;
} else {
return false;
}
};
/**
* @class HeadingCell
* @extends Ipython.TextCell
*/
/**
* @constructor HeadingCell
* @extends Ipython.TextCell
*/
var HeadingCell = function (options) {
options = this.mergeopt(HeadingCell,options)
TextCell.apply(this, [options]);
/**
* heading level of the cell, use getter and setter to access
* @property level
*/
this.level = 1;
this.cell_type = 'heading';
};
HeadingCell.options_default = {
placeholder: "Type Heading Here"
};
HeadingCell.prototype = new TextCell();
/** @method fromJSON */
HeadingCell.prototype.fromJSON = function (data) {
if (data.level != undefined){
this.level = data.level;
}
TextCell.prototype.fromJSON.apply(this, arguments);
};
/** @method toJSON */
HeadingCell.prototype.toJSON = function () {
var data = TextCell.prototype.toJSON.apply(this);
data.level = this.get_level();
return data;
};
/**
* Change heading level of cell, and re-render
* @method set_level
*/
HeadingCell.prototype.set_level = function (level) {
this.level = level;
if (this.rendered) {
this.rendered = false;
this.render();
};
};
/** The depth of header cell, based on html (h1 to h6)
* @method get_level
* @return {integer} level - for 1 to 6
*/
HeadingCell.prototype.get_level = function () {
return this.level;
};
HeadingCell.prototype.set_rendered = function (text) {
var r = this.element.find("div.text_cell_render");
r.empty();
var link = text.replace(/ /g, '_');
r.append(
$('