1
//----------------------------------------------------------------------------
2
// Copyright (C) 2008-2011 The IPython Development Team
4
// Distributed under the terms of the BSD License. The full license is in
5
// the file COPYING, distributed as part of this software.
6
//----------------------------------------------------------------------------
8
//============================================================================
10
//============================================================================
12
var IPython = (function (IPython) {
14
var utils = IPython.utils;
17
var Cell = function (notebook) {
18
this.placeholder = this.placeholder || '';
19
this.notebook = notebook;
20
this.read_only = false;
22
this.read_only = notebook.read_only;
24
this.selected = false;
26
this.create_element();
27
if (this.element !== null) {
28
this.element.data("cell", this);
31
this.cell_id = utils.uuid();
35
// Subclasses must implement create_element.
36
Cell.prototype.create_element = function () {};
39
Cell.prototype.bind_events = function () {
41
var nb = that.notebook;
42
that.element.click(function (event) {
43
if (that.selected === false) {
44
nb.select(nb.find_cell_index(that));
47
that.element.focusin(function (event) {
48
if (that.selected === false) {
49
nb.select(nb.find_cell_index(that));
55
// typeset with MathJax if MathJax is available
56
Cell.prototype.typeset = function () {
58
MathJax.Hub.Queue(["Typeset",MathJax.Hub]);
63
Cell.prototype.select = function () {
64
this.element.addClass('ui-widget-content ui-corner-all');
69
Cell.prototype.unselect = function () {
70
this.element.removeClass('ui-widget-content ui-corner-all');
71
this.selected = false;
75
Cell.prototype.get_text = function () {
79
Cell.prototype.set_text = function (text) {
83
Cell.prototype.refresh = function () {
84
this.code_mirror.refresh();
88
Cell.prototype.edit = function () {
92
Cell.prototype.render = function () {
96
Cell.prototype.toJSON = function () {
100
Cell.prototype.fromJSON = function (data) {
104
Cell.prototype.is_splittable = function () {
109
Cell.prototype.get_pre_cursor = function () {
110
var cursor = this.code_mirror.getCursor();
111
var text = this.code_mirror.getRange({line:0,ch:0}, cursor);
112
text = text.replace(/^\n+/, '').replace(/\n+$/, '');
117
Cell.prototype.get_post_cursor = function () {
118
var cursor = this.code_mirror.getCursor();
119
var last_line_num = this.code_mirror.lineCount()-1;
120
var last_line_len = this.code_mirror.getLine(last_line_num).length;
121
var end = {line:last_line_num, ch:last_line_len}
122
var text = this.code_mirror.getRange(cursor, end);
123
text = text.replace(/^\n+/, '').replace(/\n+$/, '');
128
Cell.prototype.grow = function(element) {
129
// Grow the cell by hand. This is used upon reloading from JSON, when the
130
// autogrow handler is not called.
131
var dom = element.get(0);
133
// modified split rule from
134
// http://stackoverflow.com/questions/2035910/how-to-get-the-number-of-lines-in-a-textarea/2036424#2036424
135
var lines = dom.value.split(/\r|\r\n|\n/);
136
lines_count = lines.length;
137
if (lines_count >= 1) {
138
dom.rows = lines_count;
145
Cell.prototype.toggle_line_numbers = function () {
146
if (this.code_mirror.getOption('lineNumbers') == false) {
147
this.code_mirror.setOption('lineNumbers', true);
149
this.code_mirror.setOption('lineNumbers', false);
151
this.code_mirror.refresh();