##// END OF EJS Templates
Merge pull request #7981 from Carreau/focus-after-save...
Thomas Kluyver -
r20656:49d378b4 merge
parent child Browse files
Show More
@@ -1,520 +1,521 b''
1 // Copyright (c) IPython Development Team.
1 // Copyright (c) IPython Development Team.
2 // Distributed under the terms of the Modified BSD License.
2 // Distributed under the terms of the Modified BSD License.
3
3
4 define(function(require){
4 define(function(require){
5 "use strict";
5 "use strict";
6
6
7 var ActionHandler = function (env) {
7 var ActionHandler = function (env) {
8 this.env = env || {};
8 this.env = env || {};
9 Object.seal(this);
9 Object.seal(this);
10 };
10 };
11
11
12 /**
12 /**
13 * A bunch of predefined `Simple Actions` used by IPython.
13 * A bunch of predefined `Simple Actions` used by IPython.
14 * `Simple Actions` have the following keys:
14 * `Simple Actions` have the following keys:
15 * help (optional): a short string the describe the action.
15 * help (optional): a short string the describe the action.
16 * will be used in various context, like as menu name, tool tips on buttons,
16 * will be used in various context, like as menu name, tool tips on buttons,
17 * and short description in help menu.
17 * and short description in help menu.
18 * help_index (optional): a string used to sort action in help menu.
18 * help_index (optional): a string used to sort action in help menu.
19 * icon (optional): a short string that represent the icon that have to be used with this
19 * icon (optional): a short string that represent the icon that have to be used with this
20 * action. this should mainly correspond to a Font_awesome class.
20 * action. this should mainly correspond to a Font_awesome class.
21 * handler : a function which is called when the action is activated. It will receive at first parameter
21 * handler : a function which is called when the action is activated. It will receive at first parameter
22 * a dictionary containing various handle to element of the notebook.
22 * a dictionary containing various handle to element of the notebook.
23 *
23 *
24 * action need to be registered with a **name** that can be use to refer to this action.
24 * action need to be registered with a **name** that can be use to refer to this action.
25 *
25 *
26 *
26 *
27 * if `help` is not provided it will be derived by replacing any dash by space
27 * if `help` is not provided it will be derived by replacing any dash by space
28 * in the **name** of the action. It is advised to provide a prefix to action name to
28 * in the **name** of the action. It is advised to provide a prefix to action name to
29 * avoid conflict the prefix should be all lowercase and end with a dot `.`
29 * avoid conflict the prefix should be all lowercase and end with a dot `.`
30 * in the absence of a prefix the behavior of the action is undefined.
30 * in the absence of a prefix the behavior of the action is undefined.
31 *
31 *
32 * All action provided by IPython are prefixed with `ipython.`.
32 * All action provided by IPython are prefixed with `ipython.`.
33 *
33 *
34 * One can register extra actions or replace an existing action with another one is possible
34 * One can register extra actions or replace an existing action with another one is possible
35 * but is considered undefined behavior.
35 * but is considered undefined behavior.
36 *
36 *
37 **/
37 **/
38 var _actions = {
38 var _actions = {
39 'run-select-next': {
39 'run-select-next': {
40 icon: 'fa-play',
40 icon: 'fa-play',
41 help : 'run cell, select below',
41 help : 'run cell, select below',
42 help_index : 'ba',
42 help_index : 'ba',
43 handler : function (env) {
43 handler : function (env) {
44 env.notebook.execute_cell_and_select_below();
44 env.notebook.execute_cell_and_select_below();
45 }
45 }
46 },
46 },
47 'execute-in-place':{
47 'execute-in-place':{
48 help : 'run cell',
48 help : 'run cell',
49 help_index : 'bb',
49 help_index : 'bb',
50 handler : function (env) {
50 handler : function (env) {
51 env.notebook.execute_cell();
51 env.notebook.execute_cell();
52 }
52 }
53 },
53 },
54 'execute-and-insert-after':{
54 'execute-and-insert-after':{
55 help : 'run cell, insert below',
55 help : 'run cell, insert below',
56 help_index : 'bc',
56 help_index : 'bc',
57 handler : function (env) {
57 handler : function (env) {
58 env.notebook.execute_cell_and_insert_below();
58 env.notebook.execute_cell_and_insert_below();
59 }
59 }
60 },
60 },
61 'go-to-command-mode': {
61 'go-to-command-mode': {
62 help : 'command mode',
62 help : 'command mode',
63 help_index : 'aa',
63 help_index : 'aa',
64 handler : function (env) {
64 handler : function (env) {
65 env.notebook.command_mode();
65 env.notebook.command_mode();
66 }
66 }
67 },
67 },
68 'split-cell-at-cursor': {
68 'split-cell-at-cursor': {
69 help : 'split cell',
69 help : 'split cell',
70 help_index : 'ea',
70 help_index : 'ea',
71 handler : function (env) {
71 handler : function (env) {
72 env.notebook.split_cell();
72 env.notebook.split_cell();
73 }
73 }
74 },
74 },
75 'enter-edit-mode' : {
75 'enter-edit-mode' : {
76 help_index : 'aa',
76 help_index : 'aa',
77 handler : function (env) {
77 handler : function (env) {
78 env.notebook.edit_mode();
78 env.notebook.edit_mode();
79 }
79 }
80 },
80 },
81 'select-previous-cell' : {
81 'select-previous-cell' : {
82 help: 'select cell above',
82 help: 'select cell above',
83 help_index : 'da',
83 help_index : 'da',
84 handler : function (env) {
84 handler : function (env) {
85 var index = env.notebook.get_selected_index();
85 var index = env.notebook.get_selected_index();
86 if (index !== 0 && index !== null) {
86 if (index !== 0 && index !== null) {
87 env.notebook.select_prev();
87 env.notebook.select_prev();
88 env.notebook.focus_cell();
88 env.notebook.focus_cell();
89 }
89 }
90 }
90 }
91 },
91 },
92 'select-next-cell' : {
92 'select-next-cell' : {
93 help: 'select cell below',
93 help: 'select cell below',
94 help_index : 'db',
94 help_index : 'db',
95 handler : function (env) {
95 handler : function (env) {
96 var index = env.notebook.get_selected_index();
96 var index = env.notebook.get_selected_index();
97 if (index !== (env.notebook.ncells()-1) && index !== null) {
97 if (index !== (env.notebook.ncells()-1) && index !== null) {
98 env.notebook.select_next();
98 env.notebook.select_next();
99 env.notebook.focus_cell();
99 env.notebook.focus_cell();
100 }
100 }
101 }
101 }
102 },
102 },
103 'cut-selected-cell' : {
103 'cut-selected-cell' : {
104 icon: 'fa-cut',
104 icon: 'fa-cut',
105 help_index : 'ee',
105 help_index : 'ee',
106 handler : function (env) {
106 handler : function (env) {
107 var index = env.notebook.get_selected_index();
107 var index = env.notebook.get_selected_index();
108 env.notebook.cut_cell();
108 env.notebook.cut_cell();
109 env.notebook.select(index);
109 env.notebook.select(index);
110 env.notebook.focus_cell();
110 env.notebook.focus_cell();
111 }
111 }
112 },
112 },
113 'copy-selected-cell' : {
113 'copy-selected-cell' : {
114 icon: 'fa-copy',
114 icon: 'fa-copy',
115 help_index : 'ef',
115 help_index : 'ef',
116 handler : function (env) {
116 handler : function (env) {
117 env.notebook.copy_cell();
117 env.notebook.copy_cell();
118 env.notebook.focus_cell();
118 env.notebook.focus_cell();
119 }
119 }
120 },
120 },
121 'paste-cell-before' : {
121 'paste-cell-before' : {
122 help: 'paste cell above',
122 help: 'paste cell above',
123 help_index : 'eg',
123 help_index : 'eg',
124 handler : function (env) {
124 handler : function (env) {
125 env.notebook.paste_cell_above();
125 env.notebook.paste_cell_above();
126 }
126 }
127 },
127 },
128 'paste-cell-after' : {
128 'paste-cell-after' : {
129 help: 'paste cell below',
129 help: 'paste cell below',
130 icon: 'fa-paste',
130 icon: 'fa-paste',
131 help_index : 'eh',
131 help_index : 'eh',
132 handler : function (env) {
132 handler : function (env) {
133 env.notebook.paste_cell_below();
133 env.notebook.paste_cell_below();
134 }
134 }
135 },
135 },
136 'insert-cell-before' : {
136 'insert-cell-before' : {
137 help: 'insert cell above',
137 help: 'insert cell above',
138 help_index : 'ec',
138 help_index : 'ec',
139 handler : function (env) {
139 handler : function (env) {
140 env.notebook.insert_cell_above();
140 env.notebook.insert_cell_above();
141 env.notebook.select_prev();
141 env.notebook.select_prev();
142 env.notebook.focus_cell();
142 env.notebook.focus_cell();
143 }
143 }
144 },
144 },
145 'insert-cell-after' : {
145 'insert-cell-after' : {
146 help: 'insert cell below',
146 help: 'insert cell below',
147 icon : 'fa-plus',
147 icon : 'fa-plus',
148 help_index : 'ed',
148 help_index : 'ed',
149 handler : function (env) {
149 handler : function (env) {
150 env.notebook.insert_cell_below();
150 env.notebook.insert_cell_below();
151 env.notebook.select_next();
151 env.notebook.select_next();
152 env.notebook.focus_cell();
152 env.notebook.focus_cell();
153 }
153 }
154 },
154 },
155 'change-selected-cell-to-code-cell' : {
155 'change-selected-cell-to-code-cell' : {
156 help : 'to code',
156 help : 'to code',
157 help_index : 'ca',
157 help_index : 'ca',
158 handler : function (env) {
158 handler : function (env) {
159 env.notebook.to_code();
159 env.notebook.to_code();
160 }
160 }
161 },
161 },
162 'change-selected-cell-to-markdown-cell' : {
162 'change-selected-cell-to-markdown-cell' : {
163 help : 'to markdown',
163 help : 'to markdown',
164 help_index : 'cb',
164 help_index : 'cb',
165 handler : function (env) {
165 handler : function (env) {
166 env.notebook.to_markdown();
166 env.notebook.to_markdown();
167 }
167 }
168 },
168 },
169 'change-selected-cell-to-raw-cell' : {
169 'change-selected-cell-to-raw-cell' : {
170 help : 'to raw',
170 help : 'to raw',
171 help_index : 'cc',
171 help_index : 'cc',
172 handler : function (env) {
172 handler : function (env) {
173 env.notebook.to_raw();
173 env.notebook.to_raw();
174 }
174 }
175 },
175 },
176 'change-selected-cell-to-heading-1' : {
176 'change-selected-cell-to-heading-1' : {
177 help : 'to heading 1',
177 help : 'to heading 1',
178 help_index : 'cd',
178 help_index : 'cd',
179 handler : function (env) {
179 handler : function (env) {
180 env.notebook.to_heading(undefined, 1);
180 env.notebook.to_heading(undefined, 1);
181 }
181 }
182 },
182 },
183 'change-selected-cell-to-heading-2' : {
183 'change-selected-cell-to-heading-2' : {
184 help : 'to heading 2',
184 help : 'to heading 2',
185 help_index : 'ce',
185 help_index : 'ce',
186 handler : function (env) {
186 handler : function (env) {
187 env.notebook.to_heading(undefined, 2);
187 env.notebook.to_heading(undefined, 2);
188 }
188 }
189 },
189 },
190 'change-selected-cell-to-heading-3' : {
190 'change-selected-cell-to-heading-3' : {
191 help : 'to heading 3',
191 help : 'to heading 3',
192 help_index : 'cf',
192 help_index : 'cf',
193 handler : function (env) {
193 handler : function (env) {
194 env.notebook.to_heading(undefined, 3);
194 env.notebook.to_heading(undefined, 3);
195 }
195 }
196 },
196 },
197 'change-selected-cell-to-heading-4' : {
197 'change-selected-cell-to-heading-4' : {
198 help : 'to heading 4',
198 help : 'to heading 4',
199 help_index : 'cg',
199 help_index : 'cg',
200 handler : function (env) {
200 handler : function (env) {
201 env.notebook.to_heading(undefined, 4);
201 env.notebook.to_heading(undefined, 4);
202 }
202 }
203 },
203 },
204 'change-selected-cell-to-heading-5' : {
204 'change-selected-cell-to-heading-5' : {
205 help : 'to heading 5',
205 help : 'to heading 5',
206 help_index : 'ch',
206 help_index : 'ch',
207 handler : function (env) {
207 handler : function (env) {
208 env.notebook.to_heading(undefined, 5);
208 env.notebook.to_heading(undefined, 5);
209 }
209 }
210 },
210 },
211 'change-selected-cell-to-heading-6' : {
211 'change-selected-cell-to-heading-6' : {
212 help : 'to heading 6',
212 help : 'to heading 6',
213 help_index : 'ci',
213 help_index : 'ci',
214 handler : function (env) {
214 handler : function (env) {
215 env.notebook.to_heading(undefined, 6);
215 env.notebook.to_heading(undefined, 6);
216 }
216 }
217 },
217 },
218 'toggle-output-visibility-selected-cell' : {
218 'toggle-output-visibility-selected-cell' : {
219 help : 'toggle output',
219 help : 'toggle output',
220 help_index : 'gb',
220 help_index : 'gb',
221 handler : function (env) {
221 handler : function (env) {
222 env.notebook.toggle_output();
222 env.notebook.toggle_output();
223 }
223 }
224 },
224 },
225 'toggle-output-scrolling-selected-cell' : {
225 'toggle-output-scrolling-selected-cell' : {
226 help : 'toggle output scrolling',
226 help : 'toggle output scrolling',
227 help_index : 'gc',
227 help_index : 'gc',
228 handler : function (env) {
228 handler : function (env) {
229 env.notebook.toggle_output_scroll();
229 env.notebook.toggle_output_scroll();
230 }
230 }
231 },
231 },
232 'move-selected-cell-down' : {
232 'move-selected-cell-down' : {
233 icon: 'fa-arrow-down',
233 icon: 'fa-arrow-down',
234 help_index : 'eb',
234 help_index : 'eb',
235 handler : function (env) {
235 handler : function (env) {
236 env.notebook.move_cell_down();
236 env.notebook.move_cell_down();
237 }
237 }
238 },
238 },
239 'move-selected-cell-up' : {
239 'move-selected-cell-up' : {
240 icon: 'fa-arrow-up',
240 icon: 'fa-arrow-up',
241 help_index : 'ea',
241 help_index : 'ea',
242 handler : function (env) {
242 handler : function (env) {
243 env.notebook.move_cell_up();
243 env.notebook.move_cell_up();
244 }
244 }
245 },
245 },
246 'toggle-line-number-selected-cell' : {
246 'toggle-line-number-selected-cell' : {
247 help : 'toggle line numbers',
247 help : 'toggle line numbers',
248 help_index : 'ga',
248 help_index : 'ga',
249 handler : function (env) {
249 handler : function (env) {
250 env.notebook.cell_toggle_line_numbers();
250 env.notebook.cell_toggle_line_numbers();
251 }
251 }
252 },
252 },
253 'show-keyboard-shortcut-help-dialog' : {
253 'show-keyboard-shortcut-help-dialog' : {
254 help_index : 'ge',
254 help_index : 'ge',
255 handler : function (env) {
255 handler : function (env) {
256 env.quick_help.show_keyboard_shortcuts();
256 env.quick_help.show_keyboard_shortcuts();
257 }
257 }
258 },
258 },
259 'delete-cell': {
259 'delete-cell': {
260 help: 'delete selected cell',
260 help: 'delete selected cell',
261 help_index : 'ej',
261 help_index : 'ej',
262 handler : function (env) {
262 handler : function (env) {
263 env.notebook.delete_cell();
263 env.notebook.delete_cell();
264 }
264 }
265 },
265 },
266 'interrupt-kernel':{
266 'interrupt-kernel':{
267 icon: 'fa-stop',
267 icon: 'fa-stop',
268 help_index : 'ha',
268 help_index : 'ha',
269 handler : function (env) {
269 handler : function (env) {
270 env.notebook.kernel.interrupt();
270 env.notebook.kernel.interrupt();
271 env.notebook.focus_cell();
271 env.notebook.focus_cell();
272 }
272 }
273 },
273 },
274 'restart-kernel':{
274 'restart-kernel':{
275 icon: 'fa-repeat',
275 icon: 'fa-repeat',
276 help_index : 'hb',
276 help_index : 'hb',
277 handler : function (env) {
277 handler : function (env) {
278 env.notebook.restart_kernel();
278 env.notebook.restart_kernel();
279 env.notebook.focus_cell();
279 env.notebook.focus_cell();
280 }
280 }
281 },
281 },
282 'undo-last-cell-deletion' : {
282 'undo-last-cell-deletion' : {
283 help_index : 'ei',
283 help_index : 'ei',
284 handler : function (env) {
284 handler : function (env) {
285 env.notebook.undelete_cell();
285 env.notebook.undelete_cell();
286 }
286 }
287 },
287 },
288 'merge-selected-cell-with-cell-after' : {
288 'merge-selected-cell-with-cell-after' : {
289 help : 'merge cell below',
289 help : 'merge cell below',
290 help_index : 'ek',
290 help_index : 'ek',
291 handler : function (env) {
291 handler : function (env) {
292 env.notebook.merge_cell_below();
292 env.notebook.merge_cell_below();
293 }
293 }
294 },
294 },
295 'close-pager' : {
295 'close-pager' : {
296 help_index : 'gd',
296 help_index : 'gd',
297 handler : function (env) {
297 handler : function (env) {
298 env.pager.collapse();
298 env.pager.collapse();
299 }
299 }
300 }
300 }
301
301
302 };
302 };
303
303
304 /**
304 /**
305 * A bunch of `Advance actions` for IPython.
305 * A bunch of `Advance actions` for IPython.
306 * Cf `Simple Action` plus the following properties.
306 * Cf `Simple Action` plus the following properties.
307 *
307 *
308 * handler: first argument of the handler is the event that triggerd the action
308 * handler: first argument of the handler is the event that triggerd the action
309 * (typically keypress). The handler is responsible for any modification of the
309 * (typically keypress). The handler is responsible for any modification of the
310 * event and event propagation.
310 * event and event propagation.
311 * Is also responsible for returning false if the event have to be further ignored,
311 * Is also responsible for returning false if the event have to be further ignored,
312 * true, to tell keyboard manager that it ignored the event.
312 * true, to tell keyboard manager that it ignored the event.
313 *
313 *
314 * the second parameter of the handler is the environemnt passed to Simple Actions
314 * the second parameter of the handler is the environemnt passed to Simple Actions
315 *
315 *
316 **/
316 **/
317 var custom_ignore = {
317 var custom_ignore = {
318 'ignore':{
318 'ignore':{
319 handler : function () {
319 handler : function () {
320 return true;
320 return true;
321 }
321 }
322 },
322 },
323 'move-cursor-up-or-previous-cell':{
323 'move-cursor-up-or-previous-cell':{
324 handler : function (env, event) {
324 handler : function (env, event) {
325 var index = env.notebook.get_selected_index();
325 var index = env.notebook.get_selected_index();
326 var cell = env.notebook.get_cell(index);
326 var cell = env.notebook.get_cell(index);
327 var cm = env.notebook.get_selected_cell().code_mirror;
327 var cm = env.notebook.get_selected_cell().code_mirror;
328 var cur = cm.getCursor();
328 var cur = cm.getCursor();
329 if (cell && cell.at_top() && index !== 0 && cur.ch === 0) {
329 if (cell && cell.at_top() && index !== 0 && cur.ch === 0) {
330 if(event){
330 if(event){
331 event.preventDefault();
331 event.preventDefault();
332 }
332 }
333 env.notebook.command_mode();
333 env.notebook.command_mode();
334 env.notebook.select_prev();
334 env.notebook.select_prev();
335 env.notebook.edit_mode();
335 env.notebook.edit_mode();
336 cm = env.notebook.get_selected_cell().code_mirror;
336 cm = env.notebook.get_selected_cell().code_mirror;
337 cm.setCursor(cm.lastLine(), 0);
337 cm.setCursor(cm.lastLine(), 0);
338 }
338 }
339 return false;
339 return false;
340 }
340 }
341 },
341 },
342 'move-cursor-down-or-next-cell':{
342 'move-cursor-down-or-next-cell':{
343 handler : function (env, event) {
343 handler : function (env, event) {
344 var index = env.notebook.get_selected_index();
344 var index = env.notebook.get_selected_index();
345 var cell = env.notebook.get_cell(index);
345 var cell = env.notebook.get_cell(index);
346 if (cell.at_bottom() && index !== (env.notebook.ncells()-1)) {
346 if (cell.at_bottom() && index !== (env.notebook.ncells()-1)) {
347 if(event){
347 if(event){
348 event.preventDefault();
348 event.preventDefault();
349 }
349 }
350 env.notebook.command_mode();
350 env.notebook.command_mode();
351 env.notebook.select_next();
351 env.notebook.select_next();
352 env.notebook.edit_mode();
352 env.notebook.edit_mode();
353 var cm = env.notebook.get_selected_cell().code_mirror;
353 var cm = env.notebook.get_selected_cell().code_mirror;
354 cm.setCursor(0, 0);
354 cm.setCursor(0, 0);
355 }
355 }
356 return false;
356 return false;
357 }
357 }
358 },
358 },
359 'scroll-down': {
359 'scroll-down': {
360 handler: function(env, event) {
360 handler: function(env, event) {
361 if(event){
361 if(event){
362 event.preventDefault();
362 event.preventDefault();
363 }
363 }
364 return env.notebook.scroll_manager.scroll(1);
364 return env.notebook.scroll_manager.scroll(1);
365 },
365 },
366 },
366 },
367 'scroll-up': {
367 'scroll-up': {
368 handler: function(env, event) {
368 handler: function(env, event) {
369 if(event){
369 if(event){
370 event.preventDefault();
370 event.preventDefault();
371 }
371 }
372 return env.notebook.scroll_manager.scroll(-1);
372 return env.notebook.scroll_manager.scroll(-1);
373 },
373 },
374 },
374 },
375 'save-notebook':{
375 'save-notebook':{
376 help: "Save and Checkpoint",
376 help: "Save and Checkpoint",
377 help_index : 'fb',
377 help_index : 'fb',
378 icon: 'fa-save',
378 icon: 'fa-save',
379 handler : function (env, event) {
379 handler : function (env, event) {
380 env.notebook.save_checkpoint();
380 env.notebook.save_checkpoint();
381 if(event){
381 if(event){
382 event.preventDefault();
382 event.preventDefault();
383 }
383 }
384 env.notebook.focus_cell();
384 return false;
385 return false;
385 }
386 }
386 },
387 },
387 };
388 };
388
389
389 // private stuff that prepend `.ipython` to actions names
390 // private stuff that prepend `.ipython` to actions names
390 // and uniformize/fill in missing pieces in of an action.
391 // and uniformize/fill in missing pieces in of an action.
391 var _prepare_handler = function(registry, subkey, source){
392 var _prepare_handler = function(registry, subkey, source){
392 registry['ipython.'+subkey] = {};
393 registry['ipython.'+subkey] = {};
393 registry['ipython.'+subkey].help = source[subkey].help||subkey.replace(/-/g,' ');
394 registry['ipython.'+subkey].help = source[subkey].help||subkey.replace(/-/g,' ');
394 registry['ipython.'+subkey].help_index = source[subkey].help_index;
395 registry['ipython.'+subkey].help_index = source[subkey].help_index;
395 registry['ipython.'+subkey].icon = source[subkey].icon;
396 registry['ipython.'+subkey].icon = source[subkey].icon;
396 return source[subkey].handler;
397 return source[subkey].handler;
397 };
398 };
398
399
399 // Will actually generate/register all the IPython actions
400 // Will actually generate/register all the IPython actions
400 var fun = function(){
401 var fun = function(){
401 var final_actions = {};
402 var final_actions = {};
402 var k;
403 var k;
403 for(k in _actions){
404 for(k in _actions){
404 if(_actions.hasOwnProperty(k)){
405 if(_actions.hasOwnProperty(k)){
405 // Js closure are function level not block level need to wrap in a IIFE
406 // Js closure are function level not block level need to wrap in a IIFE
406 // and append ipython to event name these things do intercept event so are wrapped
407 // and append ipython to event name these things do intercept event so are wrapped
407 // in a function that return false.
408 // in a function that return false.
408 var handler = _prepare_handler(final_actions, k, _actions);
409 var handler = _prepare_handler(final_actions, k, _actions);
409 (function(key, handler){
410 (function(key, handler){
410 final_actions['ipython.'+key].handler = function(env, event){
411 final_actions['ipython.'+key].handler = function(env, event){
411 handler(env);
412 handler(env);
412 if(event){
413 if(event){
413 event.preventDefault();
414 event.preventDefault();
414 }
415 }
415 return false;
416 return false;
416 };
417 };
417 })(k, handler);
418 })(k, handler);
418 }
419 }
419 }
420 }
420
421
421 for(k in custom_ignore){
422 for(k in custom_ignore){
422 // Js closure are function level not block level need to wrap in a IIFE
423 // Js closure are function level not block level need to wrap in a IIFE
423 // same as above, but decide for themselves wether or not they intercept events.
424 // same as above, but decide for themselves wether or not they intercept events.
424 if(custom_ignore.hasOwnProperty(k)){
425 if(custom_ignore.hasOwnProperty(k)){
425 var handler = _prepare_handler(final_actions, k, custom_ignore);
426 var handler = _prepare_handler(final_actions, k, custom_ignore);
426 (function(key, handler){
427 (function(key, handler){
427 final_actions['ipython.'+key].handler = function(env, event){
428 final_actions['ipython.'+key].handler = function(env, event){
428 return handler(env, event);
429 return handler(env, event);
429 };
430 };
430 })(k, handler);
431 })(k, handler);
431 }
432 }
432 }
433 }
433
434
434 return final_actions;
435 return final_actions;
435 };
436 };
436 ActionHandler.prototype._actions = fun();
437 ActionHandler.prototype._actions = fun();
437
438
438
439
439 /**
440 /**
440 * extend the environment variable that will be pass to handlers
441 * extend the environment variable that will be pass to handlers
441 **/
442 **/
442 ActionHandler.prototype.extend_env = function(env){
443 ActionHandler.prototype.extend_env = function(env){
443 for(var k in env){
444 for(var k in env){
444 this.env[k] = env[k];
445 this.env[k] = env[k];
445 }
446 }
446 };
447 };
447
448
448 ActionHandler.prototype.register = function(action, name, prefix){
449 ActionHandler.prototype.register = function(action, name, prefix){
449 /**
450 /**
450 * Register an `action` with an optional name and prefix.
451 * Register an `action` with an optional name and prefix.
451 *
452 *
452 * if name and prefix are not given they will be determined automatically.
453 * if name and prefix are not given they will be determined automatically.
453 * if action if just a `function` it will be wrapped in an anonymous action.
454 * if action if just a `function` it will be wrapped in an anonymous action.
454 *
455 *
455 * @return the full name to access this action .
456 * @return the full name to access this action .
456 **/
457 **/
457 action = this.normalise(action);
458 action = this.normalise(action);
458 if( !name ){
459 if( !name ){
459 name = 'autogenerated-'+String(action.handler);
460 name = 'autogenerated-'+String(action.handler);
460 }
461 }
461 prefix = prefix || 'auto';
462 prefix = prefix || 'auto';
462 var full_name = prefix+'.'+name;
463 var full_name = prefix+'.'+name;
463 this._actions[full_name] = action;
464 this._actions[full_name] = action;
464 return full_name;
465 return full_name;
465
466
466 };
467 };
467
468
468
469
469 ActionHandler.prototype.normalise = function(data){
470 ActionHandler.prototype.normalise = function(data){
470 /**
471 /**
471 * given an `action` or `function`, return a normalised `action`
472 * given an `action` or `function`, return a normalised `action`
472 * by setting all known attributes and removing unknown attributes;
473 * by setting all known attributes and removing unknown attributes;
473 **/
474 **/
474 if(typeof(data) === 'function'){
475 if(typeof(data) === 'function'){
475 data = {handler:data};
476 data = {handler:data};
476 }
477 }
477 if(typeof(data.handler) !== 'function'){
478 if(typeof(data.handler) !== 'function'){
478 throw('unknown datatype, cannot register');
479 throw('unknown datatype, cannot register');
479 }
480 }
480 var _data = data;
481 var _data = data;
481 data = {};
482 data = {};
482 data.handler = _data.handler;
483 data.handler = _data.handler;
483 data.help = _data.help || '';
484 data.help = _data.help || '';
484 data.icon = _data.icon || '';
485 data.icon = _data.icon || '';
485 data.help_index = _data.help_index || '';
486 data.help_index = _data.help_index || '';
486 return data;
487 return data;
487 };
488 };
488
489
489 ActionHandler.prototype.get_name = function(name_or_data){
490 ActionHandler.prototype.get_name = function(name_or_data){
490 /**
491 /**
491 * given an `action` or `name` of a action, return the name attached to this action.
492 * given an `action` or `name` of a action, return the name attached to this action.
492 * if given the name of and corresponding actions does not exist in registry, return `null`.
493 * if given the name of and corresponding actions does not exist in registry, return `null`.
493 **/
494 **/
494
495
495 if(typeof(name_or_data) === 'string'){
496 if(typeof(name_or_data) === 'string'){
496 if(this.exists(name_or_data)){
497 if(this.exists(name_or_data)){
497 return name_or_data;
498 return name_or_data;
498 } else {
499 } else {
499 return null;
500 return null;
500 }
501 }
501 } else {
502 } else {
502 return this.register(name_or_data);
503 return this.register(name_or_data);
503 }
504 }
504 };
505 };
505
506
506 ActionHandler.prototype.get = function(name){
507 ActionHandler.prototype.get = function(name){
507 return this._actions[name];
508 return this._actions[name];
508 };
509 };
509
510
510 ActionHandler.prototype.call = function(name, event, env){
511 ActionHandler.prototype.call = function(name, event, env){
511 return this._actions[name].handler(env|| this.env, event);
512 return this._actions[name].handler(env|| this.env, event);
512 };
513 };
513
514
514 ActionHandler.prototype.exists = function(name){
515 ActionHandler.prototype.exists = function(name){
515 return (typeof(this._actions[name]) !== 'undefined');
516 return (typeof(this._actions[name]) !== 'undefined');
516 };
517 };
517
518
518 return {init:ActionHandler};
519 return {init:ActionHandler};
519
520
520 });
521 });
General Comments 0
You need to be logged in to leave comments. Login now