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