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