##// END OF EJS Templates
Removing stale code in CodeCell.
Brian Granger -
Show More
@@ -1,291 +1,290 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 // CodeCell
10 10 //============================================================================
11 11
12 12 var IPython = (function (IPython) {
13 13 "use strict";
14 14
15 15 var utils = IPython.utils;
16 16 var key = IPython.utils.keycodes;
17 17
18 18 var CodeCell = function (kernel) {
19 19 this.kernel = kernel;
20 20 this.code_mirror = null;
21 21 this.input_prompt_number = null;
22 22 this.tooltip_on_tab = true;
23 23 IPython.Cell.apply(this, arguments);
24 24 };
25 25
26 26
27 27 CodeCell.prototype = new IPython.Cell();
28 28
29 29
30 30 CodeCell.prototype.create_element = function () {
31 31 var cell = $('<div></div>').addClass('cell border-box-sizing code_cell vbox');
32 32 cell.attr('tabindex','2');
33 33 var input = $('<div></div>').addClass('input hbox');
34 34 input.append($('<div/>').addClass('prompt input_prompt'));
35 35 var input_area = $('<div/>').addClass('input_area box-flex1');
36 36 this.code_mirror = CodeMirror(input_area.get(0), {
37 37 indentUnit : 4,
38 38 mode: 'python',
39 39 theme: 'ipython',
40 40 readOnly: this.read_only,
41 41 onKeyEvent: $.proxy(this.handle_codemirror_keyevent,this)
42 42 });
43 43 input.append(input_area);
44 44 var output = $('<div></div>');
45 45 cell.append(input).append(output);
46 46 this.element = cell;
47 47 this.output_area = new IPython.OutputArea(output, false);
48 48
49 49 // construct a completer only if class exist
50 50 // otherwise no print view
51 51 if (IPython.Completer != undefined )
52 52 {
53 53 this.completer = new IPython.Completer(this);
54 54 }
55 55 };
56 56
57 57 CodeCell.prototype.handle_codemirror_keyevent = function (editor, event) {
58 58 // This method gets called in CodeMirror's onKeyDown/onKeyPress
59 59 // handlers and is used to provide custom key handling. Its return
60 60 // value is used to determine if CodeMirror should ignore the event:
61 61 // true = ignore, false = don't ignore.
62 62
63 63 if (this.read_only){
64 64 return false;
65 65 }
66 66
67 67 var that = this;
68 68 // whatever key is pressed, first, cancel the tooltip request before
69 69 // they are sent, and remove tooltip if any, except for tab again
70 70 if(event.type === 'keydown' && event.which != key.tab ) {
71 71 IPython.tooltip.remove_and_cancel_tooltip();
72 72 };
73 73
74 74 var cur = editor.getCursor();
75 75
76 76 if (event.keyCode === key.enter && (event.shiftKey || event.ctrlKey)) {
77 77 // Always ignore shift-enter in CodeMirror as we handle it.
78 78 return true;
79 79 } else if (event.which === 40 && event.type === 'keypress' && IPython.tooltip.time_before_tooltip >= 0) {
80 80 // triger on keypress (!) otherwise inconsistent event.which depending on plateform
81 81 // browser and keyboard layout !
82 82 // Pressing '(' , request tooltip, don't forget to reappend it
83 83 IPython.tooltip.pending(that);
84 84 } else if (event.which === key.upArrow) {
85 85 // If we are not at the top, let CM handle the up arrow and
86 86 // prevent the global keydown handler from handling it.
87 87 if (!that.at_top()) {
88 88 event.stop();
89 89 return false;
90 90 } else {
91 91 return true;
92 92 };
93 93 } else if (event.which === key.downArrow) {
94 94 // If we are not at the bottom, let CM handle the down arrow and
95 95 // prevent the global keydown handler from handling it.
96 96 if (!that.at_bottom()) {
97 97 event.stop();
98 98 return false;
99 99 } else {
100 100 return true;
101 101 };
102 102 } else if (event.keyCode === key.tab && event.type == 'keydown') {
103 103 // Tab completion.
104 104 //Do not trim here because of tooltip
105 105 var pre_cursor = editor.getRange({line:cur.line,ch:0},cur);
106 106 if (pre_cursor.trim() === "") {
107 107 // Don't autocomplete if the part of the line before the cursor
108 108 // is empty. In this case, let CodeMirror handle indentation.
109 109 return false;
110 110 } else if ((pre_cursor.substr(-1) === "("|| pre_cursor.substr(-1) === " ") && that.tooltip_on_tab ) {
111 111 IPython.tooltip.request(that);
112 112 // Prevent the event from bubbling up.
113 113 event.stop();
114 114 // Prevent CodeMirror from handling the tab.
115 115 return true;
116 116 } else {
117 117 event.stop();
118 118 this.completer.startCompletion();
119 119 return true;
120 120 };
121 121 } else if (event.keyCode === key.backspace && event.type == 'keydown') {
122 122 // If backspace and the line ends with 4 spaces, remove them.
123 123 var line = editor.getLine(cur.line);
124 124 var ending = line.slice(-4);
125 125 if (ending === ' ') {
126 126 editor.replaceRange('',
127 127 {line: cur.line, ch: cur.ch-4},
128 128 {line: cur.line, ch: cur.ch}
129 129 );
130 130 event.stop();
131 131 return true;
132 132 } else {
133 133 return false;
134 134 };
135 135 } else {
136 136 // keypress/keyup also trigger on TAB press, and we don't want to
137 137 // use those to disable tab completion.
138 138 return false;
139 139 };
140 140 return false;
141 141 };
142 142
143 143 // Kernel related calls.
144 144
145 145 CodeCell.prototype.execute = function () {
146 146 this.output_area.clear_output(true, true, true);
147 147 this.set_input_prompt('*');
148 148 this.element.addClass("running");
149 var code = this.get_text();
150 149 var callbacks = {
151 150 'execute_reply': $.proxy(this._handle_execute_reply, this),
152 151 'output': $.proxy(this.output_area.handle_output, this.output_area),
153 152 'clear_output': $.proxy(this.output_area.handle_clear_output, this.output_area),
154 153 'cell': this
155 154 };
156 155 var msg_id = this.kernel.execute(this.get_text(), callbacks, {silent: false});
157 156 };
158 157
159 158
160 159 CodeCell.prototype._handle_execute_reply = function (content) {
161 160 this.set_input_prompt(content.execution_count);
162 161 this.element.removeClass("running");
163 162 // this.dirty = true;
164 163 }
165 164
166 165 // Basic cell manipulation.
167 166
168 167 CodeCell.prototype.select = function () {
169 168 IPython.Cell.prototype.select.apply(this);
170 169 this.code_mirror.refresh();
171 170 this.code_mirror.focus();
172 171 // We used to need an additional refresh() after the focus, but
173 172 // it appears that this has been fixed in CM. This bug would show
174 173 // up on FF when a newly loaded markdown cell was edited.
175 174 };
176 175
177 176
178 177 CodeCell.prototype.select_all = function () {
179 178 var start = {line: 0, ch: 0};
180 179 var nlines = this.code_mirror.lineCount();
181 180 var last_line = this.code_mirror.getLine(nlines-1);
182 181 var end = {line: nlines-1, ch: last_line.length};
183 182 this.code_mirror.setSelection(start, end);
184 183 };
185 184
186 185
187 186 CodeCell.prototype.collapse = function () {
188 187 this.output_area.collapse();
189 188 };
190 189
191 190
192 191 CodeCell.prototype.expand = function () {
193 192 this.output_area.expand();
194 193 };
195 194
196 195
197 196 CodeCell.prototype.toggle_output = function () {
198 197 this.output_area.toggle_output();
199 198 };
200 199
201 200
202 201 CodeCell.prototype.set_input_prompt = function (number) {
203 202 this.input_prompt_number = number;
204 203 var ns = number || "&nbsp;";
205 204 this.element.find('div.input_prompt').html('In&nbsp;[' + ns + ']:');
206 205 };
207 206
208 207
209 208 CodeCell.prototype.clear_input = function () {
210 209 this.code_mirror.setValue('');
211 210 };
212 211
213 212
214 213 CodeCell.prototype.get_text = function () {
215 214 return this.code_mirror.getValue();
216 215 };
217 216
218 217
219 218 CodeCell.prototype.set_text = function (code) {
220 219 return this.code_mirror.setValue(code);
221 220 };
222 221
223 222
224 223 CodeCell.prototype.at_top = function () {
225 224 var cursor = this.code_mirror.getCursor();
226 225 if (cursor.line === 0) {
227 226 return true;
228 227 } else {
229 228 return false;
230 229 }
231 230 };
232 231
233 232
234 233 CodeCell.prototype.at_bottom = function () {
235 234 var cursor = this.code_mirror.getCursor();
236 235 if (cursor.line === (this.code_mirror.lineCount()-1)) {
237 236 return true;
238 237 } else {
239 238 return false;
240 239 }
241 240 };
242 241
243 242
244 243 CodeCell.prototype.clear_output = function (stdout, stderr, other) {
245 244 this.output_area.clear_output(stdout, stderr, other);
246 245 };
247 246
248 247
249 248 // JSON serialization
250 249
251 250 CodeCell.prototype.fromJSON = function (data) {
252 251 if (data.cell_type === 'code') {
253 252 if (data.input !== undefined) {
254 253 this.set_text(data.input);
255 254 }
256 255 if (data.prompt_number !== undefined) {
257 256 this.set_input_prompt(data.prompt_number);
258 257 } else {
259 258 this.set_input_prompt();
260 259 };
261 260 this.output_area.fromJSON(data.outputs);
262 261 if (data.collapsed !== undefined) {
263 262 if (data.collapsed) {
264 263 this.collapse();
265 264 } else {
266 265 this.expand();
267 266 };
268 267 };
269 268 };
270 269 };
271 270
272 271
273 272 CodeCell.prototype.toJSON = function () {
274 273 var data = {};
275 274 data.input = this.get_text();
276 275 data.cell_type = 'code';
277 276 if (this.input_prompt_number) {
278 277 data.prompt_number = this.input_prompt_number;
279 278 };
280 279 var outputs = this.output_area.toJSON();
281 280 data.outputs = outputs;
282 281 data.language = 'python';
283 282 data.collapsed = this.collapsed;
284 283 return data;
285 284 };
286 285
287 286
288 287 IPython.CodeCell = CodeCell;
289 288
290 289 return IPython;
291 290 }(IPython));
General Comments 0
You need to be logged in to leave comments. Login now