##// END OF EJS Templates
notebook: fix, only one completion autopick
Matthias BUSSONNIER -
Show More
@@ -1,827 +1,828 b''
1 //----------------------------------------------------------------------------
1 //----------------------------------------------------------------------------
2 // Copyright (C) 2008-2011 The IPython Development Team
2 // Copyright (C) 2008-2011 The IPython Development Team
3 //
3 //
4 // Distributed under the terms of the BSD License. The full license is in
4 // Distributed under the terms of the BSD License. The full license is in
5 // the file COPYING, distributed as part of this software.
5 // the file COPYING, distributed as part of this software.
6 //----------------------------------------------------------------------------
6 //----------------------------------------------------------------------------
7
7
8 //============================================================================
8 //============================================================================
9 // CodeCell
9 // CodeCell
10 //============================================================================
10 //============================================================================
11
11
12 var IPython = (function (IPython) {
12 var IPython = (function (IPython) {
13
13
14 var utils = IPython.utils;
14 var utils = IPython.utils;
15
15
16 var CodeCell = function (notebook) {
16 var CodeCell = function (notebook) {
17 this.code_mirror = null;
17 this.code_mirror = null;
18 this.input_prompt_number = ' ';
18 this.input_prompt_number = ' ';
19 this.is_completing = false;
19 this.is_completing = false;
20 this.completion_cursor = null;
20 this.completion_cursor = null;
21 this.outputs = [];
21 this.outputs = [];
22 this.collapsed = false;
22 this.collapsed = false;
23 this.tooltip_timeout = null;
23 this.tooltip_timeout = null;
24 IPython.Cell.apply(this, arguments);
24 IPython.Cell.apply(this, arguments);
25 };
25 };
26
26
27
27
28 CodeCell.prototype = new IPython.Cell();
28 CodeCell.prototype = new IPython.Cell();
29
29
30
30
31 CodeCell.prototype.create_element = function () {
31 CodeCell.prototype.create_element = function () {
32 var cell = $('<div></div>').addClass('cell border-box-sizing code_cell vbox');
32 var cell = $('<div></div>').addClass('cell border-box-sizing code_cell vbox');
33 cell.attr('tabindex','2');
33 cell.attr('tabindex','2');
34 var input = $('<div></div>').addClass('input hbox');
34 var input = $('<div></div>').addClass('input hbox');
35 input.append($('<div/>').addClass('prompt input_prompt'));
35 input.append($('<div/>').addClass('prompt input_prompt'));
36 var input_area = $('<div/>').addClass('input_area box-flex1');
36 var input_area = $('<div/>').addClass('input_area box-flex1');
37 this.code_mirror = CodeMirror(input_area.get(0), {
37 this.code_mirror = CodeMirror(input_area.get(0), {
38 indentUnit : 4,
38 indentUnit : 4,
39 mode: 'python',
39 mode: 'python',
40 theme: 'ipython',
40 theme: 'ipython',
41 readOnly: this.read_only,
41 readOnly: this.read_only,
42 onKeyEvent: $.proxy(this.handle_codemirror_keyevent,this)
42 onKeyEvent: $.proxy(this.handle_codemirror_keyevent,this)
43 });
43 });
44 input.append(input_area);
44 input.append(input_area);
45 var output = $('<div></div>').addClass('output vbox');
45 var output = $('<div></div>').addClass('output vbox');
46 cell.append(input).append(output);
46 cell.append(input).append(output);
47 this.element = cell;
47 this.element = cell;
48 this.collapse();
48 this.collapse();
49 };
49 };
50
50
51 //TODO, try to diminish the number of parameters.
51 //TODO, try to diminish the number of parameters.
52 CodeCell.prototype.request_tooltip_after_time = function (pre_cursor,time){
52 CodeCell.prototype.request_tooltip_after_time = function (pre_cursor,time){
53 var that = this;
53 var that = this;
54 if (pre_cursor === "" || pre_cursor === "(" ) {
54 if (pre_cursor === "" || pre_cursor === "(" ) {
55 // don't do anything if line beggin with '(' or is empty
55 // don't do anything if line beggin with '(' or is empty
56 } else {
56 } else {
57 // Will set a timer to request tooltip in `time`
57 // Will set a timer to request tooltip in `time`
58 that.tooltip_timeout = setTimeout(function(){
58 that.tooltip_timeout = setTimeout(function(){
59 IPython.notebook.request_tool_tip(that, pre_cursor)
59 IPython.notebook.request_tool_tip(that, pre_cursor)
60 },time);
60 },time);
61 }
61 }
62 };
62 };
63
63
64 CodeCell.prototype.handle_codemirror_keyevent = function (editor, event) {
64 CodeCell.prototype.handle_codemirror_keyevent = function (editor, event) {
65 // This method gets called in CodeMirror's onKeyDown/onKeyPress
65 // This method gets called in CodeMirror's onKeyDown/onKeyPress
66 // handlers and is used to provide custom key handling. Its return
66 // handlers and is used to provide custom key handling. Its return
67 // value is used to determine if CodeMirror should ignore the event:
67 // value is used to determine if CodeMirror should ignore the event:
68 // true = ignore, false = don't ignore.
68 // true = ignore, false = don't ignore.
69
69
70 // note that we are comparing and setting the time to wait at each key press.
70 // note that we are comparing and setting the time to wait at each key press.
71 // a better wqy might be to generate a new function on each time change and
71 // a better wqy might be to generate a new function on each time change and
72 // assign it to CodeCell.prototype.request_tooltip_after_time
72 // assign it to CodeCell.prototype.request_tooltip_after_time
73 tooltip_wait_time = this.notebook.time_before_tooltip;
73 tooltip_wait_time = this.notebook.time_before_tooltip;
74 tooltip_on_tab = this.notebook.tooltip_on_tab;
74 tooltip_on_tab = this.notebook.tooltip_on_tab;
75 var that = this;
75 var that = this;
76 // whatever key is pressed, first, cancel the tooltip request before
76 // whatever key is pressed, first, cancel the tooltip request before
77 // they are sent, and remove tooltip if any
77 // they are sent, and remove tooltip if any
78 if(event.type === 'keydown' ){
78 if(event.type === 'keydown' ){
79 that.remove_and_cancel_tooltip();
79 that.remove_and_cancel_tooltip();
80 }
80 }
81
81
82 if (event.keyCode === 13 && (event.shiftKey || event.ctrlKey)) {
82 if (event.keyCode === 13 && (event.shiftKey || event.ctrlKey)) {
83 // Always ignore shift-enter in CodeMirror as we handle it.
83 // Always ignore shift-enter in CodeMirror as we handle it.
84 return true;
84 return true;
85 }else if (event.which === 40 && event.type === 'keypress' && tooltip_wait_time >= 0) {
85 }else if (event.which === 40 && event.type === 'keypress' && tooltip_wait_time >= 0) {
86 // triger aon keypress (!) otherwise inconsistent event.which depending on plateform
86 // triger aon keypress (!) otherwise inconsistent event.which depending on plateform
87 // browser and keyboard layout !
87 // browser and keyboard layout !
88 // Pressing '(' , request tooltip, don't forget to reappend it
88 // Pressing '(' , request tooltip, don't forget to reappend it
89 var cursor = editor.getCursor();
89 var cursor = editor.getCursor();
90 var pre_cursor = editor.getRange({line:cursor.line,ch:0},cursor).trim()+'(';
90 var pre_cursor = editor.getRange({line:cursor.line,ch:0},cursor).trim()+'(';
91 that.request_tooltip_after_time(pre_cursor,tooltip_wait_time);
91 that.request_tooltip_after_time(pre_cursor,tooltip_wait_time);
92 } else if (event.keyCode === 9 && event.type == 'keydown') {
92 } else if (event.keyCode === 9 && event.type == 'keydown') {
93 // Tab completion.
93 // Tab completion.
94 var cur = editor.getCursor();
94 var cur = editor.getCursor();
95 //Do not trim here because of tooltip
95 //Do not trim here because of tooltip
96 var pre_cursor = editor.getRange({line:cur.line,ch:0},cur);
96 var pre_cursor = editor.getRange({line:cur.line,ch:0},cur);
97 if (pre_cursor.trim() === "") {
97 if (pre_cursor.trim() === "") {
98 // Don't autocomplete if the part of the line before the cursor
98 // Don't autocomplete if the part of the line before the cursor
99 // is empty. In this case, let CodeMirror handle indentation.
99 // is empty. In this case, let CodeMirror handle indentation.
100 return false;
100 return false;
101 } else if ((pre_cursor.substr(-1) === "("|| pre_cursor.substr(-1) === " ") && tooltip_on_tab ) {
101 } else if ((pre_cursor.substr(-1) === "("|| pre_cursor.substr(-1) === " ") && tooltip_on_tab ) {
102 that.request_tooltip_after_time(pre_cursor,0);
102 that.request_tooltip_after_time(pre_cursor,0);
103 } else {
103 } else {
104 pre_cursor.trim();
104 pre_cursor.trim();
105 // Autocomplete the current line.
105 // Autocomplete the current line.
106 event.stop();
106 event.stop();
107 var line = editor.getLine(cur.line);
107 var line = editor.getLine(cur.line);
108 this.is_completing = true;
108 this.is_completing = true;
109 this.completion_cursor = cur;
109 this.completion_cursor = cur;
110 IPython.notebook.complete_cell(this, line, cur.ch);
110 IPython.notebook.complete_cell(this, line, cur.ch);
111 return true;
111 return true;
112 }
112 }
113 } else if (event.keyCode === 8 && event.type == 'keydown') {
113 } else if (event.keyCode === 8 && event.type == 'keydown') {
114 // If backspace and the line ends with 4 spaces, remove them.
114 // If backspace and the line ends with 4 spaces, remove them.
115 var cur = editor.getCursor();
115 var cur = editor.getCursor();
116 var line = editor.getLine(cur.line);
116 var line = editor.getLine(cur.line);
117 var ending = line.slice(-4);
117 var ending = line.slice(-4);
118 if (ending === ' ') {
118 if (ending === ' ') {
119 editor.replaceRange('',
119 editor.replaceRange('',
120 {line: cur.line, ch: cur.ch-4},
120 {line: cur.line, ch: cur.ch-4},
121 {line: cur.line, ch: cur.ch}
121 {line: cur.line, ch: cur.ch}
122 );
122 );
123 event.stop();
123 event.stop();
124 return true;
124 return true;
125 } else {
125 } else {
126 return false;
126 return false;
127 }
127 }
128 } else if (event.keyCode === 76 && event.ctrlKey && event.shiftKey
128 } else if (event.keyCode === 76 && event.ctrlKey && event.shiftKey
129 && event.type == 'keydown') {
129 && event.type == 'keydown') {
130 // toggle line numbers with Ctrl-Shift-L
130 // toggle line numbers with Ctrl-Shift-L
131 this.toggle_line_numbers();
131 this.toggle_line_numbers();
132 }
132 }
133 else {
133 else {
134 // keypress/keyup also trigger on TAB press, and we don't want to
134 // keypress/keyup also trigger on TAB press, and we don't want to
135 // use those to disable tab completion.
135 // use those to disable tab completion.
136 if (this.is_completing && event.keyCode !== 9) {
136 if (this.is_completing && event.keyCode !== 9) {
137 var ed_cur = editor.getCursor();
137 var ed_cur = editor.getCursor();
138 var cc_cur = this.completion_cursor;
138 var cc_cur = this.completion_cursor;
139 if (ed_cur.line !== cc_cur.line || ed_cur.ch !== cc_cur.ch) {
139 if (ed_cur.line !== cc_cur.line || ed_cur.ch !== cc_cur.ch) {
140 this.is_completing = false;
140 this.is_completing = false;
141 this.completion_cursor = null;
141 this.completion_cursor = null;
142 }
142 }
143 }
143 }
144 return false;
144 return false;
145 };
145 };
146 return false;
146 return false;
147 };
147 };
148
148
149 CodeCell.prototype.remove_and_cancel_tooltip = function() {
149 CodeCell.prototype.remove_and_cancel_tooltip = function() {
150 // note that we don't handle closing directly inside the calltip
150 // note that we don't handle closing directly inside the calltip
151 // as in the completer, because it is not focusable, so won't
151 // as in the completer, because it is not focusable, so won't
152 // get the event.
152 // get the event.
153 if (this.tooltip_timeout != null){
153 if (this.tooltip_timeout != null){
154 clearTimeout(this.tooltip_timeout);
154 clearTimeout(this.tooltip_timeout);
155 $('#tooltip').remove();
155 $('#tooltip').remove();
156 this.tooltip_timeout = null;
156 this.tooltip_timeout = null;
157 }
157 }
158 }
158 }
159
159
160 CodeCell.prototype.finish_tooltip = function (reply) {
160 CodeCell.prototype.finish_tooltip = function (reply) {
161 defstring=reply.definition;
161 defstring=reply.definition;
162 docstring=reply.docstring;
162 docstring=reply.docstring;
163 if(docstring == null){docstring="<empty docstring>"};
163 if(docstring == null){docstring="<empty docstring>"};
164 name=reply.name;
164 name=reply.name;
165
165
166 var that = this;
166 var that = this;
167 var tooltip = $('<div/>').attr('id', 'tooltip').addClass('tooltip');
167 var tooltip = $('<div/>').attr('id', 'tooltip').addClass('tooltip');
168 // remove to have the tooltip not Limited in X and Y
168 // remove to have the tooltip not Limited in X and Y
169 tooltip.addClass('smalltooltip');
169 tooltip.addClass('smalltooltip');
170 var pre=$('<pre/>').html(utils.fixConsole(docstring));
170 var pre=$('<pre/>').html(utils.fixConsole(docstring));
171 var expandlink=$('<a/>').attr('href',"#");
171 var expandlink=$('<a/>').attr('href',"#");
172 expandlink.addClass("ui-corner-all"); //rounded corner
172 expandlink.addClass("ui-corner-all"); //rounded corner
173 expandlink.attr('role',"button");
173 expandlink.attr('role',"button");
174 //expandlink.addClass('ui-button');
174 //expandlink.addClass('ui-button');
175 //expandlink.addClass('ui-state-default');
175 //expandlink.addClass('ui-state-default');
176 var expandspan=$('<span/>').text('Expand');
176 var expandspan=$('<span/>').text('Expand');
177 expandspan.addClass('ui-icon');
177 expandspan.addClass('ui-icon');
178 expandspan.addClass('ui-icon-plus');
178 expandspan.addClass('ui-icon-plus');
179 expandlink.append(expandspan);
179 expandlink.append(expandspan);
180 expandlink.attr('id','expanbutton');
180 expandlink.attr('id','expanbutton');
181 expandlink.click(function(){
181 expandlink.click(function(){
182 tooltip.removeClass('smalltooltip');
182 tooltip.removeClass('smalltooltip');
183 tooltip.addClass('bigtooltip');
183 tooltip.addClass('bigtooltip');
184 $('#expanbutton').remove();
184 $('#expanbutton').remove();
185 setTimeout(function(){that.code_mirror.focus();}, 50);
185 setTimeout(function(){that.code_mirror.focus();}, 50);
186 });
186 });
187 var morelink=$('<a/>').attr('href',"#");
187 var morelink=$('<a/>').attr('href',"#");
188 morelink.attr('role',"button");
188 morelink.attr('role',"button");
189 morelink.addClass('ui-button');
189 morelink.addClass('ui-button');
190 //morelink.addClass("ui-corner-all"); //rounded corner
190 //morelink.addClass("ui-corner-all"); //rounded corner
191 //morelink.addClass('ui-state-default');
191 //morelink.addClass('ui-state-default');
192 var morespan=$('<span/>').text('Open in Pager');
192 var morespan=$('<span/>').text('Open in Pager');
193 morespan.addClass('ui-icon');
193 morespan.addClass('ui-icon');
194 morespan.addClass('ui-icon-arrowstop-l-n');
194 morespan.addClass('ui-icon-arrowstop-l-n');
195 morelink.append(morespan);
195 morelink.append(morespan);
196 morelink.click(function(){
196 morelink.click(function(){
197 var msg_id = IPython.notebook.kernel.execute(name+"?");
197 var msg_id = IPython.notebook.kernel.execute(name+"?");
198 IPython.notebook.msg_cell_map[msg_id] = IPython.notebook.selected_cell().cell_id;
198 IPython.notebook.msg_cell_map[msg_id] = IPython.notebook.selected_cell().cell_id;
199 that.remove_and_cancel_tooltip();
199 that.remove_and_cancel_tooltip();
200 setTimeout(function(){that.code_mirror.focus();}, 50);
200 setTimeout(function(){that.code_mirror.focus();}, 50);
201 });
201 });
202
202
203 var closelink=$('<a/>').attr('href',"#");
203 var closelink=$('<a/>').attr('href',"#");
204 closelink.attr('role',"button");
204 closelink.attr('role',"button");
205 closelink.addClass('ui-button');
205 closelink.addClass('ui-button');
206 //closelink.addClass("ui-corner-all"); //rounded corner
206 //closelink.addClass("ui-corner-all"); //rounded corner
207 //closelink.adClass('ui-state-default'); // grey background and blue cross
207 //closelink.adClass('ui-state-default'); // grey background and blue cross
208 var closespan=$('<span/>').text('Close');
208 var closespan=$('<span/>').text('Close');
209 closespan.addClass('ui-icon');
209 closespan.addClass('ui-icon');
210 closespan.addClass('ui-icon-close');
210 closespan.addClass('ui-icon-close');
211 closelink.append(closespan);
211 closelink.append(closespan);
212 closelink.click(function(){
212 closelink.click(function(){
213 that.remove_and_cancel_tooltip();
213 that.remove_and_cancel_tooltip();
214 setTimeout(function(){that.code_mirror.focus();}, 50);
214 setTimeout(function(){that.code_mirror.focus();}, 50);
215 });
215 });
216 //construct the tooltip
216 //construct the tooltip
217 tooltip.append(closelink);
217 tooltip.append(closelink);
218 tooltip.append(expandlink);
218 tooltip.append(expandlink);
219 tooltip.append(morelink);
219 tooltip.append(morelink);
220 if(defstring){
220 if(defstring){
221 defstring_html = $('<pre/>').html(utils.fixConsole(defstring));
221 defstring_html = $('<pre/>').html(utils.fixConsole(defstring));
222 tooltip.append(defstring_html);
222 tooltip.append(defstring_html);
223 }
223 }
224 tooltip.append(pre);
224 tooltip.append(pre);
225 var pos = this.code_mirror.cursorCoords();
225 var pos = this.code_mirror.cursorCoords();
226 tooltip.css('left',pos.x+'px');
226 tooltip.css('left',pos.x+'px');
227 tooltip.css('top',pos.yBot+'px');
227 tooltip.css('top',pos.yBot+'px');
228 $('body').append(tooltip);
228 $('body').append(tooltip);
229
229
230 // issues with cross-closing if multiple tooltip in less than 5sec
230 // issues with cross-closing if multiple tooltip in less than 5sec
231 // keep it comented for now
231 // keep it comented for now
232 // setTimeout(that.remove_and_cancel_tooltip, 5000);
232 // setTimeout(that.remove_and_cancel_tooltip, 5000);
233 };
233 };
234
234
235 // As you type completer
235 // As you type completer
236 CodeCell.prototype.finish_completing = function (matched_text, matches) {
236 CodeCell.prototype.finish_completing = function (matched_text, matches) {
237 //return if not completing or nothing to complete
237 //return if not completing or nothing to complete
238 if (!this.is_completing || matches.length === 0) {return;}
238 if (!this.is_completing || matches.length === 0) {return;}
239
239
240 // for later readability
240 // for later readability
241 var key = { tab:9,
241 var key = { tab:9,
242 esc:27,
242 esc:27,
243 backspace:8,
243 backspace:8,
244 space:32,
244 space:32,
245 shift:16,
245 shift:16,
246 enter:13,
246 enter:13,
247 // _ is 95
247 // _ is 95
248 isCompSymbol : function (code)
248 isCompSymbol : function (code)
249 {
249 {
250 return (code > 64 && code <= 90)
250 return (code > 64 && code <= 90)
251 || (code >= 97 && code <= 122)
251 || (code >= 97 && code <= 122)
252 || (code == 95)
252 || (code == 95)
253 },
253 },
254 dismissAndAppend : function (code)
254 dismissAndAppend : function (code)
255 {
255 {
256 chararr = ['(',')','[',']','+','-','/','\\','.',' '];
256 chararr = ['(',')','[',']','+','-','/','\\','.',' '];
257 codearr = chararr.map(function(x){return x.charCodeAt(0)});
257 codearr = chararr.map(function(x){return x.charCodeAt(0)});
258 return jQuery.inArray(code, codearr) != -1;
258 return jQuery.inArray(code, codearr) != -1;
259 }
259 }
260
260
261 }
261 }
262
262
263 // smart completion, sort kwarg ending with '='
263 // smart completion, sort kwarg ending with '='
264 var newm = new Array();
264 var newm = new Array();
265 if(this.notebook.smart_completer)
265 if(this.notebook.smart_completer)
266 {
266 {
267 kwargs = new Array();
267 kwargs = new Array();
268 other = new Array();
268 other = new Array();
269 for(var i = 0 ; i<matches.length ; ++i){
269 for(var i = 0 ; i<matches.length ; ++i){
270 if(matches[i].substr(-1) === '='){
270 if(matches[i].substr(-1) === '='){
271 kwargs.push(matches[i]);
271 kwargs.push(matches[i]);
272 }else{other.push(matches[i]);}
272 }else{other.push(matches[i]);}
273 }
273 }
274 newm = kwargs.concat(other);
274 newm = kwargs.concat(other);
275 matches = newm;
275 matches = newm;
276 }
276 }
277 // end sort kwargs
277 // end sort kwargs
278
278
279 // give common prefix of a array of string
279 // give common prefix of a array of string
280 function sharedStart(A){
280 function sharedStart(A){
281 if(A.length == 1){return A[0]}
281 if(A.length > 1 ){
282 if(A.length > 1 ){
282 var tem1, tem2, s, A = A.slice(0).sort();
283 var tem1, tem2, s, A = A.slice(0).sort();
283 tem1 = A[0];
284 tem1 = A[0];
284 s = tem1.length;
285 s = tem1.length;
285 tem2 = A.pop();
286 tem2 = A.pop();
286 while(s && tem2.indexOf(tem1) == -1){
287 while(s && tem2.indexOf(tem1) == -1){
287 tem1 = tem1.substring(0, --s);
288 tem1 = tem1.substring(0, --s);
288 }
289 }
289 return tem1;
290 return tem1;
290 }
291 }
291 return "";
292 return "";
292 }
293 }
293
294
294
295
295 //try to check if the user is typing tab at least twice after a word
296 //try to check if the user is typing tab at least twice after a word
296 // and completion is "done"
297 // and completion is "done"
297 fallback_on_tooltip_after = 2
298 fallback_on_tooltip_after = 2
298 if(matches.length == 1 && matched_text === matches[0])
299 if(matches.length == 1 && matched_text === matches[0])
299 {
300 {
300 if(this.npressed >fallback_on_tooltip_after && this.prevmatch==matched_text)
301 if(this.npressed >fallback_on_tooltip_after && this.prevmatch==matched_text)
301 {
302 {
302 console.log('Ok, you really want to complete after pressing tab '+this.npressed+' times !');
303 console.log('Ok, you really want to complete after pressing tab '+this.npressed+' times !');
303 console.log('You should understand that there is no (more) completion for that !');
304 console.log('You should understand that there is no (more) completion for that !');
304 console.log("I'll show you the tooltip, will you stop bothering me ?");
305 console.log("I'll show you the tooltip, will you stop bothering me ?");
305 this.request_tooltip_after_time(matched_text+'(',0);
306 this.request_tooltip_after_time(matched_text+'(',0);
306 return;
307 return;
307 }
308 }
308 this.prevmatch = matched_text
309 this.prevmatch = matched_text
309 this.npressed = this.npressed+1;
310 this.npressed = this.npressed+1;
310 }
311 }
311 else
312 else
312 {
313 {
313 this.prevmatch = "";
314 this.prevmatch = "";
314 this.npressed = 0;
315 this.npressed = 0;
315 }
316 }
316 // end fallback on tooltip
317 // end fallback on tooltip
317 //==================================
318 //==================================
318 // Real completion logic start here
319 // Real completion logic start here
319 var that = this;
320 var that = this;
320 var cur = this.completion_cursor;
321 var cur = this.completion_cursor;
321 var done = false;
322 var done = false;
322
323
323 // call to dismmiss the completer
324 // call to dismmiss the completer
324 var close = function () {
325 var close = function () {
325 if (done) return;
326 if (done) return;
326 done = true;
327 done = true;
327 if (complete != undefined)
328 if (complete != undefined)
328 {complete.remove();}
329 {complete.remove();}
329 that.is_completing = false;
330 that.is_completing = false;
330 that.completion_cursor = null;
331 that.completion_cursor = null;
331 };
332 };
332
333
333 // update codemirror with the typed text
334 // update codemirror with the typed text
334 prev = matched_text
335 prev = matched_text
335 var update = function (inserted_text, event) {
336 var update = function (inserted_text, event) {
336 that.code_mirror.replaceRange(
337 that.code_mirror.replaceRange(
337 inserted_text,
338 inserted_text,
338 {line: cur.line, ch: (cur.ch-matched_text.length)},
339 {line: cur.line, ch: (cur.ch-matched_text.length)},
339 {line: cur.line, ch: (cur.ch+prev.length-matched_text.length)}
340 {line: cur.line, ch: (cur.ch+prev.length-matched_text.length)}
340 );
341 );
341 prev = inserted_text
342 prev = inserted_text
342 if(event != null){
343 if(event != null){
343 event.stopPropagation();
344 event.stopPropagation();
344 event.preventDefault();
345 event.preventDefault();
345 }
346 }
346 };
347 };
347 // insert the given text and exit the completer
348 // insert the given text and exit the completer
348 var insert = function (selected_text, event) {
349 var insert = function (selected_text, event) {
349 update(selected_text)
350 update(selected_text)
350 close();
351 close();
351 setTimeout(function(){that.code_mirror.focus();}, 50);
352 setTimeout(function(){that.code_mirror.focus();}, 50);
352 };
353 };
353
354
354 // insert the curent highlited selection and exit
355 // insert the curent highlited selection and exit
355 var pick = function () {
356 var pick = function () {
356 insert(select.val()[0],null);
357 insert(select.val()[0],null);
357 };
358 };
358
359
359
360
360 // Define function to clear the completer, refill it with the new
361 // Define function to clear the completer, refill it with the new
361 // matches, update the pseuso typing field. autopick insert match if
362 // matches, update the pseuso typing field. autopick insert match if
362 // only one left, in no matches (anymore) dismiss itself by pasting
363 // only one left, in no matches (anymore) dismiss itself by pasting
363 // what the user have typed until then
364 // what the user have typed until then
364 var complete_with = function(matches,typed_text,autopick,event)
365 var complete_with = function(matches,typed_text,autopick,event)
365 {
366 {
366 // If autopick an only one match, past.
367 // If autopick an only one match, past.
367 // Used to 'pick' when pressing tab
368 // Used to 'pick' when pressing tab
368 if (matches.length < 1) {
369 if (matches.length < 1) {
369 insert(typed_text,event);
370 insert(typed_text,event);
370 if(event != null){
371 if(event != null){
371 event.stopPropagation();
372 event.stopPropagation();
372 event.preventDefault();
373 event.preventDefault();
373 }
374 }
374 } else if (autopick && matches.length == 1) {
375 } else if (autopick && matches.length == 1) {
375 insert(matches[0],event);
376 insert(matches[0],event);
376 if(event != null){
377 if(event != null){
377 event.stopPropagation();
378 event.stopPropagation();
378 event.preventDefault();
379 event.preventDefault();
379 }
380 }
380 }
381 }
381 //clear the previous completion if any
382 //clear the previous completion if any
382 update(typed_text,event);
383 update(typed_text,event);
383 complete.children().children().remove();
384 complete.children().children().remove();
384 $('#asyoutype').html("<b>"+matched_text+"</b>"+typed_text.substr(matched_text.length));
385 $('#asyoutype').html("<b>"+matched_text+"</b>"+typed_text.substr(matched_text.length));
385 select = $('#asyoutypeselect');
386 select = $('#asyoutypeselect');
386 for (var i = 0; i<matches.length; ++i) {
387 for (var i = 0; i<matches.length; ++i) {
387 select.append($('<option/>').html(matches[i]));
388 select.append($('<option/>').html(matches[i]));
388 }
389 }
389 select.children().first().attr('selected','true');
390 select.children().first().attr('selected','true');
390 }
391 }
391
392
392 // create html for completer
393 // create html for completer
393 var complete = $('<div/>').addClass('completions');
394 var complete = $('<div/>').addClass('completions');
394 complete.attr('id','complete');
395 complete.attr('id','complete');
395 complete.append($('<p/>').attr('id', 'asyoutype').html('<b>fixed part</b>user part'));//pseudo input field
396 complete.append($('<p/>').attr('id', 'asyoutype').html('<b>fixed part</b>user part'));//pseudo input field
396
397
397 var select = $('<select/>').attr('multiple','true');
398 var select = $('<select/>').attr('multiple','true');
398 select.attr('id', 'asyoutypeselect')
399 select.attr('id', 'asyoutypeselect')
399 select.attr('size',Math.min(10,matches.length));
400 select.attr('size',Math.min(10,matches.length));
400 var pos = this.code_mirror.cursorCoords();
401 var pos = this.code_mirror.cursorCoords();
401
402
402 // TODO: I propose to remove enough horizontal pixel
403 // TODO: I propose to remove enough horizontal pixel
403 // to align the text later
404 // to align the text later
404 complete.css('left',pos.x+'px');
405 complete.css('left',pos.x+'px');
405 complete.css('top',pos.yBot+'px');
406 complete.css('top',pos.yBot+'px');
406 complete.append(select);
407 complete.append(select);
407
408
408 $('body').append(complete);
409 $('body').append(complete);
409
410
410 // So a first actual completion. see if all the completion start wit
411 // So a first actual completion. see if all the completion start wit
411 // the same letter and complete if necessary
412 // the same letter and complete if necessary
412 fastForward = sharedStart(matches)
413 fastForward = sharedStart(matches)
413 typed_characters = fastForward.substr(matched_text.length);
414 typed_characters = fastForward.substr(matched_text.length);
414 complete_with(matches,matched_text+typed_characters,true,null);
415 complete_with(matches,matched_text+typed_characters,true,null);
415 filterd = matches;
416 filterd = matches;
416 // Give focus to select, and make it filter the match as the user type
417 // Give focus to select, and make it filter the match as the user type
417 // by filtering the previous matches. Called by .keypress and .keydown
418 // by filtering the previous matches. Called by .keypress and .keydown
418 var downandpress = function (event,press_or_down) {
419 var downandpress = function (event,press_or_down) {
419 var code = event.which;
420 var code = event.which;
420 var autopick = false; // auto 'pick' if only one match
421 var autopick = false; // auto 'pick' if only one match
421 if (press_or_down === 0){
422 if (press_or_down === 0){
422 press = true; down = false; //Are we called from keypress or keydown
423 press = true; down = false; //Are we called from keypress or keydown
423 } else if (press_or_down == 1){
424 } else if (press_or_down == 1){
424 press = false; down = true;
425 press = false; down = true;
425 }
426 }
426 if (code === key.shift) {
427 if (code === key.shift) {
427 // nothing on Shift
428 // nothing on Shift
428 return;
429 return;
429 }
430 }
430 if (key.dismissAndAppend(code) && press) {
431 if (key.dismissAndAppend(code) && press) {
431 var newchar = String.fromCharCode(code);
432 var newchar = String.fromCharCode(code);
432 typed_characters = typed_characters+newchar;
433 typed_characters = typed_characters+newchar;
433 insert(matched_text+typed_characters,event);
434 insert(matched_text+typed_characters,event);
434 return
435 return
435 }
436 }
436 if (code === key.enter) {
437 if (code === key.enter) {
437 // Pressing ENTER will cause a pick
438 // Pressing ENTER will cause a pick
438 event.stopPropagation();
439 event.stopPropagation();
439 event.preventDefault();
440 event.preventDefault();
440 pick();
441 pick();
441 } else if (code === 38 || code === 40) {
442 } else if (code === 38 || code === 40) {
442 // We don't want the document keydown handler to handle UP/DOWN,
443 // We don't want the document keydown handler to handle UP/DOWN,
443 // but we want the default action.
444 // but we want the default action.
444 event.stopPropagation();
445 event.stopPropagation();
445 } else if ( (code == key.backspace)||(code == key.tab && down) || press || key.isCompSymbol(code)){
446 } else if ( (code == key.backspace)||(code == key.tab && down) || press || key.isCompSymbol(code)){
446 if( key.isCompSymbol(code) && press)
447 if( key.isCompSymbol(code) && press)
447 {
448 {
448 var newchar = String.fromCharCode(code);
449 var newchar = String.fromCharCode(code);
449 typed_characters = typed_characters+newchar;
450 typed_characters = typed_characters+newchar;
450 } else if (code == key.tab) {
451 } else if (code == key.tab) {
451 fastForward = sharedStart(filterd)
452 fastForward = sharedStart(filterd)
452 ffsub = fastForward.substr(matched_text.length+typed_characters.length);
453 ffsub = fastForward.substr(matched_text.length+typed_characters.length);
453 typed_characters = typed_characters+ffsub;
454 typed_characters = typed_characters+ffsub;
454 autopick = true;
455 autopick = true;
455 } else if (code == key.backspace && down) {
456 } else if (code == key.backspace && down) {
456 // cancel if user have erase everything, otherwise decrease
457 // cancel if user have erase everything, otherwise decrease
457 // what we filter with
458 // what we filter with
458 event.preventDefault();
459 event.preventDefault();
459 if (typed_characters.length <= 0)
460 if (typed_characters.length <= 0)
460 {
461 {
461 insert(matched_text,event)
462 insert(matched_text,event)
462 return
463 return
463 }
464 }
464 typed_characters = typed_characters.substr(0,typed_characters.length-1);
465 typed_characters = typed_characters.substr(0,typed_characters.length-1);
465 } else if (press && code != key.backspace && code != key.tab && code != 0){
466 } else if (press && code != key.backspace && code != key.tab && code != 0){
466 insert(matched_text+typed_characters,event);
467 insert(matched_text+typed_characters,event);
467 return
468 return
468 } else {
469 } else {
469 return
470 return
470 }
471 }
471 re = new RegExp("^"+"\%?"+matched_text+typed_characters,"");
472 re = new RegExp("^"+"\%?"+matched_text+typed_characters,"");
472 filterd = matches.filter(function(x){return re.test(x)});
473 filterd = matches.filter(function(x){return re.test(x)});
473 complete_with(filterd,matched_text+typed_characters,autopick,event);
474 complete_with(filterd,matched_text+typed_characters,autopick,event);
474 } else if( code == key.esc) {
475 } else if( code == key.esc) {
475 // dismiss the completer and go back to before invoking it
476 // dismiss the completer and go back to before invoking it
476 insert(matched_text,event);
477 insert(matched_text,event);
477 } else if( press ){ // abort only on .keypress or esc
478 } else if( press ){ // abort only on .keypress or esc
478 // abort with what the user have pressed until now
479 // abort with what the user have pressed until now
479 console.log('aborting with keycode : '+code+' is down :'+down);
480 console.log('aborting with keycode : '+code+' is down :'+down);
480 }
481 }
481 }
482 }
482 select.keydown(function (event) {
483 select.keydown(function (event) {
483 downandpress(event,1)
484 downandpress(event,1)
484 });
485 });
485 select.keypress(function (event) {
486 select.keypress(function (event) {
486 downandpress(event,0)
487 downandpress(event,0)
487 });
488 });
488 // Double click also causes a pick.
489 // Double click also causes a pick.
489 // and bind the last actions.
490 // and bind the last actions.
490 select.dblclick(pick);
491 select.dblclick(pick);
491 select.blur(close);
492 select.blur(close);
492 select.focus();
493 select.focus();
493 };
494 };
494
495
495 CodeCell.prototype.toggle_line_numbers = function () {
496 CodeCell.prototype.toggle_line_numbers = function () {
496 if (this.code_mirror.getOption('lineNumbers') == false) {
497 if (this.code_mirror.getOption('lineNumbers') == false) {
497 this.code_mirror.setOption('lineNumbers', true);
498 this.code_mirror.setOption('lineNumbers', true);
498 } else {
499 } else {
499 this.code_mirror.setOption('lineNumbers', false);
500 this.code_mirror.setOption('lineNumbers', false);
500 }
501 }
501 this.code_mirror.refresh();
502 this.code_mirror.refresh();
502 };
503 };
503
504
504 CodeCell.prototype.select = function () {
505 CodeCell.prototype.select = function () {
505 IPython.Cell.prototype.select.apply(this);
506 IPython.Cell.prototype.select.apply(this);
506 // Todo: this dance is needed because as of CodeMirror 2.12, focus is
507 // Todo: this dance is needed because as of CodeMirror 2.12, focus is
507 // not causing the cursor to blink if the editor is empty initially.
508 // not causing the cursor to blink if the editor is empty initially.
508 // While this seems to fix the issue, this should be fixed
509 // While this seems to fix the issue, this should be fixed
509 // in CodeMirror proper.
510 // in CodeMirror proper.
510 var s = this.code_mirror.getValue();
511 var s = this.code_mirror.getValue();
511 this.code_mirror.focus();
512 this.code_mirror.focus();
512 if (s === '') this.code_mirror.setValue('');
513 if (s === '') this.code_mirror.setValue('');
513 };
514 };
514
515
515
516
516 CodeCell.prototype.select_all = function () {
517 CodeCell.prototype.select_all = function () {
517 var start = {line: 0, ch: 0};
518 var start = {line: 0, ch: 0};
518 var nlines = this.code_mirror.lineCount();
519 var nlines = this.code_mirror.lineCount();
519 var last_line = this.code_mirror.getLine(nlines-1);
520 var last_line = this.code_mirror.getLine(nlines-1);
520 var end = {line: nlines-1, ch: last_line.length};
521 var end = {line: nlines-1, ch: last_line.length};
521 this.code_mirror.setSelection(start, end);
522 this.code_mirror.setSelection(start, end);
522 };
523 };
523
524
524
525
525 CodeCell.prototype.append_output = function (json) {
526 CodeCell.prototype.append_output = function (json) {
526 this.expand();
527 this.expand();
527 if (json.output_type === 'pyout') {
528 if (json.output_type === 'pyout') {
528 this.append_pyout(json);
529 this.append_pyout(json);
529 } else if (json.output_type === 'pyerr') {
530 } else if (json.output_type === 'pyerr') {
530 this.append_pyerr(json);
531 this.append_pyerr(json);
531 } else if (json.output_type === 'display_data') {
532 } else if (json.output_type === 'display_data') {
532 this.append_display_data(json);
533 this.append_display_data(json);
533 } else if (json.output_type === 'stream') {
534 } else if (json.output_type === 'stream') {
534 this.append_stream(json);
535 this.append_stream(json);
535 };
536 };
536 this.outputs.push(json);
537 this.outputs.push(json);
537 };
538 };
538
539
539
540
540 CodeCell.prototype.create_output_area = function () {
541 CodeCell.prototype.create_output_area = function () {
541 var oa = $("<div/>").addClass("hbox output_area");
542 var oa = $("<div/>").addClass("hbox output_area");
542 oa.append($('<div/>').addClass('prompt'));
543 oa.append($('<div/>').addClass('prompt'));
543 return oa;
544 return oa;
544 };
545 };
545
546
546
547
547 CodeCell.prototype.append_pyout = function (json) {
548 CodeCell.prototype.append_pyout = function (json) {
548 n = json.prompt_number || ' ';
549 n = json.prompt_number || ' ';
549 var toinsert = this.create_output_area();
550 var toinsert = this.create_output_area();
550 toinsert.find('div.prompt').addClass('output_prompt').html('Out[' + n + ']:');
551 toinsert.find('div.prompt').addClass('output_prompt').html('Out[' + n + ']:');
551 this.append_mime_type(json, toinsert);
552 this.append_mime_type(json, toinsert);
552 this.element.find('div.output').append(toinsert);
553 this.element.find('div.output').append(toinsert);
553 // If we just output latex, typeset it.
554 // If we just output latex, typeset it.
554 if ((json.latex !== undefined) || (json.html !== undefined)) {
555 if ((json.latex !== undefined) || (json.html !== undefined)) {
555 this.typeset();
556 this.typeset();
556 };
557 };
557 };
558 };
558
559
559
560
560 CodeCell.prototype.append_pyerr = function (json) {
561 CodeCell.prototype.append_pyerr = function (json) {
561 var tb = json.traceback;
562 var tb = json.traceback;
562 if (tb !== undefined && tb.length > 0) {
563 if (tb !== undefined && tb.length > 0) {
563 var s = '';
564 var s = '';
564 var len = tb.length;
565 var len = tb.length;
565 for (var i=0; i<len; i++) {
566 for (var i=0; i<len; i++) {
566 s = s + tb[i] + '\n';
567 s = s + tb[i] + '\n';
567 }
568 }
568 s = s + '\n';
569 s = s + '\n';
569 var toinsert = this.create_output_area();
570 var toinsert = this.create_output_area();
570 this.append_text(s, toinsert);
571 this.append_text(s, toinsert);
571 this.element.find('div.output').append(toinsert);
572 this.element.find('div.output').append(toinsert);
572 };
573 };
573 };
574 };
574
575
575
576
576 CodeCell.prototype.append_stream = function (json) {
577 CodeCell.prototype.append_stream = function (json) {
577 // temporary fix: if stream undefined (json file written prior to this patch),
578 // temporary fix: if stream undefined (json file written prior to this patch),
578 // default to most likely stdout:
579 // default to most likely stdout:
579 if (json.stream == undefined){
580 if (json.stream == undefined){
580 json.stream = 'stdout';
581 json.stream = 'stdout';
581 }
582 }
582 var subclass = "output_"+json.stream;
583 var subclass = "output_"+json.stream;
583 if (this.outputs.length > 0){
584 if (this.outputs.length > 0){
584 // have at least one output to consider
585 // have at least one output to consider
585 var last = this.outputs[this.outputs.length-1];
586 var last = this.outputs[this.outputs.length-1];
586 if (last.output_type == 'stream' && json.stream == last.stream){
587 if (last.output_type == 'stream' && json.stream == last.stream){
587 // latest output was in the same stream,
588 // latest output was in the same stream,
588 // so append directly into its pre tag
589 // so append directly into its pre tag
589 this.element.find('div.'+subclass).last().find('pre').append(json.text);
590 this.element.find('div.'+subclass).last().find('pre').append(json.text);
590 return;
591 return;
591 }
592 }
592 }
593 }
593
594
594 // If we got here, attach a new div
595 // If we got here, attach a new div
595 var toinsert = this.create_output_area();
596 var toinsert = this.create_output_area();
596 this.append_text(json.text, toinsert, "output_stream "+subclass);
597 this.append_text(json.text, toinsert, "output_stream "+subclass);
597 this.element.find('div.output').append(toinsert);
598 this.element.find('div.output').append(toinsert);
598 };
599 };
599
600
600
601
601 CodeCell.prototype.append_display_data = function (json) {
602 CodeCell.prototype.append_display_data = function (json) {
602 var toinsert = this.create_output_area();
603 var toinsert = this.create_output_area();
603 this.append_mime_type(json, toinsert);
604 this.append_mime_type(json, toinsert);
604 this.element.find('div.output').append(toinsert);
605 this.element.find('div.output').append(toinsert);
605 // If we just output latex, typeset it.
606 // If we just output latex, typeset it.
606 if ( (json.latex !== undefined) || (json.html !== undefined) ) {
607 if ( (json.latex !== undefined) || (json.html !== undefined) ) {
607 this.typeset();
608 this.typeset();
608 };
609 };
609 };
610 };
610
611
611
612
612 CodeCell.prototype.append_mime_type = function (json, element) {
613 CodeCell.prototype.append_mime_type = function (json, element) {
613 if (json.html !== undefined) {
614 if (json.html !== undefined) {
614 this.append_html(json.html, element);
615 this.append_html(json.html, element);
615 } else if (json.latex !== undefined) {
616 } else if (json.latex !== undefined) {
616 this.append_latex(json.latex, element);
617 this.append_latex(json.latex, element);
617 } else if (json.svg !== undefined) {
618 } else if (json.svg !== undefined) {
618 this.append_svg(json.svg, element);
619 this.append_svg(json.svg, element);
619 } else if (json.png !== undefined) {
620 } else if (json.png !== undefined) {
620 this.append_png(json.png, element);
621 this.append_png(json.png, element);
621 } else if (json.jpeg !== undefined) {
622 } else if (json.jpeg !== undefined) {
622 this.append_jpeg(json.jpeg, element);
623 this.append_jpeg(json.jpeg, element);
623 } else if (json.text !== undefined) {
624 } else if (json.text !== undefined) {
624 this.append_text(json.text, element);
625 this.append_text(json.text, element);
625 };
626 };
626 };
627 };
627
628
628
629
629 CodeCell.prototype.append_html = function (html, element) {
630 CodeCell.prototype.append_html = function (html, element) {
630 var toinsert = $("<div/>").addClass("box_flex1 output_subarea output_html rendered_html");
631 var toinsert = $("<div/>").addClass("box_flex1 output_subarea output_html rendered_html");
631 toinsert.append(html);
632 toinsert.append(html);
632 element.append(toinsert);
633 element.append(toinsert);
633 };
634 };
634
635
635
636
636 CodeCell.prototype.append_text = function (data, element, extra_class) {
637 CodeCell.prototype.append_text = function (data, element, extra_class) {
637 var toinsert = $("<div/>").addClass("box_flex1 output_subarea output_text");
638 var toinsert = $("<div/>").addClass("box_flex1 output_subarea output_text");
638 if (extra_class){
639 if (extra_class){
639 toinsert.addClass(extra_class);
640 toinsert.addClass(extra_class);
640 }
641 }
641 toinsert.append($("<pre/>").html(data));
642 toinsert.append($("<pre/>").html(data));
642 element.append(toinsert);
643 element.append(toinsert);
643 };
644 };
644
645
645
646
646 CodeCell.prototype.append_svg = function (svg, element) {
647 CodeCell.prototype.append_svg = function (svg, element) {
647 var toinsert = $("<div/>").addClass("box_flex1 output_subarea output_svg");
648 var toinsert = $("<div/>").addClass("box_flex1 output_subarea output_svg");
648 toinsert.append(svg);
649 toinsert.append(svg);
649 element.append(toinsert);
650 element.append(toinsert);
650 };
651 };
651
652
652
653
653 CodeCell.prototype.append_png = function (png, element) {
654 CodeCell.prototype.append_png = function (png, element) {
654 var toinsert = $("<div/>").addClass("box_flex1 output_subarea output_png");
655 var toinsert = $("<div/>").addClass("box_flex1 output_subarea output_png");
655 toinsert.append($("<img/>").attr('src','data:image/png;base64,'+png));
656 toinsert.append($("<img/>").attr('src','data:image/png;base64,'+png));
656 element.append(toinsert);
657 element.append(toinsert);
657 };
658 };
658
659
659
660
660 CodeCell.prototype.append_jpeg = function (jpeg, element) {
661 CodeCell.prototype.append_jpeg = function (jpeg, element) {
661 var toinsert = $("<div/>").addClass("box_flex1 output_subarea output_jpeg");
662 var toinsert = $("<div/>").addClass("box_flex1 output_subarea output_jpeg");
662 toinsert.append($("<img/>").attr('src','data:image/jpeg;base64,'+jpeg));
663 toinsert.append($("<img/>").attr('src','data:image/jpeg;base64,'+jpeg));
663 element.append(toinsert);
664 element.append(toinsert);
664 };
665 };
665
666
666
667
667 CodeCell.prototype.append_latex = function (latex, element) {
668 CodeCell.prototype.append_latex = function (latex, element) {
668 // This method cannot do the typesetting because the latex first has to
669 // This method cannot do the typesetting because the latex first has to
669 // be on the page.
670 // be on the page.
670 var toinsert = $("<div/>").addClass("box_flex1 output_subarea output_latex");
671 var toinsert = $("<div/>").addClass("box_flex1 output_subarea output_latex");
671 toinsert.append(latex);
672 toinsert.append(latex);
672 element.append(toinsert);
673 element.append(toinsert);
673 };
674 };
674
675
675
676
676 CodeCell.prototype.clear_output = function (stdout, stderr, other) {
677 CodeCell.prototype.clear_output = function (stdout, stderr, other) {
677 var output_div = this.element.find("div.output");
678 var output_div = this.element.find("div.output");
678 if (stdout && stderr && other){
679 if (stdout && stderr && other){
679 // clear all, no need for logic
680 // clear all, no need for logic
680 output_div.html("");
681 output_div.html("");
681 this.outputs = [];
682 this.outputs = [];
682 return;
683 return;
683 }
684 }
684 // remove html output
685 // remove html output
685 // each output_subarea that has an identifying class is in an output_area
686 // each output_subarea that has an identifying class is in an output_area
686 // which is the element to be removed.
687 // which is the element to be removed.
687 if (stdout){
688 if (stdout){
688 output_div.find("div.output_stdout").parent().remove();
689 output_div.find("div.output_stdout").parent().remove();
689 }
690 }
690 if (stderr){
691 if (stderr){
691 output_div.find("div.output_stderr").parent().remove();
692 output_div.find("div.output_stderr").parent().remove();
692 }
693 }
693 if (other){
694 if (other){
694 output_div.find("div.output_subarea").not("div.output_stderr").not("div.output_stdout").parent().remove();
695 output_div.find("div.output_subarea").not("div.output_stderr").not("div.output_stdout").parent().remove();
695 }
696 }
696
697
697 // remove cleared outputs from JSON list:
698 // remove cleared outputs from JSON list:
698 for (var i = this.outputs.length - 1; i >= 0; i--){
699 for (var i = this.outputs.length - 1; i >= 0; i--){
699 var out = this.outputs[i];
700 var out = this.outputs[i];
700 var output_type = out.output_type;
701 var output_type = out.output_type;
701 if (output_type == "display_data" && other){
702 if (output_type == "display_data" && other){
702 this.outputs.splice(i,1);
703 this.outputs.splice(i,1);
703 }else if (output_type == "stream"){
704 }else if (output_type == "stream"){
704 if (stdout && out.stream == "stdout"){
705 if (stdout && out.stream == "stdout"){
705 this.outputs.splice(i,1);
706 this.outputs.splice(i,1);
706 }else if (stderr && out.stream == "stderr"){
707 }else if (stderr && out.stream == "stderr"){
707 this.outputs.splice(i,1);
708 this.outputs.splice(i,1);
708 }
709 }
709 }
710 }
710 }
711 }
711 };
712 };
712
713
713
714
714 CodeCell.prototype.clear_input = function () {
715 CodeCell.prototype.clear_input = function () {
715 this.code_mirror.setValue('');
716 this.code_mirror.setValue('');
716 };
717 };
717
718
718
719
719 CodeCell.prototype.collapse = function () {
720 CodeCell.prototype.collapse = function () {
720 if (!this.collapsed) {
721 if (!this.collapsed) {
721 this.element.find('div.output').hide();
722 this.element.find('div.output').hide();
722 this.collapsed = true;
723 this.collapsed = true;
723 };
724 };
724 };
725 };
725
726
726
727
727 CodeCell.prototype.expand = function () {
728 CodeCell.prototype.expand = function () {
728 if (this.collapsed) {
729 if (this.collapsed) {
729 this.element.find('div.output').show();
730 this.element.find('div.output').show();
730 this.collapsed = false;
731 this.collapsed = false;
731 };
732 };
732 };
733 };
733
734
734
735
735 CodeCell.prototype.toggle_output = function () {
736 CodeCell.prototype.toggle_output = function () {
736 if (this.collapsed) {
737 if (this.collapsed) {
737 this.expand();
738 this.expand();
738 } else {
739 } else {
739 this.collapse();
740 this.collapse();
740 };
741 };
741 };
742 };
742
743
743 CodeCell.prototype.set_input_prompt = function (number) {
744 CodeCell.prototype.set_input_prompt = function (number) {
744 var n = number || '&nbsp;';
745 var n = number || '&nbsp;';
745 this.input_prompt_number = n;
746 this.input_prompt_number = n;
746 this.element.find('div.input_prompt').html('In&nbsp;[' + n + ']:');
747 this.element.find('div.input_prompt').html('In&nbsp;[' + n + ']:');
747 };
748 };
748
749
749
750
750 CodeCell.prototype.get_code = function () {
751 CodeCell.prototype.get_code = function () {
751 return this.code_mirror.getValue();
752 return this.code_mirror.getValue();
752 };
753 };
753
754
754
755
755 CodeCell.prototype.set_code = function (code) {
756 CodeCell.prototype.set_code = function (code) {
756 return this.code_mirror.setValue(code);
757 return this.code_mirror.setValue(code);
757 };
758 };
758
759
759
760
760 CodeCell.prototype.at_top = function () {
761 CodeCell.prototype.at_top = function () {
761 var cursor = this.code_mirror.getCursor();
762 var cursor = this.code_mirror.getCursor();
762 if (cursor.line === 0) {
763 if (cursor.line === 0) {
763 return true;
764 return true;
764 } else {
765 } else {
765 return false;
766 return false;
766 }
767 }
767 };
768 };
768
769
769
770
770 CodeCell.prototype.at_bottom = function () {
771 CodeCell.prototype.at_bottom = function () {
771 var cursor = this.code_mirror.getCursor();
772 var cursor = this.code_mirror.getCursor();
772 if (cursor.line === (this.code_mirror.lineCount()-1)) {
773 if (cursor.line === (this.code_mirror.lineCount()-1)) {
773 return true;
774 return true;
774 } else {
775 } else {
775 return false;
776 return false;
776 }
777 }
777 };
778 };
778
779
779
780
780 CodeCell.prototype.fromJSON = function (data) {
781 CodeCell.prototype.fromJSON = function (data) {
781 console.log('Import from JSON:', data);
782 console.log('Import from JSON:', data);
782 if (data.cell_type === 'code') {
783 if (data.cell_type === 'code') {
783 if (data.input !== undefined) {
784 if (data.input !== undefined) {
784 this.set_code(data.input);
785 this.set_code(data.input);
785 }
786 }
786 if (data.prompt_number !== undefined) {
787 if (data.prompt_number !== undefined) {
787 this.set_input_prompt(data.prompt_number);
788 this.set_input_prompt(data.prompt_number);
788 } else {
789 } else {
789 this.set_input_prompt();
790 this.set_input_prompt();
790 };
791 };
791 var len = data.outputs.length;
792 var len = data.outputs.length;
792 for (var i=0; i<len; i++) {
793 for (var i=0; i<len; i++) {
793 this.append_output(data.outputs[i]);
794 this.append_output(data.outputs[i]);
794 };
795 };
795 if (data.collapsed !== undefined) {
796 if (data.collapsed !== undefined) {
796 if (data.collapsed) {
797 if (data.collapsed) {
797 this.collapse();
798 this.collapse();
798 };
799 };
799 };
800 };
800 };
801 };
801 };
802 };
802
803
803
804
804 CodeCell.prototype.toJSON = function () {
805 CodeCell.prototype.toJSON = function () {
805 var data = {};
806 var data = {};
806 data.input = this.get_code();
807 data.input = this.get_code();
807 data.cell_type = 'code';
808 data.cell_type = 'code';
808 if (this.input_prompt_number !== ' ') {
809 if (this.input_prompt_number !== ' ') {
809 data.prompt_number = this.input_prompt_number;
810 data.prompt_number = this.input_prompt_number;
810 };
811 };
811 var outputs = [];
812 var outputs = [];
812 var len = this.outputs.length;
813 var len = this.outputs.length;
813 for (var i=0; i<len; i++) {
814 for (var i=0; i<len; i++) {
814 outputs[i] = this.outputs[i];
815 outputs[i] = this.outputs[i];
815 };
816 };
816 data.outputs = outputs;
817 data.outputs = outputs;
817 data.language = 'python';
818 data.language = 'python';
818 data.collapsed = this.collapsed;
819 data.collapsed = this.collapsed;
819 // console.log('Export to JSON:',data);
820 // console.log('Export to JSON:',data);
820 return data;
821 return data;
821 };
822 };
822
823
823
824
824 IPython.CodeCell = CodeCell;
825 IPython.CodeCell = CodeCell;
825
826
826 return IPython;
827 return IPython;
827 }(IPython));
828 }(IPython));
General Comments 0
You need to be logged in to leave comments. Login now