##// END OF EJS Templates
edit text cells on double-click instead of single-click...
MinRK -
Show More
@@ -1,272 +1,272 b''
1 //----------------------------------------------------------------------------
1 //----------------------------------------------------------------------------
2 // Copyright (C) 2008-2011 The IPython Development Team
2 // Copyright (C) 2008-2011 The IPython Development Team
3 //
3 //
4 // Distributed under the terms of the BSD License. The full license is in
4 // Distributed under the terms of the BSD License. The full license is in
5 // the file COPYING, distributed as part of this software.
5 // the file COPYING, distributed as part of this software.
6 //----------------------------------------------------------------------------
6 //----------------------------------------------------------------------------
7
7
8 //============================================================================
8 //============================================================================
9 // TextCell
9 // TextCell
10 //============================================================================
10 //============================================================================
11
11
12 var IPython = (function (IPython) {
12 var IPython = (function (IPython) {
13
13
14 // TextCell base class
14 // TextCell base class
15
15
16 var TextCell = function (notebook) {
16 var TextCell = function (notebook) {
17 this.code_mirror_mode = this.code_mirror_mode || 'htmlmixed';
17 this.code_mirror_mode = this.code_mirror_mode || 'htmlmixed';
18 this.placeholder = this.placeholder || '\u0000';
18 this.placeholder = this.placeholder || '\u0000';
19 IPython.Cell.apply(this, arguments);
19 IPython.Cell.apply(this, arguments);
20 this.rendered = false;
20 this.rendered = false;
21 this.cell_type = this.cell_type || 'text';
21 this.cell_type = this.cell_type || 'text';
22 };
22 };
23
23
24
24
25 TextCell.prototype = new IPython.Cell();
25 TextCell.prototype = new IPython.Cell();
26
26
27
27
28 TextCell.prototype.create_element = function () {
28 TextCell.prototype.create_element = function () {
29 var cell = $("<div>").addClass('cell text_cell border-box-sizing');
29 var cell = $("<div>").addClass('cell text_cell border-box-sizing');
30 cell.attr('tabindex','2');
30 cell.attr('tabindex','2');
31 var input_area = $('<div/>').addClass('text_cell_input');
31 var input_area = $('<div/>').addClass('text_cell_input');
32 this.code_mirror = CodeMirror(input_area.get(0), {
32 this.code_mirror = CodeMirror(input_area.get(0), {
33 indentUnit : 4,
33 indentUnit : 4,
34 mode: this.code_mirror_mode,
34 mode: this.code_mirror_mode,
35 theme: 'default',
35 theme: 'default',
36 value: this.placeholder,
36 value: this.placeholder,
37 readOnly: this.read_only
37 readOnly: this.read_only
38 });
38 });
39 // The tabindex=-1 makes this div focusable.
39 // The tabindex=-1 makes this div focusable.
40 var render_area = $('<div/>').addClass('text_cell_render').
40 var render_area = $('<div/>').addClass('text_cell_render').
41 addClass('rendered_html').attr('tabindex','-1');
41 addClass('rendered_html').attr('tabindex','-1');
42 cell.append(input_area).append(render_area);
42 cell.append(input_area).append(render_area);
43 this.element = cell;
43 this.element = cell;
44 };
44 };
45
45
46
46
47 TextCell.prototype.bind_events = function () {
47 TextCell.prototype.bind_events = function () {
48 IPython.Cell.prototype.bind_events.apply(this);
48 IPython.Cell.prototype.bind_events.apply(this);
49 var that = this;
49 var that = this;
50 this.element.keydown(function (event) {
50 this.element.keydown(function (event) {
51 if (event.which === 13) {
51 if (event.which === 13) {
52 if (that.rendered) {
52 if (that.rendered) {
53 that.edit();
53 that.edit();
54 event.preventDefault();
54 event.preventDefault();
55 }
55 }
56 }
56 }
57 });
57 });
58 };
58 };
59
59
60
60
61 TextCell.prototype.select = function () {
61 TextCell.prototype.select = function () {
62 IPython.Cell.prototype.select.apply(this);
62 IPython.Cell.prototype.select.apply(this);
63 var output = this.element.find("div.text_cell_render");
63 var output = this.element.find("div.text_cell_render");
64 output.trigger('focus');
64 output.trigger('focus');
65 };
65 };
66
66
67
67
68 TextCell.prototype.edit = function () {
68 TextCell.prototype.edit = function () {
69 if ( this.read_only ) return;
69 if ( this.read_only ) return;
70 if (this.rendered === true) {
70 if (this.rendered === true) {
71 var text_cell = this.element;
71 var text_cell = this.element;
72 var output = text_cell.find("div.text_cell_render");
72 var output = text_cell.find("div.text_cell_render");
73 output.hide();
73 output.hide();
74 text_cell.find('div.text_cell_input').show();
74 text_cell.find('div.text_cell_input').show();
75 this.code_mirror.focus();
75 this.code_mirror.focus();
76 this.code_mirror.refresh();
76 this.code_mirror.refresh();
77 this.rendered = false;
77 this.rendered = false;
78 if (this.get_source() === this.placeholder) {
78 if (this.get_source() === this.placeholder) {
79 this.set_source('');
79 this.set_source('');
80 }
80 }
81 }
81 }
82 };
82 };
83
83
84
84
85 // Subclasses must define render.
85 // Subclasses must define render.
86 TextCell.prototype.render = function () {};
86 TextCell.prototype.render = function () {};
87
87
88
88
89 TextCell.prototype.config_mathjax = function () {
89 TextCell.prototype.config_mathjax = function () {
90 var text_cell = this.element;
90 var text_cell = this.element;
91 var that = this;
91 var that = this;
92 text_cell.click(function () {
92 text_cell.dblclick(function () {
93 that.edit();
93 that.edit();
94 }).focusout(function () {
94 }).focusout(function () {
95 that.render();
95 that.render();
96 });
96 });
97
97
98 text_cell.trigger("focusout");
98 text_cell.trigger("focusout");
99 };
99 };
100
100
101
101
102 TextCell.prototype.get_source = function() {
102 TextCell.prototype.get_source = function() {
103 return this.code_mirror.getValue();
103 return this.code_mirror.getValue();
104 };
104 };
105
105
106
106
107 TextCell.prototype.set_source = function(text) {
107 TextCell.prototype.set_source = function(text) {
108 this.code_mirror.setValue(text);
108 this.code_mirror.setValue(text);
109 this.code_mirror.refresh();
109 this.code_mirror.refresh();
110 };
110 };
111
111
112
112
113 TextCell.prototype.get_rendered = function() {
113 TextCell.prototype.get_rendered = function() {
114 return this.element.find('div.text_cell_render').html();
114 return this.element.find('div.text_cell_render').html();
115 };
115 };
116
116
117
117
118 TextCell.prototype.set_rendered = function(text) {
118 TextCell.prototype.set_rendered = function(text) {
119 this.element.find('div.text_cell_render').html(text);
119 this.element.find('div.text_cell_render').html(text);
120 };
120 };
121
121
122
122
123 TextCell.prototype.at_top = function () {
123 TextCell.prototype.at_top = function () {
124 if (this.rendered) {
124 if (this.rendered) {
125 return true;
125 return true;
126 } else {
126 } else {
127 return false;
127 return false;
128 }
128 }
129 };
129 };
130
130
131
131
132 TextCell.prototype.at_bottom = function () {
132 TextCell.prototype.at_bottom = function () {
133 if (this.rendered) {
133 if (this.rendered) {
134 return true;
134 return true;
135 } else {
135 } else {
136 return false;
136 return false;
137 }
137 }
138 };
138 };
139
139
140
140
141 TextCell.prototype.fromJSON = function (data) {
141 TextCell.prototype.fromJSON = function (data) {
142 if (data.cell_type === this.cell_type) {
142 if (data.cell_type === this.cell_type) {
143 if (data.source !== undefined) {
143 if (data.source !== undefined) {
144 this.set_source(data.source);
144 this.set_source(data.source);
145 this.set_rendered(data.rendered || '');
145 this.set_rendered(data.rendered || '');
146 this.rendered = false;
146 this.rendered = false;
147 this.render();
147 this.render();
148 }
148 }
149 }
149 }
150 };
150 };
151
151
152
152
153 TextCell.prototype.toJSON = function () {
153 TextCell.prototype.toJSON = function () {
154 var data = {};
154 var data = {};
155 data.cell_type = this.cell_type;
155 data.cell_type = this.cell_type;
156 data.source = this.get_source();
156 data.source = this.get_source();
157 return data;
157 return data;
158 };
158 };
159
159
160
160
161 // HTMLCell
161 // HTMLCell
162
162
163 var HTMLCell = function (notebook) {
163 var HTMLCell = function (notebook) {
164 this.placeholder = "\u0000Type <strong>HTML</strong> and LaTeX: $\\alpha^2$";
164 this.placeholder = "\u0000Type <strong>HTML</strong> and LaTeX: $\\alpha^2$";
165 IPython.TextCell.apply(this, arguments);
165 IPython.TextCell.apply(this, arguments);
166 this.cell_type = 'html';
166 this.cell_type = 'html';
167 };
167 };
168
168
169
169
170 HTMLCell.prototype = new TextCell();
170 HTMLCell.prototype = new TextCell();
171
171
172
172
173 HTMLCell.prototype.render = function () {
173 HTMLCell.prototype.render = function () {
174 if (this.rendered === false) {
174 if (this.rendered === false) {
175 var text = this.get_source();
175 var text = this.get_source();
176 if (text === "") { text = this.placeholder; }
176 if (text === "") { text = this.placeholder; }
177 this.set_rendered(text);
177 this.set_rendered(text);
178 this.typeset();
178 this.typeset();
179 this.element.find('div.text_cell_input').hide();
179 this.element.find('div.text_cell_input').hide();
180 this.element.find("div.text_cell_render").show();
180 this.element.find("div.text_cell_render").show();
181 this.rendered = true;
181 this.rendered = true;
182 }
182 }
183 };
183 };
184
184
185
185
186 // MarkdownCell
186 // MarkdownCell
187
187
188 var MarkdownCell = function (notebook) {
188 var MarkdownCell = function (notebook) {
189 this.placeholder = "\u0000Type *Markdown* and LaTeX: $\\alpha^2$";
189 this.placeholder = "\u0000Type *Markdown* and LaTeX: $\\alpha^2$";
190 IPython.TextCell.apply(this, arguments);
190 IPython.TextCell.apply(this, arguments);
191 this.cell_type = 'markdown';
191 this.cell_type = 'markdown';
192 };
192 };
193
193
194
194
195 MarkdownCell.prototype = new TextCell();
195 MarkdownCell.prototype = new TextCell();
196
196
197
197
198 MarkdownCell.prototype.render = function () {
198 MarkdownCell.prototype.render = function () {
199 if (this.rendered === false) {
199 if (this.rendered === false) {
200 var text = this.get_source();
200 var text = this.get_source();
201 if (text === "") { text = this.placeholder; }
201 if (text === "") { text = this.placeholder; }
202 var html = IPython.markdown_converter.makeHtml(text);
202 var html = IPython.markdown_converter.makeHtml(text);
203 this.set_rendered(html);
203 this.set_rendered(html);
204 this.typeset()
204 this.typeset()
205 this.element.find('div.text_cell_input').hide();
205 this.element.find('div.text_cell_input').hide();
206 this.element.find("div.text_cell_render").show();
206 this.element.find("div.text_cell_render").show();
207 var code_snippets = this.element.find("pre > code");
207 var code_snippets = this.element.find("pre > code");
208 code_snippets.replaceWith(function () {
208 code_snippets.replaceWith(function () {
209 var code = $(this).html();
209 var code = $(this).html();
210 /* Substitute br for newlines and &nbsp; for spaces
210 /* Substitute br for newlines and &nbsp; for spaces
211 before highlighting, since prettify doesn't
211 before highlighting, since prettify doesn't
212 preserve those on all browsers */
212 preserve those on all browsers */
213 code = code.replace(/(\r\n|\n|\r)/gm, "<br/>");
213 code = code.replace(/(\r\n|\n|\r)/gm, "<br/>");
214 code = code.replace(/ /gm, '&nbsp;');
214 code = code.replace(/ /gm, '&nbsp;');
215 code = prettyPrintOne(code);
215 code = prettyPrintOne(code);
216
216
217 return '<code class="prettyprint">' + code + '</code>';
217 return '<code class="prettyprint">' + code + '</code>';
218 });
218 });
219 this.rendered = true;
219 this.rendered = true;
220 }
220 }
221 };
221 };
222
222
223
223
224 // RSTCell
224 // RSTCell
225
225
226 var RSTCell = function (notebook) {
226 var RSTCell = function (notebook) {
227 this.placeholder = "\u0000Type *ReStructured Text* and LaTeX: $\\alpha^2$";
227 this.placeholder = "\u0000Type *ReStructured Text* and LaTeX: $\\alpha^2$";
228 IPython.TextCell.apply(this, arguments);
228 IPython.TextCell.apply(this, arguments);
229 this.cell_type = 'rst';
229 this.cell_type = 'rst';
230 };
230 };
231
231
232
232
233 RSTCell.prototype = new TextCell();
233 RSTCell.prototype = new TextCell();
234
234
235
235
236 RSTCell.prototype.render = function () {
236 RSTCell.prototype.render = function () {
237 if (this.rendered === false) {
237 if (this.rendered === false) {
238 var text = this.get_source();
238 var text = this.get_source();
239 if (text === "") { text = this.placeholder; }
239 if (text === "") { text = this.placeholder; }
240 var settings = {
240 var settings = {
241 processData : false,
241 processData : false,
242 cache : false,
242 cache : false,
243 type : "POST",
243 type : "POST",
244 data : text,
244 data : text,
245 headers : {'Content-Type': 'application/x-rst'},
245 headers : {'Content-Type': 'application/x-rst'},
246 success : $.proxy(this.handle_render,this)
246 success : $.proxy(this.handle_render,this)
247 };
247 };
248 $.ajax("/rstservice/render", settings);
248 $.ajax("/rstservice/render", settings);
249 this.element.find('div.text_cell_input').hide();
249 this.element.find('div.text_cell_input').hide();
250 this.element.find("div.text_cell_render").show();
250 this.element.find("div.text_cell_render").show();
251 this.set_rendered("Rendering...");
251 this.set_rendered("Rendering...");
252 }
252 }
253 };
253 };
254
254
255
255
256 RSTCell.prototype.handle_render = function (data, status, xhr) {
256 RSTCell.prototype.handle_render = function (data, status, xhr) {
257 this.set_rendered(data);
257 this.set_rendered(data);
258 this.typeset();
258 this.typeset();
259 this.rendered = true;
259 this.rendered = true;
260 };
260 };
261
261
262
262
263 IPython.TextCell = TextCell;
263 IPython.TextCell = TextCell;
264 IPython.HTMLCell = HTMLCell;
264 IPython.HTMLCell = HTMLCell;
265 IPython.MarkdownCell = MarkdownCell;
265 IPython.MarkdownCell = MarkdownCell;
266 IPython.RSTCell = RSTCell;
266 IPython.RSTCell = RSTCell;
267
267
268
268
269 return IPython;
269 return IPython;
270
270
271 }(IPython));
271 }(IPython));
272
272
General Comments 0
You need to be logged in to leave comments. Login now