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