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