##// END OF EJS Templates
Merge pull request #7811 from abalkin/patch-2...
Kyle Kelley -
r20468:32414b78 merge
parent child Browse files
Show More
@@ -1,518 +1,520 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: 'delete selected cell',
260 help: 'delete selected cell',
259 help_index : 'ej',
261 help_index : 'ej',
260 handler : function (env) {
262 handler : function (env) {
261 env.notebook.delete_cell();
263 env.notebook.delete_cell();
262 }
264 }
263 },
265 },
264 'interrupt-kernel':{
266 'interrupt-kernel':{
265 icon: 'fa-stop',
267 icon: 'fa-stop',
266 help_index : 'ha',
268 help_index : 'ha',
267 handler : function (env) {
269 handler : function (env) {
268 env.notebook.kernel.interrupt();
270 env.notebook.kernel.interrupt();
269 env.notebook.focus_cell();
271 env.notebook.focus_cell();
270 }
272 }
271 },
273 },
272 'restart-kernel':{
274 'restart-kernel':{
273 icon: 'fa-repeat',
275 icon: 'fa-repeat',
274 help_index : 'hb',
276 help_index : 'hb',
275 handler : function (env) {
277 handler : function (env) {
276 env.notebook.restart_kernel();
278 env.notebook.restart_kernel();
277 env.notebook.focus_cell();
279 env.notebook.focus_cell();
278 }
280 }
279 },
281 },
280 'undo-last-cell-deletion' : {
282 'undo-last-cell-deletion' : {
281 help_index : 'ei',
283 help_index : 'ei',
282 handler : function (env) {
284 handler : function (env) {
283 env.notebook.undelete_cell();
285 env.notebook.undelete_cell();
284 }
286 }
285 },
287 },
286 'merge-selected-cell-with-cell-after' : {
288 'merge-selected-cell-with-cell-after' : {
287 help : 'merge cell below',
289 help : 'merge cell below',
288 help_index : 'ek',
290 help_index : 'ek',
289 handler : function (env) {
291 handler : function (env) {
290 env.notebook.merge_cell_below();
292 env.notebook.merge_cell_below();
291 }
293 }
292 },
294 },
293 'close-pager' : {
295 'close-pager' : {
294 help_index : 'gd',
296 help_index : 'gd',
295 handler : function (env) {
297 handler : function (env) {
296 env.pager.collapse();
298 env.pager.collapse();
297 }
299 }
298 }
300 }
299
301
300 };
302 };
301
303
302 /**
304 /**
303 * A bunch of `Advance actions` for IPython.
305 * A bunch of `Advance actions` for IPython.
304 * Cf `Simple Action` plus the following properties.
306 * Cf `Simple Action` plus the following properties.
305 *
307 *
306 * 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
307 * (typically keypress). The handler is responsible for any modification of the
309 * (typically keypress). The handler is responsible for any modification of the
308 * event and event propagation.
310 * event and event propagation.
309 * 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,
310 * true, to tell keyboard manager that it ignored the event.
312 * true, to tell keyboard manager that it ignored the event.
311 *
313 *
312 * 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
313 *
315 *
314 **/
316 **/
315 var custom_ignore = {
317 var custom_ignore = {
316 'ignore':{
318 'ignore':{
317 handler : function () {
319 handler : function () {
318 return true;
320 return true;
319 }
321 }
320 },
322 },
321 'move-cursor-up-or-previous-cell':{
323 'move-cursor-up-or-previous-cell':{
322 handler : function (env, event) {
324 handler : function (env, event) {
323 var index = env.notebook.get_selected_index();
325 var index = env.notebook.get_selected_index();
324 var cell = env.notebook.get_cell(index);
326 var cell = env.notebook.get_cell(index);
325 var cm = env.notebook.get_selected_cell().code_mirror;
327 var cm = env.notebook.get_selected_cell().code_mirror;
326 var cur = cm.getCursor();
328 var cur = cm.getCursor();
327 if (cell && cell.at_top() && index !== 0 && cur.ch === 0) {
329 if (cell && cell.at_top() && index !== 0 && cur.ch === 0) {
328 if(event){
330 if(event){
329 event.preventDefault();
331 event.preventDefault();
330 }
332 }
331 env.notebook.command_mode();
333 env.notebook.command_mode();
332 env.notebook.select_prev();
334 env.notebook.select_prev();
333 env.notebook.edit_mode();
335 env.notebook.edit_mode();
334 cm = env.notebook.get_selected_cell().code_mirror;
336 cm = env.notebook.get_selected_cell().code_mirror;
335 cm.setCursor(cm.lastLine(), 0);
337 cm.setCursor(cm.lastLine(), 0);
336 }
338 }
337 return false;
339 return false;
338 }
340 }
339 },
341 },
340 'move-cursor-down-or-next-cell':{
342 'move-cursor-down-or-next-cell':{
341 handler : function (env, event) {
343 handler : function (env, event) {
342 var index = env.notebook.get_selected_index();
344 var index = env.notebook.get_selected_index();
343 var cell = env.notebook.get_cell(index);
345 var cell = env.notebook.get_cell(index);
344 if (cell.at_bottom() && index !== (env.notebook.ncells()-1)) {
346 if (cell.at_bottom() && index !== (env.notebook.ncells()-1)) {
345 if(event){
347 if(event){
346 event.preventDefault();
348 event.preventDefault();
347 }
349 }
348 env.notebook.command_mode();
350 env.notebook.command_mode();
349 env.notebook.select_next();
351 env.notebook.select_next();
350 env.notebook.edit_mode();
352 env.notebook.edit_mode();
351 var cm = env.notebook.get_selected_cell().code_mirror;
353 var cm = env.notebook.get_selected_cell().code_mirror;
352 cm.setCursor(0, 0);
354 cm.setCursor(0, 0);
353 }
355 }
354 return false;
356 return false;
355 }
357 }
356 },
358 },
357 'scroll-down': {
359 'scroll-down': {
358 handler: function(env, event) {
360 handler: function(env, event) {
359 if(event){
361 if(event){
360 event.preventDefault();
362 event.preventDefault();
361 }
363 }
362 return env.notebook.scroll_manager.scroll(1);
364 return env.notebook.scroll_manager.scroll(1);
363 },
365 },
364 },
366 },
365 'scroll-up': {
367 'scroll-up': {
366 handler: function(env, event) {
368 handler: function(env, event) {
367 if(event){
369 if(event){
368 event.preventDefault();
370 event.preventDefault();
369 }
371 }
370 return env.notebook.scroll_manager.scroll(-1);
372 return env.notebook.scroll_manager.scroll(-1);
371 },
373 },
372 },
374 },
373 'save-notebook':{
375 'save-notebook':{
374 help: "Save and Checkpoint",
376 help: "Save and Checkpoint",
375 help_index : 'fb',
377 help_index : 'fb',
376 icon: 'fa-save',
378 icon: 'fa-save',
377 handler : function (env, event) {
379 handler : function (env, event) {
378 env.notebook.save_checkpoint();
380 env.notebook.save_checkpoint();
379 if(event){
381 if(event){
380 event.preventDefault();
382 event.preventDefault();
381 }
383 }
382 return false;
384 return false;
383 }
385 }
384 },
386 },
385 };
387 };
386
388
387 // private stuff that prepend `.ipython` to actions names
389 // private stuff that prepend `.ipython` to actions names
388 // and uniformize/fill in missing pieces in of an action.
390 // and uniformize/fill in missing pieces in of an action.
389 var _prepare_handler = function(registry, subkey, source){
391 var _prepare_handler = function(registry, subkey, source){
390 registry['ipython.'+subkey] = {};
392 registry['ipython.'+subkey] = {};
391 registry['ipython.'+subkey].help = source[subkey].help||subkey.replace(/-/g,' ');
393 registry['ipython.'+subkey].help = source[subkey].help||subkey.replace(/-/g,' ');
392 registry['ipython.'+subkey].help_index = source[subkey].help_index;
394 registry['ipython.'+subkey].help_index = source[subkey].help_index;
393 registry['ipython.'+subkey].icon = source[subkey].icon;
395 registry['ipython.'+subkey].icon = source[subkey].icon;
394 return source[subkey].handler;
396 return source[subkey].handler;
395 };
397 };
396
398
397 // Will actually generate/register all the IPython actions
399 // Will actually generate/register all the IPython actions
398 var fun = function(){
400 var fun = function(){
399 var final_actions = {};
401 var final_actions = {};
400 var k;
402 var k;
401 for(k in _actions){
403 for(k in _actions){
402 if(_actions.hasOwnProperty(k)){
404 if(_actions.hasOwnProperty(k)){
403 // Js closure are function level not block level need to wrap in a IIFE
405 // Js closure are function level not block level need to wrap in a IIFE
404 // and append ipython to event name these things do intercept event so are wrapped
406 // and append ipython to event name these things do intercept event so are wrapped
405 // in a function that return false.
407 // in a function that return false.
406 var handler = _prepare_handler(final_actions, k, _actions);
408 var handler = _prepare_handler(final_actions, k, _actions);
407 (function(key, handler){
409 (function(key, handler){
408 final_actions['ipython.'+key].handler = function(env, event){
410 final_actions['ipython.'+key].handler = function(env, event){
409 handler(env);
411 handler(env);
410 if(event){
412 if(event){
411 event.preventDefault();
413 event.preventDefault();
412 }
414 }
413 return false;
415 return false;
414 };
416 };
415 })(k, handler);
417 })(k, handler);
416 }
418 }
417 }
419 }
418
420
419 for(k in custom_ignore){
421 for(k in custom_ignore){
420 // Js closure are function level not block level need to wrap in a IIFE
422 // Js closure are function level not block level need to wrap in a IIFE
421 // same as above, but decide for themselves wether or not they intercept events.
423 // same as above, but decide for themselves wether or not they intercept events.
422 if(custom_ignore.hasOwnProperty(k)){
424 if(custom_ignore.hasOwnProperty(k)){
423 var handler = _prepare_handler(final_actions, k, custom_ignore);
425 var handler = _prepare_handler(final_actions, k, custom_ignore);
424 (function(key, handler){
426 (function(key, handler){
425 final_actions['ipython.'+key].handler = function(env, event){
427 final_actions['ipython.'+key].handler = function(env, event){
426 return handler(env, event);
428 return handler(env, event);
427 };
429 };
428 })(k, handler);
430 })(k, handler);
429 }
431 }
430 }
432 }
431
433
432 return final_actions;
434 return final_actions;
433 };
435 };
434 ActionHandler.prototype._actions = fun();
436 ActionHandler.prototype._actions = fun();
435
437
436
438
437 /**
439 /**
438 * extend the environment variable that will be pass to handlers
440 * extend the environment variable that will be pass to handlers
439 **/
441 **/
440 ActionHandler.prototype.extend_env = function(env){
442 ActionHandler.prototype.extend_env = function(env){
441 for(var k in env){
443 for(var k in env){
442 this.env[k] = env[k];
444 this.env[k] = env[k];
443 }
445 }
444 };
446 };
445
447
446 ActionHandler.prototype.register = function(action, name, prefix){
448 ActionHandler.prototype.register = function(action, name, prefix){
447 /**
449 /**
448 * Register an `action` with an optional name and prefix.
450 * Register an `action` with an optional name and prefix.
449 *
451 *
450 * if name and prefix are not given they will be determined automatically.
452 * if name and prefix are not given they will be determined automatically.
451 * if action if just a `function` it will be wrapped in an anonymous action.
453 * if action if just a `function` it will be wrapped in an anonymous action.
452 *
454 *
453 * @return the full name to access this action .
455 * @return the full name to access this action .
454 **/
456 **/
455 action = this.normalise(action);
457 action = this.normalise(action);
456 if( !name ){
458 if( !name ){
457 name = 'autogenerated-'+String(action.handler);
459 name = 'autogenerated-'+String(action.handler);
458 }
460 }
459 prefix = prefix || 'auto';
461 prefix = prefix || 'auto';
460 var full_name = prefix+'.'+name;
462 var full_name = prefix+'.'+name;
461 this._actions[full_name] = action;
463 this._actions[full_name] = action;
462 return full_name;
464 return full_name;
463
465
464 };
466 };
465
467
466
468
467 ActionHandler.prototype.normalise = function(data){
469 ActionHandler.prototype.normalise = function(data){
468 /**
470 /**
469 * given an `action` or `function`, return a normalised `action`
471 * given an `action` or `function`, return a normalised `action`
470 * by setting all known attributes and removing unknown attributes;
472 * by setting all known attributes and removing unknown attributes;
471 **/
473 **/
472 if(typeof(data) === 'function'){
474 if(typeof(data) === 'function'){
473 data = {handler:data};
475 data = {handler:data};
474 }
476 }
475 if(typeof(data.handler) !== 'function'){
477 if(typeof(data.handler) !== 'function'){
476 throw('unknown datatype, cannot register');
478 throw('unknown datatype, cannot register');
477 }
479 }
478 var _data = data;
480 var _data = data;
479 data = {};
481 data = {};
480 data.handler = _data.handler;
482 data.handler = _data.handler;
481 data.help = _data.help || '';
483 data.help = _data.help || '';
482 data.icon = _data.icon || '';
484 data.icon = _data.icon || '';
483 data.help_index = _data.help_index || '';
485 data.help_index = _data.help_index || '';
484 return data;
486 return data;
485 };
487 };
486
488
487 ActionHandler.prototype.get_name = function(name_or_data){
489 ActionHandler.prototype.get_name = function(name_or_data){
488 /**
490 /**
489 * given an `action` or `name` of a action, return the name attached to this action.
491 * given an `action` or `name` of a action, return the name attached to this action.
490 * if given the name of and corresponding actions does not exist in registry, return `null`.
492 * if given the name of and corresponding actions does not exist in registry, return `null`.
491 **/
493 **/
492
494
493 if(typeof(name_or_data) === 'string'){
495 if(typeof(name_or_data) === 'string'){
494 if(this.exists(name_or_data)){
496 if(this.exists(name_or_data)){
495 return name_or_data;
497 return name_or_data;
496 } else {
498 } else {
497 return null;
499 return null;
498 }
500 }
499 } else {
501 } else {
500 return this.register(name_or_data);
502 return this.register(name_or_data);
501 }
503 }
502 };
504 };
503
505
504 ActionHandler.prototype.get = function(name){
506 ActionHandler.prototype.get = function(name){
505 return this._actions[name];
507 return this._actions[name];
506 };
508 };
507
509
508 ActionHandler.prototype.call = function(name, event, env){
510 ActionHandler.prototype.call = function(name, event, env){
509 return this._actions[name].handler(env|| this.env, event);
511 return this._actions[name].handler(env|| this.env, event);
510 };
512 };
511
513
512 ActionHandler.prototype.exists = function(name){
514 ActionHandler.prototype.exists = function(name){
513 return (typeof(this._actions[name]) !== 'undefined');
515 return (typeof(this._actions[name]) !== 'undefined');
514 };
516 };
515
517
516 return {init:ActionHandler};
518 return {init:ActionHandler};
517
519
518 });
520 });
General Comments 0
You need to be logged in to leave comments. Login now