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