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