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