Show More
@@ -1,441 +1,443 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 | * An extendable module that provide base functionnality to create cell for notebook. |
|
12 | * An extendable module that provide base functionnality to create cell for notebook. | |
13 | * @module IPython |
|
13 | * @module IPython | |
14 | * @namespace IPython |
|
14 | * @namespace IPython | |
15 | * @submodule CodeCell |
|
15 | * @submodule CodeCell | |
16 | */ |
|
16 | */ | |
17 |
|
17 | |||
18 |
|
18 | |||
19 | /* local util for codemirror */ |
|
19 | /* local util for codemirror */ | |
20 | var posEq = function(a, b) {return a.line == b.line && a.ch == b.ch;} |
|
20 | var posEq = function(a, b) {return a.line == b.line && a.ch == b.ch;} | |
21 |
|
21 | |||
22 | /** |
|
22 | /** | |
23 | * |
|
23 | * | |
24 | * function to delete until previous non blanking space character |
|
24 | * function to delete until previous non blanking space character | |
25 | * or first multiple of 4 tabstop. |
|
25 | * or first multiple of 4 tabstop. | |
26 | * @private |
|
26 | * @private | |
27 | */ |
|
27 | */ | |
28 | CodeMirror.commands.delSpaceToPrevTabStop = function(cm){ |
|
28 | CodeMirror.commands.delSpaceToPrevTabStop = function(cm){ | |
29 | var from = cm.getCursor(true), to = cm.getCursor(false), sel = !posEq(from, to); |
|
29 | var from = cm.getCursor(true), to = cm.getCursor(false), sel = !posEq(from, to); | |
30 | if (!posEq(from, to)) {cm.replaceRange("", from, to); return} |
|
30 | if (!posEq(from, to)) {cm.replaceRange("", from, to); return} | |
31 | var cur = cm.getCursor(), line = cm.getLine(cur.line); |
|
31 | var cur = cm.getCursor(), line = cm.getLine(cur.line); | |
32 | var tabsize = cm.getOption('tabSize'); |
|
32 | var tabsize = cm.getOption('tabSize'); | |
33 | var chToPrevTabStop = cur.ch-(Math.ceil(cur.ch/tabsize)-1)*tabsize; |
|
33 | var chToPrevTabStop = cur.ch-(Math.ceil(cur.ch/tabsize)-1)*tabsize; | |
34 | var from = {ch:cur.ch-chToPrevTabStop,line:cur.line} |
|
34 | var from = {ch:cur.ch-chToPrevTabStop,line:cur.line} | |
35 | var select = cm.getRange(from,cur) |
|
35 | var select = cm.getRange(from,cur) | |
36 | if( select.match(/^\ +$/) != null){ |
|
36 | if( select.match(/^\ +$/) != null){ | |
37 | cm.replaceRange("",from,cur) |
|
37 | cm.replaceRange("",from,cur) | |
38 | } else { |
|
38 | } else { | |
39 | cm.deleteH(-1,"char") |
|
39 | cm.deleteH(-1,"char") | |
40 | } |
|
40 | } | |
41 | }; |
|
41 | }; | |
42 |
|
42 | |||
43 |
|
43 | |||
44 | var IPython = (function (IPython) { |
|
44 | var IPython = (function (IPython) { | |
45 | "use strict"; |
|
45 | "use strict"; | |
46 |
|
46 | |||
47 | var utils = IPython.utils; |
|
47 | var utils = IPython.utils; | |
48 | var key = IPython.utils.keycodes; |
|
48 | var key = IPython.utils.keycodes; | |
49 |
|
49 | |||
50 | /** |
|
50 | /** | |
51 | * A Cell conceived to write code. |
|
51 | * A Cell conceived to write code. | |
52 | * |
|
52 | * | |
53 | * The kernel doesn't have to be set at creation time, in that case |
|
53 | * The kernel doesn't have to be set at creation time, in that case | |
54 | * it will be null and set_kernel has to be called later. |
|
54 | * it will be null and set_kernel has to be called later. | |
55 | * @class CodeCell |
|
55 | * @class CodeCell | |
56 | * @extends IPython.Cell |
|
56 | * @extends IPython.Cell | |
57 | * |
|
57 | * | |
58 | * @constructor |
|
58 | * @constructor | |
59 | * @param {Object|null} kernel |
|
59 | * @param {Object|null} kernel | |
60 | * @param {object|undefined} [options] |
|
60 | * @param {object|undefined} [options] | |
61 | * @param [options.cm_config] {object} config to pass to CodeMirror |
|
61 | * @param [options.cm_config] {object} config to pass to CodeMirror | |
62 | */ |
|
62 | */ | |
63 | var CodeCell = function (kernel, options) { |
|
63 | var CodeCell = function (kernel, options) { | |
64 | this.kernel = kernel || null; |
|
64 | this.kernel = kernel || null; | |
65 | this.code_mirror = null; |
|
65 | this.code_mirror = null; | |
66 | this.input_prompt_number = null; |
|
66 | this.input_prompt_number = null; | |
67 | this.collapsed = false; |
|
67 | this.collapsed = false; | |
68 | this.default_mode = 'ipython'; |
|
68 | this.default_mode = 'ipython'; | |
69 | this.cell_type = "code"; |
|
69 | this.cell_type = "code"; | |
70 |
|
70 | |||
71 |
|
71 | |||
72 | var cm_overwrite_options = { |
|
72 | var cm_overwrite_options = { | |
73 | onKeyEvent: $.proxy(this.handle_codemirror_keyevent,this) |
|
73 | onKeyEvent: $.proxy(this.handle_codemirror_keyevent,this) | |
74 | }; |
|
74 | }; | |
75 |
|
75 | |||
76 | options = this.mergeopt(CodeCell, options, {cm_config:cm_overwrite_options}); |
|
76 | options = this.mergeopt(CodeCell, options, {cm_config:cm_overwrite_options}); | |
77 |
|
77 | |||
78 | IPython.Cell.apply(this,[options]); |
|
78 | IPython.Cell.apply(this,[options]); | |
79 |
|
79 | |||
80 | var that = this; |
|
80 | var that = this; | |
81 | this.element.focusout( |
|
81 | this.element.focusout( | |
82 | function() { that.auto_highlight(); } |
|
82 | function() { that.auto_highlight(); } | |
83 | ); |
|
83 | ); | |
84 | }; |
|
84 | }; | |
85 |
|
85 | |||
86 | CodeCell.options_default = { |
|
86 | CodeCell.options_default = { | |
87 | cm_config : { |
|
87 | cm_config : { | |
88 | extraKeys: { |
|
88 | extraKeys: { | |
89 | "Tab" : "indentMore", |
|
89 | "Tab" : "indentMore", | |
90 | "Shift-Tab" : "indentLess", |
|
90 | "Shift-Tab" : "indentLess", | |
91 | "Backspace" : "delSpaceToPrevTabStop", |
|
91 | "Backspace" : "delSpaceToPrevTabStop", | |
92 | "Cmd-/" : "toggleComment", |
|
92 | "Cmd-/" : "toggleComment", | |
93 | "Ctrl-/" : "toggleComment" |
|
93 | "Ctrl-/" : "toggleComment" | |
94 | }, |
|
94 | }, | |
95 | mode: 'ipython', |
|
95 | mode: 'ipython', | |
96 | theme: 'ipython', |
|
96 | theme: 'ipython', | |
97 | matchBrackets: true |
|
97 | matchBrackets: true | |
98 | } |
|
98 | } | |
99 | }; |
|
99 | }; | |
100 |
|
100 | |||
101 |
|
101 | |||
102 | CodeCell.prototype = new IPython.Cell(); |
|
102 | CodeCell.prototype = new IPython.Cell(); | |
103 |
|
103 | |||
104 | /** |
|
104 | /** | |
105 | * @method auto_highlight |
|
105 | * @method auto_highlight | |
106 | */ |
|
106 | */ | |
107 | CodeCell.prototype.auto_highlight = function () { |
|
107 | CodeCell.prototype.auto_highlight = function () { | |
108 | this._auto_highlight(IPython.config.cell_magic_highlight) |
|
108 | this._auto_highlight(IPython.config.cell_magic_highlight) | |
109 | }; |
|
109 | }; | |
110 |
|
110 | |||
111 | /** @method create_element */ |
|
111 | /** @method create_element */ | |
112 | CodeCell.prototype.create_element = function () { |
|
112 | CodeCell.prototype.create_element = function () { | |
113 | IPython.Cell.prototype.create_element.apply(this, arguments); |
|
113 | IPython.Cell.prototype.create_element.apply(this, arguments); | |
114 |
|
114 | |||
115 | var cell = $('<div></div>').addClass('cell border-box-sizing code_cell'); |
|
115 | var cell = $('<div></div>').addClass('cell border-box-sizing code_cell'); | |
116 | cell.attr('tabindex','2'); |
|
116 | cell.attr('tabindex','2'); | |
117 |
|
117 | |||
118 | this.celltoolbar = new IPython.CellToolbar(this); |
|
118 | this.celltoolbar = new IPython.CellToolbar(this); | |
119 |
|
119 | |||
120 | var input = $('<div></div>').addClass('input'); |
|
120 | var input = $('<div></div>').addClass('input'); | |
121 | var vbox = $('<div/>').addClass('vbox box-flex1') |
|
121 | var vbox = $('<div/>').addClass('vbox box-flex1') | |
122 | input.append($('<div/>').addClass('prompt input_prompt')); |
|
122 | input.append($('<div/>').addClass('prompt input_prompt')); | |
123 | vbox.append(this.celltoolbar.element); |
|
123 | vbox.append(this.celltoolbar.element); | |
124 | var input_area = $('<div/>').addClass('input_area'); |
|
124 | var input_area = $('<div/>').addClass('input_area'); | |
125 | this.code_mirror = CodeMirror(input_area.get(0), this.cm_config); |
|
125 | this.code_mirror = CodeMirror(input_area.get(0), this.cm_config); | |
126 | $(this.code_mirror.getInputField()).attr("spellcheck", "false"); |
|
126 | $(this.code_mirror.getInputField()).attr("spellcheck", "false"); | |
127 | vbox.append(input_area); |
|
127 | vbox.append(input_area); | |
128 | input.append(vbox); |
|
128 | input.append(vbox); | |
129 | var output = $('<div></div>'); |
|
129 | var output = $('<div></div>'); | |
130 | cell.append(input).append(output); |
|
130 | cell.append(input).append(output); | |
131 | this.element = cell; |
|
131 | this.element = cell; | |
132 | this.output_area = new IPython.OutputArea(output, true); |
|
132 | this.output_area = new IPython.OutputArea(output, true); | |
133 |
|
133 | |||
134 | // construct a completer only if class exist |
|
134 | // construct a completer only if class exist | |
135 | // otherwise no print view |
|
135 | // otherwise no print view | |
136 | if (IPython.Completer !== undefined) |
|
136 | if (IPython.Completer !== undefined) | |
137 | { |
|
137 | { | |
138 | this.completer = new IPython.Completer(this); |
|
138 | this.completer = new IPython.Completer(this); | |
139 | } |
|
139 | } | |
140 | }; |
|
140 | }; | |
141 |
|
141 | |||
142 | /** |
|
142 | /** | |
143 | * This method gets called in CodeMirror's onKeyDown/onKeyPress |
|
143 | * This method gets called in CodeMirror's onKeyDown/onKeyPress | |
144 | * handlers and is used to provide custom key handling. Its return |
|
144 | * handlers and is used to provide custom key handling. Its return | |
145 | * value is used to determine if CodeMirror should ignore the event: |
|
145 | * value is used to determine if CodeMirror should ignore the event: | |
146 | * true = ignore, false = don't ignore. |
|
146 | * true = ignore, false = don't ignore. | |
147 | * @method handle_codemirror_keyevent |
|
147 | * @method handle_codemirror_keyevent | |
148 | */ |
|
148 | */ | |
149 | CodeCell.prototype.handle_codemirror_keyevent = function (editor, event) { |
|
149 | CodeCell.prototype.handle_codemirror_keyevent = function (editor, event) { | |
150 |
|
150 | |||
151 | var that = this; |
|
151 | var that = this; | |
152 | // whatever key is pressed, first, cancel the tooltip request before |
|
152 | // whatever key is pressed, first, cancel the tooltip request before | |
153 | // they are sent, and remove tooltip if any, except for tab again |
|
153 | // they are sent, and remove tooltip if any, except for tab again | |
154 | if (event.type === 'keydown' && event.which != key.TAB ) { |
|
154 | if (event.type === 'keydown' && event.which != key.TAB ) { | |
155 | IPython.tooltip.remove_and_cancel_tooltip(); |
|
155 | IPython.tooltip.remove_and_cancel_tooltip(); | |
156 | }; |
|
156 | }; | |
157 |
|
157 | |||
158 | var cur = editor.getCursor(); |
|
158 | var cur = editor.getCursor(); | |
159 | if (event.keyCode === key.ENTER){ |
|
159 | if (event.keyCode === key.ENTER){ | |
160 | this.auto_highlight(); |
|
160 | this.auto_highlight(); | |
161 | } |
|
161 | } | |
162 |
|
162 | |||
163 | if (event.keyCode === key.ENTER && (event.shiftKey || event.ctrlKey)) { |
|
163 | if (event.keyCode === key.ENTER && (event.shiftKey || event.ctrlKey)) { | |
164 | // Always ignore shift-enter in CodeMirror as we handle it. |
|
164 | // Always ignore shift-enter in CodeMirror as we handle it. | |
165 | return true; |
|
165 | return true; | |
166 | } else if (event.which === 40 && event.type === 'keypress' && IPython.tooltip.time_before_tooltip >= 0) { |
|
166 | } else if (event.which === 40 && event.type === 'keypress' && IPython.tooltip.time_before_tooltip >= 0) { | |
167 | // triger on keypress (!) otherwise inconsistent event.which depending on plateform |
|
167 | // triger on keypress (!) otherwise inconsistent event.which depending on plateform | |
168 | // browser and keyboard layout ! |
|
168 | // browser and keyboard layout ! | |
169 | // Pressing '(' , request tooltip, don't forget to reappend it |
|
169 | // Pressing '(' , request tooltip, don't forget to reappend it | |
170 | IPython.tooltip.pending(that); |
|
170 | // The second argument says to hide the tooltip if the docstring | |
|
171 | // is actually empty | |||
|
172 | IPython.tooltip.pending(that, true); | |||
171 | } else if (event.which === key.UPARROW && event.type === 'keydown') { |
|
173 | } else if (event.which === key.UPARROW && event.type === 'keydown') { | |
172 | // If we are not at the top, let CM handle the up arrow and |
|
174 | // If we are not at the top, let CM handle the up arrow and | |
173 | // prevent the global keydown handler from handling it. |
|
175 | // prevent the global keydown handler from handling it. | |
174 | if (!that.at_top()) { |
|
176 | if (!that.at_top()) { | |
175 | event.stop(); |
|
177 | event.stop(); | |
176 | return false; |
|
178 | return false; | |
177 | } else { |
|
179 | } else { | |
178 | return true; |
|
180 | return true; | |
179 | }; |
|
181 | }; | |
180 | } else if (event.which === key.ESC) { |
|
182 | } else if (event.which === key.ESC) { | |
181 | IPython.tooltip.remove_and_cancel_tooltip(true); |
|
183 | IPython.tooltip.remove_and_cancel_tooltip(true); | |
182 | return true; |
|
184 | return true; | |
183 | } else if (event.which === key.DOWNARROW && event.type === 'keydown') { |
|
185 | } else if (event.which === key.DOWNARROW && event.type === 'keydown') { | |
184 | // If we are not at the bottom, let CM handle the down arrow and |
|
186 | // If we are not at the bottom, let CM handle the down arrow and | |
185 | // prevent the global keydown handler from handling it. |
|
187 | // prevent the global keydown handler from handling it. | |
186 | if (!that.at_bottom()) { |
|
188 | if (!that.at_bottom()) { | |
187 | event.stop(); |
|
189 | event.stop(); | |
188 | return false; |
|
190 | return false; | |
189 | } else { |
|
191 | } else { | |
190 | return true; |
|
192 | return true; | |
191 | }; |
|
193 | }; | |
192 | } else if (event.keyCode === key.TAB && event.type == 'keydown' && event.shiftKey) { |
|
194 | } else if (event.keyCode === key.TAB && event.type == 'keydown' && event.shiftKey) { | |
193 | if (editor.somethingSelected()){ |
|
195 | if (editor.somethingSelected()){ | |
194 | var anchor = editor.getCursor("anchor"); |
|
196 | var anchor = editor.getCursor("anchor"); | |
195 | var head = editor.getCursor("head"); |
|
197 | var head = editor.getCursor("head"); | |
196 | if( anchor.line != head.line){ |
|
198 | if( anchor.line != head.line){ | |
197 | return false; |
|
199 | return false; | |
198 | } |
|
200 | } | |
199 | } |
|
201 | } | |
200 | IPython.tooltip.request(that); |
|
202 | IPython.tooltip.request(that); | |
201 | event.stop(); |
|
203 | event.stop(); | |
202 | return true; |
|
204 | return true; | |
203 | } else if (event.keyCode === key.TAB && event.type == 'keydown') { |
|
205 | } else if (event.keyCode === key.TAB && event.type == 'keydown') { | |
204 | // Tab completion. |
|
206 | // Tab completion. | |
205 | //Do not trim here because of tooltip |
|
207 | //Do not trim here because of tooltip | |
206 | if (editor.somethingSelected()){return false} |
|
208 | if (editor.somethingSelected()){return false} | |
207 | var pre_cursor = editor.getRange({line:cur.line,ch:0},cur); |
|
209 | var pre_cursor = editor.getRange({line:cur.line,ch:0},cur); | |
208 | if (pre_cursor.trim() === "") { |
|
210 | if (pre_cursor.trim() === "") { | |
209 | // Don't autocomplete if the part of the line before the cursor |
|
211 | // Don't autocomplete if the part of the line before the cursor | |
210 | // is empty. In this case, let CodeMirror handle indentation. |
|
212 | // is empty. In this case, let CodeMirror handle indentation. | |
211 | return false; |
|
213 | return false; | |
212 | } else if ((pre_cursor.substr(-1) === "("|| pre_cursor.substr(-1) === " ") && IPython.config.tooltip_on_tab ) { |
|
214 | } else if ((pre_cursor.substr(-1) === "("|| pre_cursor.substr(-1) === " ") && IPython.config.tooltip_on_tab ) { | |
213 | IPython.tooltip.request(that); |
|
215 | IPython.tooltip.request(that); | |
214 | // Prevent the event from bubbling up. |
|
216 | // Prevent the event from bubbling up. | |
215 | event.stop(); |
|
217 | event.stop(); | |
216 | // Prevent CodeMirror from handling the tab. |
|
218 | // Prevent CodeMirror from handling the tab. | |
217 | return true; |
|
219 | return true; | |
218 | } else { |
|
220 | } else { | |
219 | event.stop(); |
|
221 | event.stop(); | |
220 | this.completer.startCompletion(); |
|
222 | this.completer.startCompletion(); | |
221 | return true; |
|
223 | return true; | |
222 | }; |
|
224 | }; | |
223 | } else { |
|
225 | } else { | |
224 | // keypress/keyup also trigger on TAB press, and we don't want to |
|
226 | // keypress/keyup also trigger on TAB press, and we don't want to | |
225 | // use those to disable tab completion. |
|
227 | // use those to disable tab completion. | |
226 | return false; |
|
228 | return false; | |
227 | }; |
|
229 | }; | |
228 | return false; |
|
230 | return false; | |
229 | }; |
|
231 | }; | |
230 |
|
232 | |||
231 |
|
233 | |||
232 | // Kernel related calls. |
|
234 | // Kernel related calls. | |
233 |
|
235 | |||
234 | CodeCell.prototype.set_kernel = function (kernel) { |
|
236 | CodeCell.prototype.set_kernel = function (kernel) { | |
235 | this.kernel = kernel; |
|
237 | this.kernel = kernel; | |
236 | } |
|
238 | } | |
237 |
|
239 | |||
238 | /** |
|
240 | /** | |
239 | * Execute current code cell to the kernel |
|
241 | * Execute current code cell to the kernel | |
240 | * @method execute |
|
242 | * @method execute | |
241 | */ |
|
243 | */ | |
242 | CodeCell.prototype.execute = function () { |
|
244 | CodeCell.prototype.execute = function () { | |
243 | this.output_area.clear_output(true, true, true); |
|
245 | this.output_area.clear_output(true, true, true); | |
244 | this.set_input_prompt('*'); |
|
246 | this.set_input_prompt('*'); | |
245 | this.element.addClass("running"); |
|
247 | this.element.addClass("running"); | |
246 | var callbacks = { |
|
248 | var callbacks = { | |
247 | 'execute_reply': $.proxy(this._handle_execute_reply, this), |
|
249 | 'execute_reply': $.proxy(this._handle_execute_reply, this), | |
248 | 'output': $.proxy(this.output_area.handle_output, this.output_area), |
|
250 | 'output': $.proxy(this.output_area.handle_output, this.output_area), | |
249 | 'clear_output': $.proxy(this.output_area.handle_clear_output, this.output_area), |
|
251 | 'clear_output': $.proxy(this.output_area.handle_clear_output, this.output_area), | |
250 | 'set_next_input': $.proxy(this._handle_set_next_input, this), |
|
252 | 'set_next_input': $.proxy(this._handle_set_next_input, this), | |
251 | 'input_request': $.proxy(this._handle_input_request, this) |
|
253 | 'input_request': $.proxy(this._handle_input_request, this) | |
252 | }; |
|
254 | }; | |
253 | var msg_id = this.kernel.execute(this.get_text(), callbacks, {silent: false}); |
|
255 | var msg_id = this.kernel.execute(this.get_text(), callbacks, {silent: false}); | |
254 | }; |
|
256 | }; | |
255 |
|
257 | |||
256 | /** |
|
258 | /** | |
257 | * @method _handle_execute_reply |
|
259 | * @method _handle_execute_reply | |
258 | * @private |
|
260 | * @private | |
259 | */ |
|
261 | */ | |
260 | CodeCell.prototype._handle_execute_reply = function (content) { |
|
262 | CodeCell.prototype._handle_execute_reply = function (content) { | |
261 | this.set_input_prompt(content.execution_count); |
|
263 | this.set_input_prompt(content.execution_count); | |
262 | this.element.removeClass("running"); |
|
264 | this.element.removeClass("running"); | |
263 | $([IPython.events]).trigger('set_dirty.Notebook', {value: true}); |
|
265 | $([IPython.events]).trigger('set_dirty.Notebook', {value: true}); | |
264 | } |
|
266 | } | |
265 |
|
267 | |||
266 | /** |
|
268 | /** | |
267 | * @method _handle_set_next_input |
|
269 | * @method _handle_set_next_input | |
268 | * @private |
|
270 | * @private | |
269 | */ |
|
271 | */ | |
270 | CodeCell.prototype._handle_set_next_input = function (text) { |
|
272 | CodeCell.prototype._handle_set_next_input = function (text) { | |
271 | var data = {'cell': this, 'text': text} |
|
273 | var data = {'cell': this, 'text': text} | |
272 | $([IPython.events]).trigger('set_next_input.Notebook', data); |
|
274 | $([IPython.events]).trigger('set_next_input.Notebook', data); | |
273 | } |
|
275 | } | |
274 |
|
276 | |||
275 | /** |
|
277 | /** | |
276 | * @method _handle_input_request |
|
278 | * @method _handle_input_request | |
277 | * @private |
|
279 | * @private | |
278 | */ |
|
280 | */ | |
279 | CodeCell.prototype._handle_input_request = function (content) { |
|
281 | CodeCell.prototype._handle_input_request = function (content) { | |
280 | this.output_area.append_raw_input(content); |
|
282 | this.output_area.append_raw_input(content); | |
281 | } |
|
283 | } | |
282 |
|
284 | |||
283 |
|
285 | |||
284 | // Basic cell manipulation. |
|
286 | // Basic cell manipulation. | |
285 |
|
287 | |||
286 | CodeCell.prototype.select = function () { |
|
288 | CodeCell.prototype.select = function () { | |
287 | IPython.Cell.prototype.select.apply(this); |
|
289 | IPython.Cell.prototype.select.apply(this); | |
288 | this.code_mirror.refresh(); |
|
290 | this.code_mirror.refresh(); | |
289 | this.code_mirror.focus(); |
|
291 | this.code_mirror.focus(); | |
290 | this.auto_highlight(); |
|
292 | this.auto_highlight(); | |
291 | // We used to need an additional refresh() after the focus, but |
|
293 | // We used to need an additional refresh() after the focus, but | |
292 | // it appears that this has been fixed in CM. This bug would show |
|
294 | // it appears that this has been fixed in CM. This bug would show | |
293 | // up on FF when a newly loaded markdown cell was edited. |
|
295 | // up on FF when a newly loaded markdown cell was edited. | |
294 | }; |
|
296 | }; | |
295 |
|
297 | |||
296 |
|
298 | |||
297 | CodeCell.prototype.select_all = function () { |
|
299 | CodeCell.prototype.select_all = function () { | |
298 | var start = {line: 0, ch: 0}; |
|
300 | var start = {line: 0, ch: 0}; | |
299 | var nlines = this.code_mirror.lineCount(); |
|
301 | var nlines = this.code_mirror.lineCount(); | |
300 | var last_line = this.code_mirror.getLine(nlines-1); |
|
302 | var last_line = this.code_mirror.getLine(nlines-1); | |
301 | var end = {line: nlines-1, ch: last_line.length}; |
|
303 | var end = {line: nlines-1, ch: last_line.length}; | |
302 | this.code_mirror.setSelection(start, end); |
|
304 | this.code_mirror.setSelection(start, end); | |
303 | }; |
|
305 | }; | |
304 |
|
306 | |||
305 |
|
307 | |||
306 | CodeCell.prototype.collapse = function () { |
|
308 | CodeCell.prototype.collapse = function () { | |
307 | this.collapsed = true; |
|
309 | this.collapsed = true; | |
308 | this.output_area.collapse(); |
|
310 | this.output_area.collapse(); | |
309 | }; |
|
311 | }; | |
310 |
|
312 | |||
311 |
|
313 | |||
312 | CodeCell.prototype.expand = function () { |
|
314 | CodeCell.prototype.expand = function () { | |
313 | this.collapsed = false; |
|
315 | this.collapsed = false; | |
314 | this.output_area.expand(); |
|
316 | this.output_area.expand(); | |
315 | }; |
|
317 | }; | |
316 |
|
318 | |||
317 |
|
319 | |||
318 | CodeCell.prototype.toggle_output = function () { |
|
320 | CodeCell.prototype.toggle_output = function () { | |
319 | this.collapsed = Boolean(1 - this.collapsed); |
|
321 | this.collapsed = Boolean(1 - this.collapsed); | |
320 | this.output_area.toggle_output(); |
|
322 | this.output_area.toggle_output(); | |
321 | }; |
|
323 | }; | |
322 |
|
324 | |||
323 |
|
325 | |||
324 | CodeCell.prototype.toggle_output_scroll = function () { |
|
326 | CodeCell.prototype.toggle_output_scroll = function () { | |
325 | this.output_area.toggle_scroll(); |
|
327 | this.output_area.toggle_scroll(); | |
326 | }; |
|
328 | }; | |
327 |
|
329 | |||
328 |
|
330 | |||
329 | CodeCell.input_prompt_classical = function (prompt_value, lines_number) { |
|
331 | CodeCell.input_prompt_classical = function (prompt_value, lines_number) { | |
330 | var ns = prompt_value || " "; |
|
332 | var ns = prompt_value || " "; | |
331 | return 'In [' + ns + ']:' |
|
333 | return 'In [' + ns + ']:' | |
332 | }; |
|
334 | }; | |
333 |
|
335 | |||
334 | CodeCell.input_prompt_continuation = function (prompt_value, lines_number) { |
|
336 | CodeCell.input_prompt_continuation = function (prompt_value, lines_number) { | |
335 | var html = [CodeCell.input_prompt_classical(prompt_value, lines_number)]; |
|
337 | var html = [CodeCell.input_prompt_classical(prompt_value, lines_number)]; | |
336 | for(var i=1; i < lines_number; i++){html.push(['...:'])}; |
|
338 | for(var i=1; i < lines_number; i++){html.push(['...:'])}; | |
337 | return html.join('</br>') |
|
339 | return html.join('</br>') | |
338 | }; |
|
340 | }; | |
339 |
|
341 | |||
340 | CodeCell.input_prompt_function = CodeCell.input_prompt_classical; |
|
342 | CodeCell.input_prompt_function = CodeCell.input_prompt_classical; | |
341 |
|
343 | |||
342 |
|
344 | |||
343 | CodeCell.prototype.set_input_prompt = function (number) { |
|
345 | CodeCell.prototype.set_input_prompt = function (number) { | |
344 | var nline = 1 |
|
346 | var nline = 1 | |
345 | if( this.code_mirror != undefined) { |
|
347 | if( this.code_mirror != undefined) { | |
346 | nline = this.code_mirror.lineCount(); |
|
348 | nline = this.code_mirror.lineCount(); | |
347 | } |
|
349 | } | |
348 | this.input_prompt_number = number; |
|
350 | this.input_prompt_number = number; | |
349 | var prompt_html = CodeCell.input_prompt_function(this.input_prompt_number, nline); |
|
351 | var prompt_html = CodeCell.input_prompt_function(this.input_prompt_number, nline); | |
350 | this.element.find('div.input_prompt').html(prompt_html); |
|
352 | this.element.find('div.input_prompt').html(prompt_html); | |
351 | }; |
|
353 | }; | |
352 |
|
354 | |||
353 |
|
355 | |||
354 | CodeCell.prototype.clear_input = function () { |
|
356 | CodeCell.prototype.clear_input = function () { | |
355 | this.code_mirror.setValue(''); |
|
357 | this.code_mirror.setValue(''); | |
356 | }; |
|
358 | }; | |
357 |
|
359 | |||
358 |
|
360 | |||
359 | CodeCell.prototype.get_text = function () { |
|
361 | CodeCell.prototype.get_text = function () { | |
360 | return this.code_mirror.getValue(); |
|
362 | return this.code_mirror.getValue(); | |
361 | }; |
|
363 | }; | |
362 |
|
364 | |||
363 |
|
365 | |||
364 | CodeCell.prototype.set_text = function (code) { |
|
366 | CodeCell.prototype.set_text = function (code) { | |
365 | return this.code_mirror.setValue(code); |
|
367 | return this.code_mirror.setValue(code); | |
366 | }; |
|
368 | }; | |
367 |
|
369 | |||
368 |
|
370 | |||
369 | CodeCell.prototype.at_top = function () { |
|
371 | CodeCell.prototype.at_top = function () { | |
370 | var cursor = this.code_mirror.getCursor(); |
|
372 | var cursor = this.code_mirror.getCursor(); | |
371 | if (cursor.line === 0 && cursor.ch === 0) { |
|
373 | if (cursor.line === 0 && cursor.ch === 0) { | |
372 | return true; |
|
374 | return true; | |
373 | } else { |
|
375 | } else { | |
374 | return false; |
|
376 | return false; | |
375 | } |
|
377 | } | |
376 | }; |
|
378 | }; | |
377 |
|
379 | |||
378 |
|
380 | |||
379 | CodeCell.prototype.at_bottom = function () { |
|
381 | CodeCell.prototype.at_bottom = function () { | |
380 | var cursor = this.code_mirror.getCursor(); |
|
382 | var cursor = this.code_mirror.getCursor(); | |
381 | if (cursor.line === (this.code_mirror.lineCount()-1) && cursor.ch === this.code_mirror.getLine(cursor.line).length) { |
|
383 | if (cursor.line === (this.code_mirror.lineCount()-1) && cursor.ch === this.code_mirror.getLine(cursor.line).length) { | |
382 | return true; |
|
384 | return true; | |
383 | } else { |
|
385 | } else { | |
384 | return false; |
|
386 | return false; | |
385 | } |
|
387 | } | |
386 | }; |
|
388 | }; | |
387 |
|
389 | |||
388 |
|
390 | |||
389 | CodeCell.prototype.clear_output = function (stdout, stderr, other) { |
|
391 | CodeCell.prototype.clear_output = function (stdout, stderr, other) { | |
390 | this.output_area.clear_output(stdout, stderr, other); |
|
392 | this.output_area.clear_output(stdout, stderr, other); | |
391 | }; |
|
393 | }; | |
392 |
|
394 | |||
393 |
|
395 | |||
394 | // JSON serialization |
|
396 | // JSON serialization | |
395 |
|
397 | |||
396 | CodeCell.prototype.fromJSON = function (data) { |
|
398 | CodeCell.prototype.fromJSON = function (data) { | |
397 | IPython.Cell.prototype.fromJSON.apply(this, arguments); |
|
399 | IPython.Cell.prototype.fromJSON.apply(this, arguments); | |
398 | if (data.cell_type === 'code') { |
|
400 | if (data.cell_type === 'code') { | |
399 | if (data.input !== undefined) { |
|
401 | if (data.input !== undefined) { | |
400 | this.set_text(data.input); |
|
402 | this.set_text(data.input); | |
401 | // make this value the starting point, so that we can only undo |
|
403 | // make this value the starting point, so that we can only undo | |
402 | // to this state, instead of a blank cell |
|
404 | // to this state, instead of a blank cell | |
403 | this.code_mirror.clearHistory(); |
|
405 | this.code_mirror.clearHistory(); | |
404 | this.auto_highlight(); |
|
406 | this.auto_highlight(); | |
405 | } |
|
407 | } | |
406 | if (data.prompt_number !== undefined) { |
|
408 | if (data.prompt_number !== undefined) { | |
407 | this.set_input_prompt(data.prompt_number); |
|
409 | this.set_input_prompt(data.prompt_number); | |
408 | } else { |
|
410 | } else { | |
409 | this.set_input_prompt(); |
|
411 | this.set_input_prompt(); | |
410 | }; |
|
412 | }; | |
411 | this.output_area.fromJSON(data.outputs); |
|
413 | this.output_area.fromJSON(data.outputs); | |
412 | if (data.collapsed !== undefined) { |
|
414 | if (data.collapsed !== undefined) { | |
413 | if (data.collapsed) { |
|
415 | if (data.collapsed) { | |
414 | this.collapse(); |
|
416 | this.collapse(); | |
415 | } else { |
|
417 | } else { | |
416 | this.expand(); |
|
418 | this.expand(); | |
417 | }; |
|
419 | }; | |
418 | }; |
|
420 | }; | |
419 | }; |
|
421 | }; | |
420 | }; |
|
422 | }; | |
421 |
|
423 | |||
422 |
|
424 | |||
423 | CodeCell.prototype.toJSON = function () { |
|
425 | CodeCell.prototype.toJSON = function () { | |
424 | var data = IPython.Cell.prototype.toJSON.apply(this); |
|
426 | var data = IPython.Cell.prototype.toJSON.apply(this); | |
425 | data.input = this.get_text(); |
|
427 | data.input = this.get_text(); | |
426 | data.cell_type = 'code'; |
|
428 | data.cell_type = 'code'; | |
427 | if (this.input_prompt_number) { |
|
429 | if (this.input_prompt_number) { | |
428 | data.prompt_number = this.input_prompt_number; |
|
430 | data.prompt_number = this.input_prompt_number; | |
429 | }; |
|
431 | }; | |
430 | var outputs = this.output_area.toJSON(); |
|
432 | var outputs = this.output_area.toJSON(); | |
431 | data.outputs = outputs; |
|
433 | data.outputs = outputs; | |
432 | data.language = 'python'; |
|
434 | data.language = 'python'; | |
433 | data.collapsed = this.collapsed; |
|
435 | data.collapsed = this.collapsed; | |
434 | return data; |
|
436 | return data; | |
435 | }; |
|
437 | }; | |
436 |
|
438 | |||
437 |
|
439 | |||
438 | IPython.CodeCell = CodeCell; |
|
440 | IPython.CodeCell = CodeCell; | |
439 |
|
441 | |||
440 | return IPython; |
|
442 | return IPython; | |
441 | }(IPython)); |
|
443 | }(IPython)); |
@@ -1,355 +1,363 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 | // Tooltip |
|
8 | // Tooltip | |
9 | //============================================================================ |
|
9 | //============================================================================ | |
10 | // |
|
10 | // | |
11 | // you can set the autocall time by setting `IPython.tooltip.time_before_tooltip` in ms |
|
11 | // you can set the autocall time by setting `IPython.tooltip.time_before_tooltip` in ms | |
12 | // |
|
12 | // | |
13 | // you can configure the differents action of pressing tab several times in a row by |
|
13 | // you can configure the differents action of pressing tab several times in a row by | |
14 | // setting/appending different fonction in the array |
|
14 | // setting/appending different fonction in the array | |
15 | // IPython.tooltip.tabs_functions |
|
15 | // IPython.tooltip.tabs_functions | |
16 | // |
|
16 | // | |
17 | // eg : |
|
17 | // eg : | |
18 | // IPython.tooltip.tabs_functions[4] = function (){console.log('this is the action of the 4th tab pressing')} |
|
18 | // IPython.tooltip.tabs_functions[4] = function (){console.log('this is the action of the 4th tab pressing')} | |
19 | // |
|
19 | // | |
20 | var IPython = (function (IPython) { |
|
20 | var IPython = (function (IPython) { | |
21 | "use strict"; |
|
21 | "use strict"; | |
22 |
|
22 | |||
23 | var utils = IPython.utils; |
|
23 | var utils = IPython.utils; | |
24 |
|
24 | |||
25 | // tooltip constructor |
|
25 | // tooltip constructor | |
26 | var Tooltip = function () { |
|
26 | var Tooltip = function () { | |
27 | var that = this; |
|
27 | var that = this; | |
28 | this.time_before_tooltip = 1200; |
|
28 | this.time_before_tooltip = 1200; | |
29 |
|
29 | |||
30 | // handle to html |
|
30 | // handle to html | |
31 | this.tooltip = $('#tooltip'); |
|
31 | this.tooltip = $('#tooltip'); | |
32 | this._hidden = true; |
|
32 | this._hidden = true; | |
33 |
|
33 | |||
34 | // variable for consecutive call |
|
34 | // variable for consecutive call | |
35 | this._old_cell = null; |
|
35 | this._old_cell = null; | |
36 | this._old_request = null; |
|
36 | this._old_request = null; | |
37 | this._consecutive_counter = 0; |
|
37 | this._consecutive_counter = 0; | |
38 |
|
38 | |||
39 | // 'sticky ?' |
|
39 | // 'sticky ?' | |
40 | this._sticky = false; |
|
40 | this._sticky = false; | |
41 |
|
41 | |||
|
42 | // display tooltip if the docstring is empty? | |||
|
43 | this._hide_if_no_docstring = false; | |||
|
44 | ||||
42 | // contain the button in the upper right corner |
|
45 | // contain the button in the upper right corner | |
43 | this.buttons = $('<div/>').addClass('tooltipbuttons'); |
|
46 | this.buttons = $('<div/>').addClass('tooltipbuttons'); | |
44 |
|
47 | |||
45 | // will contain the docstring |
|
48 | // will contain the docstring | |
46 | this.text = $('<div/>').addClass('tooltiptext').addClass('smalltooltip'); |
|
49 | this.text = $('<div/>').addClass('tooltiptext').addClass('smalltooltip'); | |
47 |
|
50 | |||
48 | // build the buttons menu on the upper right |
|
51 | // build the buttons menu on the upper right | |
49 | // expand the tooltip to see more |
|
52 | // expand the tooltip to see more | |
50 | var expandlink = $('<a/>').attr('href', "#").addClass("ui-corner-all") //rounded corner |
|
53 | var expandlink = $('<a/>').attr('href', "#").addClass("ui-corner-all") //rounded corner | |
51 | .attr('role', "button").attr('id', 'expanbutton').attr('title', 'Grow the tooltip vertically (press tab 2 times)').click(function () { |
|
54 | .attr('role', "button").attr('id', 'expanbutton').attr('title', 'Grow the tooltip vertically (press tab 2 times)').click(function () { | |
52 | that.expand() |
|
55 | that.expand() | |
53 | }).append( |
|
56 | }).append( | |
54 | $('<span/>').text('Expand').addClass('ui-icon').addClass('ui-icon-plus')); |
|
57 | $('<span/>').text('Expand').addClass('ui-icon').addClass('ui-icon-plus')); | |
55 |
|
58 | |||
56 | // open in pager |
|
59 | // open in pager | |
57 | var morelink = $('<a/>').attr('href', "#").attr('role', "button").addClass('ui-button').attr('title', 'show the current docstring in pager (press tab 4 times)'); |
|
60 | var morelink = $('<a/>').attr('href', "#").attr('role', "button").addClass('ui-button').attr('title', 'show the current docstring in pager (press tab 4 times)'); | |
58 | var morespan = $('<span/>').text('Open in Pager').addClass('ui-icon').addClass('ui-icon-arrowstop-l-n'); |
|
61 | var morespan = $('<span/>').text('Open in Pager').addClass('ui-icon').addClass('ui-icon-arrowstop-l-n'); | |
59 | morelink.append(morespan); |
|
62 | morelink.append(morespan); | |
60 | morelink.click(function () { |
|
63 | morelink.click(function () { | |
61 | that.showInPager(that._old_cell); |
|
64 | that.showInPager(that._old_cell); | |
62 | }); |
|
65 | }); | |
63 |
|
66 | |||
64 | // close the tooltip |
|
67 | // close the tooltip | |
65 | var closelink = $('<a/>').attr('href', "#").attr('role', "button").addClass('ui-button'); |
|
68 | var closelink = $('<a/>').attr('href', "#").attr('role', "button").addClass('ui-button'); | |
66 | var closespan = $('<span/>').text('Close').addClass('ui-icon').addClass('ui-icon-close'); |
|
69 | var closespan = $('<span/>').text('Close').addClass('ui-icon').addClass('ui-icon-close'); | |
67 | closelink.append(closespan); |
|
70 | closelink.append(closespan); | |
68 | closelink.click(function () { |
|
71 | closelink.click(function () { | |
69 | that.remove_and_cancel_tooltip(true); |
|
72 | that.remove_and_cancel_tooltip(true); | |
70 | }); |
|
73 | }); | |
71 |
|
74 | |||
72 | this._clocklink = $('<a/>').attr('href', "#"); |
|
75 | this._clocklink = $('<a/>').attr('href', "#"); | |
73 | this._clocklink.attr('role', "button"); |
|
76 | this._clocklink.attr('role', "button"); | |
74 | this._clocklink.addClass('ui-button'); |
|
77 | this._clocklink.addClass('ui-button'); | |
75 | this._clocklink.attr('title', 'Tootip is not dismissed while typing for 10 seconds'); |
|
78 | this._clocklink.attr('title', 'Tootip is not dismissed while typing for 10 seconds'); | |
76 | var clockspan = $('<span/>').text('Close'); |
|
79 | var clockspan = $('<span/>').text('Close'); | |
77 | clockspan.addClass('ui-icon'); |
|
80 | clockspan.addClass('ui-icon'); | |
78 | clockspan.addClass('ui-icon-clock'); |
|
81 | clockspan.addClass('ui-icon-clock'); | |
79 | this._clocklink.append(clockspan); |
|
82 | this._clocklink.append(clockspan); | |
80 | this._clocklink.click(function () { |
|
83 | this._clocklink.click(function () { | |
81 | that.cancel_stick(); |
|
84 | that.cancel_stick(); | |
82 | }); |
|
85 | }); | |
83 |
|
86 | |||
84 |
|
87 | |||
85 |
|
88 | |||
86 |
|
89 | |||
87 | //construct the tooltip |
|
90 | //construct the tooltip | |
88 | // add in the reverse order you want them to appear |
|
91 | // add in the reverse order you want them to appear | |
89 | this.buttons.append(closelink); |
|
92 | this.buttons.append(closelink); | |
90 | this.buttons.append(expandlink); |
|
93 | this.buttons.append(expandlink); | |
91 | this.buttons.append(morelink); |
|
94 | this.buttons.append(morelink); | |
92 | this.buttons.append(this._clocklink); |
|
95 | this.buttons.append(this._clocklink); | |
93 | this._clocklink.hide(); |
|
96 | this._clocklink.hide(); | |
94 |
|
97 | |||
95 |
|
98 | |||
96 | // we need a phony element to make the small arrow |
|
99 | // we need a phony element to make the small arrow | |
97 | // of the tooltip in css |
|
100 | // of the tooltip in css | |
98 | // we will move the arrow later |
|
101 | // we will move the arrow later | |
99 | this.arrow = $('<div/>').addClass('pretooltiparrow'); |
|
102 | this.arrow = $('<div/>').addClass('pretooltiparrow'); | |
100 | this.tooltip.append(this.buttons); |
|
103 | this.tooltip.append(this.buttons); | |
101 | this.tooltip.append(this.arrow); |
|
104 | this.tooltip.append(this.arrow); | |
102 | this.tooltip.append(this.text); |
|
105 | this.tooltip.append(this.text); | |
103 |
|
106 | |||
104 | // function that will be called if you press tab 1, 2, 3... times in a row |
|
107 | // function that will be called if you press tab 1, 2, 3... times in a row | |
105 | this.tabs_functions = [function (cell, text) { |
|
108 | this.tabs_functions = [function (cell, text) { | |
106 | that._request_tooltip(cell, text); |
|
109 | that._request_tooltip(cell, text); | |
107 | }, function () { |
|
110 | }, function () { | |
108 | that.expand(); |
|
111 | that.expand(); | |
109 | }, function () { |
|
112 | }, function () { | |
110 | that.stick(); |
|
113 | that.stick(); | |
111 | }, function (cell) { |
|
114 | }, function (cell) { | |
112 | that.cancel_stick(); |
|
115 | that.cancel_stick(); | |
113 | that.showInPager(cell); |
|
116 | that.showInPager(cell); | |
114 | }]; |
|
117 | }]; | |
115 | // call after all the tabs function above have bee call to clean their effects |
|
118 | // call after all the tabs function above have bee call to clean their effects | |
116 | // if necessary |
|
119 | // if necessary | |
117 | this.reset_tabs_function = function (cell, text) { |
|
120 | this.reset_tabs_function = function (cell, text) { | |
118 | this._old_cell = (cell) ? cell : null; |
|
121 | this._old_cell = (cell) ? cell : null; | |
119 | this._old_request = (text) ? text : null; |
|
122 | this._old_request = (text) ? text : null; | |
120 | this._consecutive_counter = 0; |
|
123 | this._consecutive_counter = 0; | |
121 | } |
|
124 | } | |
122 | }; |
|
125 | }; | |
123 |
|
126 | |||
124 | Tooltip.prototype.showInPager = function (cell) { |
|
127 | Tooltip.prototype.showInPager = function (cell) { | |
125 | // reexecute last call in pager by appending ? to show back in pager |
|
128 | // reexecute last call in pager by appending ? to show back in pager | |
126 | var that = this; |
|
129 | var that = this; | |
127 | var empty = function () {}; |
|
130 | var empty = function () {}; | |
128 | cell.kernel.execute( |
|
131 | cell.kernel.execute( | |
129 | that.name + '?', { |
|
132 | that.name + '?', { | |
130 | 'execute_reply': empty, |
|
133 | 'execute_reply': empty, | |
131 | 'output': empty, |
|
134 | 'output': empty, | |
132 | 'clear_output': empty, |
|
135 | 'clear_output': empty, | |
133 | 'cell': cell |
|
136 | 'cell': cell | |
134 | }, { |
|
137 | }, { | |
135 | 'silent': false |
|
138 | 'silent': false | |
136 | }); |
|
139 | }); | |
137 | this.remove_and_cancel_tooltip(); |
|
140 | this.remove_and_cancel_tooltip(); | |
138 | } |
|
141 | } | |
139 |
|
142 | |||
140 | // grow the tooltip verticaly |
|
143 | // grow the tooltip verticaly | |
141 | Tooltip.prototype.expand = function () { |
|
144 | Tooltip.prototype.expand = function () { | |
142 | this.text.removeClass('smalltooltip'); |
|
145 | this.text.removeClass('smalltooltip'); | |
143 | this.text.addClass('bigtooltip'); |
|
146 | this.text.addClass('bigtooltip'); | |
144 | $('#expanbutton').hide('slow'); |
|
147 | $('#expanbutton').hide('slow'); | |
145 | } |
|
148 | } | |
146 |
|
149 | |||
147 | // deal with all the logic of hiding the tooltip |
|
150 | // deal with all the logic of hiding the tooltip | |
148 | // and reset it's status |
|
151 | // and reset it's status | |
149 | Tooltip.prototype._hide = function () { |
|
152 | Tooltip.prototype._hide = function () { | |
150 | this.tooltip.fadeOut('fast'); |
|
153 | this.tooltip.fadeOut('fast'); | |
151 | $('#expanbutton').show('slow'); |
|
154 | $('#expanbutton').show('slow'); | |
152 | this.text.removeClass('bigtooltip'); |
|
155 | this.text.removeClass('bigtooltip'); | |
153 | this.text.addClass('smalltooltip'); |
|
156 | this.text.addClass('smalltooltip'); | |
154 | // keep scroll top to be sure to always see the first line |
|
157 | // keep scroll top to be sure to always see the first line | |
155 | this.text.scrollTop(0); |
|
158 | this.text.scrollTop(0); | |
156 | this._hidden = true; |
|
159 | this._hidden = true; | |
157 | this.code_mirror = null; |
|
160 | this.code_mirror = null; | |
158 | } |
|
161 | } | |
159 |
|
162 | |||
160 | Tooltip.prototype.remove_and_cancel_tooltip = function (force) { |
|
163 | Tooltip.prototype.remove_and_cancel_tooltip = function (force) { | |
161 | // note that we don't handle closing directly inside the calltip |
|
164 | // note that we don't handle closing directly inside the calltip | |
162 | // as in the completer, because it is not focusable, so won't |
|
165 | // as in the completer, because it is not focusable, so won't | |
163 | // get the event. |
|
166 | // get the event. | |
164 | if (this._sticky == false || force == true) { |
|
167 | if (this._sticky == false || force == true) { | |
165 | this.cancel_stick(); |
|
168 | this.cancel_stick(); | |
166 | this._hide(); |
|
169 | this._hide(); | |
167 | } |
|
170 | } | |
168 | this.cancel_pending(); |
|
171 | this.cancel_pending(); | |
169 | this.reset_tabs_function(); |
|
172 | this.reset_tabs_function(); | |
170 | } |
|
173 | } | |
171 |
|
174 | |||
172 | // cancel autocall done after '(' for example. |
|
175 | // cancel autocall done after '(' for example. | |
173 | Tooltip.prototype.cancel_pending = function () { |
|
176 | Tooltip.prototype.cancel_pending = function () { | |
174 | if (this._tooltip_timeout != null) { |
|
177 | if (this._tooltip_timeout != null) { | |
175 | clearTimeout(this._tooltip_timeout); |
|
178 | clearTimeout(this._tooltip_timeout); | |
176 | this._tooltip_timeout = null; |
|
179 | this._tooltip_timeout = null; | |
177 | } |
|
180 | } | |
178 | } |
|
181 | } | |
179 |
|
182 | |||
180 | // will trigger tooltip after timeout |
|
183 | // will trigger tooltip after timeout | |
181 | Tooltip.prototype.pending = function (cell) { |
|
184 | Tooltip.prototype.pending = function (cell, hide_if_no_docstring) { | |
182 | var that = this; |
|
185 | var that = this; | |
183 | this._tooltip_timeout = setTimeout(function () { |
|
186 | this._tooltip_timeout = setTimeout(function () { | |
184 | that.request(cell) |
|
187 | that.request(cell, hide_if_no_docstring) | |
185 | }, that.time_before_tooltip); |
|
188 | }, that.time_before_tooltip); | |
186 | } |
|
189 | } | |
187 |
|
190 | |||
188 | Tooltip.prototype._request_tooltip = function (cell, func) { |
|
191 | Tooltip.prototype._request_tooltip = function (cell, func) { | |
189 | // use internally just to make the request to the kernel |
|
192 | // use internally just to make the request to the kernel | |
190 | // Feel free to shorten this logic if you are better |
|
193 | // Feel free to shorten this logic if you are better | |
191 | // than me in regEx |
|
194 | // than me in regEx | |
192 | // basicaly you shoul be able to get xxx.xxx.xxx from |
|
195 | // basicaly you shoul be able to get xxx.xxx.xxx from | |
193 | // something(range(10), kwarg=smth) ; xxx.xxx.xxx( firstarg, rand(234,23), kwarg1=2, |
|
196 | // something(range(10), kwarg=smth) ; xxx.xxx.xxx( firstarg, rand(234,23), kwarg1=2, | |
194 | // remove everything between matchin bracket (need to iterate) |
|
197 | // remove everything between matchin bracket (need to iterate) | |
195 | var matchBracket = /\([^\(\)]+\)/g; |
|
198 | var matchBracket = /\([^\(\)]+\)/g; | |
196 | var endBracket = /\([^\(]*$/g; |
|
199 | var endBracket = /\([^\(]*$/g; | |
197 | var oldfunc = func; |
|
200 | var oldfunc = func; | |
198 |
|
201 | |||
199 | func = func.replace(matchBracket, ""); |
|
202 | func = func.replace(matchBracket, ""); | |
200 | while (oldfunc != func) { |
|
203 | while (oldfunc != func) { | |
201 | oldfunc = func; |
|
204 | oldfunc = func; | |
202 | func = func.replace(matchBracket, ""); |
|
205 | func = func.replace(matchBracket, ""); | |
203 | } |
|
206 | } | |
204 | // remove everything after last open bracket |
|
207 | // remove everything after last open bracket | |
205 | func = func.replace(endBracket, ""); |
|
208 | func = func.replace(endBracket, ""); | |
206 |
|
209 | |||
207 | var re = /[a-z_][0-9a-z._]+$/gi; // casse insensitive |
|
210 | var re = /[a-z_][0-9a-z._]+$/gi; // casse insensitive | |
208 | var callbacks = { |
|
211 | var callbacks = { | |
209 | 'object_info_reply': $.proxy(this._show, this) |
|
212 | 'object_info_reply': $.proxy(this._show, this) | |
210 | } |
|
213 | } | |
211 | var msg_id = cell.kernel.object_info_request(re.exec(func), callbacks); |
|
214 | var msg_id = cell.kernel.object_info_request(re.exec(func), callbacks); | |
212 | } |
|
215 | } | |
213 |
|
216 | |||
214 | // make an imediate completion request |
|
217 | // make an imediate completion request | |
215 | Tooltip.prototype.request = function (cell) { |
|
218 | Tooltip.prototype.request = function (cell, hide_if_no_docstring) { | |
216 | // request(codecell) |
|
219 | // request(codecell) | |
217 | // Deal with extracting the text from the cell and counting |
|
220 | // Deal with extracting the text from the cell and counting | |
218 | // call in a row |
|
221 | // call in a row | |
219 | this.cancel_pending(); |
|
222 | this.cancel_pending(); | |
220 | var editor = cell.code_mirror; |
|
223 | var editor = cell.code_mirror; | |
221 | var cursor = editor.getCursor(); |
|
224 | var cursor = editor.getCursor(); | |
222 | var text = editor.getRange({ |
|
225 | var text = editor.getRange({ | |
223 | line: cursor.line, |
|
226 | line: cursor.line, | |
224 | ch: 0 |
|
227 | ch: 0 | |
225 | }, cursor).trim(); |
|
228 | }, cursor).trim(); | |
226 |
|
229 | |||
|
230 | this._hide_if_no_docstring = hide_if_no_docstring; | |||
|
231 | ||||
227 | if(editor.somethingSelected()){ |
|
232 | if(editor.somethingSelected()){ | |
228 | text = editor.getSelection(); |
|
233 | text = editor.getSelection(); | |
229 | } |
|
234 | } | |
230 |
|
235 | |||
231 | // need a permanent handel to code_mirror for future auto recall |
|
236 | // need a permanent handel to code_mirror for future auto recall | |
232 | this.code_mirror = editor; |
|
237 | this.code_mirror = editor; | |
233 |
|
238 | |||
234 | // now we treat the different number of keypress |
|
239 | // now we treat the different number of keypress | |
235 | // first if same cell, same text, increment counter by 1 |
|
240 | // first if same cell, same text, increment counter by 1 | |
236 | if (this._old_cell == cell && this._old_request == text && this._hidden == false) { |
|
241 | if (this._old_cell == cell && this._old_request == text && this._hidden == false) { | |
237 | this._consecutive_counter++; |
|
242 | this._consecutive_counter++; | |
238 | } else { |
|
243 | } else { | |
239 | // else reset |
|
244 | // else reset | |
240 | this.cancel_stick(); |
|
245 | this.cancel_stick(); | |
241 | this.reset_tabs_function (cell, text); |
|
246 | this.reset_tabs_function (cell, text); | |
242 | } |
|
247 | } | |
243 |
|
248 | |||
244 | // don't do anything if line beggin with '(' or is empty |
|
249 | // don't do anything if line beggin with '(' or is empty | |
245 | if (text === "" || text === "(") { |
|
250 | if (text === "" || text === "(") { | |
246 | return; |
|
251 | return; | |
247 | } |
|
252 | } | |
248 |
|
253 | |||
249 | this.tabs_functions[this._consecutive_counter](cell, text); |
|
254 | this.tabs_functions[this._consecutive_counter](cell, text); | |
250 |
|
255 | |||
251 | // then if we are at the end of list function, reset |
|
256 | // then if we are at the end of list function, reset | |
252 | if (this._consecutive_counter == this.tabs_functions.length) this.reset_tabs_function (cell, text); |
|
257 | if (this._consecutive_counter == this.tabs_functions.length) this.reset_tabs_function (cell, text); | |
253 |
|
258 | |||
254 | return; |
|
259 | return; | |
255 | } |
|
260 | } | |
256 |
|
261 | |||
257 | // cancel the option of having the tooltip to stick |
|
262 | // cancel the option of having the tooltip to stick | |
258 | Tooltip.prototype.cancel_stick = function () { |
|
263 | Tooltip.prototype.cancel_stick = function () { | |
259 | clearTimeout(this._stick_timeout); |
|
264 | clearTimeout(this._stick_timeout); | |
260 | this._stick_timeout = null; |
|
265 | this._stick_timeout = null; | |
261 | this._clocklink.hide('slow'); |
|
266 | this._clocklink.hide('slow'); | |
262 | this._sticky = false; |
|
267 | this._sticky = false; | |
263 | } |
|
268 | } | |
264 |
|
269 | |||
265 | // put the tooltip in a sicky state for 10 seconds |
|
270 | // put the tooltip in a sicky state for 10 seconds | |
266 | // it won't be removed by remove_and_cancell() unless you called with |
|
271 | // it won't be removed by remove_and_cancell() unless you called with | |
267 | // the first parameter set to true. |
|
272 | // the first parameter set to true. | |
268 | // remove_and_cancell_tooltip(true) |
|
273 | // remove_and_cancell_tooltip(true) | |
269 | Tooltip.prototype.stick = function (time) { |
|
274 | Tooltip.prototype.stick = function (time) { | |
270 | time = (time != undefined) ? time : 10; |
|
275 | time = (time != undefined) ? time : 10; | |
271 | var that = this; |
|
276 | var that = this; | |
272 | this._sticky = true; |
|
277 | this._sticky = true; | |
273 | this._clocklink.show('slow'); |
|
278 | this._clocklink.show('slow'); | |
274 | this._stick_timeout = setTimeout(function () { |
|
279 | this._stick_timeout = setTimeout(function () { | |
275 | that._sticky = false; |
|
280 | that._sticky = false; | |
276 | that._clocklink.hide('slow'); |
|
281 | that._clocklink.hide('slow'); | |
277 | }, time * 1000); |
|
282 | }, time * 1000); | |
278 | } |
|
283 | } | |
279 |
|
284 | |||
280 | // should be called with the kernel reply to actually show the tooltip |
|
285 | // should be called with the kernel reply to actually show the tooltip | |
281 | Tooltip.prototype._show = function (reply) { |
|
286 | Tooltip.prototype._show = function (reply) { | |
282 | // move the bubble if it is not hidden |
|
287 | // move the bubble if it is not hidden | |
283 | // otherwise fade it |
|
288 | // otherwise fade it | |
284 | this.name = reply.name; |
|
289 | this.name = reply.name; | |
285 |
|
290 | |||
286 | // do some math to have the tooltip arrow on more or less on left or right |
|
291 | // do some math to have the tooltip arrow on more or less on left or right | |
287 | // width of the editor |
|
292 | // width of the editor | |
288 | var w = $(this.code_mirror.getScrollerElement()).width(); |
|
293 | var w = $(this.code_mirror.getScrollerElement()).width(); | |
289 | // ofset of the editor |
|
294 | // ofset of the editor | |
290 | var o = $(this.code_mirror.getScrollerElement()).offset(); |
|
295 | var o = $(this.code_mirror.getScrollerElement()).offset(); | |
291 |
|
296 | |||
292 | // whatever anchor/head order but arrow at mid x selection |
|
297 | // whatever anchor/head order but arrow at mid x selection | |
293 | var anchor = this.code_mirror.cursorCoords(false); |
|
298 | var anchor = this.code_mirror.cursorCoords(false); | |
294 | var head = this.code_mirror.cursorCoords(true); |
|
299 | var head = this.code_mirror.cursorCoords(true); | |
295 | var xinit = (head.left+anchor.left)/2; |
|
300 | var xinit = (head.left+anchor.left)/2; | |
296 | var xinter = o.left + (xinit - o.left) / w * (w - 450); |
|
301 | var xinter = o.left + (xinit - o.left) / w * (w - 450); | |
297 | var posarrowleft = xinit - xinter; |
|
302 | var posarrowleft = xinit - xinter; | |
298 |
|
303 | |||
299 | if (this._hidden == false) { |
|
304 | if (this._hidden == false) { | |
300 | this.tooltip.animate({ |
|
305 | this.tooltip.animate({ | |
301 | 'left': xinter - 30 + 'px', |
|
306 | 'left': xinter - 30 + 'px', | |
302 | 'top': (head.bottom + 10) + 'px' |
|
307 | 'top': (head.bottom + 10) + 'px' | |
303 | }); |
|
308 | }); | |
304 | } else { |
|
309 | } else { | |
305 | this.tooltip.css({ |
|
310 | this.tooltip.css({ | |
306 | 'left': xinter - 30 + 'px' |
|
311 | 'left': xinter - 30 + 'px' | |
307 | }); |
|
312 | }); | |
308 | this.tooltip.css({ |
|
313 | this.tooltip.css({ | |
309 | 'top': (head.bottom + 10) + 'px' |
|
314 | 'top': (head.bottom + 10) + 'px' | |
310 | }); |
|
315 | }); | |
311 | } |
|
316 | } | |
312 | this.arrow.animate({ |
|
317 | this.arrow.animate({ | |
313 | 'left': posarrowleft + 'px' |
|
318 | 'left': posarrowleft + 'px' | |
314 | }); |
|
319 | }); | |
315 | this.tooltip.fadeIn('fast'); |
|
|||
316 | this._hidden = false; |
|
|||
317 |
|
320 | |||
318 | // build docstring |
|
321 | // build docstring | |
319 | var defstring = reply.call_def; |
|
322 | var defstring = reply.call_def; | |
320 | if (defstring == null) { |
|
323 | if (defstring == null) { | |
321 | defstring = reply.init_definition; |
|
324 | defstring = reply.init_definition; | |
322 | } |
|
325 | } | |
323 | if (defstring == null) { |
|
326 | if (defstring == null) { | |
324 | defstring = reply.definition; |
|
327 | defstring = reply.definition; | |
325 | } |
|
328 | } | |
326 |
|
329 | |||
327 | var docstring = reply.call_docstring; |
|
330 | var docstring = reply.call_docstring; | |
328 | if (docstring == null) { |
|
331 | if (docstring == null) { | |
329 | docstring = reply.init_docstring; |
|
332 | docstring = reply.init_docstring; | |
330 | } |
|
333 | } | |
331 | if (docstring == null) { |
|
334 | if (docstring == null) { | |
332 | docstring = reply.docstring; |
|
335 | docstring = reply.docstring; | |
333 | } |
|
336 | } | |
334 | if (docstring == null) { |
|
337 | ||
|
338 | if (docstring == null && this._hide_if_no_docstring) { | |||
|
339 | return; | |||
|
340 | } else { | |||
335 | docstring = "<empty docstring>"; |
|
341 | docstring = "<empty docstring>"; | |
336 | } |
|
342 | } | |
337 |
|
343 | |||
|
344 | this.tooltip.fadeIn('fast'); | |||
|
345 | this._hidden = false; | |||
338 | this.text.children().remove(); |
|
346 | this.text.children().remove(); | |
339 |
|
347 | |||
340 | var pre = $('<pre/>').html(utils.fixConsole(docstring)); |
|
348 | var pre = $('<pre/>').html(utils.fixConsole(docstring)); | |
341 | if (defstring) { |
|
349 | if (defstring) { | |
342 | var defstring_html = $('<pre/>').html(utils.fixConsole(defstring)); |
|
350 | var defstring_html = $('<pre/>').html(utils.fixConsole(defstring)); | |
343 | this.text.append(defstring_html); |
|
351 | this.text.append(defstring_html); | |
344 | } |
|
352 | } | |
345 | this.text.append(pre); |
|
353 | this.text.append(pre); | |
346 | // keep scroll top to be sure to always see the first line |
|
354 | // keep scroll top to be sure to always see the first line | |
347 | this.text.scrollTop(0); |
|
355 | this.text.scrollTop(0); | |
348 | } |
|
356 | } | |
349 |
|
357 | |||
350 |
|
358 | |||
351 | IPython.Tooltip = Tooltip; |
|
359 | IPython.Tooltip = Tooltip; | |
352 |
|
360 | |||
353 | return IPython; |
|
361 | return IPython; | |
354 |
|
362 | |||
355 | }(IPython)); |
|
363 | }(IPython)); |
General Comments 0
You need to be logged in to leave comments.
Login now