Show More
@@ -1,240 +1,252 | |||
|
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 | // Cell |
|
10 | 10 | //============================================================================ |
|
11 | 11 | /** |
|
12 | 12 | * @module Cell |
|
13 | 13 | * An extendable module that provide base functionnality to create cell for notebook. |
|
14 | 14 | */ |
|
15 | 15 | |
|
16 | 16 | var IPython = (function (IPython) { |
|
17 | 17 | |
|
18 | 18 | var utils = IPython.utils; |
|
19 | 19 | |
|
20 | 20 | /** |
|
21 |
* The Base `Cell` class from which to inherit |
|
|
21 | * The Base `Cell` class from which to inherit | |
|
22 | 22 | * @class Cell |
|
23 | 23 | */ |
|
24 | 24 | |
|
25 | 25 | /* |
|
26 | 26 | * @constructor |
|
27 | 27 | */ |
|
28 | 28 | var Cell = function () { |
|
29 | 29 | this.placeholder = this.placeholder || ''; |
|
30 | 30 | this.read_only = false; |
|
31 | 31 | this.selected = false; |
|
32 | 32 | this.element = null; |
|
33 | 33 | this.metadata = {}; |
|
34 | 34 | // load this from metadata later ? |
|
35 | 35 | this.user_highlight == 'auto'; |
|
36 | 36 | this.create_element(); |
|
37 | 37 | if (this.element !== null) { |
|
38 | 38 | this.element.data("cell", this); |
|
39 | 39 | this.bind_events(); |
|
40 | 40 | } |
|
41 | 41 | this.cell_id = utils.uuid(); |
|
42 | 42 | }; |
|
43 | 43 | |
|
44 | 44 | |
|
45 | 45 | /** |
|
46 | 46 | * Empty. Subclasses must implement create_element. |
|
47 |
* This should contain all the code to create the DOM element in notebook |
|
|
47 | * This should contain all the code to create the DOM element in notebook | |
|
48 | 48 | * and will be called by Base Class constructor. |
|
49 | 49 | * @method create_element |
|
50 | 50 | */ |
|
51 | 51 | Cell.prototype.create_element = function () {}; |
|
52 | 52 | |
|
53 | 53 | |
|
54 | 54 | /** |
|
55 |
* |
|
|
56 | * This should contain all the code to create the DOM element in notebook | |
|
57 | * and will be called by Base Class constructor. | |
|
55 | * Subclasses can implement override bind_events. | |
|
56 | * Be carefull to call the parent method when overwriting as it fires event. | |
|
57 | * this will be triggerd after create_element in constructor. | |
|
58 | 58 | * @method bind_events |
|
59 | 59 | */ |
|
60 | 60 | Cell.prototype.bind_events = function () { |
|
61 | 61 | var that = this; |
|
62 | 62 | // We trigger events so that Cell doesn't have to depend on Notebook. |
|
63 | 63 | that.element.click(function (event) { |
|
64 | 64 | if (that.selected === false) { |
|
65 | 65 | $([IPython.events]).trigger('select.Cell', {'cell':that}); |
|
66 | 66 | } |
|
67 | 67 | }); |
|
68 | 68 | that.element.focusin(function (event) { |
|
69 | 69 | if (that.selected === false) { |
|
70 | 70 | $([IPython.events]).trigger('select.Cell', {'cell':that}); |
|
71 | 71 | } |
|
72 | 72 | }); |
|
73 | 73 | }; |
|
74 | 74 | |
|
75 | /** | |
|
76 | * Triger typsetting of math by mathjax on current cell element | |
|
77 | * @method typeset | |
|
78 | */ | |
|
75 | 79 | Cell.prototype.typeset = function () { |
|
76 | 80 | if (window.MathJax){ |
|
77 | 81 | var cell_math = this.element.get(0); |
|
78 | 82 | MathJax.Hub.Queue(["Typeset",MathJax.Hub,cell_math]); |
|
79 | 83 | } |
|
80 | 84 | }; |
|
81 | 85 | |
|
86 | /** | |
|
87 | * should be triggerd when cell is selected | |
|
88 | * @method select | |
|
89 | */ | |
|
82 | 90 | Cell.prototype.select = function () { |
|
83 | 91 | this.element.addClass('ui-widget-content ui-corner-all'); |
|
84 | 92 | this.selected = true; |
|
85 | 93 | }; |
|
86 | 94 | |
|
87 | 95 | |
|
96 | /** | |
|
97 | * should be triggerd when cell is unselected | |
|
98 | * @method unselect | |
|
99 | */ | |
|
88 | 100 | Cell.prototype.unselect = function () { |
|
89 | 101 | this.element.removeClass('ui-widget-content ui-corner-all'); |
|
90 | 102 | this.selected = false; |
|
91 | 103 | }; |
|
92 | 104 | |
|
93 | 105 | |
|
94 | 106 | Cell.prototype.get_text = function () { |
|
95 | 107 | }; |
|
96 | 108 | |
|
97 | 109 | |
|
98 | 110 | Cell.prototype.set_text = function (text) { |
|
99 | 111 | }; |
|
100 | 112 | |
|
101 | 113 | |
|
102 | 114 | Cell.prototype.refresh = function () { |
|
103 | 115 | this.code_mirror.refresh(); |
|
104 | 116 | }; |
|
105 | 117 | |
|
106 | 118 | |
|
107 | 119 | Cell.prototype.edit = function () { |
|
108 | 120 | }; |
|
109 | 121 | |
|
110 | 122 | |
|
111 | 123 | Cell.prototype.render = function () { |
|
112 | 124 | }; |
|
113 | 125 | |
|
114 | 126 | |
|
115 | 127 | Cell.prototype.toJSON = function () { |
|
116 | 128 | var data = {}; |
|
117 | 129 | data.metadata = this.metadata; |
|
118 | 130 | return data; |
|
119 | 131 | }; |
|
120 | 132 | |
|
121 | 133 | |
|
122 | 134 | Cell.prototype.fromJSON = function (data) { |
|
123 | 135 | if (data.metadata !== undefined) { |
|
124 | 136 | this.metadata = data.metadata; |
|
125 | 137 | } |
|
126 | 138 | }; |
|
127 | 139 | |
|
128 | 140 | |
|
129 | 141 | Cell.prototype.is_splittable = function () { |
|
130 | 142 | return true; |
|
131 | 143 | }; |
|
132 | 144 | |
|
133 | 145 | |
|
134 | 146 | Cell.prototype.get_pre_cursor = function () { |
|
135 | 147 | var cursor = this.code_mirror.getCursor(); |
|
136 | 148 | var text = this.code_mirror.getRange({line:0,ch:0}, cursor); |
|
137 | 149 | text = text.replace(/^\n+/, '').replace(/\n+$/, ''); |
|
138 | 150 | return text; |
|
139 | 151 | } |
|
140 | 152 | |
|
141 | 153 | |
|
142 | 154 | Cell.prototype.get_post_cursor = function () { |
|
143 | 155 | var cursor = this.code_mirror.getCursor(); |
|
144 | 156 | var last_line_num = this.code_mirror.lineCount()-1; |
|
145 | 157 | var last_line_len = this.code_mirror.getLine(last_line_num).length; |
|
146 | 158 | var end = {line:last_line_num, ch:last_line_len} |
|
147 | 159 | var text = this.code_mirror.getRange(cursor, end); |
|
148 | 160 | text = text.replace(/^\n+/, '').replace(/\n+$/, ''); |
|
149 | 161 | return text; |
|
150 | 162 | }; |
|
151 | 163 | |
|
152 | 164 | |
|
153 | 165 | Cell.prototype.grow = function(element) { |
|
154 | 166 | // Grow the cell by hand. This is used upon reloading from JSON, when the |
|
155 | 167 | // autogrow handler is not called. |
|
156 | 168 | var dom = element.get(0); |
|
157 | 169 | var lines_count = 0; |
|
158 | 170 | // modified split rule from |
|
159 | 171 | // http://stackoverflow.com/questions/2035910/how-to-get-the-number-of-lines-in-a-textarea/2036424#2036424 |
|
160 | 172 | var lines = dom.value.split(/\r|\r\n|\n/); |
|
161 | 173 | lines_count = lines.length; |
|
162 | 174 | if (lines_count >= 1) { |
|
163 | 175 | dom.rows = lines_count; |
|
164 | 176 | } else { |
|
165 | 177 | dom.rows = 1; |
|
166 | 178 | } |
|
167 | 179 | }; |
|
168 | 180 | |
|
169 | 181 | |
|
170 | 182 | Cell.prototype.toggle_line_numbers = function () { |
|
171 | 183 | if (this.code_mirror.getOption('lineNumbers') == false) { |
|
172 | 184 | this.code_mirror.setOption('lineNumbers', true); |
|
173 | 185 | } else { |
|
174 | 186 | this.code_mirror.setOption('lineNumbers', false); |
|
175 | 187 | } |
|
176 | 188 | this.code_mirror.refresh(); |
|
177 | 189 | }; |
|
178 | 190 | |
|
179 | 191 | Cell.prototype.force_highlight = function(mode) { |
|
180 | 192 | this.user_highlight = mode; |
|
181 | 193 | this.auto_highlight(); |
|
182 | 194 | }; |
|
183 | 195 | |
|
184 | 196 | Cell.prototype._auto_highlight = function (modes) { |
|
185 | 197 | //Here we handle manually selected modes |
|
186 | 198 | if( this.user_highlight != undefined && this.user_highlight != 'auto' ) |
|
187 | 199 | { |
|
188 | 200 | var mode = this.user_highlight; |
|
189 | 201 | CodeMirror.autoLoadMode(this.code_mirror, mode); |
|
190 | 202 | this.code_mirror.setOption('mode', mode); |
|
191 | 203 | return; |
|
192 | 204 | } |
|
193 | 205 | var first_line = this.code_mirror.getLine(0); |
|
194 | 206 | // loop on every pairs |
|
195 | 207 | for( var mode in modes) { |
|
196 | 208 | var regs = modes[mode]['reg']; |
|
197 | 209 | // only one key every time but regexp can't be keys... |
|
198 | 210 | for(var reg in regs ) { |
|
199 | 211 | // here we handle non magic_modes |
|
200 | 212 | if(first_line.match(regs[reg]) != null) { |
|
201 | 213 | if (mode.search('magic_') != 0) { |
|
202 | 214 | this.code_mirror.setOption('mode',mode); |
|
203 | 215 | CodeMirror.autoLoadMode(this.code_mirror, mode); |
|
204 | 216 | return; |
|
205 | 217 | } |
|
206 | 218 | var open = modes[mode]['open']|| "%%"; |
|
207 | 219 | var close = modes[mode]['close']|| "%%end"; |
|
208 | 220 | var mmode = mode; |
|
209 | 221 | mode = mmode.substr(6); |
|
210 | 222 | CodeMirror.autoLoadMode(this.code_mirror, mode); |
|
211 | 223 | // create on the fly a mode that swhitch between |
|
212 | 224 | // plain/text and smth else otherwise `%%` is |
|
213 | 225 | // source of some highlight issues. |
|
214 | 226 | // we use patchedGetMode to circumvent a bug in CM |
|
215 | 227 | CodeMirror.defineMode(mmode , function(config) { |
|
216 | 228 | return CodeMirror.multiplexingMode( |
|
217 | 229 | CodeMirror.patchedGetMode(config, 'text/plain'), |
|
218 | 230 | // always set someting on close |
|
219 | 231 | {open: open, close: close, |
|
220 | 232 | mode: CodeMirror.patchedGetMode(config, mode), |
|
221 | 233 | delimStyle: "delimit" |
|
222 | 234 | } |
|
223 | 235 | ); |
|
224 | 236 | }); |
|
225 | 237 | this.code_mirror.setOption('mode', mmode); |
|
226 | 238 | return; |
|
227 | 239 | } |
|
228 | 240 | } |
|
229 | 241 | } |
|
230 | 242 | // fallback on default (python) |
|
231 | 243 | var default_mode = this.default_mode || 'text/plain'; |
|
232 | 244 | this.code_mirror.setOption('mode', default_mode); |
|
233 | 245 | }; |
|
234 | 246 | |
|
235 | 247 | IPython.Cell = Cell; |
|
236 | 248 | |
|
237 | 249 | return IPython; |
|
238 | 250 | |
|
239 | 251 | }(IPython)); |
|
240 | 252 |
@@ -1,528 +1,529 | |||
|
1 | 1 | //---------------------------------------------------------------------------- |
|
2 | 2 | // Copyright (C) 2008-2012 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 | /** |
|
13 | 13 | A module that allow to create different type of Text Cell |
|
14 | 14 | */ |
|
15 | 15 | var IPython = (function (IPython) { |
|
16 | 16 | |
|
17 | 17 | // TextCell base class |
|
18 | 18 | var key = IPython.utils.keycodes; |
|
19 | 19 | |
|
20 | 20 | /** |
|
21 | 21 | * Construct a new TextCell, codemirror mode is by default 'htmlmixed', and cell type is 'text' |
|
22 | 22 | * cell start as not redered. |
|
23 | 23 | * |
|
24 | 24 | * @class TextCell |
|
25 | 25 | * @constructor TextCell |
|
26 | * @extend Cell | |
|
26 | 27 | */ |
|
27 | 28 | var TextCell = function () { |
|
28 | 29 | this.code_mirror_mode = this.code_mirror_mode || 'htmlmixed'; |
|
29 | 30 | IPython.Cell.apply(this, arguments); |
|
30 | 31 | this.rendered = false; |
|
31 | 32 | this.cell_type = this.cell_type || 'text'; |
|
32 | 33 | }; |
|
33 | 34 | |
|
34 | 35 | TextCell.prototype = new IPython.Cell(); |
|
35 | 36 | |
|
36 | 37 | /** |
|
37 | 38 | * Create the DOM element of the TextCell |
|
38 | 39 | * @method create_element |
|
39 | 40 | * @private |
|
40 | 41 | */ |
|
41 | 42 | TextCell.prototype.create_element = function () { |
|
42 | 43 | var cell = $("<div>").addClass('cell text_cell border-box-sizing'); |
|
43 | 44 | cell.attr('tabindex','2'); |
|
44 | 45 | var input_area = $('<div/>').addClass('text_cell_input border-box-sizing'); |
|
45 | 46 | this.code_mirror = CodeMirror(input_area.get(0), { |
|
46 | 47 | indentUnit : 4, |
|
47 | 48 | mode: this.code_mirror_mode, |
|
48 | 49 | theme: 'default', |
|
49 | 50 | value: this.placeholder, |
|
50 | 51 | readOnly: this.read_only, |
|
51 | 52 | lineWrapping : true, |
|
52 | 53 | extraKeys: {"Tab": "indentMore","Shift-Tab" : "indentLess"}, |
|
53 | 54 | onKeyEvent: $.proxy(this.handle_codemirror_keyevent,this) |
|
54 | 55 | }); |
|
55 | 56 | // The tabindex=-1 makes this div focusable. |
|
56 | 57 | var render_area = $('<div/>').addClass('text_cell_render border-box-sizing'). |
|
57 | 58 | addClass('rendered_html').attr('tabindex','-1'); |
|
58 | 59 | cell.append(input_area).append(render_area); |
|
59 | 60 | this.element = cell; |
|
60 | 61 | }; |
|
61 | 62 | |
|
62 | 63 | |
|
63 | 64 | /** |
|
64 | 65 | * Bind the DOM evet to cell actions |
|
65 | 66 | * Need to be called after TextCell.create_element |
|
66 | 67 | * @private |
|
67 | 68 | * @method bind_event |
|
68 | 69 | */ |
|
69 | 70 | TextCell.prototype.bind_events = function () { |
|
70 | 71 | IPython.Cell.prototype.bind_events.apply(this); |
|
71 | 72 | var that = this; |
|
72 | 73 | this.element.keydown(function (event) { |
|
73 | 74 | if (event.which === 13 && !event.shiftKey) { |
|
74 | 75 | if (that.rendered) { |
|
75 | 76 | that.edit(); |
|
76 | 77 | return false; |
|
77 | 78 | }; |
|
78 | 79 | }; |
|
79 | 80 | }); |
|
80 | 81 | this.element.dblclick(function () { |
|
81 | 82 | that.edit(); |
|
82 | 83 | }); |
|
83 | 84 | }; |
|
84 | 85 | |
|
85 | 86 | /** |
|
86 | 87 | * This method gets called in CodeMirror's onKeyDown/onKeyPress |
|
87 | 88 | * handlers and is used to provide custom key handling. |
|
88 | 89 | * |
|
89 | 90 | * Subclass should override this method to have custom handeling |
|
90 | 91 | * |
|
91 | 92 | * @method handle_codemirror_keyevent |
|
92 | 93 | * @param {CodeMirror} editor - The codemirror instance bound to the cell |
|
93 | 94 | * @param {event} event - |
|
94 | 95 | * @return {Boolean} `true` if CodeMirror should ignore the event, `false` Otherwise |
|
95 | 96 | */ |
|
96 | 97 | TextCell.prototype.handle_codemirror_keyevent = function (editor, event) { |
|
97 | 98 | |
|
98 | 99 | if (event.keyCode === 13 && (event.shiftKey || event.ctrlKey)) { |
|
99 | 100 | // Always ignore shift-enter in CodeMirror as we handle it. |
|
100 | 101 | return true; |
|
101 | 102 | } |
|
102 | 103 | return false; |
|
103 | 104 | }; |
|
104 | 105 | |
|
105 | 106 | /** |
|
106 | 107 | * Select the current cell and trigger 'focus' |
|
107 | 108 | * @method select |
|
108 | 109 | */ |
|
109 | 110 | TextCell.prototype.select = function () { |
|
110 | 111 | IPython.Cell.prototype.select.apply(this); |
|
111 | 112 | var output = this.element.find("div.text_cell_render"); |
|
112 | 113 | output.trigger('focus'); |
|
113 | 114 | }; |
|
114 | 115 | |
|
115 | 116 | /** |
|
116 | 117 | * unselect the current cell and `render` it |
|
117 | 118 | * @method unselect |
|
118 | 119 | */ |
|
119 | 120 | TextCell.prototype.unselect = function() { |
|
120 | 121 | // render on selection of another cell |
|
121 | 122 | this.render(); |
|
122 | 123 | IPython.Cell.prototype.unselect.apply(this); |
|
123 | 124 | }; |
|
124 | 125 | |
|
125 | 126 | /** |
|
126 | 127 | * |
|
127 | 128 | * put the current cell in edition mode |
|
128 | 129 | * @method edit |
|
129 | 130 | */ |
|
130 | 131 | TextCell.prototype.edit = function () { |
|
131 | 132 | if ( this.read_only ) return; |
|
132 | 133 | if (this.rendered === true) { |
|
133 | 134 | var text_cell = this.element; |
|
134 | 135 | var output = text_cell.find("div.text_cell_render"); |
|
135 | 136 | output.hide(); |
|
136 | 137 | text_cell.find('div.text_cell_input').show(); |
|
137 | 138 | this.code_mirror.refresh(); |
|
138 | 139 | this.code_mirror.focus(); |
|
139 | 140 | // We used to need an additional refresh() after the focus, but |
|
140 | 141 | // it appears that this has been fixed in CM. This bug would show |
|
141 | 142 | // up on FF when a newly loaded markdown cell was edited. |
|
142 | 143 | this.rendered = false; |
|
143 | 144 | if (this.get_text() === this.placeholder) { |
|
144 | 145 | this.set_text(''); |
|
145 | 146 | this.refresh(); |
|
146 | 147 | } |
|
147 | 148 | } |
|
148 | 149 | }; |
|
149 | 150 | |
|
150 | 151 | |
|
151 | 152 | /** |
|
152 | 153 | * Empty, Subclasses must define render. |
|
153 | 154 | * @method render |
|
154 | 155 | */ |
|
155 | 156 | TextCell.prototype.render = function () {}; |
|
156 | 157 | |
|
157 | 158 | |
|
158 | 159 | /** |
|
159 | 160 | * setter: {{#crossLink "TextCell/set_text"}}{{/crossLink}} |
|
160 | 161 | * @method get_text |
|
161 | 162 | * @retrun {string} CodeMirror current text value |
|
162 | 163 | */ |
|
163 | 164 | TextCell.prototype.get_text = function() { |
|
164 | 165 | return this.code_mirror.getValue(); |
|
165 | 166 | }; |
|
166 | 167 | |
|
167 | 168 | /** |
|
168 | 169 | * @param {string} text - Codemiror text value |
|
169 | 170 | * @see TextCell#get_text |
|
170 | 171 | * @method set_text |
|
171 | 172 | * */ |
|
172 | 173 | TextCell.prototype.set_text = function(text) { |
|
173 | 174 | this.code_mirror.setValue(text); |
|
174 | 175 | this.code_mirror.refresh(); |
|
175 | 176 | }; |
|
176 | 177 | |
|
177 | 178 | /** |
|
178 | 179 | * setter :{{#crossLink "TextCell/set_rendered"}}{{/crossLink}} |
|
179 | 180 | * @method get_rendered |
|
180 | 181 | * @return {html} html of rendered element |
|
181 | 182 | * */ |
|
182 | 183 | TextCell.prototype.get_rendered = function() { |
|
183 | 184 | return this.element.find('div.text_cell_render').html(); |
|
184 | 185 | }; |
|
185 | 186 | |
|
186 | 187 | /** |
|
187 | 188 | * @method set_rendered |
|
188 | 189 | */ |
|
189 | 190 | TextCell.prototype.set_rendered = function(text) { |
|
190 | 191 | this.element.find('div.text_cell_render').html(text); |
|
191 | 192 | }; |
|
192 | 193 | |
|
193 | 194 | /** |
|
194 | 195 | * not deprecated, but implementation wrong |
|
195 | 196 | * @method at_top |
|
196 | 197 | * @deprecated |
|
197 | 198 | * @return {Boolean} true is cell rendered, false otherwise |
|
198 | 199 | * I doubt this is what it is supposed to do |
|
199 | 200 | * this implementation is completly false |
|
200 | 201 | */ |
|
201 | 202 | TextCell.prototype.at_top = function () { |
|
202 | 203 | if (this.rendered) { |
|
203 | 204 | return true; |
|
204 | 205 | } else { |
|
205 | 206 | return false; |
|
206 | 207 | } |
|
207 | 208 | }; |
|
208 | 209 | |
|
209 | 210 | |
|
210 | 211 | /** |
|
211 | 212 | * not deprecated, but implementation wrong |
|
212 | 213 | * @method at_bottom |
|
213 | 214 | * @deprecated |
|
214 | 215 | * @return {Boolean} true is cell rendered, false otherwise |
|
215 | 216 | * I doubt this is what it is supposed to do |
|
216 | 217 | * this implementation is completly false |
|
217 | 218 | * */ |
|
218 | 219 | TextCell.prototype.at_bottom = function () { |
|
219 | 220 | if (this.rendered) { |
|
220 | 221 | return true; |
|
221 | 222 | } else { |
|
222 | 223 | return false; |
|
223 | 224 | } |
|
224 | 225 | }; |
|
225 | 226 | |
|
226 | 227 | /** |
|
227 | 228 | * Create Text cell from JSON |
|
228 | 229 | * @param {json} data - JSON serialized text-cell |
|
229 | 230 | * @method fromJSON |
|
230 | 231 | */ |
|
231 | 232 | TextCell.prototype.fromJSON = function (data) { |
|
232 | 233 | IPython.Cell.prototype.fromJSON.apply(this, arguments); |
|
233 | 234 | if (data.cell_type === this.cell_type) { |
|
234 | 235 | if (data.source !== undefined) { |
|
235 | 236 | this.set_text(data.source); |
|
236 | 237 | // make this value the starting point, so that we can only undo |
|
237 | 238 | // to this state, instead of a blank cell |
|
238 | 239 | this.code_mirror.clearHistory(); |
|
239 | 240 | this.set_rendered(data.rendered || ''); |
|
240 | 241 | this.rendered = false; |
|
241 | 242 | this.render(); |
|
242 | 243 | } |
|
243 | 244 | } |
|
244 | 245 | }; |
|
245 | 246 | |
|
246 | 247 | /** Generate JSON from cell |
|
247 | 248 | * @return {object} cell data serialised to json |
|
248 | 249 | */ |
|
249 | 250 | TextCell.prototype.toJSON = function () { |
|
250 | 251 | var data = IPython.Cell.prototype.toJSON.apply(this); |
|
251 | 252 | data.cell_type = this.cell_type; |
|
252 | 253 | data.source = this.get_text(); |
|
253 | 254 | return data; |
|
254 | 255 | }; |
|
255 | 256 | |
|
256 | 257 | |
|
257 | 258 | /** |
|
258 | 259 | * @constructor HtmlCell |
|
259 | 260 | * @class HtmlCell |
|
260 | 261 | * @extends TextCell |
|
261 | 262 | */ |
|
262 | 263 | var HTMLCell = function () { |
|
263 | 264 | this.placeholder = "Type <strong>HTML</strong> and LaTeX: $\\alpha^2$"; |
|
264 | 265 | IPython.TextCell.apply(this, arguments); |
|
265 | 266 | this.cell_type = 'html'; |
|
266 | 267 | }; |
|
267 | 268 | |
|
268 | 269 | |
|
269 | 270 | HTMLCell.prototype = new TextCell(); |
|
270 | 271 | |
|
271 | 272 | /** |
|
272 | 273 | * @method render |
|
273 | 274 | */ |
|
274 | 275 | HTMLCell.prototype.render = function () { |
|
275 | 276 | if (this.rendered === false) { |
|
276 | 277 | var text = this.get_text(); |
|
277 | 278 | if (text === "") { text = this.placeholder; } |
|
278 | 279 | this.set_rendered(text); |
|
279 | 280 | this.typeset(); |
|
280 | 281 | this.element.find('div.text_cell_input').hide(); |
|
281 | 282 | this.element.find("div.text_cell_render").show(); |
|
282 | 283 | this.rendered = true; |
|
283 | 284 | } |
|
284 | 285 | }; |
|
285 | 286 | |
|
286 | 287 | |
|
287 | 288 | /** |
|
288 | 289 | * @class MarkdownCell |
|
289 | 290 | * @constructor MarkdownCell |
|
290 | 291 | * @extends HtmlCell |
|
291 | 292 | */ |
|
292 | 293 | var MarkdownCell = function () { |
|
293 | 294 | this.placeholder = "Type *Markdown* and LaTeX: $\\alpha^2$"; |
|
294 | 295 | IPython.TextCell.apply(this, arguments); |
|
295 | 296 | this.cell_type = 'markdown'; |
|
296 | 297 | }; |
|
297 | 298 | |
|
298 | 299 | |
|
299 | 300 | MarkdownCell.prototype = new TextCell(); |
|
300 | 301 | |
|
301 | 302 | /** |
|
302 | 303 | * @method render |
|
303 | 304 | */ |
|
304 | 305 | MarkdownCell.prototype.render = function () { |
|
305 | 306 | if (this.rendered === false) { |
|
306 | 307 | var text = this.get_text(); |
|
307 | 308 | if (text === "") { text = this.placeholder; } |
|
308 | 309 | text = IPython.mathjaxutils.remove_math(text) |
|
309 | 310 | var html = IPython.markdown_converter.makeHtml(text); |
|
310 | 311 | html = IPython.mathjaxutils.replace_math(html) |
|
311 | 312 | try { |
|
312 | 313 | this.set_rendered(html); |
|
313 | 314 | } catch (e) { |
|
314 | 315 | console.log("Error running Javascript in Markdown:"); |
|
315 | 316 | console.log(e); |
|
316 | 317 | this.set_rendered($("<div/>").addClass("js-error").html( |
|
317 | 318 | "Error rendering Markdown!<br/>" + e.toString()) |
|
318 | 319 | ); |
|
319 | 320 | } |
|
320 | 321 | this.element.find('div.text_cell_input').hide(); |
|
321 | 322 | this.element.find("div.text_cell_render").show(); |
|
322 | 323 | var code_snippets = this.element.find("pre > code"); |
|
323 | 324 | code_snippets.replaceWith(function () { |
|
324 | 325 | var code = $(this).html(); |
|
325 | 326 | /* Substitute br for newlines and for spaces |
|
326 | 327 | before highlighting, since prettify doesn't |
|
327 | 328 | preserve those on all browsers */ |
|
328 | 329 | code = code.replace(/(\r\n|\n|\r)/gm, "<br/>"); |
|
329 | 330 | code = code.replace(/ /gm, ' '); |
|
330 | 331 | code = prettyPrintOne(code); |
|
331 | 332 | |
|
332 | 333 | return '<code class="prettyprint">' + code + '</code>'; |
|
333 | 334 | }); |
|
334 | 335 | this.typeset() |
|
335 | 336 | this.rendered = true; |
|
336 | 337 | } |
|
337 | 338 | }; |
|
338 | 339 | |
|
339 | 340 | |
|
340 | 341 | // RawCell |
|
341 | 342 | |
|
342 | 343 | /** |
|
343 | 344 | * @class RawCell |
|
344 | 345 | * @constructor RawCell |
|
345 | 346 | * @extends TextCell |
|
346 | 347 | */ |
|
347 | 348 | var RawCell = function () { |
|
348 | 349 | this.placeholder = "Type plain text and LaTeX: $\\alpha^2$"; |
|
349 | 350 | this.code_mirror_mode = 'rst'; |
|
350 | 351 | IPython.TextCell.apply(this, arguments); |
|
351 | 352 | this.cell_type = 'raw'; |
|
352 | 353 | var that = this |
|
353 | 354 | |
|
354 | 355 | this.element.focusout( |
|
355 | 356 | function() { that.auto_highlight(); } |
|
356 | 357 | ); |
|
357 | 358 | }; |
|
358 | 359 | |
|
359 | 360 | |
|
360 | 361 | RawCell.prototype = new TextCell(); |
|
361 | 362 | |
|
362 | 363 | /** |
|
363 | 364 | * Trigger autodetection of highlight scheme for current cell |
|
364 | 365 | * @method auto_highlight |
|
365 | 366 | */ |
|
366 | 367 | RawCell.prototype.auto_highlight = function () { |
|
367 | 368 | this._auto_highlight(IPython.config.raw_cell_highlight); |
|
368 | 369 | }; |
|
369 | 370 | |
|
370 | 371 | /** @method render **/ |
|
371 | 372 | RawCell.prototype.render = function () { |
|
372 | 373 | this.rendered = true; |
|
373 | 374 | this.edit(); |
|
374 | 375 | }; |
|
375 | 376 | |
|
376 | 377 | |
|
377 | 378 | /** @method handle_codemirror_keyevent **/ |
|
378 | 379 | RawCell.prototype.handle_codemirror_keyevent = function (editor, event) { |
|
379 | 380 | |
|
380 | 381 | var that = this; |
|
381 | 382 | if (event.which === key.UPARROW && event.type === 'keydown') { |
|
382 | 383 | // If we are not at the top, let CM handle the up arrow and |
|
383 | 384 | // prevent the global keydown handler from handling it. |
|
384 | 385 | if (!that.at_top()) { |
|
385 | 386 | event.stop(); |
|
386 | 387 | return false; |
|
387 | 388 | } else { |
|
388 | 389 | return true; |
|
389 | 390 | }; |
|
390 | 391 | } else if (event.which === key.DOWNARROW && event.type === 'keydown') { |
|
391 | 392 | // If we are not at the bottom, let CM handle the down arrow and |
|
392 | 393 | // prevent the global keydown handler from handling it. |
|
393 | 394 | if (!that.at_bottom()) { |
|
394 | 395 | event.stop(); |
|
395 | 396 | return false; |
|
396 | 397 | } else { |
|
397 | 398 | return true; |
|
398 | 399 | }; |
|
399 | 400 | }; |
|
400 | 401 | return false; |
|
401 | 402 | }; |
|
402 | 403 | |
|
403 | 404 | /** @method select **/ |
|
404 | 405 | RawCell.prototype.select = function () { |
|
405 | 406 | IPython.Cell.prototype.select.apply(this); |
|
406 | 407 | this.code_mirror.refresh(); |
|
407 | 408 | this.code_mirror.focus(); |
|
408 | 409 | }; |
|
409 | 410 | |
|
410 | 411 | /** @method at_top **/ |
|
411 | 412 | RawCell.prototype.at_top = function () { |
|
412 | 413 | var cursor = this.code_mirror.getCursor(); |
|
413 | 414 | if (cursor.line === 0 && cursor.ch === 0) { |
|
414 | 415 | return true; |
|
415 | 416 | } else { |
|
416 | 417 | return false; |
|
417 | 418 | } |
|
418 | 419 | }; |
|
419 | 420 | |
|
420 | 421 | |
|
421 | 422 | /** @method at_bottom **/ |
|
422 | 423 | RawCell.prototype.at_bottom = function () { |
|
423 | 424 | var cursor = this.code_mirror.getCursor(); |
|
424 | 425 | if (cursor.line === (this.code_mirror.lineCount()-1) && cursor.ch === this.code_mirror.getLine(cursor.line).length) { |
|
425 | 426 | return true; |
|
426 | 427 | } else { |
|
427 | 428 | return false; |
|
428 | 429 | } |
|
429 | 430 | }; |
|
430 | 431 | |
|
431 | 432 | |
|
432 |
/** |
|
|
433 | /** | |
|
433 | 434 | * @class HeadingCell |
|
434 | 435 | * @extends TextCell |
|
435 | 436 | */ |
|
436 | 437 | |
|
437 | 438 | /** |
|
438 | 439 | * @constructor HeadingCell |
|
439 | 440 | * @extends TextCell |
|
440 | 441 | */ |
|
441 | 442 | var HeadingCell = function () { |
|
442 | 443 | this.placeholder = "Type Heading Here"; |
|
443 | 444 | IPython.TextCell.apply(this, arguments); |
|
444 | 445 | /** |
|
445 | 446 | * heading level of the cell, use getter and setter to access |
|
446 | 447 | * @property level |
|
447 | 448 | */ |
|
448 | 449 | this.level = 1; |
|
449 | 450 | this.cell_type = 'heading'; |
|
450 | 451 | }; |
|
451 | 452 | |
|
452 | 453 | |
|
453 | 454 | HeadingCell.prototype = new TextCell(); |
|
454 | 455 | |
|
455 | 456 | /** @method fromJSON */ |
|
456 | 457 | HeadingCell.prototype.fromJSON = function (data) { |
|
457 | 458 | if (data.level != undefined){ |
|
458 | 459 | this.level = data.level; |
|
459 | 460 | } |
|
460 | 461 | IPython.TextCell.prototype.fromJSON.apply(this, arguments); |
|
461 | 462 | }; |
|
462 | 463 | |
|
463 | 464 | |
|
464 | 465 | /** @method toJSON */ |
|
465 | 466 | HeadingCell.prototype.toJSON = function () { |
|
466 | 467 | var data = IPython.TextCell.prototype.toJSON.apply(this); |
|
467 | 468 | data.level = this.get_level(); |
|
468 | 469 | return data; |
|
469 | 470 | }; |
|
470 | 471 | |
|
471 | 472 | |
|
472 | 473 | /** |
|
473 | 474 | * Change heading level of cell, and re-render |
|
474 | 475 | * @method set_level |
|
475 | 476 | */ |
|
476 | 477 | HeadingCell.prototype.set_level = function (level) { |
|
477 | 478 | this.level = level; |
|
478 | 479 | if (this.rendered) { |
|
479 | 480 | this.rendered = false; |
|
480 | 481 | this.render(); |
|
481 | 482 | }; |
|
482 | 483 | }; |
|
483 | 484 | |
|
484 | 485 | /** The depth of header cell, based on html (h1 to h6) |
|
485 | 486 | * @method get_level |
|
486 | 487 | * @return {integer} level - for 1 to 6 |
|
487 | 488 | */ |
|
488 | 489 | HeadingCell.prototype.get_level = function () { |
|
489 | 490 | return this.level; |
|
490 | 491 | }; |
|
491 | 492 | |
|
492 | 493 | |
|
493 | 494 | HeadingCell.prototype.set_rendered = function (text) { |
|
494 | 495 | var r = this.element.find("div.text_cell_render"); |
|
495 | 496 | r.empty(); |
|
496 | 497 | r.append($('<h'+this.level+'/>').html(text)); |
|
497 | 498 | }; |
|
498 | 499 | |
|
499 | 500 | |
|
500 | 501 | HeadingCell.prototype.get_rendered = function () { |
|
501 | 502 | var r = this.element.find("div.text_cell_render"); |
|
502 | 503 | return r.children().first().html(); |
|
503 | 504 | }; |
|
504 | 505 | |
|
505 | 506 | |
|
506 | 507 | HeadingCell.prototype.render = function () { |
|
507 | 508 | if (this.rendered === false) { |
|
508 | 509 | var text = this.get_text(); |
|
509 | 510 | if (text === "") { text = this.placeholder; } |
|
510 | 511 | this.set_rendered(text); |
|
511 | 512 | this.typeset(); |
|
512 | 513 | this.element.find('div.text_cell_input').hide(); |
|
513 | 514 | this.element.find("div.text_cell_render").show(); |
|
514 | 515 | this.rendered = true; |
|
515 | 516 | }; |
|
516 | 517 | }; |
|
517 | 518 | |
|
518 | 519 | IPython.TextCell = TextCell; |
|
519 | 520 | IPython.HTMLCell = HTMLCell; |
|
520 | 521 | IPython.MarkdownCell = MarkdownCell; |
|
521 | 522 | IPython.RawCell = RawCell; |
|
522 | 523 | IPython.HeadingCell = HeadingCell; |
|
523 | 524 | |
|
524 | 525 | |
|
525 | 526 | return IPython; |
|
526 | 527 | |
|
527 | 528 | }(IPython)); |
|
528 | 529 |
General Comments 0
You need to be logged in to leave comments.
Login now