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