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([
5
'jquery',
6
'base/js/namespace',
7
'base/js/dialog',
8
'base/js/utils',
9
'notebook/js/tour',
10
'bootstrap',
11
'moment',
12
], function($, IPython, dialog, utils, tour, bootstrap, moment) {
13
"use strict";
14
15
var MenuBar = function (selector, options) {
16
/**
17
* Constructor
18
*
19
* A MenuBar Class to generate the menubar of IPython notebook
20
*
21
* Parameters:
22
* selector: string
23
* options: dictionary
24
* Dictionary of keyword arguments.
25
* notebook: Notebook instance
26
* contents: ContentManager instance
27
* events: $(Events) instance
28
* save_widget: SaveWidget instance
29
* quick_help: QuickHelp instance
30
* base_url : string
31
* notebook_path : string
32
* notebook_name : string
33
*/
34
options = options || {};
35
this.base_url = options.base_url || utils.get_body_data("baseUrl");
36
this.selector = selector;
37
this.notebook = options.notebook;
38
this.contents = options.contents;
39
this.events = options.events;
40
this.save_widget = options.save_widget;
41
this.quick_help = options.quick_help;
42
43
try {
44
this.tour = new tour.Tour(this.notebook, this.events);
45
} catch (e) {
46
this.tour = undefined;
47
console.log("Failed to instantiate Notebook Tour", e);
48
}
49
50
if (this.selector !== undefined) {
51
this.element = $(selector);
52
this.style();
53
this.bind_events();
54
}
55
};
56
57
// TODO: This has definitively nothing to do with style ...
58
MenuBar.prototype.style = function () {
59
var that = this;
60
this.element.find("li").click(function (event, ui) {
61
// The selected cell loses focus when the menu is entered, so we
62
// re-select it upon selection.
63
var i = that.notebook.get_selected_index();
64
that.notebook.select(i);
65
}
66
);
67
};
68
69
MenuBar.prototype._nbconvert = function (format, download) {
70
download = download || false;
71
var notebook_path = this.notebook.notebook_path;
72
var url = utils.url_join_encode(
73
this.base_url,
74
'nbconvert',
75
format,
76
notebook_path
77
) + "?download=" + download.toString();
78
79
var w = window.open('', IPython._target);
80
if (this.notebook.dirty) {
81
this.notebook.save_notebook().then(function() {
82
w.location = url;
83
});
84
} else {
85
w.location = url;
86
}
87
};
88
89
MenuBar.prototype._size_header = function() {
90
/**
91
* Update header spacer size.
92
*/
93
this.events.trigger('resize-header.Page');
94
};
95
96
MenuBar.prototype.bind_events = function () {
97
/**
98
* File
99
*/
100
var that = this;
101
102
this.element.find('#open_notebook').click(function () {
103
var parent = utils.url_path_split(that.notebook.notebook_path)[0];
104
window.open(utils.url_join_encode(that.base_url, 'tree', parent), IPython._target);
105
});
106
this.element.find('#copy_notebook').click(function () {
107
if (that.notebook.dirty) {
108
that.notebook.save_notebook({async : false});
109
}
110
that.notebook.copy_notebook();
111
return false;
112
});
113
this.element.find('#download_ipynb').click(function () {
114
var base_url = that.notebook.base_url;
115
var notebook_path = that.notebook.notebook_path;
116
if (that.notebook.dirty) {
117
that.notebook.save_notebook({async : false});
118
}
119
120
var url = utils.url_join_encode(base_url, 'files', notebook_path);
121
window.open(url + '?download=1');
122
});
123
124
this.element.find('#print_preview').click(function () {
125
that._nbconvert('html', false);
126
});
127
128
this.element.find('#download_html').click(function () {
129
that._nbconvert('html', true);
130
});
131
132
this.element.find('#download_markdown').click(function () {
133
that._nbconvert('markdown', true);
134
});
135
136
this.element.find('#download_rst').click(function () {
137
that._nbconvert('rst', true);
138
});
139
140
this.element.find('#download_pdf').click(function () {
141
that._nbconvert('pdf', true);
142
});
143
144
this.element.find('#download_script').click(function () {
145
that._nbconvert('script', true);
146
});
147
148
this.element.find('#rename_notebook').click(function () {
149
that.save_widget.rename_notebook({notebook: that.notebook});
150
});
151
152
this.element.find('#save_checkpoint').click(function () {
153
that.notebook.save_checkpoint();
154
});
155
156
this.element.find('#restore_checkpoint').click(function () {
157
});
158
159
this.element.find('#trust_notebook').click(function () {
160
that.notebook.trust_notebook();
161
});
162
this.events.on('trust_changed.Notebook', function (event, trusted) {
163
if (trusted) {
164
that.element.find('#trust_notebook')
165
.addClass("disabled").off('click')
166
.find("a").text("Trusted Notebook");
167
} else {
168
that.element.find('#trust_notebook')
169
.removeClass("disabled").on('click', function () {
170
that.notebook.trust_notebook();
171
})
172
.find("a").text("Trust Notebook");
173
}
174
});
175
176
this.element.find('#kill_and_exit').click(function () {
177
var close_window = function () {
178
/**
179
* allow closing of new tabs in Chromium, impossible in FF
180
*/
181
window.open('', '_self', '');
182
window.close();
183
};
184
// finish with close on success or failure
185
that.notebook.session.delete(close_window, close_window);
186
});
187
188
// Edit
189
this.element.find('#cut_cell').click(function () {
190
that.notebook.cut_cell();
191
});
192
this.element.find('#copy_cell').click(function () {
193
that.notebook.copy_cell();
194
});
195
this.element.find('#delete_cell').click(function () {
196
that.notebook.delete_cell();
197
});
198
this.element.find('#undelete_cell').click(function () {
199
that.notebook.undelete_cell();
200
});
201
this.element.find('#split_cell').click(function () {
202
that.notebook.split_cell();
203
});
204
this.element.find('#merge_cell_above').click(function () {
205
that.notebook.merge_cell_above();
206
});
207
this.element.find('#merge_cell_below').click(function () {
208
that.notebook.merge_cell_below();
209
});
210
this.element.find('#move_cell_up').click(function () {
211
that.notebook.move_cell_up();
212
});
213
this.element.find('#move_cell_down').click(function () {
214
that.notebook.move_cell_down();
215
});
216
this.element.find('#edit_nb_metadata').click(function () {
217
that.notebook.edit_metadata({
218
notebook: that.notebook,
219
keyboard_manager: that.notebook.keyboard_manager});
220
});
221
222
// View
223
this.element.find('#toggle_header').click(function () {
224
$('#header-container').toggle();
225
$('.header-bar').toggle();
226
that._size_header();
227
});
228
this.element.find('#toggle_toolbar').click(function () {
229
$('div#maintoolbar').toggle();
230
that._size_header();
231
});
232
// Insert
233
this.element.find('#insert_cell_above').click(function () {
234
that.notebook.insert_cell_above('code');
235
that.notebook.select_prev();
236
});
237
this.element.find('#insert_cell_below').click(function () {
238
that.notebook.insert_cell_below('code');
239
that.notebook.select_next();
240
});
241
// Cell
242
this.element.find('#run_cell').click(function () {
243
that.notebook.execute_cell();
244
});
245
this.element.find('#run_cell_select_below').click(function () {
246
that.notebook.execute_cell_and_select_below();
247
});
248
this.element.find('#run_cell_insert_below').click(function () {
249
that.notebook.execute_cell_and_insert_below();
250
});
251
this.element.find('#run_all_cells').click(function () {
252
that.notebook.execute_all_cells();
253
});
254
this.element.find('#run_all_cells_above').click(function () {
255
that.notebook.execute_cells_above();
256
});
257
this.element.find('#run_all_cells_below').click(function () {
258
that.notebook.execute_cells_below();
259
});
260
this.element.find('#to_code').click(function () {
261
that.notebook.to_code();
262
});
263
this.element.find('#to_markdown').click(function () {
264
that.notebook.to_markdown();
265
});
266
this.element.find('#to_raw').click(function () {
267
that.notebook.to_raw();
268
});
269
270
this.element.find('#toggle_current_output').click(function () {
271
that.notebook.toggle_output();
272
});
273
this.element.find('#toggle_current_output_scroll').click(function () {
274
that.notebook.toggle_output_scroll();
275
});
276
this.element.find('#clear_current_output').click(function () {
277
that.notebook.clear_output();
278
});
279
280
this.element.find('#toggle_all_output').click(function () {
281
that.notebook.toggle_all_output();
282
});
283
this.element.find('#toggle_all_output_scroll').click(function () {
284
that.notebook.toggle_all_output_scroll();
285
});
286
this.element.find('#clear_all_output').click(function () {
287
that.notebook.clear_all_output();
288
});
289
290
// Kernel
291
this.element.find('#int_kernel').click(function () {
292
that.notebook.kernel.interrupt();
293
});
294
this.element.find('#restart_kernel').click(function () {
295
that.notebook.restart_kernel();
296
});
297
this.element.find('#reconnect_kernel').click(function () {
298
that.notebook.kernel.reconnect();
299
});
300
// Help
301
if (this.tour) {
302
this.element.find('#notebook_tour').click(function () {
303
that.tour.start();
304
});
305
} else {
306
this.element.find('#notebook_tour').addClass("disabled");
307
}
308
this.element.find('#keyboard_shortcuts').click(function () {
309
that.quick_help.show_keyboard_shortcuts();
310
});
311
312
this.update_restore_checkpoint(null);
313
314
this.events.on('checkpoints_listed.Notebook', function (event, data) {
315
that.update_restore_checkpoint(that.notebook.checkpoints);
316
});
317
318
this.events.on('checkpoint_created.Notebook', function (event, data) {
319
that.update_restore_checkpoint(that.notebook.checkpoints);
320
});
321
322
this.events.on('notebook_loaded.Notebook', function() {
323
var langinfo = that.notebook.metadata.language_info || {};
324
that.update_nbconvert_script(langinfo);
325
});
326
327
this.events.on('kernel_ready.Kernel', function(event, data) {
328
var langinfo = data.kernel.info_reply.language_info || {};
329
that.update_nbconvert_script(langinfo);
330
that.add_kernel_help_links(data.kernel.info_reply.help_links || []);
331
});
332
};
333
334
MenuBar.prototype.update_restore_checkpoint = function(checkpoints) {
335
var ul = this.element.find("#restore_checkpoint").find("ul");
336
ul.empty();
337
if (!checkpoints || checkpoints.length === 0) {
338
ul.append(
339
$("<li/>")
340
.addClass("disabled")
341
.append(
342
$("<a/>")
343
.text("No checkpoints")
344
)
345
);
346
return;
347
}
348
349
var that = this;
350
checkpoints.map(function (checkpoint) {
351
var d = new Date(checkpoint.last_modified);
352
ul.append(
353
$("<li/>").append(
354
$("<a/>")
355
.attr("href", "#")
356
.text(moment(d).format("LLLL"))
357
.click(function () {
358
that.notebook.restore_checkpoint_dialog(checkpoint);
359
})
360
)
361
);
362
});
363
};
364
365
MenuBar.prototype.update_nbconvert_script = function(langinfo) {
366
/**
367
* Set the 'Download as foo' menu option for the relevant language.
368
*/
369
var el = this.element.find('#download_script');
370
371
// Set menu entry text to e.g. "Python (.py)"
372
var langname = (langinfo.name || 'Script');
373
langname = langname.charAt(0).toUpperCase()+langname.substr(1); // Capitalise
374
el.find('a').text(langname + ' ('+(langinfo.file_extension || 'txt')+')');
375
};
376
377
MenuBar.prototype.add_kernel_help_links = function(help_links) {
378
/** add links from kernel_info to the help menu */
379
var divider = $("#kernel-help-links");
380
if (divider.length === 0) {
381
// insert kernel help section above about link
382
var about = $("#notebook_about").parent();
383
divider = $("<li>")
384
.attr('id', "kernel-help-links")
385
.addClass('divider');
386
about.prev().before(divider);
387
}
388
// remove previous entries
389
while (!divider.next().hasClass('divider')) {
390
divider.next().remove();
391
}
392
if (help_links.length === 0) {
393
// no help links, remove the divider
394
divider.remove();
395
return;
396
}
397
var cursor = divider;
398
help_links.map(function (link) {
399
cursor.after($("<li>")
400
.append($("<a>")
401
.attr('target', '_blank')
402
.attr('title', 'Opens in a new window')
403
.attr('href', link.url)
404
.append($("<i>")
405
.addClass("fa fa-external-link menu-icon pull-right")
406
)
407
.append($("<span>")
408
.text(link.text)
409
)
410
)
411
);
412
cursor = cursor.next();
413
});
414
415
};
416
417
// Backwards compatability.
418
IPython.MenuBar = MenuBar;
419
420
return {'MenuBar': MenuBar};
421
});
422
423