##// END OF EJS Templates
use show method in toggle method
Matthias BUSSONNIER -
Show More
@@ -1,325 +1,321 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 // Cell
10 10 //============================================================================
11 11 /**
12 12 * An extendable module that provide base functionnality to create cell for notebook.
13 13 * @module IPython
14 14 * @namespace IPython
15 15 * @submodule Cell
16 16 */
17 17
18 18 var IPython = (function (IPython) {
19 19
20 20 var utils = IPython.utils;
21 21
22 22 /**
23 23 * The Base `Cell` class from which to inherit
24 24 * @class Cell
25 25 */
26 26
27 27 /*
28 28 * @constructor
29 29 */
30 30 var Cell = function () {
31 31 this.placeholder = this.placeholder || '';
32 32 this.read_only = false;
33 33 this.selected = false;
34 34 this.element = null;
35 35 this.metadata = {};
36 36 // load this from metadata later ?
37 37 this.user_highlight = 'auto';
38 38 this.create_element();
39 39 if (this.element !== null) {
40 40 this.element.data("cell", this);
41 41 this.bind_events();
42 42 }
43 43 this.cell_id = utils.uuid();
44 44 };
45 45
46 46
47 47 /**
48 48 * Empty. Subclasses must implement create_element.
49 49 * This should contain all the code to create the DOM element in notebook
50 50 * and will be called by Base Class constructor.
51 51 * @method create_element
52 52 */
53 53 Cell.prototype.create_element = function () {
54 54 };
55 55
56 56
57 57 /**
58 58 * Subclasses can implement override bind_events.
59 59 * Be carefull to call the parent method when overwriting as it fires event.
60 60 * this will be triggerd after create_element in constructor.
61 61 * @method bind_events
62 62 */
63 63 Cell.prototype.bind_events = function () {
64 64 var that = this;
65 65 // We trigger events so that Cell doesn't have to depend on Notebook.
66 66 that.element.click(function (event) {
67 67 if (that.selected === false) {
68 68 $([IPython.events]).trigger('select.Cell', {'cell':that});
69 69 }
70 70 });
71 71 that.element.focusin(function (event) {
72 72 if (that.selected === false) {
73 73 $([IPython.events]).trigger('select.Cell', {'cell':that});
74 74 }
75 75 });
76 76 };
77 77
78 78 /**
79 79 * Triger typsetting of math by mathjax on current cell element
80 80 * @method typeset
81 81 */
82 82 Cell.prototype.typeset = function () {
83 83 if (window.MathJax){
84 84 var cell_math = this.element.get(0);
85 85 MathJax.Hub.Queue(["Typeset",MathJax.Hub,cell_math]);
86 86 }
87 87 };
88 88
89 89 /**
90 90 * should be triggerd when cell is selected
91 91 * @method select
92 92 */
93 93 Cell.prototype.select = function () {
94 94 this.element.addClass('selected');
95 95 this.selected = true;
96 96 };
97 97
98 98
99 99 /**
100 100 * should be triggerd when cell is unselected
101 101 * @method unselect
102 102 */
103 103 Cell.prototype.unselect = function () {
104 104 this.element.removeClass('selected');
105 105 this.selected = false;
106 106 };
107 107
108 108 /**
109 109 * should be overritten by subclass
110 110 * @method get_text
111 111 */
112 112 Cell.prototype.get_text = function () {
113 113 };
114 114
115 115 /**
116 116 * should be overritten by subclass
117 117 * @method set_text
118 118 * @param {string} text
119 119 */
120 120 Cell.prototype.set_text = function (text) {
121 121 };
122 122
123 123 /**
124 124 * Refresh codemirror instance
125 125 * @method refresh
126 126 */
127 127 Cell.prototype.refresh = function () {
128 128 this.code_mirror.refresh();
129 129 };
130 130
131 131
132 132 /**
133 133 * should be overritten by subclass
134 134 * @method edit
135 135 **/
136 136 Cell.prototype.edit = function () {
137 137 };
138 138
139 139
140 140 /**
141 141 * should be overritten by subclass
142 142 * @method render
143 143 **/
144 144 Cell.prototype.render = function () {
145 145 };
146 146
147 147 /**
148 148 * should be overritten by subclass
149 149 * serialise cell to json.
150 150 * @method toJSON
151 151 **/
152 152 Cell.prototype.toJSON = function () {
153 153 var data = {};
154 154 data.metadata = this.metadata;
155 155 return data;
156 156 };
157 157
158 158
159 159 /**
160 160 * should be overritten by subclass
161 161 * @method fromJSON
162 162 **/
163 163 Cell.prototype.fromJSON = function (data) {
164 164 if (data.metadata !== undefined) {
165 165 this.metadata = data.metadata;
166 166 }
167 167 this.celltoolbar.rebuild();
168 168 };
169 169
170 170
171 171 /**
172 172 * can the cell be splitted in 2 cells.
173 173 * @method is_splittable
174 174 **/
175 175 Cell.prototype.is_splittable = function () {
176 176 return true;
177 177 };
178 178
179 179
180 180 /**
181 181 * @return {String} - the text before the cursor
182 182 * @method get_pre_cursor
183 183 **/
184 184 Cell.prototype.get_pre_cursor = function () {
185 185 var cursor = this.code_mirror.getCursor();
186 186 var text = this.code_mirror.getRange({line:0,ch:0}, cursor);
187 187 text = text.replace(/^\n+/, '').replace(/\n+$/, '');
188 188 return text;
189 189 }
190 190
191 191
192 192 /**
193 193 * @return {String} - the text after the cursor
194 194 * @method get_post_cursor
195 195 **/
196 196 Cell.prototype.get_post_cursor = function () {
197 197 var cursor = this.code_mirror.getCursor();
198 198 var last_line_num = this.code_mirror.lineCount()-1;
199 199 var last_line_len = this.code_mirror.getLine(last_line_num).length;
200 200 var end = {line:last_line_num, ch:last_line_len}
201 201 var text = this.code_mirror.getRange(cursor, end);
202 202 text = text.replace(/^\n+/, '').replace(/\n+$/, '');
203 203 return text;
204 204 };
205 205
206 206
207 207 /** Grow the cell by hand. This is used upon reloading from JSON, when the
208 208 * autogrow handler is not called.
209 209 *
210 210 * could be made static
211 211 *
212 212 * @param {Dom element} - element
213 213 * @method grow
214 214 **/
215 215 Cell.prototype.grow = function(element) {
216 216 var dom = element.get(0);
217 217 var lines_count = 0;
218 218 // modified split rule from
219 219 // http://stackoverflow.com/questions/2035910/how-to-get-the-number-of-lines-in-a-textarea/2036424#2036424
220 220 var lines = dom.value.split(/\r|\r\n|\n/);
221 221 lines_count = lines.length;
222 222 if (lines_count >= 1) {
223 223 dom.rows = lines_count;
224 224 } else {
225 225 dom.rows = 1;
226 226 }
227 227 };
228 228
229 229 /**
230 230 * Show/Hide CodeMirror LineNumber
231 * @method show_line_number
231 * @method show_line_numbers
232 232 *
233 233 * @param value {Bool} show (true), or hide (false) the line number in CodeMirror
234 234 **/
235 235 Cell.prototype.show_line_numbers = function (value) {
236 236 this.code_mirror.setOption('lineNumbers', value);
237 237 this.code_mirror.refresh();
238 238 };
239 239
240 240 /**
241 241 * Toggle CodeMirror LineNumber
242 242 * @method toggle_line_numbers
243 243 **/
244 244 Cell.prototype.toggle_line_numbers = function () {
245 if (this.code_mirror.getOption('lineNumbers') == false) {
246 this.code_mirror.setOption('lineNumbers', true);
247 } else {
248 this.code_mirror.setOption('lineNumbers', false);
249 }
250 this.code_mirror.refresh();
245 var val = this.code_mirror.getOption('lineNumbers');
246 this.show_line_numbers(!val);
251 247 };
252 248
253 249 /**
254 250 * force codemirror highlight mode
255 251 * @method force_highlight
256 252 * @param {object} - CodeMirror mode
257 253 **/
258 254 Cell.prototype.force_highlight = function(mode) {
259 255 this.user_highlight = mode;
260 256 this.auto_highlight();
261 257 };
262 258
263 259 /**
264 260 * Try to autodetect cell highlight mode, or use selected mode
265 261 * @methods _auto_highlight
266 262 * @private
267 263 * @param {String|object|undefined} - CodeMirror mode | 'auto'
268 264 **/
269 265 Cell.prototype._auto_highlight = function (modes) {
270 266 //Here we handle manually selected modes
271 267 if( this.user_highlight != undefined && this.user_highlight != 'auto' )
272 268 {
273 269 var mode = this.user_highlight;
274 270 CodeMirror.autoLoadMode(this.code_mirror, mode);
275 271 this.code_mirror.setOption('mode', mode);
276 272 return;
277 273 }
278 274 var first_line = this.code_mirror.getLine(0);
279 275 // loop on every pairs
280 276 for( var mode in modes) {
281 277 var regs = modes[mode]['reg'];
282 278 // only one key every time but regexp can't be keys...
283 279 for(var reg in regs ) {
284 280 // here we handle non magic_modes
285 281 if(first_line.match(regs[reg]) != null) {
286 282 if (mode.search('magic_') != 0) {
287 283 this.code_mirror.setOption('mode',mode);
288 284 CodeMirror.autoLoadMode(this.code_mirror, mode);
289 285 return;
290 286 }
291 287 var open = modes[mode]['open']|| "%%";
292 288 var close = modes[mode]['close']|| "%%end";
293 289 var mmode = mode;
294 290 mode = mmode.substr(6);
295 291 CodeMirror.autoLoadMode(this.code_mirror, mode);
296 292 // create on the fly a mode that swhitch between
297 293 // plain/text and smth else otherwise `%%` is
298 294 // source of some highlight issues.
299 295 // we use patchedGetMode to circumvent a bug in CM
300 296 CodeMirror.defineMode(mmode , function(config) {
301 297 return CodeMirror.multiplexingMode(
302 298 CodeMirror.patchedGetMode(config, 'text/plain'),
303 299 // always set someting on close
304 300 {open: open, close: close,
305 301 mode: CodeMirror.patchedGetMode(config, mode),
306 302 delimStyle: "delimit"
307 303 }
308 304 );
309 305 });
310 306 this.code_mirror.setOption('mode', mmode);
311 307 return;
312 308 }
313 309 }
314 310 }
315 311 // fallback on default (python)
316 312 var default_mode = this.default_mode || 'text/plain';
317 313 this.code_mirror.setOption('mode', default_mode);
318 314 };
319 315
320 316 IPython.Cell = Cell;
321 317
322 318 return IPython;
323 319
324 320 }(IPython));
325 321
General Comments 0
You need to be logged in to leave comments. Login now