Show More
@@ -1,251 +1,257 b'' | |||||
1 | //---------------------------------------------------------------------------- |
|
1 | //---------------------------------------------------------------------------- | |
2 | // Copyright (C) 2011 The IPython Development Team |
|
2 | // Copyright (C) 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 | // Keyboard management |
|
9 | // Keyboard management | |
10 | //============================================================================ |
|
10 | //============================================================================ | |
11 |
|
11 | |||
12 | IPython.namespace('IPython.keyboard'); |
|
12 | IPython.namespace('IPython.keyboard'); | |
13 |
|
13 | |||
14 | IPython.keyboard = (function (IPython) { |
|
14 | IPython.keyboard = (function (IPython) { | |
15 | "use strict"; |
|
15 | "use strict"; | |
16 |
|
16 | |||
17 | // Setup global keycodes and inverse keycodes. |
|
17 | // Setup global keycodes and inverse keycodes. | |
18 |
|
18 | |||
19 | // See http://unixpapa.com/js/key.html for a complete description. The short of |
|
19 | // See http://unixpapa.com/js/key.html for a complete description. The short of | |
20 | // it is that there are different keycode sets. Firefox uses the "Mozilla keycodes" |
|
20 | // it is that there are different keycode sets. Firefox uses the "Mozilla keycodes" | |
21 | // and Webkit/IE use the "IE keycodes". These keycode sets are mostly the same |
|
21 | // and Webkit/IE use the "IE keycodes". These keycode sets are mostly the same | |
22 | // but have minor differences. |
|
22 | // but have minor differences. | |
23 |
|
23 | |||
24 | // These apply to Firefox, (Webkit and IE) |
|
24 | // These apply to Firefox, (Webkit and IE) | |
25 | var _keycodes = { |
|
25 | var _keycodes = { | |
26 | 'a': 65, 'b': 66, 'c': 67, 'd': 68, 'e': 69, 'f': 70, 'g': 71, 'h': 72, 'i': 73, |
|
26 | 'a': 65, 'b': 66, 'c': 67, 'd': 68, 'e': 69, 'f': 70, 'g': 71, 'h': 72, 'i': 73, | |
27 | 'j': 74, 'k': 75, 'l': 76, 'm': 77, 'n': 78, 'o': 79, 'p': 80, 'q': 81, 'r': 82, |
|
27 | 'j': 74, 'k': 75, 'l': 76, 'm': 77, 'n': 78, 'o': 79, 'p': 80, 'q': 81, 'r': 82, | |
28 | 's': 83, 't': 84, 'u': 85, 'v': 86, 'w': 87, 'x': 88, 'y': 89, 'z': 90, |
|
28 | 's': 83, 't': 84, 'u': 85, 'v': 86, 'w': 87, 'x': 88, 'y': 89, 'z': 90, | |
29 | '1 !': 49, '2 @': 50, '3 #': 51, '4 $': 52, '5 %': 53, '6 ^': 54, |
|
29 | '1 !': 49, '2 @': 50, '3 #': 51, '4 $': 52, '5 %': 53, '6 ^': 54, | |
30 | '7 &': 55, '8 *': 56, '9 (': 57, '0 )': 48, |
|
30 | '7 &': 55, '8 *': 56, '9 (': 57, '0 )': 48, | |
31 | '[ {': 219, '] }': 221, '` ~': 192, ', <': 188, '. >': 190, '/ ?': 191, |
|
31 | '[ {': 219, '] }': 221, '` ~': 192, ', <': 188, '. >': 190, '/ ?': 191, | |
32 | '\\ |': 220, '\' "': 222, |
|
32 | '\\ |': 220, '\' "': 222, | |
33 | 'numpad0': 96, 'numpad1': 97, 'numpad2': 98, 'numpad3': 99, 'numpad4': 100, |
|
33 | 'numpad0': 96, 'numpad1': 97, 'numpad2': 98, 'numpad3': 99, 'numpad4': 100, | |
34 | 'numpad5': 101, 'numpad6': 102, 'numpad7': 103, 'numpad8': 104, 'numpad9': 105, |
|
34 | 'numpad5': 101, 'numpad6': 102, 'numpad7': 103, 'numpad8': 104, 'numpad9': 105, | |
35 | 'multiply': 106, 'add': 107, 'subtract': 109, 'decimal': 110, 'divide': 111, |
|
35 | 'multiply': 106, 'add': 107, 'subtract': 109, 'decimal': 110, 'divide': 111, | |
36 | 'f1': 112, 'f2': 113, 'f3': 114, 'f4': 115, 'f5': 116, 'f6': 117, 'f7': 118, |
|
36 | 'f1': 112, 'f2': 113, 'f3': 114, 'f4': 115, 'f5': 116, 'f6': 117, 'f7': 118, | |
37 | 'f8': 119, 'f9': 120, 'f11': 122, 'f12': 123, 'f13': 124, 'f14': 125, 'f15': 126, |
|
37 | 'f8': 119, 'f9': 120, 'f11': 122, 'f12': 123, 'f13': 124, 'f14': 125, 'f15': 126, | |
38 | 'backspace': 8, 'tab': 9, 'enter': 13, 'shift': 16, 'ctrl': 17, 'alt': 18, |
|
38 | 'backspace': 8, 'tab': 9, 'enter': 13, 'shift': 16, 'ctrl': 17, 'alt': 18, | |
39 | 'meta': 91, 'capslock': 20, 'esc': 27, 'space': 32, 'pageup': 33, 'pagedown': 34, |
|
39 | 'meta': 91, 'capslock': 20, 'esc': 27, 'space': 32, 'pageup': 33, 'pagedown': 34, | |
40 | 'end': 35, 'home': 36, 'left': 37, 'up': 38, 'right': 39, 'down': 40, |
|
40 | 'end': 35, 'home': 36, 'left': 37, 'up': 38, 'right': 39, 'down': 40, | |
41 | 'insert': 45, 'delete': 46, 'numlock': 144, |
|
41 | 'insert': 45, 'delete': 46, 'numlock': 144, | |
42 | }; |
|
42 | }; | |
43 |
|
43 | |||
44 | // These apply to Firefox and Opera |
|
44 | // These apply to Firefox and Opera | |
45 | var _mozilla_keycodes = { |
|
45 | var _mozilla_keycodes = { | |
46 | '; :': 59, '= +': 61, '- _': 173, 'meta': 224 |
|
46 | '; :': 59, '= +': 61, '- _': 173, 'meta': 224 | |
47 | } |
|
47 | } | |
48 |
|
48 | |||
49 | // This apply to Webkit and IE |
|
49 | // This apply to Webkit and IE | |
50 | var _ie_keycodes = { |
|
50 | var _ie_keycodes = { | |
51 | '; :': 186, '= +': 187, '- _': 189, |
|
51 | '; :': 186, '= +': 187, '- _': 189, | |
52 | } |
|
52 | } | |
53 |
|
53 | |||
54 | var browser = IPython.utils.browser[0]; |
|
54 | var browser = IPython.utils.browser[0]; | |
55 | var platform = IPython.utils.platform; |
|
55 | var platform = IPython.utils.platform; | |
56 |
|
56 | |||
57 | if (browser === 'Firefox' || browser === 'Opera') { |
|
57 | if (browser === 'Firefox' || browser === 'Opera') { | |
58 | $.extend(_keycodes, _mozilla_keycodes); |
|
58 | $.extend(_keycodes, _mozilla_keycodes); | |
59 | } else if (browser === 'Safari' || browser === 'Chrome' || browser === 'MSIE') { |
|
59 | } else if (browser === 'Safari' || browser === 'Chrome' || browser === 'MSIE') { | |
60 | $.extend(_keycodes, _ie_keycodes); |
|
60 | $.extend(_keycodes, _ie_keycodes); | |
61 | } |
|
61 | } | |
62 |
|
62 | |||
63 | var keycodes = {}; |
|
63 | var keycodes = {}; | |
64 | var inv_keycodes = {}; |
|
64 | var inv_keycodes = {}; | |
65 | for (var name in _keycodes) { |
|
65 | for (var name in _keycodes) { | |
66 | var names = name.split(' '); |
|
66 | var names = name.split(' '); | |
67 | if (names.length === 1) { |
|
67 | if (names.length === 1) { | |
68 | var n = names[0] |
|
68 | var n = names[0] | |
69 | keycodes[n] = _keycodes[n] |
|
69 | keycodes[n] = _keycodes[n] | |
70 | inv_keycodes[_keycodes[n]] = n |
|
70 | inv_keycodes[_keycodes[n]] = n | |
71 | } else { |
|
71 | } else { | |
72 | var primary = names[0]; |
|
72 | var primary = names[0]; | |
73 | var secondary = names[1]; |
|
73 | var secondary = names[1]; | |
74 | keycodes[primary] = _keycodes[name] |
|
74 | keycodes[primary] = _keycodes[name] | |
75 | keycodes[secondary] = _keycodes[name] |
|
75 | keycodes[secondary] = _keycodes[name] | |
76 | inv_keycodes[_keycodes[name]] = primary |
|
76 | inv_keycodes[_keycodes[name]] = primary | |
77 | } |
|
77 | } | |
78 | } |
|
78 | } | |
79 |
|
79 | |||
80 | var normalize_key = function (key) { |
|
80 | var normalize_key = function (key) { | |
81 | return inv_keycodes[keycodes[key]]; |
|
81 | return inv_keycodes[keycodes[key]]; | |
82 | } |
|
82 | } | |
83 |
|
83 | |||
84 | var normalize_shortcut = function (shortcut) { |
|
84 | var normalize_shortcut = function (shortcut) { | |
85 | // Put a shortcut into normalized form: |
|
85 | // Put a shortcut into normalized form: | |
86 | // 1. Make lowercase |
|
86 | // 1. Make lowercase | |
87 | // 2. Replace cmd by meta |
|
87 | // 2. Replace cmd by meta | |
88 | // 3. Sort '+' separated modifiers into the order alt+ctrl+meta+shift |
|
88 | // 3. Sort '+' separated modifiers into the order alt+ctrl+meta+shift | |
89 | // 4. Normalize keys |
|
89 | // 4. Normalize keys | |
90 | shortcut = shortcut.toLowerCase().replace('cmd', 'meta'); |
|
90 | shortcut = shortcut.toLowerCase().replace('cmd', 'meta'); | |
91 | var values = shortcut.split("+"); |
|
91 | var values = shortcut.split("+"); | |
92 | if (values.length === 1) { |
|
92 | if (values.length === 1) { | |
93 | return normalize_key(values[0]) |
|
93 | return normalize_key(values[0]) | |
94 | } else { |
|
94 | } else { | |
95 | var modifiers = values.slice(0,-1); |
|
95 | var modifiers = values.slice(0,-1); | |
96 | var key = normalize_key(values[values.length-1]); |
|
96 | var key = normalize_key(values[values.length-1]); | |
97 | modifiers.sort(); |
|
97 | modifiers.sort(); | |
98 | return modifiers.join('+') + '+' + key; |
|
98 | return modifiers.join('+') + '+' + key; | |
99 | } |
|
99 | } | |
100 | } |
|
100 | } | |
101 |
|
101 | |||
102 | var shortcut_to_event = function (shortcut, type) { |
|
102 | var shortcut_to_event = function (shortcut, type) { | |
103 | // Convert a shortcut (shift+r) to a jQuery Event object |
|
103 | // Convert a shortcut (shift+r) to a jQuery Event object | |
104 | type = type || 'keydown'; |
|
104 | type = type || 'keydown'; | |
105 | shortcut = normalize_shortcut(shortcut); |
|
105 | shortcut = normalize_shortcut(shortcut); | |
106 | var values = shortcut.split("+"); |
|
106 | var values = shortcut.split("+"); | |
107 | var modifiers = values.slice(0,-1); |
|
107 | var modifiers = values.slice(0,-1); | |
108 | var key = values[values.length-1]; |
|
108 | var key = values[values.length-1]; | |
109 | var opts = {which: keycodes[key]}; |
|
109 | var opts = {which: keycodes[key]}; | |
110 | if (modifiers.indexOf('alt') !== -1) {opts.altKey = true;} |
|
110 | if (modifiers.indexOf('alt') !== -1) {opts.altKey = true;} | |
111 | if (modifiers.indexOf('ctrl') !== -1) {opts.ctrlKey = true;} |
|
111 | if (modifiers.indexOf('ctrl') !== -1) {opts.ctrlKey = true;} | |
112 | if (modifiers.indexOf('meta') !== -1) {opts.metaKey = true;} |
|
112 | if (modifiers.indexOf('meta') !== -1) {opts.metaKey = true;} | |
113 | if (modifiers.indexOf('shift') !== -1) {opts.shiftKey = true;} |
|
113 | if (modifiers.indexOf('shift') !== -1) {opts.shiftKey = true;} | |
114 | return $.Event(type, opts); |
|
114 | return $.Event(type, opts); | |
115 | } |
|
115 | } | |
116 |
|
116 | |||
117 | var event_to_shortcut = function (event) { |
|
117 | var event_to_shortcut = function (event) { | |
118 | // Convert a jQuery Event object to a shortcut (shift+r) |
|
118 | // Convert a jQuery Event object to a shortcut (shift+r) | |
119 | var shortcut = ''; |
|
119 | var shortcut = ''; | |
120 | var key = inv_keycodes[event.which] |
|
120 | var key = inv_keycodes[event.which] | |
121 | if (event.altKey && key !== 'alt') {shortcut += 'alt+';} |
|
121 | if (event.altKey && key !== 'alt') {shortcut += 'alt+';} | |
122 | if (event.ctrlKey && key !== 'ctrl') {shortcut += 'ctrl+';} |
|
122 | if (event.ctrlKey && key !== 'ctrl') {shortcut += 'ctrl+';} | |
123 | if (event.metaKey && key !== 'meta') {shortcut += 'meta+';} |
|
123 | if (event.metaKey && key !== 'meta') {shortcut += 'meta+';} | |
124 | if (event.shiftKey && key !== 'shift') {shortcut += 'shift+';} |
|
124 | if (event.shiftKey && key !== 'shift') {shortcut += 'shift+';} | |
125 | shortcut += key; |
|
125 | shortcut += key; | |
126 | return shortcut |
|
126 | return shortcut | |
127 | } |
|
127 | } | |
128 |
|
128 | |||
129 | var trigger_keydown = function (shortcut, element) { |
|
129 | var trigger_keydown = function (shortcut, element) { | |
130 | // Trigger shortcut keydown on an element |
|
130 | // Trigger shortcut keydown on an element | |
131 | element = element || document; |
|
131 | element = element || document; | |
132 | element = $(element); |
|
132 | element = $(element); | |
133 | var event = shortcut_to_event(shortcut, 'keydown'); |
|
133 | var event = shortcut_to_event(shortcut, 'keydown'); | |
134 | element.trigger(event); |
|
134 | element.trigger(event); | |
135 | } |
|
135 | } | |
136 |
|
136 | |||
137 |
|
137 | |||
138 | // Shortcut manager class |
|
138 | // Shortcut manager class | |
139 |
|
139 | |||
140 | var ShortcutManager = function (delay) { |
|
140 | var ShortcutManager = function (delay) { | |
141 | this._shortcuts = {} |
|
141 | this._shortcuts = {} | |
142 | this._counts = {} |
|
142 | this._counts = {} | |
143 | this._timers = {} |
|
143 | this._timers = {} | |
144 | this.delay = delay || 800; // delay in milliseconds |
|
144 | this.delay = delay || 800; // delay in milliseconds | |
145 | } |
|
145 | } | |
146 |
|
146 | |||
147 | ShortcutManager.prototype.help = function () { |
|
147 | ShortcutManager.prototype.help = function () { | |
148 | var help = []; |
|
148 | var help = []; | |
149 | for (var shortcut in this._shortcuts) { |
|
149 | for (var shortcut in this._shortcuts) { | |
150 | var help_string = this._shortcuts[shortcut]['help']; |
|
150 | var help_string = this._shortcuts[shortcut]['help']; | |
151 | var help_index = this._shortcuts[shortcut]['help_index']; |
|
151 | var help_index = this._shortcuts[shortcut]['help_index']; | |
152 | if (help_string) { |
|
152 | if (help_string) { | |
153 | if (platform === 'MacOS') { |
|
153 | if (platform === 'MacOS') { | |
154 | shortcut = shortcut.replace('meta', 'cmd'); |
|
154 | shortcut = shortcut.replace('meta', 'cmd'); | |
155 | } |
|
155 | } | |
156 | help.push({ |
|
156 | help.push({ | |
157 | shortcut: shortcut, |
|
157 | shortcut: shortcut, | |
158 | help: help_string, |
|
158 | help: help_string, | |
159 | help_index: help_index} |
|
159 | help_index: help_index} | |
160 | ); |
|
160 | ); | |
161 | } |
|
161 | } | |
162 | } |
|
162 | } | |
163 | help.sort(function (a, b) { |
|
163 | help.sort(function (a, b) { | |
164 | if (a.help_index > b.help_index) |
|
164 | if (a.help_index > b.help_index) | |
165 | return 1; |
|
165 | return 1; | |
166 | if (a.help_index < b.help_index) |
|
166 | if (a.help_index < b.help_index) | |
167 | return -1; |
|
167 | return -1; | |
168 | return 0; |
|
168 | return 0; | |
169 | }); |
|
169 | }); | |
170 | return help; |
|
170 | return help; | |
171 | } |
|
171 | } | |
172 |
|
172 | |||
173 | ShortcutManager.prototype.clear_shortcuts = function () { |
|
173 | ShortcutManager.prototype.clear_shortcuts = function () { | |
174 | this._shortcuts = {}; |
|
174 | this._shortcuts = {}; | |
175 | } |
|
175 | } | |
176 |
|
176 | |||
177 | ShortcutManager.prototype.add_shortcut = function (shortcut, data) { |
|
177 | ShortcutManager.prototype.add_shortcut = function (shortcut, data) { | |
178 | if (typeof(data) === 'function') { |
|
178 | if (typeof(data) === 'function') { | |
179 | data = {help: '', help_index: '', handler: data} |
|
179 | data = {help: '', help_index: '', handler: data} | |
180 | } |
|
180 | } | |
181 | data.help_index = data.help_index || ''; |
|
181 | data.help_index = data.help_index || ''; | |
182 | data.help = data.help || ''; |
|
182 | data.help = data.help || ''; | |
183 | data.count = data.count || 1; |
|
183 | data.count = data.count || 1; | |
184 | if (data.help_index === '') { |
|
184 | if (data.help_index === '') { | |
185 | data.help_index = 'zz'; |
|
185 | data.help_index = 'zz'; | |
186 | } |
|
186 | } | |
187 | shortcut = normalize_shortcut(shortcut); |
|
187 | shortcut = normalize_shortcut(shortcut); | |
188 | this._counts[shortcut] = 0; |
|
188 | this._counts[shortcut] = 0; | |
189 | this._shortcuts[shortcut] = data; |
|
189 | this._shortcuts[shortcut] = data; | |
190 | } |
|
190 | } | |
191 |
|
191 | |||
192 | ShortcutManager.prototype.add_shortcuts = function (data) { |
|
192 | ShortcutManager.prototype.add_shortcuts = function (data) { | |
193 | for (var shortcut in data) { |
|
193 | for (var shortcut in data) { | |
194 | this.add_shortcut(shortcut, data[shortcut]); |
|
194 | this.add_shortcut(shortcut, data[shortcut]); | |
195 | } |
|
195 | } | |
196 | } |
|
196 | } | |
197 |
|
197 | |||
198 | ShortcutManager.prototype.remove_shortcut = function (shortcut) { |
|
198 | ShortcutManager.prototype.remove_shortcut = function (shortcut) { | |
199 | shortcut = normalize_shortcut(shortcut); |
|
199 | shortcut = normalize_shortcut(shortcut); | |
200 | delete this._counts[shortcut]; |
|
200 | delete this._counts[shortcut]; | |
201 | delete this._shortcuts[shortcut]; |
|
201 | delete this._shortcuts[shortcut]; | |
202 | } |
|
202 | } | |
203 |
|
203 | |||
204 | ShortcutManager.prototype.count_handler = function (shortcut, event, data) { |
|
204 | ShortcutManager.prototype.count_handler = function (shortcut, event, data) { | |
205 | var that = this; |
|
205 | var that = this; | |
206 | var c = this._counts; |
|
206 | var c = this._counts; | |
207 | var t = this._timers; |
|
207 | var t = this._timers; | |
208 | var timer = null; |
|
208 | var timer = null; | |
209 | if (c[shortcut] === data.count-1) { |
|
209 | if (c[shortcut] === data.count-1) { | |
210 | c[shortcut] = 0; |
|
210 | c[shortcut] = 0; | |
211 | var timer = t[shortcut]; |
|
211 | var timer = t[shortcut]; | |
212 | if (timer) {clearTimeout(timer); delete t[shortcut];} |
|
212 | if (timer) {clearTimeout(timer); delete t[shortcut];} | |
213 | return data.handler(event); |
|
213 | return data.handler(event); | |
214 | } else { |
|
214 | } else { | |
215 | c[shortcut] = c[shortcut] + 1; |
|
215 | c[shortcut] = c[shortcut] + 1; | |
216 | timer = setTimeout(function () { |
|
216 | timer = setTimeout(function () { | |
217 | c[shortcut] = 0; |
|
217 | c[shortcut] = 0; | |
218 | }, that.delay); |
|
218 | }, that.delay); | |
219 | t[shortcut] = timer; |
|
219 | t[shortcut] = timer; | |
220 | } |
|
220 | } | |
221 | return false; |
|
221 | return false; | |
222 | } |
|
222 | } | |
223 |
|
223 | |||
224 | ShortcutManager.prototype.call_handler = function (event) { |
|
224 | ShortcutManager.prototype.call_handler = function (event) { | |
225 | var shortcut = event_to_shortcut(event); |
|
225 | var shortcut = event_to_shortcut(event); | |
226 | var data = this._shortcuts[shortcut]; |
|
226 | var data = this._shortcuts[shortcut]; | |
227 | if (data) { |
|
227 | if (data) { | |
228 | var handler = data['handler']; |
|
228 | var handler = data['handler']; | |
229 | if (handler) { |
|
229 | if (handler) { | |
230 | if (data.count === 1) { |
|
230 | if (data.count === 1) { | |
231 | return handler(event); |
|
231 | return handler(event); | |
232 | } else if (data.count > 1) { |
|
232 | } else if (data.count > 1) { | |
233 | return this.count_handler(shortcut, event, data); |
|
233 | return this.count_handler(shortcut, event, data); | |
234 | } |
|
234 | } | |
235 | } |
|
235 | } | |
236 | } |
|
236 | } | |
237 | return true; |
|
237 | return true; | |
238 | } |
|
238 | } | |
239 |
|
239 | |||
|
240 | ShortcutManager.prototype.use_shortcut = function (event) { | |||
|
241 | var shortcut = event_to_shortcut(event); | |||
|
242 | var data = this._shortcuts[shortcut]; | |||
|
243 | return !( data === undefined ) | |||
|
244 | } | |||
|
245 | ||||
240 | return { |
|
246 | return { | |
241 | keycodes : keycodes, |
|
247 | keycodes : keycodes, | |
242 | inv_keycodes : inv_keycodes, |
|
248 | inv_keycodes : inv_keycodes, | |
243 | ShortcutManager : ShortcutManager, |
|
249 | ShortcutManager : ShortcutManager, | |
244 | normalize_key : normalize_key, |
|
250 | normalize_key : normalize_key, | |
245 | normalize_shortcut : normalize_shortcut, |
|
251 | normalize_shortcut : normalize_shortcut, | |
246 | shortcut_to_event : shortcut_to_event, |
|
252 | shortcut_to_event : shortcut_to_event, | |
247 | event_to_shortcut : event_to_shortcut, |
|
253 | event_to_shortcut : event_to_shortcut, | |
248 | trigger_keydown : trigger_keydown |
|
254 | trigger_keydown : trigger_keydown | |
249 | } |
|
255 | } | |
250 |
|
256 | |||
251 | }(IPython)); |
|
257 | }(IPython)); |
@@ -1,596 +1,600 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 | * 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 Cell |
|
15 | * @submodule Cell | |
16 | */ |
|
16 | */ | |
17 |
|
17 | |||
18 | var IPython = (function (IPython) { |
|
18 | var IPython = (function (IPython) { | |
19 | "use strict"; |
|
19 | "use strict"; | |
20 |
|
20 | |||
21 | var utils = IPython.utils; |
|
21 | var utils = IPython.utils; | |
22 | var keycodes = IPython.keyboard.keycodes; |
|
22 | var keycodes = IPython.keyboard.keycodes; | |
23 |
|
23 | |||
24 | /** |
|
24 | /** | |
25 | * The Base `Cell` class from which to inherit |
|
25 | * The Base `Cell` class from which to inherit | |
26 | * @class Cell |
|
26 | * @class Cell | |
27 | **/ |
|
27 | **/ | |
28 |
|
28 | |||
29 | /* |
|
29 | /* | |
30 | * @constructor |
|
30 | * @constructor | |
31 | * |
|
31 | * | |
32 | * @param {object|undefined} [options] |
|
32 | * @param {object|undefined} [options] | |
33 | * @param [options.cm_config] {object} config to pass to CodeMirror, will extend default parameters |
|
33 | * @param [options.cm_config] {object} config to pass to CodeMirror, will extend default parameters | |
34 | */ |
|
34 | */ | |
35 | var Cell = function (options) { |
|
35 | var Cell = function (options) { | |
36 |
|
36 | |||
37 | options = this.mergeopt(Cell, options); |
|
37 | options = this.mergeopt(Cell, options); | |
38 | // superclass default overwrite our default |
|
38 | // superclass default overwrite our default | |
39 |
|
39 | |||
40 | this.placeholder = options.placeholder || ''; |
|
40 | this.placeholder = options.placeholder || ''; | |
41 | this.read_only = options.cm_config.readOnly; |
|
41 | this.read_only = options.cm_config.readOnly; | |
42 | this.selected = false; |
|
42 | this.selected = false; | |
43 | this.rendered = false; |
|
43 | this.rendered = false; | |
44 | this.mode = 'command'; |
|
44 | this.mode = 'command'; | |
45 | this.metadata = {}; |
|
45 | this.metadata = {}; | |
46 | // load this from metadata later ? |
|
46 | // load this from metadata later ? | |
47 | this.user_highlight = 'auto'; |
|
47 | this.user_highlight = 'auto'; | |
48 | this.cm_config = options.cm_config; |
|
48 | this.cm_config = options.cm_config; | |
49 | this.cell_id = utils.uuid(); |
|
49 | this.cell_id = utils.uuid(); | |
50 | this._options = options; |
|
50 | this._options = options; | |
51 |
|
51 | |||
52 | // For JS VM engines optimization, attributes should be all set (even |
|
52 | // For JS VM engines optimization, attributes should be all set (even | |
53 | // to null) in the constructor, and if possible, if different subclass |
|
53 | // to null) in the constructor, and if possible, if different subclass | |
54 | // have new attributes with same name, they should be created in the |
|
54 | // have new attributes with same name, they should be created in the | |
55 | // same order. Easiest is to create and set to null in parent class. |
|
55 | // same order. Easiest is to create and set to null in parent class. | |
56 |
|
56 | |||
57 | this.element = null; |
|
57 | this.element = null; | |
58 | this.cell_type = this.cell_type || null; |
|
58 | this.cell_type = this.cell_type || null; | |
59 | this.code_mirror = null; |
|
59 | this.code_mirror = null; | |
60 |
|
60 | |||
61 | this.create_element(); |
|
61 | this.create_element(); | |
62 | if (this.element !== null) { |
|
62 | if (this.element !== null) { | |
63 | this.element.data("cell", this); |
|
63 | this.element.data("cell", this); | |
64 | this.bind_events(); |
|
64 | this.bind_events(); | |
65 | this.init_classes(); |
|
65 | this.init_classes(); | |
66 | } |
|
66 | } | |
67 | }; |
|
67 | }; | |
68 |
|
68 | |||
69 | Cell.options_default = { |
|
69 | Cell.options_default = { | |
70 | cm_config : { |
|
70 | cm_config : { | |
71 | indentUnit : 4, |
|
71 | indentUnit : 4, | |
72 | readOnly: false, |
|
72 | readOnly: false, | |
73 | theme: "default" |
|
73 | theme: "default" | |
74 | } |
|
74 | } | |
75 | }; |
|
75 | }; | |
76 |
|
76 | |||
77 | // FIXME: Workaround CM Bug #332 (Safari segfault on drag) |
|
77 | // FIXME: Workaround CM Bug #332 (Safari segfault on drag) | |
78 | // by disabling drag/drop altogether on Safari |
|
78 | // by disabling drag/drop altogether on Safari | |
79 | // https://github.com/marijnh/CodeMirror/issues/332 |
|
79 | // https://github.com/marijnh/CodeMirror/issues/332 | |
80 | if (utils.browser[0] == "Safari") { |
|
80 | if (utils.browser[0] == "Safari") { | |
81 | Cell.options_default.cm_config.dragDrop = false; |
|
81 | Cell.options_default.cm_config.dragDrop = false; | |
82 | } |
|
82 | } | |
83 |
|
83 | |||
84 | Cell.prototype.mergeopt = function(_class, options, overwrite){ |
|
84 | Cell.prototype.mergeopt = function(_class, options, overwrite){ | |
85 | options = options || {}; |
|
85 | options = options || {}; | |
86 | overwrite = overwrite || {}; |
|
86 | overwrite = overwrite || {}; | |
87 | return $.extend(true, {}, _class.options_default, options, overwrite); |
|
87 | return $.extend(true, {}, _class.options_default, options, overwrite); | |
88 | }; |
|
88 | }; | |
89 |
|
89 | |||
90 | /** |
|
90 | /** | |
91 | * Empty. Subclasses must implement create_element. |
|
91 | * Empty. Subclasses must implement create_element. | |
92 | * This should contain all the code to create the DOM element in notebook |
|
92 | * This should contain all the code to create the DOM element in notebook | |
93 | * and will be called by Base Class constructor. |
|
93 | * and will be called by Base Class constructor. | |
94 | * @method create_element |
|
94 | * @method create_element | |
95 | */ |
|
95 | */ | |
96 | Cell.prototype.create_element = function () { |
|
96 | Cell.prototype.create_element = function () { | |
97 | }; |
|
97 | }; | |
98 |
|
98 | |||
99 | Cell.prototype.init_classes = function () { |
|
99 | Cell.prototype.init_classes = function () { | |
100 | // Call after this.element exists to initialize the css classes |
|
100 | // Call after this.element exists to initialize the css classes | |
101 | // related to selected, rendered and mode. |
|
101 | // related to selected, rendered and mode. | |
102 | if (this.selected) { |
|
102 | if (this.selected) { | |
103 | this.element.addClass('selected'); |
|
103 | this.element.addClass('selected'); | |
104 | } else { |
|
104 | } else { | |
105 | this.element.addClass('unselected'); |
|
105 | this.element.addClass('unselected'); | |
106 | } |
|
106 | } | |
107 | if (this.rendered) { |
|
107 | if (this.rendered) { | |
108 | this.element.addClass('rendered'); |
|
108 | this.element.addClass('rendered'); | |
109 | } else { |
|
109 | } else { | |
110 | this.element.addClass('unrendered'); |
|
110 | this.element.addClass('unrendered'); | |
111 | } |
|
111 | } | |
112 | if (this.mode === 'edit') { |
|
112 | if (this.mode === 'edit') { | |
113 | this.element.addClass('edit_mode'); |
|
113 | this.element.addClass('edit_mode'); | |
114 | } else { |
|
114 | } else { | |
115 | this.element.addClass('command_mode'); |
|
115 | this.element.addClass('command_mode'); | |
116 | } |
|
116 | } | |
117 | }; |
|
117 | }; | |
118 |
|
118 | |||
119 | /** |
|
119 | /** | |
120 | * Subclasses can implement override bind_events. |
|
120 | * Subclasses can implement override bind_events. | |
121 | * Be carefull to call the parent method when overwriting as it fires event. |
|
121 | * Be carefull to call the parent method when overwriting as it fires event. | |
122 | * this will be triggerd after create_element in constructor. |
|
122 | * this will be triggerd after create_element in constructor. | |
123 | * @method bind_events |
|
123 | * @method bind_events | |
124 | */ |
|
124 | */ | |
125 | Cell.prototype.bind_events = function () { |
|
125 | Cell.prototype.bind_events = function () { | |
126 | var that = this; |
|
126 | var that = this; | |
127 | // We trigger events so that Cell doesn't have to depend on Notebook. |
|
127 | // We trigger events so that Cell doesn't have to depend on Notebook. | |
128 | that.element.click(function (event) { |
|
128 | that.element.click(function (event) { | |
129 | if (!that.selected) { |
|
129 | if (!that.selected) { | |
130 | $([IPython.events]).trigger('select.Cell', {'cell':that}); |
|
130 | $([IPython.events]).trigger('select.Cell', {'cell':that}); | |
131 | } |
|
131 | } | |
132 | }); |
|
132 | }); | |
133 | that.element.focusin(function (event) { |
|
133 | that.element.focusin(function (event) { | |
134 | if (!that.selected) { |
|
134 | if (!that.selected) { | |
135 | $([IPython.events]).trigger('select.Cell', {'cell':that}); |
|
135 | $([IPython.events]).trigger('select.Cell', {'cell':that}); | |
136 | } |
|
136 | } | |
137 | }); |
|
137 | }); | |
138 | if (this.code_mirror) { |
|
138 | if (this.code_mirror) { | |
139 | this.code_mirror.on("change", function(cm, change) { |
|
139 | this.code_mirror.on("change", function(cm, change) { | |
140 | $([IPython.events]).trigger("set_dirty.Notebook", {value: true}); |
|
140 | $([IPython.events]).trigger("set_dirty.Notebook", {value: true}); | |
141 | }); |
|
141 | }); | |
142 | } |
|
142 | } | |
143 | if (this.code_mirror) { |
|
143 | if (this.code_mirror) { | |
144 | this.code_mirror.on('focus', function(cm, change) { |
|
144 | this.code_mirror.on('focus', function(cm, change) { | |
145 | $([IPython.events]).trigger('edit_mode.Cell', {cell: that}); |
|
145 | $([IPython.events]).trigger('edit_mode.Cell', {cell: that}); | |
146 | }); |
|
146 | }); | |
147 | } |
|
147 | } | |
148 | if (this.code_mirror) { |
|
148 | if (this.code_mirror) { | |
149 | this.code_mirror.on('blur', function(cm, change) { |
|
149 | this.code_mirror.on('blur', function(cm, change) { | |
150 | // Check if this unfocus event is legit. |
|
150 | // Check if this unfocus event is legit. | |
151 | if (!that.should_cancel_blur()) { |
|
151 | if (!that.should_cancel_blur()) { | |
152 | $([IPython.events]).trigger('command_mode.Cell', {cell: that}); |
|
152 | $([IPython.events]).trigger('command_mode.Cell', {cell: that}); | |
153 | } |
|
153 | } | |
154 | }); |
|
154 | }); | |
155 | } |
|
155 | } | |
156 | }; |
|
156 | }; | |
157 |
|
157 | |||
158 | /** |
|
158 | /** | |
159 | * This method gets called in CodeMirror's onKeyDown/onKeyPress |
|
159 | * This method gets called in CodeMirror's onKeyDown/onKeyPress | |
160 | * handlers and is used to provide custom key handling. |
|
160 | * handlers and is used to provide custom key handling. | |
161 | * |
|
161 | * | |
162 | * To have custom handling, subclasses should override this method, but still call it |
|
162 | * To have custom handling, subclasses should override this method, but still call it | |
163 | * in order to process the Edit mode keyboard shortcuts. |
|
163 | * in order to process the Edit mode keyboard shortcuts. | |
164 | * |
|
164 | * | |
165 | * @method handle_codemirror_keyevent |
|
165 | * @method handle_codemirror_keyevent | |
166 | * @param {CodeMirror} editor - The codemirror instance bound to the cell |
|
166 | * @param {CodeMirror} editor - The codemirror instance bound to the cell | |
167 | * @param {event} event - |
|
167 | * @param {event} event - | |
168 | * @return {Boolean} `true` if CodeMirror should ignore the event, `false` Otherwise |
|
168 | * @return {Boolean} `true` if CodeMirror should ignore the event, `false` Otherwise | |
169 | */ |
|
169 | */ | |
170 | Cell.prototype.handle_codemirror_keyevent = function (editor, event) { |
|
170 | Cell.prototype.handle_codemirror_keyevent = function (editor, event) { | |
171 | var that = this; |
|
171 | var that = this; | |
|
172 | var shortcuts = IPython.keyboard_manager.edit_shortcuts; | |||
172 |
|
173 | |||
|
174 | // if this is an edit_shortcuts shortcut, we've already handled it. | |||
|
175 | if (shortcuts.use_shortcut(event)) { return true; } | |||
|
176 | ||||
173 | if (event.keyCode === keycodes.enter && (event.shiftKey || event.ctrlKey || event.altKey)) { |
|
177 | if (event.keyCode === keycodes.enter && (event.shiftKey || event.ctrlKey || event.altKey)) { | |
174 | // Always ignore shift-enter in CodeMirror as we handle it. |
|
178 | // Always ignore shift-enter in CodeMirror as we handle it. | |
175 | return true; |
|
179 | return true; | |
176 | } else if (event.which === keycodes.up && event.type === 'keydown') { |
|
180 | } else if (event.which === keycodes.up && event.type === 'keydown') { | |
177 | // If we are not at the top, let CM handle the up arrow and |
|
181 | // If we are not at the top, let CM handle the up arrow and | |
178 | // prevent the global keydown handler from handling it. |
|
182 | // prevent the global keydown handler from handling it. | |
179 | if (!that.at_top()) { |
|
183 | if (!that.at_top()) { | |
180 | event.stop(); |
|
184 | event.stop(); | |
181 | return false; |
|
185 | return false; | |
182 | } else { |
|
186 | } else { | |
183 | return true; |
|
187 | return true; | |
184 | }; |
|
188 | }; | |
185 | } else if (event.which === keycodes.down && event.type === 'keydown') { |
|
189 | } else if (event.which === keycodes.down && event.type === 'keydown') { | |
186 | // If we are not at the bottom, let CM handle the down arrow and |
|
190 | // If we are not at the bottom, let CM handle the down arrow and | |
187 | // prevent the global keydown handler from handling it. |
|
191 | // prevent the global keydown handler from handling it. | |
188 | if (!that.at_bottom()) { |
|
192 | if (!that.at_bottom()) { | |
189 | event.stop(); |
|
193 | event.stop(); | |
190 | return false; |
|
194 | return false; | |
191 | } else { |
|
195 | } else { | |
192 | return true; |
|
196 | return true; | |
193 | }; |
|
197 | }; | |
194 | } else if (event.which === keycodes.esc && event.type === 'keydown') { |
|
198 | } else if (event.which === keycodes.esc && event.type === 'keydown') { | |
195 | if (that.code_mirror.options.keyMap === "vim-insert") { |
|
199 | if (that.code_mirror.options.keyMap === "vim-insert") { | |
196 | // vim keyMap is active and in insert mode. In this case we leave vim |
|
200 | // vim keyMap is active and in insert mode. In this case we leave vim | |
197 | // insert mode, but remain in notebook edit mode. |
|
201 | // insert mode, but remain in notebook edit mode. | |
198 | // Let' CM handle this event and prevent global handling. |
|
202 | // Let' CM handle this event and prevent global handling. | |
199 | event.stop(); |
|
203 | event.stop(); | |
200 | return false; |
|
204 | return false; | |
201 | } else { |
|
205 | } else { | |
202 | // vim keyMap is not active. Leave notebook edit mode. |
|
206 | // vim keyMap is not active. Leave notebook edit mode. | |
203 | // Don't let CM handle the event, defer to global handling. |
|
207 | // Don't let CM handle the event, defer to global handling. | |
204 | return true; |
|
208 | return true; | |
205 | } |
|
209 | } | |
206 | } |
|
210 | } | |
207 | return false; |
|
211 | return false; | |
208 | }; |
|
212 | }; | |
209 |
|
213 | |||
210 |
|
214 | |||
211 | /** |
|
215 | /** | |
212 | * Triger typsetting of math by mathjax on current cell element |
|
216 | * Triger typsetting of math by mathjax on current cell element | |
213 | * @method typeset |
|
217 | * @method typeset | |
214 | */ |
|
218 | */ | |
215 | Cell.prototype.typeset = function () { |
|
219 | Cell.prototype.typeset = function () { | |
216 | if (window.MathJax) { |
|
220 | if (window.MathJax) { | |
217 | var cell_math = this.element.get(0); |
|
221 | var cell_math = this.element.get(0); | |
218 | MathJax.Hub.Queue(["Typeset", MathJax.Hub, cell_math]); |
|
222 | MathJax.Hub.Queue(["Typeset", MathJax.Hub, cell_math]); | |
219 | } |
|
223 | } | |
220 | }; |
|
224 | }; | |
221 |
|
225 | |||
222 | /** |
|
226 | /** | |
223 | * handle cell level logic when a cell is selected |
|
227 | * handle cell level logic when a cell is selected | |
224 | * @method select |
|
228 | * @method select | |
225 | * @return is the action being taken |
|
229 | * @return is the action being taken | |
226 | */ |
|
230 | */ | |
227 | Cell.prototype.select = function () { |
|
231 | Cell.prototype.select = function () { | |
228 | if (!this.selected) { |
|
232 | if (!this.selected) { | |
229 | this.element.addClass('selected'); |
|
233 | this.element.addClass('selected'); | |
230 | this.element.removeClass('unselected'); |
|
234 | this.element.removeClass('unselected'); | |
231 | this.selected = true; |
|
235 | this.selected = true; | |
232 | return true; |
|
236 | return true; | |
233 | } else { |
|
237 | } else { | |
234 | return false; |
|
238 | return false; | |
235 | } |
|
239 | } | |
236 | }; |
|
240 | }; | |
237 |
|
241 | |||
238 | /** |
|
242 | /** | |
239 | * handle cell level logic when a cell is unselected |
|
243 | * handle cell level logic when a cell is unselected | |
240 | * @method unselect |
|
244 | * @method unselect | |
241 | * @return is the action being taken |
|
245 | * @return is the action being taken | |
242 | */ |
|
246 | */ | |
243 | Cell.prototype.unselect = function () { |
|
247 | Cell.prototype.unselect = function () { | |
244 | if (this.selected) { |
|
248 | if (this.selected) { | |
245 | this.element.addClass('unselected'); |
|
249 | this.element.addClass('unselected'); | |
246 | this.element.removeClass('selected'); |
|
250 | this.element.removeClass('selected'); | |
247 | this.selected = false; |
|
251 | this.selected = false; | |
248 | return true; |
|
252 | return true; | |
249 | } else { |
|
253 | } else { | |
250 | return false; |
|
254 | return false; | |
251 | } |
|
255 | } | |
252 | }; |
|
256 | }; | |
253 |
|
257 | |||
254 | /** |
|
258 | /** | |
255 | * handle cell level logic when a cell is rendered |
|
259 | * handle cell level logic when a cell is rendered | |
256 | * @method render |
|
260 | * @method render | |
257 | * @return is the action being taken |
|
261 | * @return is the action being taken | |
258 | */ |
|
262 | */ | |
259 | Cell.prototype.render = function () { |
|
263 | Cell.prototype.render = function () { | |
260 | if (!this.rendered) { |
|
264 | if (!this.rendered) { | |
261 | this.element.addClass('rendered'); |
|
265 | this.element.addClass('rendered'); | |
262 | this.element.removeClass('unrendered'); |
|
266 | this.element.removeClass('unrendered'); | |
263 | this.rendered = true; |
|
267 | this.rendered = true; | |
264 | return true; |
|
268 | return true; | |
265 | } else { |
|
269 | } else { | |
266 | return false; |
|
270 | return false; | |
267 | } |
|
271 | } | |
268 | }; |
|
272 | }; | |
269 |
|
273 | |||
270 | /** |
|
274 | /** | |
271 | * handle cell level logic when a cell is unrendered |
|
275 | * handle cell level logic when a cell is unrendered | |
272 | * @method unrender |
|
276 | * @method unrender | |
273 | * @return is the action being taken |
|
277 | * @return is the action being taken | |
274 | */ |
|
278 | */ | |
275 | Cell.prototype.unrender = function () { |
|
279 | Cell.prototype.unrender = function () { | |
276 | if (this.rendered) { |
|
280 | if (this.rendered) { | |
277 | this.element.addClass('unrendered'); |
|
281 | this.element.addClass('unrendered'); | |
278 | this.element.removeClass('rendered'); |
|
282 | this.element.removeClass('rendered'); | |
279 | this.rendered = false; |
|
283 | this.rendered = false; | |
280 | return true; |
|
284 | return true; | |
281 | } else { |
|
285 | } else { | |
282 | return false; |
|
286 | return false; | |
283 | } |
|
287 | } | |
284 | }; |
|
288 | }; | |
285 |
|
289 | |||
286 | /** |
|
290 | /** | |
287 | * Either delegates keyboard shortcut handling to either IPython keyboard |
|
291 | * Either delegates keyboard shortcut handling to either IPython keyboard | |
288 | * manager when in command mode, or CodeMirror when in edit mode |
|
292 | * manager when in command mode, or CodeMirror when in edit mode | |
289 | * |
|
293 | * | |
290 | * @method handle_keyevent |
|
294 | * @method handle_keyevent | |
291 | * @param {CodeMirror} editor - The codemirror instance bound to the cell |
|
295 | * @param {CodeMirror} editor - The codemirror instance bound to the cell | |
292 | * @param {event} event - |
|
296 | * @param {event} event - | |
293 | * @return {Boolean} `true` if CodeMirror should ignore the event, `false` Otherwise |
|
297 | * @return {Boolean} `true` if CodeMirror should ignore the event, `false` Otherwise | |
294 | */ |
|
298 | */ | |
295 | Cell.prototype.handle_keyevent = function (editor, event) { |
|
299 | Cell.prototype.handle_keyevent = function (editor, event) { | |
296 |
|
300 | |||
297 | // console.log('CM', this.mode, event.which, event.type) |
|
301 | // console.log('CM', this.mode, event.which, event.type) | |
298 |
|
302 | |||
299 | if (this.mode === 'command') { |
|
303 | if (this.mode === 'command') { | |
300 | return true; |
|
304 | return true; | |
301 | } else if (this.mode === 'edit') { |
|
305 | } else if (this.mode === 'edit') { | |
302 | return this.handle_codemirror_keyevent(editor, event); |
|
306 | return this.handle_codemirror_keyevent(editor, event); | |
303 | } |
|
307 | } | |
304 | }; |
|
308 | }; | |
305 |
|
309 | |||
306 | /** |
|
310 | /** | |
307 | * @method at_top |
|
311 | * @method at_top | |
308 | * @return {Boolean} |
|
312 | * @return {Boolean} | |
309 | */ |
|
313 | */ | |
310 | Cell.prototype.at_top = function () { |
|
314 | Cell.prototype.at_top = function () { | |
311 | var cm = this.code_mirror |
|
315 | var cm = this.code_mirror | |
312 | var cursor = cm.getCursor(); |
|
316 | var cursor = cm.getCursor(); | |
313 | if (cursor.line === 0 && cm.findPosV(cursor, -1, 'line').hitSide) { |
|
317 | if (cursor.line === 0 && cm.findPosV(cursor, -1, 'line').hitSide) { | |
314 | return true; |
|
318 | return true; | |
315 | } else { |
|
319 | } else { | |
316 | return false; |
|
320 | return false; | |
317 | } |
|
321 | } | |
318 | }; |
|
322 | }; | |
319 |
|
323 | |||
320 | /** |
|
324 | /** | |
321 | * @method at_bottom |
|
325 | * @method at_bottom | |
322 | * @return {Boolean} |
|
326 | * @return {Boolean} | |
323 | * */ |
|
327 | * */ | |
324 | Cell.prototype.at_bottom = function () { |
|
328 | Cell.prototype.at_bottom = function () { | |
325 | var cm = this.code_mirror |
|
329 | var cm = this.code_mirror | |
326 | var cursor = cm.getCursor(); |
|
330 | var cursor = cm.getCursor(); | |
327 | if (cursor.line === (cm.lineCount()-1) && cm.findPosV(cursor, 1, 'line').hitSide) { |
|
331 | if (cursor.line === (cm.lineCount()-1) && cm.findPosV(cursor, 1, 'line').hitSide) { | |
328 | return true; |
|
332 | return true; | |
329 | } else { |
|
333 | } else { | |
330 | return false; |
|
334 | return false; | |
331 | } |
|
335 | } | |
332 | }; |
|
336 | }; | |
333 | /** |
|
337 | /** | |
334 | * enter the command mode for the cell |
|
338 | * enter the command mode for the cell | |
335 | * @method command_mode |
|
339 | * @method command_mode | |
336 | * @return is the action being taken |
|
340 | * @return is the action being taken | |
337 | */ |
|
341 | */ | |
338 | Cell.prototype.command_mode = function () { |
|
342 | Cell.prototype.command_mode = function () { | |
339 | if (this.mode !== 'command') { |
|
343 | if (this.mode !== 'command') { | |
340 | this.element.addClass('command_mode'); |
|
344 | this.element.addClass('command_mode'); | |
341 | this.element.removeClass('edit_mode'); |
|
345 | this.element.removeClass('edit_mode'); | |
342 | this.mode = 'command'; |
|
346 | this.mode = 'command'; | |
343 | return true; |
|
347 | return true; | |
344 | } else { |
|
348 | } else { | |
345 | return false; |
|
349 | return false; | |
346 | } |
|
350 | } | |
347 | }; |
|
351 | }; | |
348 |
|
352 | |||
349 | /** |
|
353 | /** | |
350 | * enter the edit mode for the cell |
|
354 | * enter the edit mode for the cell | |
351 | * @method command_mode |
|
355 | * @method command_mode | |
352 | * @return is the action being taken |
|
356 | * @return is the action being taken | |
353 | */ |
|
357 | */ | |
354 | Cell.prototype.edit_mode = function () { |
|
358 | Cell.prototype.edit_mode = function () { | |
355 | if (this.mode !== 'edit') { |
|
359 | if (this.mode !== 'edit') { | |
356 | this.element.addClass('edit_mode'); |
|
360 | this.element.addClass('edit_mode'); | |
357 | this.element.removeClass('command_mode'); |
|
361 | this.element.removeClass('command_mode'); | |
358 | this.mode = 'edit'; |
|
362 | this.mode = 'edit'; | |
359 | return true; |
|
363 | return true; | |
360 | } else { |
|
364 | } else { | |
361 | return false; |
|
365 | return false; | |
362 | } |
|
366 | } | |
363 | }; |
|
367 | }; | |
364 |
|
368 | |||
365 | /** |
|
369 | /** | |
366 | * Determine whether or not the unfocus event should be aknowledged. |
|
370 | * Determine whether or not the unfocus event should be aknowledged. | |
367 | * |
|
371 | * | |
368 | * @method should_cancel_blur |
|
372 | * @method should_cancel_blur | |
369 | * |
|
373 | * | |
370 | * @return results {bool} Whether or not to ignore the cell's blur event. |
|
374 | * @return results {bool} Whether or not to ignore the cell's blur event. | |
371 | **/ |
|
375 | **/ | |
372 | Cell.prototype.should_cancel_blur = function () { |
|
376 | Cell.prototype.should_cancel_blur = function () { | |
373 | return false; |
|
377 | return false; | |
374 | }; |
|
378 | }; | |
375 |
|
379 | |||
376 | /** |
|
380 | /** | |
377 | * Focus the cell in the DOM sense |
|
381 | * Focus the cell in the DOM sense | |
378 | * @method focus_cell |
|
382 | * @method focus_cell | |
379 | */ |
|
383 | */ | |
380 | Cell.prototype.focus_cell = function () { |
|
384 | Cell.prototype.focus_cell = function () { | |
381 | this.element.focus(); |
|
385 | this.element.focus(); | |
382 | }; |
|
386 | }; | |
383 |
|
387 | |||
384 | /** |
|
388 | /** | |
385 | * Focus the editor area so a user can type |
|
389 | * Focus the editor area so a user can type | |
386 | * |
|
390 | * | |
387 | * NOTE: If codemirror is focused via a mouse click event, you don't want to |
|
391 | * NOTE: If codemirror is focused via a mouse click event, you don't want to | |
388 | * call this because it will cause a page jump. |
|
392 | * call this because it will cause a page jump. | |
389 | * @method focus_editor |
|
393 | * @method focus_editor | |
390 | */ |
|
394 | */ | |
391 | Cell.prototype.focus_editor = function () { |
|
395 | Cell.prototype.focus_editor = function () { | |
392 | this.refresh(); |
|
396 | this.refresh(); | |
393 | this.code_mirror.focus(); |
|
397 | this.code_mirror.focus(); | |
394 | }; |
|
398 | }; | |
395 |
|
399 | |||
396 | /** |
|
400 | /** | |
397 | * Refresh codemirror instance |
|
401 | * Refresh codemirror instance | |
398 | * @method refresh |
|
402 | * @method refresh | |
399 | */ |
|
403 | */ | |
400 | Cell.prototype.refresh = function () { |
|
404 | Cell.prototype.refresh = function () { | |
401 | this.code_mirror.refresh(); |
|
405 | this.code_mirror.refresh(); | |
402 | }; |
|
406 | }; | |
403 |
|
407 | |||
404 | /** |
|
408 | /** | |
405 | * should be overritten by subclass |
|
409 | * should be overritten by subclass | |
406 | * @method get_text |
|
410 | * @method get_text | |
407 | */ |
|
411 | */ | |
408 | Cell.prototype.get_text = function () { |
|
412 | Cell.prototype.get_text = function () { | |
409 | }; |
|
413 | }; | |
410 |
|
414 | |||
411 | /** |
|
415 | /** | |
412 | * should be overritten by subclass |
|
416 | * should be overritten by subclass | |
413 | * @method set_text |
|
417 | * @method set_text | |
414 | * @param {string} text |
|
418 | * @param {string} text | |
415 | */ |
|
419 | */ | |
416 | Cell.prototype.set_text = function (text) { |
|
420 | Cell.prototype.set_text = function (text) { | |
417 | }; |
|
421 | }; | |
418 |
|
422 | |||
419 | /** |
|
423 | /** | |
420 | * should be overritten by subclass |
|
424 | * should be overritten by subclass | |
421 | * serialise cell to json. |
|
425 | * serialise cell to json. | |
422 | * @method toJSON |
|
426 | * @method toJSON | |
423 | **/ |
|
427 | **/ | |
424 | Cell.prototype.toJSON = function () { |
|
428 | Cell.prototype.toJSON = function () { | |
425 | var data = {}; |
|
429 | var data = {}; | |
426 | data.metadata = this.metadata; |
|
430 | data.metadata = this.metadata; | |
427 | data.cell_type = this.cell_type; |
|
431 | data.cell_type = this.cell_type; | |
428 | return data; |
|
432 | return data; | |
429 | }; |
|
433 | }; | |
430 |
|
434 | |||
431 |
|
435 | |||
432 | /** |
|
436 | /** | |
433 | * should be overritten by subclass |
|
437 | * should be overritten by subclass | |
434 | * @method fromJSON |
|
438 | * @method fromJSON | |
435 | **/ |
|
439 | **/ | |
436 | Cell.prototype.fromJSON = function (data) { |
|
440 | Cell.prototype.fromJSON = function (data) { | |
437 | if (data.metadata !== undefined) { |
|
441 | if (data.metadata !== undefined) { | |
438 | this.metadata = data.metadata; |
|
442 | this.metadata = data.metadata; | |
439 | } |
|
443 | } | |
440 | this.celltoolbar.rebuild(); |
|
444 | this.celltoolbar.rebuild(); | |
441 | }; |
|
445 | }; | |
442 |
|
446 | |||
443 |
|
447 | |||
444 | /** |
|
448 | /** | |
445 | * can the cell be split into two cells |
|
449 | * can the cell be split into two cells | |
446 | * @method is_splittable |
|
450 | * @method is_splittable | |
447 | **/ |
|
451 | **/ | |
448 | Cell.prototype.is_splittable = function () { |
|
452 | Cell.prototype.is_splittable = function () { | |
449 | return true; |
|
453 | return true; | |
450 | }; |
|
454 | }; | |
451 |
|
455 | |||
452 |
|
456 | |||
453 | /** |
|
457 | /** | |
454 | * can the cell be merged with other cells |
|
458 | * can the cell be merged with other cells | |
455 | * @method is_mergeable |
|
459 | * @method is_mergeable | |
456 | **/ |
|
460 | **/ | |
457 | Cell.prototype.is_mergeable = function () { |
|
461 | Cell.prototype.is_mergeable = function () { | |
458 | return true; |
|
462 | return true; | |
459 | }; |
|
463 | }; | |
460 |
|
464 | |||
461 |
|
465 | |||
462 | /** |
|
466 | /** | |
463 | * @return {String} - the text before the cursor |
|
467 | * @return {String} - the text before the cursor | |
464 | * @method get_pre_cursor |
|
468 | * @method get_pre_cursor | |
465 | **/ |
|
469 | **/ | |
466 | Cell.prototype.get_pre_cursor = function () { |
|
470 | Cell.prototype.get_pre_cursor = function () { | |
467 | var cursor = this.code_mirror.getCursor(); |
|
471 | var cursor = this.code_mirror.getCursor(); | |
468 | var text = this.code_mirror.getRange({line:0, ch:0}, cursor); |
|
472 | var text = this.code_mirror.getRange({line:0, ch:0}, cursor); | |
469 | text = text.replace(/^\n+/, '').replace(/\n+$/, ''); |
|
473 | text = text.replace(/^\n+/, '').replace(/\n+$/, ''); | |
470 | return text; |
|
474 | return text; | |
471 | }; |
|
475 | }; | |
472 |
|
476 | |||
473 |
|
477 | |||
474 | /** |
|
478 | /** | |
475 | * @return {String} - the text after the cursor |
|
479 | * @return {String} - the text after the cursor | |
476 | * @method get_post_cursor |
|
480 | * @method get_post_cursor | |
477 | **/ |
|
481 | **/ | |
478 | Cell.prototype.get_post_cursor = function () { |
|
482 | Cell.prototype.get_post_cursor = function () { | |
479 | var cursor = this.code_mirror.getCursor(); |
|
483 | var cursor = this.code_mirror.getCursor(); | |
480 | var last_line_num = this.code_mirror.lineCount()-1; |
|
484 | var last_line_num = this.code_mirror.lineCount()-1; | |
481 | var last_line_len = this.code_mirror.getLine(last_line_num).length; |
|
485 | var last_line_len = this.code_mirror.getLine(last_line_num).length; | |
482 | var end = {line:last_line_num, ch:last_line_len}; |
|
486 | var end = {line:last_line_num, ch:last_line_len}; | |
483 | var text = this.code_mirror.getRange(cursor, end); |
|
487 | var text = this.code_mirror.getRange(cursor, end); | |
484 | text = text.replace(/^\n+/, '').replace(/\n+$/, ''); |
|
488 | text = text.replace(/^\n+/, '').replace(/\n+$/, ''); | |
485 | return text; |
|
489 | return text; | |
486 | }; |
|
490 | }; | |
487 |
|
491 | |||
488 | /** |
|
492 | /** | |
489 | * Show/Hide CodeMirror LineNumber |
|
493 | * Show/Hide CodeMirror LineNumber | |
490 | * @method show_line_numbers |
|
494 | * @method show_line_numbers | |
491 | * |
|
495 | * | |
492 | * @param value {Bool} show (true), or hide (false) the line number in CodeMirror |
|
496 | * @param value {Bool} show (true), or hide (false) the line number in CodeMirror | |
493 | **/ |
|
497 | **/ | |
494 | Cell.prototype.show_line_numbers = function (value) { |
|
498 | Cell.prototype.show_line_numbers = function (value) { | |
495 | this.code_mirror.setOption('lineNumbers', value); |
|
499 | this.code_mirror.setOption('lineNumbers', value); | |
496 | this.code_mirror.refresh(); |
|
500 | this.code_mirror.refresh(); | |
497 | }; |
|
501 | }; | |
498 |
|
502 | |||
499 | /** |
|
503 | /** | |
500 | * Toggle CodeMirror LineNumber |
|
504 | * Toggle CodeMirror LineNumber | |
501 | * @method toggle_line_numbers |
|
505 | * @method toggle_line_numbers | |
502 | **/ |
|
506 | **/ | |
503 | Cell.prototype.toggle_line_numbers = function () { |
|
507 | Cell.prototype.toggle_line_numbers = function () { | |
504 | var val = this.code_mirror.getOption('lineNumbers'); |
|
508 | var val = this.code_mirror.getOption('lineNumbers'); | |
505 | this.show_line_numbers(!val); |
|
509 | this.show_line_numbers(!val); | |
506 | }; |
|
510 | }; | |
507 |
|
511 | |||
508 | /** |
|
512 | /** | |
509 | * Force codemirror highlight mode |
|
513 | * Force codemirror highlight mode | |
510 | * @method force_highlight |
|
514 | * @method force_highlight | |
511 | * @param {object} - CodeMirror mode |
|
515 | * @param {object} - CodeMirror mode | |
512 | **/ |
|
516 | **/ | |
513 | Cell.prototype.force_highlight = function(mode) { |
|
517 | Cell.prototype.force_highlight = function(mode) { | |
514 | this.user_highlight = mode; |
|
518 | this.user_highlight = mode; | |
515 | this.auto_highlight(); |
|
519 | this.auto_highlight(); | |
516 | }; |
|
520 | }; | |
517 |
|
521 | |||
518 | /** |
|
522 | /** | |
519 | * Try to autodetect cell highlight mode, or use selected mode |
|
523 | * Try to autodetect cell highlight mode, or use selected mode | |
520 | * @methods _auto_highlight |
|
524 | * @methods _auto_highlight | |
521 | * @private |
|
525 | * @private | |
522 | * @param {String|object|undefined} - CodeMirror mode | 'auto' |
|
526 | * @param {String|object|undefined} - CodeMirror mode | 'auto' | |
523 | **/ |
|
527 | **/ | |
524 | Cell.prototype._auto_highlight = function (modes) { |
|
528 | Cell.prototype._auto_highlight = function (modes) { | |
525 | //Here we handle manually selected modes |
|
529 | //Here we handle manually selected modes | |
526 | var mode; |
|
530 | var mode; | |
527 | if( this.user_highlight !== undefined && this.user_highlight != 'auto' ) |
|
531 | if( this.user_highlight !== undefined && this.user_highlight != 'auto' ) | |
528 | { |
|
532 | { | |
529 | mode = this.user_highlight; |
|
533 | mode = this.user_highlight; | |
530 | CodeMirror.autoLoadMode(this.code_mirror, mode); |
|
534 | CodeMirror.autoLoadMode(this.code_mirror, mode); | |
531 | this.code_mirror.setOption('mode', mode); |
|
535 | this.code_mirror.setOption('mode', mode); | |
532 | return; |
|
536 | return; | |
533 | } |
|
537 | } | |
534 | var current_mode = this.code_mirror.getOption('mode', mode); |
|
538 | var current_mode = this.code_mirror.getOption('mode', mode); | |
535 | var first_line = this.code_mirror.getLine(0); |
|
539 | var first_line = this.code_mirror.getLine(0); | |
536 | // loop on every pairs |
|
540 | // loop on every pairs | |
537 | for(mode in modes) { |
|
541 | for(mode in modes) { | |
538 | var regs = modes[mode].reg; |
|
542 | var regs = modes[mode].reg; | |
539 | // only one key every time but regexp can't be keys... |
|
543 | // only one key every time but regexp can't be keys... | |
540 | for(var i=0; i<regs.length; i++) { |
|
544 | for(var i=0; i<regs.length; i++) { | |
541 | // here we handle non magic_modes |
|
545 | // here we handle non magic_modes | |
542 | if(first_line.match(regs[i]) !== null) { |
|
546 | if(first_line.match(regs[i]) !== null) { | |
543 | if(current_mode == mode){ |
|
547 | if(current_mode == mode){ | |
544 | return; |
|
548 | return; | |
545 | } |
|
549 | } | |
546 | if (mode.search('magic_') !== 0) { |
|
550 | if (mode.search('magic_') !== 0) { | |
547 | this.code_mirror.setOption('mode', mode); |
|
551 | this.code_mirror.setOption('mode', mode); | |
548 | CodeMirror.autoLoadMode(this.code_mirror, mode); |
|
552 | CodeMirror.autoLoadMode(this.code_mirror, mode); | |
549 | return; |
|
553 | return; | |
550 | } |
|
554 | } | |
551 | var open = modes[mode].open || "%%"; |
|
555 | var open = modes[mode].open || "%%"; | |
552 | var close = modes[mode].close || "%%end"; |
|
556 | var close = modes[mode].close || "%%end"; | |
553 | var mmode = mode; |
|
557 | var mmode = mode; | |
554 | mode = mmode.substr(6); |
|
558 | mode = mmode.substr(6); | |
555 | if(current_mode == mode){ |
|
559 | if(current_mode == mode){ | |
556 | return; |
|
560 | return; | |
557 | } |
|
561 | } | |
558 | CodeMirror.autoLoadMode(this.code_mirror, mode); |
|
562 | CodeMirror.autoLoadMode(this.code_mirror, mode); | |
559 | // create on the fly a mode that swhitch between |
|
563 | // create on the fly a mode that swhitch between | |
560 | // plain/text and smth else otherwise `%%` is |
|
564 | // plain/text and smth else otherwise `%%` is | |
561 | // source of some highlight issues. |
|
565 | // source of some highlight issues. | |
562 | // we use patchedGetMode to circumvent a bug in CM |
|
566 | // we use patchedGetMode to circumvent a bug in CM | |
563 | CodeMirror.defineMode(mmode , function(config) { |
|
567 | CodeMirror.defineMode(mmode , function(config) { | |
564 | return CodeMirror.multiplexingMode( |
|
568 | return CodeMirror.multiplexingMode( | |
565 | CodeMirror.patchedGetMode(config, 'text/plain'), |
|
569 | CodeMirror.patchedGetMode(config, 'text/plain'), | |
566 | // always set someting on close |
|
570 | // always set someting on close | |
567 | {open: open, close: close, |
|
571 | {open: open, close: close, | |
568 | mode: CodeMirror.patchedGetMode(config, mode), |
|
572 | mode: CodeMirror.patchedGetMode(config, mode), | |
569 | delimStyle: "delimit" |
|
573 | delimStyle: "delimit" | |
570 | } |
|
574 | } | |
571 | ); |
|
575 | ); | |
572 | }); |
|
576 | }); | |
573 | this.code_mirror.setOption('mode', mmode); |
|
577 | this.code_mirror.setOption('mode', mmode); | |
574 | return; |
|
578 | return; | |
575 | } |
|
579 | } | |
576 | } |
|
580 | } | |
577 | } |
|
581 | } | |
578 | // fallback on default |
|
582 | // fallback on default | |
579 | var default_mode; |
|
583 | var default_mode; | |
580 | try { |
|
584 | try { | |
581 | default_mode = this._options.cm_config.mode; |
|
585 | default_mode = this._options.cm_config.mode; | |
582 | } catch(e) { |
|
586 | } catch(e) { | |
583 | default_mode = 'text/plain'; |
|
587 | default_mode = 'text/plain'; | |
584 | } |
|
588 | } | |
585 | if( current_mode === default_mode){ |
|
589 | if( current_mode === default_mode){ | |
586 | return; |
|
590 | return; | |
587 | } |
|
591 | } | |
588 | this.code_mirror.setOption('mode', default_mode); |
|
592 | this.code_mirror.setOption('mode', default_mode); | |
589 | }; |
|
593 | }; | |
590 |
|
594 | |||
591 | IPython.Cell = Cell; |
|
595 | IPython.Cell = Cell; | |
592 |
|
596 | |||
593 | return IPython; |
|
597 | return IPython; | |
594 |
|
598 | |||
595 | }(IPython)); |
|
599 | }(IPython)); | |
596 |
|
600 |
General Comments 0
You need to be logged in to leave comments.
Login now