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