Show More
@@ -1,218 +1,240 | |||
|
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 | /** | |
|
12 | * @module Cell | |
|
13 | * An extendable module that provide base functionnality to create cell for notebook. | |
|
14 | */ | |
|
11 | 15 | |
|
12 | 16 | var IPython = (function (IPython) { |
|
13 | 17 | |
|
14 | 18 | var utils = IPython.utils; |
|
15 | 19 | |
|
20 | /** | |
|
21 | * The Base `Cell` class from which to inherit | |
|
22 | * @class Cell | |
|
23 | */ | |
|
16 | 24 | |
|
25 | /* | |
|
26 | * @constructor | |
|
27 | */ | |
|
17 | 28 | var Cell = function () { |
|
18 | 29 | this.placeholder = this.placeholder || ''; |
|
19 | 30 | this.read_only = false; |
|
20 | 31 | this.selected = false; |
|
21 | 32 | this.element = null; |
|
22 | 33 | this.metadata = {}; |
|
23 | 34 | // load this from metadata later ? |
|
24 | 35 | this.user_highlight == 'auto'; |
|
25 | 36 | this.create_element(); |
|
26 | 37 | if (this.element !== null) { |
|
27 | 38 | this.element.data("cell", this); |
|
28 | 39 | this.bind_events(); |
|
29 | 40 | } |
|
30 | 41 | this.cell_id = utils.uuid(); |
|
31 | 42 | }; |
|
32 | 43 | |
|
33 | 44 | |
|
34 | // Subclasses must implement create_element. | |
|
45 | /** | |
|
46 | * Empty. Subclasses must implement create_element. | |
|
47 | * This should contain all the code to create the DOM element in notebook | |
|
48 | * and will be called by Base Class constructor. | |
|
49 | * @method create_element | |
|
50 | */ | |
|
35 | 51 | Cell.prototype.create_element = function () {}; |
|
36 | 52 | |
|
37 | 53 | |
|
54 | /** | |
|
55 | * Empty. Subclasses must implement create_element. | |
|
56 | * This should contain all the code to create the DOM element in notebook | |
|
57 | * and will be called by Base Class constructor. | |
|
58 | * @method bind_events | |
|
59 | */ | |
|
38 | 60 | Cell.prototype.bind_events = function () { |
|
39 | 61 | var that = this; |
|
40 | 62 | // We trigger events so that Cell doesn't have to depend on Notebook. |
|
41 | 63 | that.element.click(function (event) { |
|
42 | 64 | if (that.selected === false) { |
|
43 | 65 | $([IPython.events]).trigger('select.Cell', {'cell':that}); |
|
44 | 66 | } |
|
45 | 67 | }); |
|
46 | 68 | that.element.focusin(function (event) { |
|
47 | 69 | if (that.selected === false) { |
|
48 | 70 | $([IPython.events]).trigger('select.Cell', {'cell':that}); |
|
49 | 71 | } |
|
50 | 72 | }); |
|
51 | 73 | }; |
|
52 | 74 | |
|
53 | 75 | Cell.prototype.typeset = function () { |
|
54 | 76 | if (window.MathJax){ |
|
55 | 77 | var cell_math = this.element.get(0); |
|
56 | 78 | MathJax.Hub.Queue(["Typeset",MathJax.Hub,cell_math]); |
|
57 | 79 | } |
|
58 | 80 | }; |
|
59 | 81 | |
|
60 | 82 | Cell.prototype.select = function () { |
|
61 | 83 | this.element.addClass('ui-widget-content ui-corner-all'); |
|
62 | 84 | this.selected = true; |
|
63 | 85 | }; |
|
64 | 86 | |
|
65 | 87 | |
|
66 | 88 | Cell.prototype.unselect = function () { |
|
67 | 89 | this.element.removeClass('ui-widget-content ui-corner-all'); |
|
68 | 90 | this.selected = false; |
|
69 | 91 | }; |
|
70 | 92 | |
|
71 | 93 | |
|
72 | 94 | Cell.prototype.get_text = function () { |
|
73 | 95 | }; |
|
74 | 96 | |
|
75 | 97 | |
|
76 | 98 | Cell.prototype.set_text = function (text) { |
|
77 | 99 | }; |
|
78 | 100 | |
|
79 | 101 | |
|
80 | 102 | Cell.prototype.refresh = function () { |
|
81 | 103 | this.code_mirror.refresh(); |
|
82 | 104 | }; |
|
83 | 105 | |
|
84 | 106 | |
|
85 | 107 | Cell.prototype.edit = function () { |
|
86 | 108 | }; |
|
87 | 109 | |
|
88 | 110 | |
|
89 | 111 | Cell.prototype.render = function () { |
|
90 | 112 | }; |
|
91 | 113 | |
|
92 | 114 | |
|
93 | 115 | Cell.prototype.toJSON = function () { |
|
94 | 116 | var data = {}; |
|
95 | 117 | data.metadata = this.metadata; |
|
96 | 118 | return data; |
|
97 | 119 | }; |
|
98 | 120 | |
|
99 | 121 | |
|
100 | 122 | Cell.prototype.fromJSON = function (data) { |
|
101 | 123 | if (data.metadata !== undefined) { |
|
102 | 124 | this.metadata = data.metadata; |
|
103 | 125 | } |
|
104 | 126 | }; |
|
105 | 127 | |
|
106 | 128 | |
|
107 | 129 | Cell.prototype.is_splittable = function () { |
|
108 | 130 | return true; |
|
109 | 131 | }; |
|
110 | 132 | |
|
111 | 133 | |
|
112 | 134 | Cell.prototype.get_pre_cursor = function () { |
|
113 | 135 | var cursor = this.code_mirror.getCursor(); |
|
114 | 136 | var text = this.code_mirror.getRange({line:0,ch:0}, cursor); |
|
115 | 137 | text = text.replace(/^\n+/, '').replace(/\n+$/, ''); |
|
116 | 138 | return text; |
|
117 | 139 | } |
|
118 | 140 | |
|
119 | 141 | |
|
120 | 142 | Cell.prototype.get_post_cursor = function () { |
|
121 | 143 | var cursor = this.code_mirror.getCursor(); |
|
122 | 144 | var last_line_num = this.code_mirror.lineCount()-1; |
|
123 | 145 | var last_line_len = this.code_mirror.getLine(last_line_num).length; |
|
124 | 146 | var end = {line:last_line_num, ch:last_line_len} |
|
125 | 147 | var text = this.code_mirror.getRange(cursor, end); |
|
126 | 148 | text = text.replace(/^\n+/, '').replace(/\n+$/, ''); |
|
127 | 149 | return text; |
|
128 | 150 | }; |
|
129 | 151 | |
|
130 | 152 | |
|
131 | 153 | Cell.prototype.grow = function(element) { |
|
132 | 154 | // Grow the cell by hand. This is used upon reloading from JSON, when the |
|
133 | 155 | // autogrow handler is not called. |
|
134 | 156 | var dom = element.get(0); |
|
135 | 157 | var lines_count = 0; |
|
136 | 158 | // modified split rule from |
|
137 | 159 | // http://stackoverflow.com/questions/2035910/how-to-get-the-number-of-lines-in-a-textarea/2036424#2036424 |
|
138 | 160 | var lines = dom.value.split(/\r|\r\n|\n/); |
|
139 | 161 | lines_count = lines.length; |
|
140 | 162 | if (lines_count >= 1) { |
|
141 | 163 | dom.rows = lines_count; |
|
142 | 164 | } else { |
|
143 | 165 | dom.rows = 1; |
|
144 | 166 | } |
|
145 | 167 | }; |
|
146 | 168 | |
|
147 | 169 | |
|
148 | 170 | Cell.prototype.toggle_line_numbers = function () { |
|
149 | 171 | if (this.code_mirror.getOption('lineNumbers') == false) { |
|
150 | 172 | this.code_mirror.setOption('lineNumbers', true); |
|
151 | 173 | } else { |
|
152 | 174 | this.code_mirror.setOption('lineNumbers', false); |
|
153 | 175 | } |
|
154 | 176 | this.code_mirror.refresh(); |
|
155 | 177 | }; |
|
156 | 178 | |
|
157 | 179 | Cell.prototype.force_highlight = function(mode) { |
|
158 | 180 | this.user_highlight = mode; |
|
159 | 181 | this.auto_highlight(); |
|
160 | 182 | }; |
|
161 | 183 | |
|
162 | 184 | Cell.prototype._auto_highlight = function (modes) { |
|
163 | 185 | //Here we handle manually selected modes |
|
164 | 186 | if( this.user_highlight != undefined && this.user_highlight != 'auto' ) |
|
165 | 187 | { |
|
166 | 188 | var mode = this.user_highlight; |
|
167 | 189 | CodeMirror.autoLoadMode(this.code_mirror, mode); |
|
168 | 190 | this.code_mirror.setOption('mode', mode); |
|
169 | 191 | return; |
|
170 | 192 | } |
|
171 | 193 | var first_line = this.code_mirror.getLine(0); |
|
172 | 194 | // loop on every pairs |
|
173 | 195 | for( var mode in modes) { |
|
174 | 196 | var regs = modes[mode]['reg']; |
|
175 | 197 | // only one key every time but regexp can't be keys... |
|
176 | 198 | for(var reg in regs ) { |
|
177 | 199 | // here we handle non magic_modes |
|
178 | 200 | if(first_line.match(regs[reg]) != null) { |
|
179 | 201 | if (mode.search('magic_') != 0) { |
|
180 | 202 | this.code_mirror.setOption('mode',mode); |
|
181 | 203 | CodeMirror.autoLoadMode(this.code_mirror, mode); |
|
182 | 204 | return; |
|
183 | 205 | } |
|
184 | 206 | var open = modes[mode]['open']|| "%%"; |
|
185 | 207 | var close = modes[mode]['close']|| "%%end"; |
|
186 | 208 | var mmode = mode; |
|
187 | 209 | mode = mmode.substr(6); |
|
188 | 210 | CodeMirror.autoLoadMode(this.code_mirror, mode); |
|
189 | 211 | // create on the fly a mode that swhitch between |
|
190 | 212 | // plain/text and smth else otherwise `%%` is |
|
191 | 213 | // source of some highlight issues. |
|
192 | 214 | // we use patchedGetMode to circumvent a bug in CM |
|
193 | 215 | CodeMirror.defineMode(mmode , function(config) { |
|
194 | 216 | return CodeMirror.multiplexingMode( |
|
195 | 217 | CodeMirror.patchedGetMode(config, 'text/plain'), |
|
196 | 218 | // always set someting on close |
|
197 | 219 | {open: open, close: close, |
|
198 | 220 | mode: CodeMirror.patchedGetMode(config, mode), |
|
199 | 221 | delimStyle: "delimit" |
|
200 | 222 | } |
|
201 | 223 | ); |
|
202 | 224 | }); |
|
203 | 225 | this.code_mirror.setOption('mode', mmode); |
|
204 | 226 | return; |
|
205 | 227 | } |
|
206 | 228 | } |
|
207 | 229 | } |
|
208 | 230 | // fallback on default (python) |
|
209 | 231 | var default_mode = this.default_mode || 'text/plain'; |
|
210 | 232 | this.code_mirror.setOption('mode', default_mode); |
|
211 | 233 | }; |
|
212 | 234 | |
|
213 | 235 | IPython.Cell = Cell; |
|
214 | 236 | |
|
215 | 237 | return IPython; |
|
216 | 238 | |
|
217 | 239 | }(IPython)); |
|
218 | 240 |
General Comments 0
You need to be logged in to leave comments.
Login now