Show More
@@ -1,733 +1,737 | |||||
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' && this.tooltip_timeout != null){ | |
77 | CodeCell.prototype.remove_and_cancell_tooltip(that.tooltip_timeout); |
|
77 | CodeCell.prototype.remove_and_cancell_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_cancell_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 | clearTimeout(timeout); | |
154 | $('#tooltip').remove(); |
|
154 | $('#tooltip').remove(); | |
155 | } |
|
155 | } | |
156 |
|
156 | |||
157 | CodeCell.prototype.finish_tooltip = function (reply) { |
|
157 | CodeCell.prototype.finish_tooltip = function (reply) { | |
158 | defstring=reply.definition; |
|
158 | defstring=reply.definition; | |
159 | docstring=reply.docstring; |
|
159 | docstring=reply.docstring; | |
160 | if(docstring == null){docstring="<empty docstring>"}; |
|
160 | if(docstring == null){docstring="<empty docstring>"}; | |
161 | name=reply.name; |
|
161 | name=reply.name; | |
162 |
|
162 | |||
163 | var that = this; |
|
163 | var that = this; | |
164 | var tooltip = $('<div/>').attr('id', 'tooltip').addClass('tooltip'); |
|
164 | var tooltip = $('<div/>').attr('id', 'tooltip').addClass('tooltip'); | |
165 | // remove to have the tooltip not Limited in X and Y |
|
165 | // remove to have the tooltip not Limited in X and Y | |
166 | tooltip.addClass('smalltooltip'); |
|
166 | tooltip.addClass('smalltooltip'); | |
167 | var pre=$('<pre/>').html(utils.fixConsole(docstring)); |
|
167 | var pre=$('<pre/>').html(utils.fixConsole(docstring)); | |
168 | var expandlink=$('<a/>').attr('href',"#"); |
|
168 | var expandlink=$('<a/>').attr('href',"#"); | |
169 | expandlink.addClass("ui-corner-all"); //rounded corner |
|
169 | expandlink.addClass("ui-corner-all"); //rounded corner | |
170 | expandlink.attr('role',"button"); |
|
170 | expandlink.attr('role',"button"); | |
171 | //expandlink.addClass('ui-button'); |
|
171 | //expandlink.addClass('ui-button'); | |
172 | //expandlink.addClass('ui-state-default'); |
|
172 | //expandlink.addClass('ui-state-default'); | |
173 | var expandspan=$('<span/>').text('Expand'); |
|
173 | var expandspan=$('<span/>').text('Expand'); | |
174 | expandspan.addClass('ui-icon'); |
|
174 | expandspan.addClass('ui-icon'); | |
175 | expandspan.addClass('ui-icon-plus'); |
|
175 | expandspan.addClass('ui-icon-plus'); | |
176 | expandlink.append(expandspan); |
|
176 | expandlink.append(expandspan); | |
177 | expandlink.attr('id','expanbutton'); |
|
177 | expandlink.attr('id','expanbutton'); | |
178 | expandlink.click(function(){ |
|
178 | expandlink.click(function(){ | |
179 | tooltip.removeClass('smalltooltip'); |
|
179 | tooltip.removeClass('smalltooltip'); | |
180 | tooltip.addClass('bigtooltip'); |
|
180 | tooltip.addClass('bigtooltip'); | |
181 | $('#expanbutton').remove(); |
|
181 | $('#expanbutton').remove(); | |
182 | setTimeout(function(){that.code_mirror.focus();}, 50); |
|
182 | setTimeout(function(){that.code_mirror.focus();}, 50); | |
183 | }); |
|
183 | }); | |
184 | var morelink=$('<a/>').attr('href',"#"); |
|
184 | var morelink=$('<a/>').attr('href',"#"); | |
185 | morelink.attr('role',"button"); |
|
185 | morelink.attr('role',"button"); | |
186 | morelink.addClass('ui-button'); |
|
186 | morelink.addClass('ui-button'); | |
187 | //morelink.addClass("ui-corner-all"); //rounded corner |
|
187 | //morelink.addClass("ui-corner-all"); //rounded corner | |
188 | //morelink.addClass('ui-state-default'); |
|
188 | //morelink.addClass('ui-state-default'); | |
189 | var morespan=$('<span/>').text('Open in Pager'); |
|
189 | var morespan=$('<span/>').text('Open in Pager'); | |
190 | morespan.addClass('ui-icon'); |
|
190 | morespan.addClass('ui-icon'); | |
191 | morespan.addClass('ui-icon-arrowstop-l-n'); |
|
191 | morespan.addClass('ui-icon-arrowstop-l-n'); | |
192 | morelink.append(morespan); |
|
192 | morelink.append(morespan); | |
193 | morelink.click(function(){ |
|
193 | morelink.click(function(){ | |
194 | var msg_id = IPython.notebook.kernel.execute(name+"?"); |
|
194 | var msg_id = IPython.notebook.kernel.execute(name+"?"); | |
195 | IPython.notebook.msg_cell_map[msg_id] = IPython.notebook.selected_cell().cell_id; |
|
195 | IPython.notebook.msg_cell_map[msg_id] = IPython.notebook.selected_cell().cell_id; | |
196 | CodeCell.prototype.remove_and_cancell_tooltip(that.tooltip_timeout); |
|
196 | CodeCell.prototype.remove_and_cancell_tooltip(that.tooltip_timeout); | |
197 | setTimeout(function(){that.code_mirror.focus();}, 50); |
|
197 | setTimeout(function(){that.code_mirror.focus();}, 50); | |
198 | }); |
|
198 | }); | |
199 |
|
199 | |||
200 | var closelink=$('<a/>').attr('href',"#"); |
|
200 | var closelink=$('<a/>').attr('href',"#"); | |
201 | closelink.attr('role',"button"); |
|
201 | closelink.attr('role',"button"); | |
202 | closelink.addClass('ui-button'); |
|
202 | closelink.addClass('ui-button'); | |
203 | //closelink.addClass("ui-corner-all"); //rounded corner |
|
203 | //closelink.addClass("ui-corner-all"); //rounded corner | |
204 | //closelink.adClass('ui-state-default'); // grey background and blue cross |
|
204 | //closelink.adClass('ui-state-default'); // grey background and blue cross | |
205 | var closespan=$('<span/>').text('Close'); |
|
205 | var closespan=$('<span/>').text('Close'); | |
206 | closespan.addClass('ui-icon'); |
|
206 | closespan.addClass('ui-icon'); | |
207 | closespan.addClass('ui-icon-close'); |
|
207 | closespan.addClass('ui-icon-close'); | |
208 | closelink.append(closespan); |
|
208 | closelink.append(closespan); | |
209 | closelink.click(function(){ |
|
209 | closelink.click(function(){ | |
210 | CodeCell.prototype.remove_and_cancell_tooltip(that.tooltip_timeout); |
|
210 | CodeCell.prototype.remove_and_cancell_tooltip(that.tooltip_timeout); | |
211 | setTimeout(function(){that.code_mirror.focus();}, 50); |
|
211 | setTimeout(function(){that.code_mirror.focus();}, 50); | |
212 | }); |
|
212 | }); | |
213 | //construct the tooltip |
|
213 | //construct the tooltip | |
214 | tooltip.append(closelink); |
|
214 | tooltip.append(closelink); | |
215 | tooltip.append(expandlink); |
|
215 | tooltip.append(expandlink); | |
216 | tooltip.append(morelink); |
|
216 | tooltip.append(morelink); | |
217 | if(defstring){ |
|
217 | if(defstring){ | |
218 | defstring_html= $('<pre/>').html(utils.fixConsole(defstring)); |
|
218 | defstring_html= $('<pre/>').html(utils.fixConsole(defstring)); | |
219 | tooltip.append(defstring_html); |
|
219 | tooltip.append(defstring_html); | |
220 | } |
|
220 | } | |
221 | tooltip.append(pre); |
|
221 | tooltip.append(pre); | |
222 | var pos = this.code_mirror.cursorCoords(); |
|
222 | var pos = this.code_mirror.cursorCoords(); | |
223 | tooltip.css('left',pos.x+'px'); |
|
223 | tooltip.css('left',pos.x+'px'); | |
224 | tooltip.css('top',pos.yBot+'px'); |
|
224 | tooltip.css('top',pos.yBot+'px'); | |
225 | $('body').append(tooltip); |
|
225 | $('body').append(tooltip); | |
226 |
|
226 | |||
227 | // issues with cross-closing if multiple tooltip in less than 5sec |
|
227 | // issues with cross-closing if multiple tooltip in less than 5sec | |
228 | // keep it comented for now |
|
228 | // keep it comented for now | |
229 | // setTimeout(CodeCell.prototype.remove_and_cancell_tooltip, 5000); |
|
229 | // setTimeout(CodeCell.prototype.remove_and_cancell_tooltip, 5000); | |
230 | }; |
|
230 | }; | |
231 |
|
231 | |||
232 | // As you type completer |
|
232 | // As you type completer | |
233 | CodeCell.prototype.finish_completing = function (matched_text, matches) { |
|
233 | CodeCell.prototype.finish_completing = function (matched_text, matches) { | |
234 |
|
234 | |||
235 | // smart completion, sort kwarg ending with '=' |
|
235 | // smart completion, sort kwarg ending with '=' | |
236 | var newm = new Array(); |
|
236 | var newm = new Array(); | |
237 | if(this.notebook.smart_completer) |
|
237 | if(this.notebook.smart_completer) | |
238 | { |
|
238 | { | |
239 | kwargs = new Array(); |
|
239 | kwargs = new Array(); | |
240 | other = new Array(); |
|
240 | other = new Array(); | |
241 | for(var i=0;i<matches.length; ++i){ |
|
241 | for(var i=0;i<matches.length; ++i){ | |
242 | if(matches[i].substr(-1) === '='){ |
|
242 | if(matches[i].substr(-1) === '='){ | |
243 | kwargs.push(matches[i]); |
|
243 | kwargs.push(matches[i]); | |
244 | }else{other.push(matches[i]);} |
|
244 | }else{other.push(matches[i]);} | |
245 | } |
|
245 | } | |
246 | newm = kwargs.concat(other); |
|
246 | newm = kwargs.concat(other); | |
247 | matches=newm; |
|
247 | matches=newm; | |
248 | } |
|
248 | } | |
249 | // end sort kwargs |
|
249 | // end sort kwargs | |
250 |
|
250 | |||
251 | if (!this.is_completing || matches.length === 0) {return;} |
|
251 | if (!this.is_completing || matches.length === 0) {return;} | |
252 |
|
252 | |||
253 | //try to check if the user is typing tab at least twice after a word |
|
253 | //try to check if the user is typing tab at least twice after a word | |
254 | // and completion is "done" |
|
254 | // and completion is "done" | |
255 | fallback_on_tooltip_after=2 |
|
255 | fallback_on_tooltip_after=2 | |
256 | if(matches.length==1 && matched_text === matches[0]) |
|
256 | if(matches.length==1 && matched_text === matches[0]) | |
257 | { |
|
257 | { | |
258 | if(this.npressed >fallback_on_tooltip_after && this.prevmatch==matched_text) |
|
258 | if(this.npressed >fallback_on_tooltip_after && this.prevmatch==matched_text) | |
259 | { |
|
259 | { | |
260 | console.log('Ok, you really want to complete after pressing tab '+this.npressed+' times !'); |
|
260 | console.log('Ok, you really want to complete after pressing tab '+this.npressed+' times !'); | |
261 | console.log('You should understand that there is no (more) completion for that !'); |
|
261 | console.log('You should understand that there is no (more) completion for that !'); | |
262 | console.log("I'll show you the tooltip, will you stop bothering me ?"); |
|
262 | console.log("I'll show you the tooltip, will you stop bothering me ?"); | |
263 | this.request_tooltip_after_time(matched_text+'(',0,this); |
|
263 | this.request_tooltip_after_time(matched_text+'(',0,this); | |
264 | return; |
|
264 | return; | |
265 | } |
|
265 | } | |
266 | this.prevmatch=matched_text |
|
266 | this.prevmatch=matched_text | |
267 | this.npressed=this.npressed+1; |
|
267 | this.npressed=this.npressed+1; | |
268 | } |
|
268 | } | |
269 | else |
|
269 | else | |
270 | { |
|
270 | { | |
271 | this.prevmatch=""; |
|
271 | this.prevmatch=""; | |
272 | this.npressed=0; |
|
272 | this.npressed=0; | |
273 | } |
|
273 | } | |
274 | // end fallback on tooltip |
|
274 | // end fallback on tooltip | |
275 |
|
275 | |||
276 | // Real completion logic start here |
|
276 | // Real completion logic start here | |
277 | var that = this; |
|
277 | var that = this; | |
278 | var cur = this.completion_cursor; |
|
278 | var cur = this.completion_cursor; | |
279 | var done = false; |
|
279 | var done = false; | |
280 |
|
280 | |||
281 | // call to dismmiss the completer |
|
281 | // call to dismmiss the completer | |
282 | var close = function () { |
|
282 | var close = function () { | |
283 | if (done) return; |
|
283 | if (done) return; | |
284 | done = true; |
|
284 | done = true; | |
285 | if (complete!=undefined) |
|
285 | if (complete!=undefined) | |
286 | {complete.remove();} |
|
286 | {complete.remove();} | |
287 | that.is_completing = false; |
|
287 | that.is_completing = false; | |
288 | that.completion_cursor = null; |
|
288 | that.completion_cursor = null; | |
289 | }; |
|
289 | }; | |
290 |
|
290 | |||
291 | // insert the given text and exit the completer |
|
291 | // insert the given text and exit the completer | |
292 | var insert = function (selected_text) { |
|
292 | var insert = function (selected_text) { | |
293 | that.code_mirror.replaceRange( |
|
293 | that.code_mirror.replaceRange( | |
294 | selected_text, |
|
294 | selected_text, | |
295 | {line: cur.line, ch: (cur.ch-matched_text.length)}, |
|
295 | {line: cur.line, ch: (cur.ch-matched_text.length)}, | |
296 | {line: cur.line, ch: cur.ch} |
|
296 | {line: cur.line, ch: cur.ch} | |
297 | ); |
|
297 | ); | |
298 | event.stopPropagation(); |
|
298 | event.stopPropagation(); | |
299 | event.preventDefault(); |
|
299 | event.preventDefault(); | |
300 | close(); |
|
300 | close(); | |
301 | setTimeout(function(){that.code_mirror.focus();}, 50); |
|
301 | setTimeout(function(){that.code_mirror.focus();}, 50); | |
302 | }; |
|
302 | }; | |
303 |
|
303 | |||
304 | // insert the curent highlited selection and exit |
|
304 | // insert the curent highlited selection and exit | |
305 | var pick = function () { |
|
305 | var pick = function () { | |
306 | insert(select.val()[0]); |
|
306 | insert(select.val()[0]); | |
307 | }; |
|
307 | }; | |
308 |
|
308 | |||
309 | // if only one match, complete to it, don't ask user |
|
309 | // if only one match, complete to it, don't ask user | |
310 | if (matches.length === 1) { |
|
310 | if (matches.length === 1) { | |
311 | insert(matches[0]); |
|
311 | insert(matches[0]); | |
312 | return; |
|
312 | return; | |
313 | }; |
|
313 | }; | |
314 |
|
314 | |||
315 |
|
315 | |||
316 | // Define function to clear the completer, refill it with the new |
|
316 | // Define function to clear the completer, refill it with the new | |
317 | // matches, update the pseuso typing field. Note that this is case |
|
317 | // matches, update the pseuso typing field. Note that this is case | |
318 | // insensitive for now |
|
318 | // insensitive for now | |
319 | var complete_with = function(matches,typed_text) |
|
319 | var complete_with = function(matches,typed_text) | |
320 | { |
|
320 | { | |
321 | //clear the previous completion if any |
|
321 | //clear the previous completion if any | |
322 | if (matches.length < 1) { |
|
322 | if (matches.length < 1) { | |
323 | insert(typed_text); |
|
323 | insert(typed_text); | |
324 | } |
|
324 | } | |
325 | complete.children().children().remove(); |
|
325 | complete.children().children().remove(); | |
326 | $('#asyoutype').text(typed_text); |
|
326 | $('#asyoutype').text(typed_text); | |
327 | select=$('#asyoutypeselect'); |
|
327 | select=$('#asyoutypeselect'); | |
328 | for (var i=0; i<matches.length; ++i) { |
|
328 | for (var i=0; i<matches.length; ++i) { | |
329 | select.append($('<option/>').html(matches[i])); |
|
329 | select.append($('<option/>').html(matches[i])); | |
330 | } |
|
330 | } | |
331 | select.children().first().attr('selected','true'); |
|
331 | select.children().first().attr('selected','true'); | |
332 | } |
|
332 | } | |
333 |
|
333 | |||
334 | // create html for completer |
|
334 | // create html for completer | |
335 | var complete = $('<div/>').addClass('completions'); |
|
335 | var complete = $('<div/>').addClass('completions'); | |
336 | complete.attr('id','complete'); |
|
336 | complete.attr('id','complete'); | |
337 | complete.append($('<p/>').attr('id', 'asyoutype').html(matched_text));//pseudo input field |
|
337 | complete.append($('<p/>').attr('id', 'asyoutype').html(matched_text));//pseudo input field | |
338 |
|
338 | |||
339 | var select = $('<select/>').attr('multiple','true'); |
|
339 | var select = $('<select/>').attr('multiple','true'); | |
340 | select.attr('id', 'asyoutypeselect') |
|
340 | select.attr('id', 'asyoutypeselect') | |
341 | select.attr('size',Math.min(10,matches.length)); |
|
341 | select.attr('size',Math.min(10,matches.length)); | |
342 | var pos = this.code_mirror.cursorCoords(); |
|
342 | var pos = this.code_mirror.cursorCoords(); | |
343 |
|
343 | |||
344 | // TODO: I propose to remove enough horizontal pixel |
|
344 | // TODO: I propose to remove enough horizontal pixel | |
345 | // to align the text later |
|
345 | // to align the text later | |
346 | complete.css('left',pos.x+'px'); |
|
346 | complete.css('left',pos.x+'px'); | |
347 | complete.css('top',pos.yBot+'px'); |
|
347 | complete.css('top',pos.yBot+'px'); | |
348 | complete.append(select); |
|
348 | complete.append(select); | |
349 |
|
349 | |||
350 | $('body').append(complete); |
|
350 | $('body').append(complete); | |
351 |
|
351 | |||
352 | //do a first actual completion |
|
352 | //do a first actual completion | |
353 | complete_with(matches,matched_text); |
|
353 | complete_with(matches,matched_text); | |
354 |
|
354 | |||
355 | // Give focus to select, and make it filter the match as the user type |
|
355 | // Give focus to select, and make it filter the match as the user type | |
356 | // by filtering the previous matches |
|
356 | // by filtering the previous matches | |
357 | typed_characters = ""; |
|
357 | typed_characters = ""; | |
358 | select.keydown(function (event) { |
|
358 | select.keydown(function (event) { | |
359 | var code = event.which; |
|
359 | var code = event.which; | |
|
360 | if (code === 16) { | |||
|
361 | // nothing on Shift | |||
|
362 | return; | |||
|
363 | } | |||
360 | if (code === 13 || code === 32) { |
|
364 | if (code === 13 || code === 32) { | |
361 | // Pressing SPACE or ENTER will cause a pick |
|
365 | // Pressing SPACE or ENTER will cause a pick | |
362 | event.stopPropagation(); |
|
366 | event.stopPropagation(); | |
363 | event.preventDefault(); |
|
367 | event.preventDefault(); | |
364 | pick(); |
|
368 | pick(); | |
365 | } else if (code === 38 || code === 40) { |
|
369 | } else if (code === 38 || code === 40) { | |
366 | // We don't want the document keydown handler to handle UP/DOWN, |
|
370 | // We don't want the document keydown handler to handle UP/DOWN, | |
367 | // but we want the default action. |
|
371 | // but we want the default action. | |
368 | event.stopPropagation(); |
|
372 | event.stopPropagation(); | |
369 |
} else if (code>64 && code < |
|
373 | } else if (code>64 && code <=122 || code==8){ | |
370 | // issues with _-.. on chrome at least |
|
374 | // issues with _-.. on chrome at least | |
371 | if(code != 8) |
|
375 | if(code != 8) | |
372 | { |
|
376 | { | |
373 | var newchar = String.fromCharCode(code).toLowerCase(); |
|
377 | var newchar = String.fromCharCode(code).toLowerCase(); | |
374 | typed_characters=typed_characters+newchar; |
|
378 | typed_characters=typed_characters+newchar; | |
375 | } else { |
|
379 | } else { | |
376 | // 8 is backspace remove 1 char cancel if |
|
380 | // 8 is backspace remove 1 char cancel if | |
377 | // user have erase everything, otherwise |
|
381 | // user have erase everything, otherwise | |
378 | // decrease what we filter with |
|
382 | // decrease what we filter with | |
379 | if (typed_characters.length <= 0) |
|
383 | if (typed_characters.length <= 0) | |
380 | { |
|
384 | { | |
381 | insert(matched_text) |
|
385 | insert(matched_text) | |
382 | } |
|
386 | } | |
383 | typed_characters=typed_characters.substr(0,typed_characters.length-1); |
|
387 | typed_characters=typed_characters.substr(0,typed_characters.length-1); | |
384 | } |
|
388 | } | |
385 | re = new RegExp("^"+"\%?"+matched_text+typed_characters,"i"); |
|
389 | re = new RegExp("^"+"\%?"+matched_text+typed_characters,"i"); | |
386 | filterd= matches.filter(function(x){return re.test(x)}); |
|
390 | filterd= matches.filter(function(x){return re.test(x)}); | |
387 | complete_with(filterd,matched_text+typed_characters); |
|
391 | complete_with(filterd,matched_text+typed_characters); | |
388 | } else { |
|
392 | } else { | |
389 | // abort with what the user have pressed until now |
|
393 | // abort with what the user have pressed until now | |
390 | console.log('aborting with keycode : '+code); |
|
394 | console.log('aborting with keycode : '+code); | |
391 | insert(matched_text+typed_characters); |
|
395 | insert(matched_text+typed_characters); | |
392 | } |
|
396 | } | |
393 | }); |
|
397 | }); | |
394 | // Double click also causes a pick. |
|
398 | // Double click also causes a pick. | |
395 | // and bind the last actions. |
|
399 | // and bind the last actions. | |
396 | select.dblclick(pick); |
|
400 | select.dblclick(pick); | |
397 | select.blur(close); |
|
401 | select.blur(close); | |
398 | select.focus(); |
|
402 | select.focus(); | |
399 | }; |
|
403 | }; | |
400 |
|
404 | |||
401 | CodeCell.prototype.toggle_line_numbers = function () { |
|
405 | CodeCell.prototype.toggle_line_numbers = function () { | |
402 | if (this.code_mirror.getOption('lineNumbers') == false) { |
|
406 | if (this.code_mirror.getOption('lineNumbers') == false) { | |
403 | this.code_mirror.setOption('lineNumbers', true); |
|
407 | this.code_mirror.setOption('lineNumbers', true); | |
404 | } else { |
|
408 | } else { | |
405 | this.code_mirror.setOption('lineNumbers', false); |
|
409 | this.code_mirror.setOption('lineNumbers', false); | |
406 | } |
|
410 | } | |
407 | this.code_mirror.refresh(); |
|
411 | this.code_mirror.refresh(); | |
408 | }; |
|
412 | }; | |
409 |
|
413 | |||
410 | CodeCell.prototype.select = function () { |
|
414 | CodeCell.prototype.select = function () { | |
411 | IPython.Cell.prototype.select.apply(this); |
|
415 | IPython.Cell.prototype.select.apply(this); | |
412 | // Todo: this dance is needed because as of CodeMirror 2.12, focus is |
|
416 | // Todo: this dance is needed because as of CodeMirror 2.12, focus is | |
413 | // not causing the cursor to blink if the editor is empty initially. |
|
417 | // not causing the cursor to blink if the editor is empty initially. | |
414 | // While this seems to fix the issue, this should be fixed |
|
418 | // While this seems to fix the issue, this should be fixed | |
415 | // in CodeMirror proper. |
|
419 | // in CodeMirror proper. | |
416 | var s = this.code_mirror.getValue(); |
|
420 | var s = this.code_mirror.getValue(); | |
417 | this.code_mirror.focus(); |
|
421 | this.code_mirror.focus(); | |
418 | if (s === '') this.code_mirror.setValue(''); |
|
422 | if (s === '') this.code_mirror.setValue(''); | |
419 | }; |
|
423 | }; | |
420 |
|
424 | |||
421 |
|
425 | |||
422 | CodeCell.prototype.select_all = function () { |
|
426 | CodeCell.prototype.select_all = function () { | |
423 | var start = {line: 0, ch: 0}; |
|
427 | var start = {line: 0, ch: 0}; | |
424 | var nlines = this.code_mirror.lineCount(); |
|
428 | var nlines = this.code_mirror.lineCount(); | |
425 | var last_line = this.code_mirror.getLine(nlines-1); |
|
429 | var last_line = this.code_mirror.getLine(nlines-1); | |
426 | var end = {line: nlines-1, ch: last_line.length}; |
|
430 | var end = {line: nlines-1, ch: last_line.length}; | |
427 | this.code_mirror.setSelection(start, end); |
|
431 | this.code_mirror.setSelection(start, end); | |
428 | }; |
|
432 | }; | |
429 |
|
433 | |||
430 |
|
434 | |||
431 | CodeCell.prototype.append_output = function (json) { |
|
435 | CodeCell.prototype.append_output = function (json) { | |
432 | this.expand(); |
|
436 | this.expand(); | |
433 | if (json.output_type === 'pyout') { |
|
437 | if (json.output_type === 'pyout') { | |
434 | this.append_pyout(json); |
|
438 | this.append_pyout(json); | |
435 | } else if (json.output_type === 'pyerr') { |
|
439 | } else if (json.output_type === 'pyerr') { | |
436 | this.append_pyerr(json); |
|
440 | this.append_pyerr(json); | |
437 | } else if (json.output_type === 'display_data') { |
|
441 | } else if (json.output_type === 'display_data') { | |
438 | this.append_display_data(json); |
|
442 | this.append_display_data(json); | |
439 | } else if (json.output_type === 'stream') { |
|
443 | } else if (json.output_type === 'stream') { | |
440 | this.append_stream(json); |
|
444 | this.append_stream(json); | |
441 | }; |
|
445 | }; | |
442 | this.outputs.push(json); |
|
446 | this.outputs.push(json); | |
443 | }; |
|
447 | }; | |
444 |
|
448 | |||
445 |
|
449 | |||
446 | CodeCell.prototype.create_output_area = function () { |
|
450 | CodeCell.prototype.create_output_area = function () { | |
447 | var oa = $("<div/>").addClass("hbox output_area"); |
|
451 | var oa = $("<div/>").addClass("hbox output_area"); | |
448 | oa.append($('<div/>').addClass('prompt')); |
|
452 | oa.append($('<div/>').addClass('prompt')); | |
449 | return oa; |
|
453 | return oa; | |
450 | }; |
|
454 | }; | |
451 |
|
455 | |||
452 |
|
456 | |||
453 | CodeCell.prototype.append_pyout = function (json) { |
|
457 | CodeCell.prototype.append_pyout = function (json) { | |
454 | n = json.prompt_number || ' '; |
|
458 | n = json.prompt_number || ' '; | |
455 | var toinsert = this.create_output_area(); |
|
459 | var toinsert = this.create_output_area(); | |
456 | toinsert.find('div.prompt').addClass('output_prompt').html('Out[' + n + ']:'); |
|
460 | toinsert.find('div.prompt').addClass('output_prompt').html('Out[' + n + ']:'); | |
457 | this.append_mime_type(json, toinsert); |
|
461 | this.append_mime_type(json, toinsert); | |
458 | this.element.find('div.output').append(toinsert); |
|
462 | this.element.find('div.output').append(toinsert); | |
459 | // If we just output latex, typeset it. |
|
463 | // If we just output latex, typeset it. | |
460 | if ((json.latex !== undefined) || (json.html !== undefined)) { |
|
464 | if ((json.latex !== undefined) || (json.html !== undefined)) { | |
461 | MathJax.Hub.Queue(["Typeset",MathJax.Hub]); |
|
465 | MathJax.Hub.Queue(["Typeset",MathJax.Hub]); | |
462 | }; |
|
466 | }; | |
463 | }; |
|
467 | }; | |
464 |
|
468 | |||
465 |
|
469 | |||
466 | CodeCell.prototype.append_pyerr = function (json) { |
|
470 | CodeCell.prototype.append_pyerr = function (json) { | |
467 | var tb = json.traceback; |
|
471 | var tb = json.traceback; | |
468 | if (tb !== undefined && tb.length > 0) { |
|
472 | if (tb !== undefined && tb.length > 0) { | |
469 | var s = ''; |
|
473 | var s = ''; | |
470 | var len = tb.length; |
|
474 | var len = tb.length; | |
471 | for (var i=0; i<len; i++) { |
|
475 | for (var i=0; i<len; i++) { | |
472 | s = s + tb[i] + '\n'; |
|
476 | s = s + tb[i] + '\n'; | |
473 | } |
|
477 | } | |
474 | s = s + '\n'; |
|
478 | s = s + '\n'; | |
475 | var toinsert = this.create_output_area(); |
|
479 | var toinsert = this.create_output_area(); | |
476 | this.append_text(s, toinsert); |
|
480 | this.append_text(s, toinsert); | |
477 | this.element.find('div.output').append(toinsert); |
|
481 | this.element.find('div.output').append(toinsert); | |
478 | }; |
|
482 | }; | |
479 | }; |
|
483 | }; | |
480 |
|
484 | |||
481 |
|
485 | |||
482 | CodeCell.prototype.append_stream = function (json) { |
|
486 | CodeCell.prototype.append_stream = function (json) { | |
483 | // temporary fix: if stream undefined (json file written prior to this patch), |
|
487 | // temporary fix: if stream undefined (json file written prior to this patch), | |
484 | // default to most likely stdout: |
|
488 | // default to most likely stdout: | |
485 | if (json.stream == undefined){ |
|
489 | if (json.stream == undefined){ | |
486 | json.stream = 'stdout'; |
|
490 | json.stream = 'stdout'; | |
487 | } |
|
491 | } | |
488 | var subclass = "output_"+json.stream; |
|
492 | var subclass = "output_"+json.stream; | |
489 | if (this.outputs.length > 0){ |
|
493 | if (this.outputs.length > 0){ | |
490 | // have at least one output to consider |
|
494 | // have at least one output to consider | |
491 | var last = this.outputs[this.outputs.length-1]; |
|
495 | var last = this.outputs[this.outputs.length-1]; | |
492 | if (last.output_type == 'stream' && json.stream == last.stream){ |
|
496 | if (last.output_type == 'stream' && json.stream == last.stream){ | |
493 | // latest output was in the same stream, |
|
497 | // latest output was in the same stream, | |
494 | // so append directly into its pre tag |
|
498 | // so append directly into its pre tag | |
495 | this.element.find('div.'+subclass).last().find('pre').append(json.text); |
|
499 | this.element.find('div.'+subclass).last().find('pre').append(json.text); | |
496 | return; |
|
500 | return; | |
497 | } |
|
501 | } | |
498 | } |
|
502 | } | |
499 |
|
503 | |||
500 | // If we got here, attach a new div |
|
504 | // If we got here, attach a new div | |
501 | var toinsert = this.create_output_area(); |
|
505 | var toinsert = this.create_output_area(); | |
502 | this.append_text(json.text, toinsert, "output_stream "+subclass); |
|
506 | this.append_text(json.text, toinsert, "output_stream "+subclass); | |
503 | this.element.find('div.output').append(toinsert); |
|
507 | this.element.find('div.output').append(toinsert); | |
504 | }; |
|
508 | }; | |
505 |
|
509 | |||
506 |
|
510 | |||
507 | CodeCell.prototype.append_display_data = function (json) { |
|
511 | CodeCell.prototype.append_display_data = function (json) { | |
508 | var toinsert = this.create_output_area(); |
|
512 | var toinsert = this.create_output_area(); | |
509 | this.append_mime_type(json, toinsert); |
|
513 | this.append_mime_type(json, toinsert); | |
510 | this.element.find('div.output').append(toinsert); |
|
514 | this.element.find('div.output').append(toinsert); | |
511 | // If we just output latex, typeset it. |
|
515 | // If we just output latex, typeset it. | |
512 | if ( (json.latex !== undefined) || (json.html !== undefined) ) { |
|
516 | if ( (json.latex !== undefined) || (json.html !== undefined) ) { | |
513 | MathJax.Hub.Queue(["Typeset",MathJax.Hub]); |
|
517 | MathJax.Hub.Queue(["Typeset",MathJax.Hub]); | |
514 | }; |
|
518 | }; | |
515 | }; |
|
519 | }; | |
516 |
|
520 | |||
517 |
|
521 | |||
518 | CodeCell.prototype.append_mime_type = function (json, element) { |
|
522 | CodeCell.prototype.append_mime_type = function (json, element) { | |
519 | if (json.html !== undefined) { |
|
523 | if (json.html !== undefined) { | |
520 | this.append_html(json.html, element); |
|
524 | this.append_html(json.html, element); | |
521 | } else if (json.latex !== undefined) { |
|
525 | } else if (json.latex !== undefined) { | |
522 | this.append_latex(json.latex, element); |
|
526 | this.append_latex(json.latex, element); | |
523 | } else if (json.svg !== undefined) { |
|
527 | } else if (json.svg !== undefined) { | |
524 | this.append_svg(json.svg, element); |
|
528 | this.append_svg(json.svg, element); | |
525 | } else if (json.png !== undefined) { |
|
529 | } else if (json.png !== undefined) { | |
526 | this.append_png(json.png, element); |
|
530 | this.append_png(json.png, element); | |
527 | } else if (json.jpeg !== undefined) { |
|
531 | } else if (json.jpeg !== undefined) { | |
528 | this.append_jpeg(json.jpeg, element); |
|
532 | this.append_jpeg(json.jpeg, element); | |
529 | } else if (json.text !== undefined) { |
|
533 | } else if (json.text !== undefined) { | |
530 | this.append_text(json.text, element); |
|
534 | this.append_text(json.text, element); | |
531 | }; |
|
535 | }; | |
532 | }; |
|
536 | }; | |
533 |
|
537 | |||
534 |
|
538 | |||
535 | CodeCell.prototype.append_html = function (html, element) { |
|
539 | CodeCell.prototype.append_html = function (html, element) { | |
536 | var toinsert = $("<div/>").addClass("box_flex1 output_subarea output_html rendered_html"); |
|
540 | var toinsert = $("<div/>").addClass("box_flex1 output_subarea output_html rendered_html"); | |
537 | toinsert.append(html); |
|
541 | toinsert.append(html); | |
538 | element.append(toinsert); |
|
542 | element.append(toinsert); | |
539 | }; |
|
543 | }; | |
540 |
|
544 | |||
541 |
|
545 | |||
542 | CodeCell.prototype.append_text = function (data, element, extra_class) { |
|
546 | CodeCell.prototype.append_text = function (data, element, extra_class) { | |
543 | var toinsert = $("<div/>").addClass("box_flex1 output_subarea output_text"); |
|
547 | var toinsert = $("<div/>").addClass("box_flex1 output_subarea output_text"); | |
544 | if (extra_class){ |
|
548 | if (extra_class){ | |
545 | toinsert.addClass(extra_class); |
|
549 | toinsert.addClass(extra_class); | |
546 | } |
|
550 | } | |
547 | toinsert.append($("<pre/>").html(data)); |
|
551 | toinsert.append($("<pre/>").html(data)); | |
548 | element.append(toinsert); |
|
552 | element.append(toinsert); | |
549 | }; |
|
553 | }; | |
550 |
|
554 | |||
551 |
|
555 | |||
552 | CodeCell.prototype.append_svg = function (svg, element) { |
|
556 | CodeCell.prototype.append_svg = function (svg, element) { | |
553 | var toinsert = $("<div/>").addClass("box_flex1 output_subarea output_svg"); |
|
557 | var toinsert = $("<div/>").addClass("box_flex1 output_subarea output_svg"); | |
554 | toinsert.append(svg); |
|
558 | toinsert.append(svg); | |
555 | element.append(toinsert); |
|
559 | element.append(toinsert); | |
556 | }; |
|
560 | }; | |
557 |
|
561 | |||
558 |
|
562 | |||
559 | CodeCell.prototype.append_png = function (png, element) { |
|
563 | CodeCell.prototype.append_png = function (png, element) { | |
560 | var toinsert = $("<div/>").addClass("box_flex1 output_subarea output_png"); |
|
564 | var toinsert = $("<div/>").addClass("box_flex1 output_subarea output_png"); | |
561 | toinsert.append($("<img/>").attr('src','data:image/png;base64,'+png)); |
|
565 | toinsert.append($("<img/>").attr('src','data:image/png;base64,'+png)); | |
562 | element.append(toinsert); |
|
566 | element.append(toinsert); | |
563 | }; |
|
567 | }; | |
564 |
|
568 | |||
565 |
|
569 | |||
566 | CodeCell.prototype.append_jpeg = function (jpeg, element) { |
|
570 | CodeCell.prototype.append_jpeg = function (jpeg, element) { | |
567 | var toinsert = $("<div/>").addClass("box_flex1 output_subarea output_jpeg"); |
|
571 | var toinsert = $("<div/>").addClass("box_flex1 output_subarea output_jpeg"); | |
568 | toinsert.append($("<img/>").attr('src','data:image/jpeg;base64,'+jpeg)); |
|
572 | toinsert.append($("<img/>").attr('src','data:image/jpeg;base64,'+jpeg)); | |
569 | element.append(toinsert); |
|
573 | element.append(toinsert); | |
570 | }; |
|
574 | }; | |
571 |
|
575 | |||
572 |
|
576 | |||
573 | CodeCell.prototype.append_latex = function (latex, element) { |
|
577 | CodeCell.prototype.append_latex = function (latex, element) { | |
574 | // This method cannot do the typesetting because the latex first has to |
|
578 | // This method cannot do the typesetting because the latex first has to | |
575 | // be on the page. |
|
579 | // be on the page. | |
576 | var toinsert = $("<div/>").addClass("box_flex1 output_subarea output_latex"); |
|
580 | var toinsert = $("<div/>").addClass("box_flex1 output_subarea output_latex"); | |
577 | toinsert.append(latex); |
|
581 | toinsert.append(latex); | |
578 | element.append(toinsert); |
|
582 | element.append(toinsert); | |
579 | }; |
|
583 | }; | |
580 |
|
584 | |||
581 |
|
585 | |||
582 | CodeCell.prototype.clear_output = function (stdout, stderr, other) { |
|
586 | CodeCell.prototype.clear_output = function (stdout, stderr, other) { | |
583 | var output_div = this.element.find("div.output"); |
|
587 | var output_div = this.element.find("div.output"); | |
584 | if (stdout && stderr && other){ |
|
588 | if (stdout && stderr && other){ | |
585 | // clear all, no need for logic |
|
589 | // clear all, no need for logic | |
586 | output_div.html(""); |
|
590 | output_div.html(""); | |
587 | this.outputs = []; |
|
591 | this.outputs = []; | |
588 | return; |
|
592 | return; | |
589 | } |
|
593 | } | |
590 | // remove html output |
|
594 | // remove html output | |
591 | // each output_subarea that has an identifying class is in an output_area |
|
595 | // each output_subarea that has an identifying class is in an output_area | |
592 | // which is the element to be removed. |
|
596 | // which is the element to be removed. | |
593 | if (stdout){ |
|
597 | if (stdout){ | |
594 | output_div.find("div.output_stdout").parent().remove(); |
|
598 | output_div.find("div.output_stdout").parent().remove(); | |
595 | } |
|
599 | } | |
596 | if (stderr){ |
|
600 | if (stderr){ | |
597 | output_div.find("div.output_stderr").parent().remove(); |
|
601 | output_div.find("div.output_stderr").parent().remove(); | |
598 | } |
|
602 | } | |
599 | if (other){ |
|
603 | if (other){ | |
600 | output_div.find("div.output_subarea").not("div.output_stderr").not("div.output_stdout").parent().remove(); |
|
604 | output_div.find("div.output_subarea").not("div.output_stderr").not("div.output_stdout").parent().remove(); | |
601 | } |
|
605 | } | |
602 |
|
606 | |||
603 | // remove cleared outputs from JSON list: |
|
607 | // remove cleared outputs from JSON list: | |
604 | for (var i = this.outputs.length - 1; i >= 0; i--){ |
|
608 | for (var i = this.outputs.length - 1; i >= 0; i--){ | |
605 | var out = this.outputs[i]; |
|
609 | var out = this.outputs[i]; | |
606 | var output_type = out.output_type; |
|
610 | var output_type = out.output_type; | |
607 | if (output_type == "display_data" && other){ |
|
611 | if (output_type == "display_data" && other){ | |
608 | this.outputs.splice(i,1); |
|
612 | this.outputs.splice(i,1); | |
609 | }else if (output_type == "stream"){ |
|
613 | }else if (output_type == "stream"){ | |
610 | if (stdout && out.stream == "stdout"){ |
|
614 | if (stdout && out.stream == "stdout"){ | |
611 | this.outputs.splice(i,1); |
|
615 | this.outputs.splice(i,1); | |
612 | }else if (stderr && out.stream == "stderr"){ |
|
616 | }else if (stderr && out.stream == "stderr"){ | |
613 | this.outputs.splice(i,1); |
|
617 | this.outputs.splice(i,1); | |
614 | } |
|
618 | } | |
615 | } |
|
619 | } | |
616 | } |
|
620 | } | |
617 | }; |
|
621 | }; | |
618 |
|
622 | |||
619 |
|
623 | |||
620 | CodeCell.prototype.clear_input = function () { |
|
624 | CodeCell.prototype.clear_input = function () { | |
621 | this.code_mirror.setValue(''); |
|
625 | this.code_mirror.setValue(''); | |
622 | }; |
|
626 | }; | |
623 |
|
627 | |||
624 |
|
628 | |||
625 | CodeCell.prototype.collapse = function () { |
|
629 | CodeCell.prototype.collapse = function () { | |
626 | if (!this.collapsed) { |
|
630 | if (!this.collapsed) { | |
627 | this.element.find('div.output').hide(); |
|
631 | this.element.find('div.output').hide(); | |
628 | this.collapsed = true; |
|
632 | this.collapsed = true; | |
629 | }; |
|
633 | }; | |
630 | }; |
|
634 | }; | |
631 |
|
635 | |||
632 |
|
636 | |||
633 | CodeCell.prototype.expand = function () { |
|
637 | CodeCell.prototype.expand = function () { | |
634 | if (this.collapsed) { |
|
638 | if (this.collapsed) { | |
635 | this.element.find('div.output').show(); |
|
639 | this.element.find('div.output').show(); | |
636 | this.collapsed = false; |
|
640 | this.collapsed = false; | |
637 | }; |
|
641 | }; | |
638 | }; |
|
642 | }; | |
639 |
|
643 | |||
640 |
|
644 | |||
641 | CodeCell.prototype.toggle_output = function () { |
|
645 | CodeCell.prototype.toggle_output = function () { | |
642 | if (this.collapsed) { |
|
646 | if (this.collapsed) { | |
643 | this.expand(); |
|
647 | this.expand(); | |
644 | } else { |
|
648 | } else { | |
645 | this.collapse(); |
|
649 | this.collapse(); | |
646 | }; |
|
650 | }; | |
647 | }; |
|
651 | }; | |
648 |
|
652 | |||
649 | CodeCell.prototype.set_input_prompt = function (number) { |
|
653 | CodeCell.prototype.set_input_prompt = function (number) { | |
650 | var n = number || ' '; |
|
654 | var n = number || ' '; | |
651 | this.input_prompt_number = n; |
|
655 | this.input_prompt_number = n; | |
652 | this.element.find('div.input_prompt').html('In [' + n + ']:'); |
|
656 | this.element.find('div.input_prompt').html('In [' + n + ']:'); | |
653 | }; |
|
657 | }; | |
654 |
|
658 | |||
655 |
|
659 | |||
656 | CodeCell.prototype.get_code = function () { |
|
660 | CodeCell.prototype.get_code = function () { | |
657 | return this.code_mirror.getValue(); |
|
661 | return this.code_mirror.getValue(); | |
658 | }; |
|
662 | }; | |
659 |
|
663 | |||
660 |
|
664 | |||
661 | CodeCell.prototype.set_code = function (code) { |
|
665 | CodeCell.prototype.set_code = function (code) { | |
662 | return this.code_mirror.setValue(code); |
|
666 | return this.code_mirror.setValue(code); | |
663 | }; |
|
667 | }; | |
664 |
|
668 | |||
665 |
|
669 | |||
666 | CodeCell.prototype.at_top = function () { |
|
670 | CodeCell.prototype.at_top = function () { | |
667 | var cursor = this.code_mirror.getCursor(); |
|
671 | var cursor = this.code_mirror.getCursor(); | |
668 | if (cursor.line === 0) { |
|
672 | if (cursor.line === 0) { | |
669 | return true; |
|
673 | return true; | |
670 | } else { |
|
674 | } else { | |
671 | return false; |
|
675 | return false; | |
672 | } |
|
676 | } | |
673 | }; |
|
677 | }; | |
674 |
|
678 | |||
675 |
|
679 | |||
676 | CodeCell.prototype.at_bottom = function () { |
|
680 | CodeCell.prototype.at_bottom = function () { | |
677 | var cursor = this.code_mirror.getCursor(); |
|
681 | var cursor = this.code_mirror.getCursor(); | |
678 | if (cursor.line === (this.code_mirror.lineCount()-1)) { |
|
682 | if (cursor.line === (this.code_mirror.lineCount()-1)) { | |
679 | return true; |
|
683 | return true; | |
680 | } else { |
|
684 | } else { | |
681 | return false; |
|
685 | return false; | |
682 | } |
|
686 | } | |
683 | }; |
|
687 | }; | |
684 |
|
688 | |||
685 |
|
689 | |||
686 | CodeCell.prototype.fromJSON = function (data) { |
|
690 | CodeCell.prototype.fromJSON = function (data) { | |
687 | console.log('Import from JSON:', data); |
|
691 | console.log('Import from JSON:', data); | |
688 | if (data.cell_type === 'code') { |
|
692 | if (data.cell_type === 'code') { | |
689 | if (data.input !== undefined) { |
|
693 | if (data.input !== undefined) { | |
690 | this.set_code(data.input); |
|
694 | this.set_code(data.input); | |
691 | } |
|
695 | } | |
692 | if (data.prompt_number !== undefined) { |
|
696 | if (data.prompt_number !== undefined) { | |
693 | this.set_input_prompt(data.prompt_number); |
|
697 | this.set_input_prompt(data.prompt_number); | |
694 | } else { |
|
698 | } else { | |
695 | this.set_input_prompt(); |
|
699 | this.set_input_prompt(); | |
696 | }; |
|
700 | }; | |
697 | var len = data.outputs.length; |
|
701 | var len = data.outputs.length; | |
698 | for (var i=0; i<len; i++) { |
|
702 | for (var i=0; i<len; i++) { | |
699 | this.append_output(data.outputs[i]); |
|
703 | this.append_output(data.outputs[i]); | |
700 | }; |
|
704 | }; | |
701 | if (data.collapsed !== undefined) { |
|
705 | if (data.collapsed !== undefined) { | |
702 | if (data.collapsed) { |
|
706 | if (data.collapsed) { | |
703 | this.collapse(); |
|
707 | this.collapse(); | |
704 | }; |
|
708 | }; | |
705 | }; |
|
709 | }; | |
706 | }; |
|
710 | }; | |
707 | }; |
|
711 | }; | |
708 |
|
712 | |||
709 |
|
713 | |||
710 | CodeCell.prototype.toJSON = function () { |
|
714 | CodeCell.prototype.toJSON = function () { | |
711 | var data = {}; |
|
715 | var data = {}; | |
712 | data.input = this.get_code(); |
|
716 | data.input = this.get_code(); | |
713 | data.cell_type = 'code'; |
|
717 | data.cell_type = 'code'; | |
714 | if (this.input_prompt_number !== ' ') { |
|
718 | if (this.input_prompt_number !== ' ') { | |
715 | data.prompt_number = this.input_prompt_number; |
|
719 | data.prompt_number = this.input_prompt_number; | |
716 | }; |
|
720 | }; | |
717 | var outputs = []; |
|
721 | var outputs = []; | |
718 | var len = this.outputs.length; |
|
722 | var len = this.outputs.length; | |
719 | for (var i=0; i<len; i++) { |
|
723 | for (var i=0; i<len; i++) { | |
720 | outputs[i] = this.outputs[i]; |
|
724 | outputs[i] = this.outputs[i]; | |
721 | }; |
|
725 | }; | |
722 | data.outputs = outputs; |
|
726 | data.outputs = outputs; | |
723 | data.language = 'python'; |
|
727 | data.language = 'python'; | |
724 | data.collapsed = this.collapsed; |
|
728 | data.collapsed = this.collapsed; | |
725 | // console.log('Export to JSON:',data); |
|
729 | // console.log('Export to JSON:',data); | |
726 | return data; |
|
730 | return data; | |
727 | }; |
|
731 | }; | |
728 |
|
732 | |||
729 |
|
733 | |||
730 | IPython.CodeCell = CodeCell; |
|
734 | IPython.CodeCell = CodeCell; | |
731 |
|
735 | |||
732 | return IPython; |
|
736 | return IPython; | |
733 | }(IPython)); |
|
737 | }(IPython)); |
General Comments 0
You need to be logged in to leave comments.
Login now