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