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