Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Avatar for KuCalc : devops.
Download
50654 views
1
//----------------------------------------------------------------------------
2
// Copyright (C) 2008-2011 The IPython Development Team
3
//
4
// Distributed under the terms of the BSD License. The full license is in
5
// the file COPYING, distributed as part of this software.
6
//----------------------------------------------------------------------------
7
8
//============================================================================
9
// MenuBar
10
//============================================================================
11
12
/**
13
* @module IPython
14
* @namespace IPython
15
* @submodule MenuBar
16
*/
17
18
19
var IPython = (function (IPython) {
20
"use strict";
21
22
var utils = IPython.utils;
23
24
/**
25
* A MenuBar Class to generate the menubar of IPython notebook
26
* @Class MenuBar
27
*
28
* @constructor
29
*
30
*
31
* @param selector {string} selector for the menubar element in DOM
32
* @param {object} [options]
33
* @param [options.base_url] {String} String to use for the
34
* base project url. Default is to inspect
35
* $('body').data('baseUrl');
36
* does not support change for now is set through this option
37
*/
38
var MenuBar = function (selector, options) {
39
options = options || {};
40
this.base_url = options.base_url || IPython.utils.get_body_data("baseUrl");
41
this.selector = selector;
42
if (this.selector !== undefined) {
43
this.element = $(selector);
44
this.style();
45
this.bind_events();
46
}
47
};
48
49
MenuBar.prototype.style = function () {
50
this.element.addClass('border-box-sizing');
51
this.element.find("li").click(function (event, ui) {
52
// The selected cell loses focus when the menu is entered, so we
53
// re-select it upon selection.
54
var i = IPython.notebook.get_selected_index();
55
IPython.notebook.select(i);
56
}
57
);
58
};
59
60
MenuBar.prototype._nbconvert = function (format, download) {
61
download = download || false;
62
var notebook_path = IPython.notebook.notebook_path;
63
var notebook_name = IPython.notebook.notebook_name;
64
if (IPython.notebook.dirty) {
65
IPython.notebook.save_notebook({async : false});
66
}
67
var url = utils.url_join_encode(
68
this.base_url,
69
'nbconvert',
70
format,
71
notebook_path,
72
notebook_name
73
) + "?download=" + download.toString();
74
75
window.open(url);
76
};
77
78
MenuBar.prototype.bind_events = function () {
79
// File
80
var that = this;
81
this.element.find('#new_notebook').click(function () {
82
IPython.notebook.new_notebook();
83
});
84
this.element.find('#open_notebook').click(function () {
85
window.open(utils.url_join_encode(
86
IPython.notebook.base_url,
87
'tree',
88
IPython.notebook.notebook_path
89
));
90
});
91
this.element.find('#copy_notebook').click(function () {
92
IPython.notebook.copy_notebook();
93
return false;
94
});
95
this.element.find('#download_ipynb').click(function () {
96
var base_url = IPython.notebook.base_url;
97
var notebook_path = IPython.notebook.notebook_path;
98
var notebook_name = IPython.notebook.notebook_name;
99
if (IPython.notebook.dirty) {
100
IPython.notebook.save_notebook({async : false});
101
}
102
103
var url = utils.url_join_encode(
104
base_url,
105
'files',
106
notebook_path,
107
notebook_name
108
);
109
window.location.assign(url);
110
});
111
112
this.element.find('#print_preview').click(function () {
113
that._nbconvert('html', false);
114
});
115
116
this.element.find('#download_py').click(function () {
117
that._nbconvert('python', true);
118
});
119
120
this.element.find('#download_html').click(function () {
121
that._nbconvert('html', true);
122
});
123
124
this.element.find('#download_rst').click(function () {
125
that._nbconvert('rst', true);
126
});
127
128
this.element.find('#rename_notebook').click(function () {
129
IPython.save_widget.rename_notebook();
130
});
131
this.element.find('#save_checkpoint').click(function () {
132
IPython.notebook.save_checkpoint();
133
});
134
this.element.find('#restore_checkpoint').click(function () {
135
});
136
this.element.find('#trust_notebook').click(function () {
137
IPython.notebook.trust_notebook();
138
});
139
$([IPython.events]).on('trust_changed.Notebook', function (event, trusted) {
140
if (trusted) {
141
that.element.find('#trust_notebook')
142
.addClass("disabled")
143
.find("a").text("Trusted Notebook");
144
} else {
145
that.element.find('#trust_notebook')
146
.removeClass("disabled")
147
.find("a").text("Trust Notebook");
148
}
149
});
150
this.element.find('#kill_and_exit').click(function () {
151
IPython.notebook.session.delete();
152
setTimeout(function(){
153
// allow closing of new tabs in Chromium, impossible in FF
154
window.open('', '_self', '');
155
window.close();
156
}, 500);
157
});
158
// Edit
159
this.element.find('#cut_cell').click(function () {
160
IPython.notebook.cut_cell();
161
});
162
this.element.find('#copy_cell').click(function () {
163
IPython.notebook.copy_cell();
164
});
165
this.element.find('#delete_cell').click(function () {
166
IPython.notebook.delete_cell();
167
});
168
this.element.find('#undelete_cell').click(function () {
169
IPython.notebook.undelete_cell();
170
});
171
this.element.find('#split_cell').click(function () {
172
IPython.notebook.split_cell();
173
});
174
this.element.find('#merge_cell_above').click(function () {
175
IPython.notebook.merge_cell_above();
176
});
177
this.element.find('#merge_cell_below').click(function () {
178
IPython.notebook.merge_cell_below();
179
});
180
this.element.find('#move_cell_up').click(function () {
181
IPython.notebook.move_cell_up();
182
});
183
this.element.find('#move_cell_down').click(function () {
184
IPython.notebook.move_cell_down();
185
});
186
this.element.find('#edit_nb_metadata').click(function () {
187
IPython.notebook.edit_metadata();
188
});
189
190
// View
191
this.element.find('#toggle_header').click(function () {
192
$('div#header').toggle();
193
IPython.layout_manager.do_resize();
194
});
195
this.element.find('#toggle_toolbar').click(function () {
196
$('div#maintoolbar').toggle();
197
IPython.layout_manager.do_resize();
198
});
199
// Insert
200
this.element.find('#insert_cell_above').click(function () {
201
IPython.notebook.insert_cell_above('code');
202
IPython.notebook.select_prev();
203
});
204
this.element.find('#insert_cell_below').click(function () {
205
IPython.notebook.insert_cell_below('code');
206
IPython.notebook.select_next();
207
});
208
// Cell
209
this.element.find('#run_cell').click(function () {
210
IPython.notebook.execute_cell();
211
});
212
this.element.find('#run_cell_select_below').click(function () {
213
IPython.notebook.execute_cell_and_select_below();
214
});
215
this.element.find('#run_cell_insert_below').click(function () {
216
IPython.notebook.execute_cell_and_insert_below();
217
});
218
this.element.find('#run_all_cells').click(function () {
219
IPython.notebook.execute_all_cells();
220
});
221
this.element.find('#run_all_cells_above').click(function () {
222
IPython.notebook.execute_cells_above();
223
});
224
this.element.find('#run_all_cells_below').click(function () {
225
IPython.notebook.execute_cells_below();
226
});
227
this.element.find('#to_code').click(function () {
228
IPython.notebook.to_code();
229
});
230
this.element.find('#to_markdown').click(function () {
231
IPython.notebook.to_markdown();
232
});
233
this.element.find('#to_raw').click(function () {
234
IPython.notebook.to_raw();
235
});
236
this.element.find('#to_heading1').click(function () {
237
IPython.notebook.to_heading(undefined, 1);
238
});
239
this.element.find('#to_heading2').click(function () {
240
IPython.notebook.to_heading(undefined, 2);
241
});
242
this.element.find('#to_heading3').click(function () {
243
IPython.notebook.to_heading(undefined, 3);
244
});
245
this.element.find('#to_heading4').click(function () {
246
IPython.notebook.to_heading(undefined, 4);
247
});
248
this.element.find('#to_heading5').click(function () {
249
IPython.notebook.to_heading(undefined, 5);
250
});
251
this.element.find('#to_heading6').click(function () {
252
IPython.notebook.to_heading(undefined, 6);
253
});
254
255
this.element.find('#toggle_current_output').click(function () {
256
IPython.notebook.toggle_output();
257
});
258
this.element.find('#toggle_current_output_scroll').click(function () {
259
IPython.notebook.toggle_output_scroll();
260
});
261
this.element.find('#clear_current_output').click(function () {
262
IPython.notebook.clear_output();
263
});
264
265
this.element.find('#toggle_all_output').click(function () {
266
IPython.notebook.toggle_all_output();
267
});
268
this.element.find('#toggle_all_output_scroll').click(function () {
269
IPython.notebook.toggle_all_output_scroll();
270
});
271
this.element.find('#clear_all_output').click(function () {
272
IPython.notebook.clear_all_output();
273
});
274
275
// Kernel
276
this.element.find('#int_kernel').click(function () {
277
IPython.notebook.session.interrupt_kernel();
278
});
279
this.element.find('#restart_kernel').click(function () {
280
IPython.notebook.restart_kernel();
281
});
282
// Help
283
if (IPython.tour) {
284
this.element.find('#notebook_tour').click(function () {
285
IPython.tour.start();
286
});
287
} else {
288
this.element.find('#notebook_tour').addClass("disabled");
289
}
290
this.element.find('#keyboard_shortcuts').click(function () {
291
IPython.quick_help.show_keyboard_shortcuts();
292
});
293
294
this.update_restore_checkpoint(null);
295
296
$([IPython.events]).on('checkpoints_listed.Notebook', function (event, data) {
297
that.update_restore_checkpoint(IPython.notebook.checkpoints);
298
});
299
300
$([IPython.events]).on('checkpoint_created.Notebook', function (event, data) {
301
that.update_restore_checkpoint(IPython.notebook.checkpoints);
302
});
303
};
304
305
MenuBar.prototype.update_restore_checkpoint = function(checkpoints) {
306
var ul = this.element.find("#restore_checkpoint").find("ul");
307
ul.empty();
308
if (!checkpoints || checkpoints.length === 0) {
309
ul.append(
310
$("<li/>")
311
.addClass("disabled")
312
.append(
313
$("<a/>")
314
.text("No checkpoints")
315
)
316
);
317
return;
318
}
319
320
checkpoints.map(function (checkpoint) {
321
var d = new Date(checkpoint.last_modified);
322
ul.append(
323
$("<li/>").append(
324
$("<a/>")
325
.attr("href", "#")
326
.text(d.format("mmm dd HH:MM:ss"))
327
.click(function () {
328
IPython.notebook.restore_checkpoint_dialog(checkpoint);
329
})
330
)
331
);
332
});
333
};
334
335
IPython.MenuBar = MenuBar;
336
337
return IPython;
338
339
}(IPython));
340
341