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