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