/
insertcell.js
402 lines (356 loc) · 16.4 KB
/
insertcell.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
define([
'jquery',
'base/js/namespace',
'base/js/utils',
'base/js/i18n',
'base/js/keyboard',
'./codecell',
'./textcell',
'services/config'
], function (
$,
Jupyter,
utils,
i18n,
keyboard,
codecell,
textcell,
configmod
) {
function load_ipython_extension() {
function Notebook(selector, options) {
IPython.notebook.config = options.config;
IPython.notebook.config.loaded.then(IPython.notebook.validate_config.bind(this))
IPython.notebook.class_config = new configmod.ConfigWithDefaults(IPython.notebook.config,
Notebook.options_default, 'Notebook');
IPython.notebook.base_url = options.base_url;
IPython.notebook.notebook_path = options.notebook_path;
IPython.notebook.notebook_name = options.notebook_name;
IPython.notebook.events = options.events;
IPython.notebook.keyboard_manager = options.keyboard_manager;
IPython.notebook.contents = options.contents;
IPython.notebook.save_widget = options.save_widget;
IPython.notebook.tooltip = new tooltip.Tooltip(IPython.notebook.events);
IPython.notebook.ws_url = options.ws_url;
IPython.notebook._session_starting = false;
IPython.notebook.last_modified = null;
// debug 484
IPython.notebook._last_modified = 'init';
// Firefox workaround
IPython.notebook._ff_beforeunload_fired = false;
// Create default scroll manager.
IPython.notebook.scroll_manager = new scrollmanager.ScrollManager(this);
// TODO: This code smells (and the other `= this` line a couple lines down)
// We need a better way to deal with circular instance references.
IPython.notebook.keyboard_manager.notebook = this;
IPython.notebook.save_widget.notebook = this;
mathjaxutils.init();
if (marked) {
marked.setOptions({
gfm: true,
tables: true,
// FIXME: probably want central config for CodeMirror theme when we have js config
langPrefix: "cm-s-ipython language-",
highlight: function (code, lang, callback) {
if (!lang) {
// no language, no highlight
if (callback) {
callback(null, code);
return;
} else {
return code;
}
}
utils.requireCodeMirrorMode(lang, function (spec) {
var el = document.createElement("div");
var mode = CodeMirror.getMode({}, spec);
if (!mode) {
console.log("No CodeMirror mode: " + lang);
callback(null, code);
return;
}
try {
CodeMirror.runMode(code, spec, el);
callback(null, el.innerHTML);
} catch (err) {
console.log("Failed to highlight " + lang + " code", err);
callback(err, code);
}
}, function (err) {
console.log("No CodeMirror mode: " + lang);
console.log("Require CodeMirror mode error: " + err);
callback(null, code);
});
}
});
}
IPython.notebook.element = $(selector);
IPython.notebook.element.scroll();
IPython.notebook.element.data("notebook", this);
IPython.notebook.session = null;
IPython.notebook.kernel = null;
IPython.notebook.kernel_busy = false;
IPython.notebook.clipboard = null;
IPython.notebook.clipboard_attachments = null;
IPython.notebook.undelete_backup_stack = [];
IPython.notebook.paste_enabled = false;
IPython.notebook.paste_attachments_enabled = false;
IPython.notebook.writable = false;
// It is important to start out in command mode to match the intial mode
// of the KeyboardManager.
IPython.notebook.mode = 'command';
IPython.notebook.set_dirty(false);
IPython.notebook.metadata = {};
IPython.notebook._checkpoint_after_save = false;
IPython.notebook.last_checkpoint = null;
IPython.notebook.checkpoints = [];
IPython.notebook.autosave_interval = 0;
IPython.notebook.autosave_timer = null;
// autosave *at most* every two minutes
IPython.notebook.minimum_autosave_interval = 120000;
IPython.notebook.notebook_name_blacklist_re = /[\/\\:]/;
IPython.notebook.nbformat = 4; // Increment this when changing the nbformat
IPython.notebook.nbformat_minor = IPython.notebook.current_nbformat_minor = 1; // Increment this when changing the nbformat
IPython.notebook.codemirror_mode = 'text';
IPython.notebook.create_elements();
IPython.notebook.bind_events();
IPython.notebook.kernel_selector = null;
IPython.notebook.dirty = null;
IPython.notebook.trusted = null;
IPython.notebook._changed_on_disk_dialog = null;
IPython.notebook._fully_loaded = false;
// Trigger cell toolbar registration.
default_celltoolbar.register(this);
rawcell_celltoolbar.register(this);
slideshow_celltoolbar.register(this);
attachments_celltoolbar.register(this);
tags_celltoolbar.register(this);
var that = this;
Object.defineProperty(this, 'line_numbers', {
get: function () {
var d = that.config.data || {};
var cmc = (d['Cell'] || {}) ['cm_config'] || {};
return cmc['lineNumbers'] || false;
},
set: function (value) {
that.config.update({
'Cell': {
'cm_config': {
'lineNumbers': value
}
}
});
}
});
Object.defineProperty(this, 'header', {
get: function () {
return that.class_config.get_sync('Header');
},
set: function (value) {
that.class_config.set('Header', value);
}
});
Object.defineProperty(this, 'toolbar', {
get: function () {
return that.class_config.get_sync('Toolbar');
},
set: function (value) {
that.class_config.set('Toolbar', value);
}
});
IPython.notebook.class_config.get('Header').then(function (header) {
if (header === false) {
that.keyboard_manager.actions.call('jupyter-notebook:hide-header');
}
});
IPython.notebook.class_config.get('Toolbar').then(function (toolbar) {
if (toolbar === false) {
that.keyboard_manager.actions.call('jupyter-notebook:hide-toolbar');
}
});
// prevent assign to miss-typed properties.
Object.seal(this);
};
Notebook.options_default = {
// can be any cell type, or the special values of
// 'above', 'below', or 'selected' to get the value from another cell.
default_cell_type: 'code',
Header: true,
Toolbar: true
};
// Notebook.prototype.validate_config = function() {
// var code_cell = IPython.notebook.config.data['CodeCell'] || {};
// console.log(code_cell);
// var cm_keymap = (code_cell['cm_config'] || {})['keyMap'];
// if (cm_keymap && CodeMirror.keyMap[cm_keymap] === undefined) {
// console.warn('CodeMirror keymap not found, ignoring: ' + cm_keymap);
// delete code_cell.cm_config.keyMap;
// }
//};
var insert_cell_original= function(type, index){
var ncells = IPython.notebook.ncells();
index = Math.min(index, ncells);
index = Math.max(index, 0);
var cell = null;
type = type || IPython.notebook.class_config.get_sync('default_cell_type');
if (type === 'above') {
if (index > 0) {
type = IPython.notebook.get_cell(index-1).cell_type;
} else {
type = 'code';
}
} else if (type === 'below') {
if (index < ncells) {
type = IPython.notebook.get_cell(index).cell_type;
} else {
type = 'code';
}
} else if (type === 'selected') {
type = IPython.notebook.get_selected_cell().cell_type;
}
if (ncells === 0 || IPython.notebook.is_valid_cell_index(index) || index === ncells) {
var cell_options = {
events: IPython.notebook.events,
config: IPython.notebook.config,
keyboard_manager: IPython.notebook.keyboard_manager,
notebook: this,
tooltip: IPython.notebook.tooltip
};
switch(type) {
case 'code':
cell = new codecell.CodeCell(IPython.notebook.kernel, cell_options);
cell.set_input_prompt();
break;
case 'markdown':
cell = new textcell.MarkdownCell(cell_options);
break;
case 'raw':
cell = new textcell.RawCell(cell_options);
break;
default:
console.log("Unrecognized cell type: ", type, cellmod);
cell = new cellmod.UnrecognizedCell(cell_options);
}
if(Jupyter.notebook.insert_element_at_index("code", index)) {
cell.render();
IPython.notebook.events.trigger('create.Cell', {'cell': cell, 'index': index});
cell.refresh();
// We used to select the cell after we refresh it, but there
// are now cases were this method is called where select is
// not appropriate. The selection logic should be handled by the
// caller of the the top level insert_cell methods.
IPython.notebook.set_dirty(true);
}
}
return cell;
};
var insert_cell_extension = function (type, index) {
console.log("use exte");
// num cells
var ncells = IPython.notebook.ncells();
index = Math.min(index, ncells);
index = Math.max(index, 0);
var cell = null;
type = type || IPython.notebook.class_config.get_sync('default_cell_type');
// Determine where to place the new cell and what type it should be
// Type of the new cell is the type of the origin cell
if (type === 'above') {
if (index > 0) {
type = IPython.notebook.get_cell(index-1).cell_type;
} else {
type = 'code';
}
} else if (type === 'below') {
if (index < ncells) {
type = IPython.notebook.get_cell(index).cell_type;
} else {
type = 'code';
}
} else if (type === 'selected') {
type = IPython.notebook.get_selected_cell().cell_type;
}
console.log("here");
// Configure the cell's options and set its cell-type.
if (ncells === 0 || IPython.notebook.is_valid_cell_index(index) || index === ncells) {
var cell_options = {
events: IPython.notebook.events,
config: IPython.notebook.config,
keyboard_manager: IPython.notebook.keyboard_manager,
notebook: this,
tooltip: IPython.notebook.tooltip
};
switch(type) {
case 'code':
//codecell=new codecell();
cell = new codecell.CodeCell(IPython.notebook.kernel, cell_options);
cell.set_input_prompt();
break;
case 'markdown':
cell = new textcell.MarkdownCell(cell_options);
break;
case 'raw':
cell = new textcell.RawCell(cell_options);
break;
default:
console.log("Unrecognized cell type: ", type, cellmod);
cell = new cellmod.UnrecognizedCell(cell_options);
}
// re-render new cell and trigger cell-creation event
if(Jupyter.notebook.insert_cell_at_index("code", index)) {
cell.render();
IPython.notebook.events.trigger('create.Cell', {'cell': cell, 'index': index});
cell.refresh();
// We used to select the cell after we refresh it, but there
// are now cases were this method is called where select is
// not appropriate. The selection logic should be handled by the
// caller of the the top level insert_cell methods.
IPython.notebook.set_dirty(true);
}
}
var cell = IPython.notebook.get_cell(index);
// Set the cell's appearance
cell.code_mirror.doc.setValue("Temporary cell (will not be saved)");
cell.metadata.cell_type = "temp";
cell.metadata.mentor_academy_cell_type = "temporary";
var input=cell.element[0].children.item(0);
var inner_prompt=input.children.item(0);
var inner_cell=input.children.item(1);
input_area=inner_cell.children.item(1);
input_area.style.background="#ececc2";
function dispatchAddedTempCellEvent() {
const EVENT_TITLE = 'Added Temp Cell';
const NUM_CELLS_IN_PART = 1;
const NUM_CELLS = Jupyter.notebook.get_cells().length;
const NEW_PART_START_INDEX = NUM_CELLS - NUM_CELLS_IN_PART;
var addedTempCellEvent = new CustomEvent(EVENT_TITLE, {
detail: {
"start_index" : NEW_PART_START_INDEX,
"end_index" : NUM_CELLS - 1
}
});
document.dispatchEvent(addedTempCellEvent);
}
dispatchAddedTempCellEvent();
return cell;
};
IPython.Notebook.prototype.insert_cell_below = function (type, index) {
if (index === null || index === undefined) {
index = Math.max(IPython.notebook.get_selected_index(index), IPython.notebook.get_anchor_index());
}
// console.log("Insert Cell Below");
return insert_cell_extension(type, index+1);
};
IPython.Notebook.prototype.insert_cell_above = function (type, index) {
if (index === null || index === undefined) {
index = Math.min(IPython.notebook.get_selected_index(index), IPython.notebook.get_anchor_index());
}
// console.log("Insert Cell Above");
return insert_cell_extension(type, index);
};
}
if (IPython.notebook.metadata.umich.submit === "yes") {
return {
load_ipython_extension: load_ipython_extension
};
}
});