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