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