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