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