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/utils',
8
'base/js/dialog',
9
'codemirror/lib/codemirror',
10
'codemirror/mode/meta',
11
'bootstrap',
12
], function($, IPython, utils, dialog, CodeMirror) {
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
* codemirror: CodeMirror instance
26
* contents: ContentManager instance
27
* events: $(Events) instance
28
* base_url : string
29
* file_path : string
30
*/
31
options = options || {};
32
this.base_url = options.base_url || utils.get_body_data("baseUrl");
33
this.selector = selector;
34
this.editor = options.editor;
35
this.events = options.events;
36
this.save_widget = options.save_widget;
37
38
if (this.selector !== undefined) {
39
this.element = $(selector);
40
this.bind_events();
41
}
42
this._load_mode_menu();
43
Object.seal(this);
44
};
45
46
MenuBar.prototype.bind_events = function () {
47
var that = this;
48
var editor = that.editor;
49
50
// File
51
this.element.find('#new-file').click(function () {
52
var w = window.open(undefined, IPython._target);
53
// Create a new file in the current directory
54
var parent = utils.url_path_split(editor.file_path)[0];
55
editor.contents.new_untitled(parent, {type: "file"}).then(
56
function (data) {
57
w.location = utils.url_join_encode(
58
that.base_url, 'edit', data.path
59
);
60
},
61
function(error) {
62
w.close();
63
dialog.modal({
64
title : 'Creating New File Failed',
65
body : "The error was: " + error.message,
66
buttons : {'OK' : {'class' : 'btn-primary'}}
67
});
68
}
69
);
70
});
71
this.element.find('#save-file').click(function () {
72
editor.save();
73
});
74
this.element.find('#rename-file').click(function () {
75
that.save_widget.rename();
76
});
77
this.element.find('#download-file').click(function () {
78
window.open(utils.url_join_encode(
79
that.base_url, 'files', that.editor.file_path
80
) + '?download=1');
81
});
82
83
// Edit
84
this.element.find('#menu-find').click(function () {
85
editor.codemirror.execCommand("find");
86
});
87
this.element.find('#menu-replace').click(function () {
88
editor.codemirror.execCommand("replace");
89
});
90
this.element.find('#menu-keymap-default').click(function () {
91
editor.update_codemirror_options({
92
vimMode: false,
93
keyMap: 'default'
94
});
95
});
96
this.element.find('#menu-keymap-sublime').click(function () {
97
editor.update_codemirror_options({
98
vimMode: false,
99
keyMap: 'sublime'
100
});
101
});
102
this.element.find('#menu-keymap-emacs').click(function () {
103
editor.update_codemirror_options({
104
vimMode: false,
105
keyMap: 'emacs'
106
});
107
});
108
this.element.find('#menu-keymap-vim').click(function () {
109
editor.update_codemirror_options({
110
vimMode: true,
111
keyMap: 'vim'
112
});
113
});
114
115
// View
116
this.element.find('#menu-line-numbers').click(function () {
117
var current = editor.codemirror.getOption('lineNumbers');
118
var value = Boolean(1-current);
119
editor.update_codemirror_options({lineNumbers: value});
120
});
121
122
this.events.on("config_changed.Editor", function () {
123
var keyMap = editor.codemirror.getOption('keyMap') || "default";
124
that.element.find(".selected-keymap").removeClass("selected-keymap");
125
that.element.find("#menu-keymap-" + keyMap).addClass("selected-keymap");
126
});
127
128
this.events.on("mode_changed.Editor", function (evt, modeinfo) {
129
that.element.find("#current-mode")
130
.text(modeinfo.name)
131
.attr(
132
'title',
133
"The current language is " + modeinfo.name
134
);
135
});
136
};
137
138
MenuBar.prototype._load_mode_menu = function () {
139
var list = this.element.find("#mode-menu");
140
var editor = this.editor;
141
function make_set_mode(info) {
142
return function () {
143
editor.set_codemirror_mode(info);
144
};
145
}
146
for (var i = 0; i < CodeMirror.modeInfo.length; i++) {
147
var info = CodeMirror.modeInfo[i];
148
list.append($("<li>").append(
149
$("<a>").attr("href", "#")
150
.text(info.name)
151
.click(make_set_mode(info))
152
.attr('title',
153
"Set language to " + info.name
154
)
155
));
156
}
157
};
158
159
return {'MenuBar': MenuBar};
160
});
161
162