notebook/IPython/html/static/edit/js/menubar.js

161 lines
5.4 KiB
JavaScript
Raw Normal View History

2014-11-06 08:48:23 +08:00
// Copyright (c) IPython Development Team.
// Distributed under the terms of the Modified BSD License.
define([
'jquery',
2014-12-08 05:40:42 +08:00
'base/js/namespace',
2014-11-06 08:48:23 +08:00
'base/js/utils',
2014-12-03 04:15:36 +08:00
'base/js/dialog',
2014-12-08 05:40:42 +08:00
'codemirror/lib/codemirror',
'codemirror/mode/meta',
2014-11-06 08:48:23 +08:00
'bootstrap',
2014-12-08 05:40:42 +08:00
], function($, IPython, utils, dialog, CodeMirror) {
2014-11-06 08:48:23 +08:00
"use strict";
var MenuBar = function (selector, options) {
2014-12-04 05:42:43 +08:00
/**
* Constructor
*
* A MenuBar Class to generate the menubar of IPython notebook
*
* Parameters:
* selector: string
* options: dictionary
* Dictionary of keyword arguments.
* codemirror: CodeMirror instance
* contents: ContentManager instance
* events: $(Events) instance
* base_url : string
* file_path : string
*/
2014-11-06 08:48:23 +08:00
options = options || {};
this.base_url = options.base_url || utils.get_body_data("baseUrl");
this.selector = selector;
2014-11-06 09:19:14 +08:00
this.editor = options.editor;
this.events = options.events;
2014-12-05 09:09:06 +08:00
this.save_widget = options.save_widget;
2014-11-06 08:48:23 +08:00
if (this.selector !== undefined) {
this.element = $(selector);
this.bind_events();
}
2014-12-08 05:40:42 +08:00
this._load_mode_menu();
2014-12-05 09:09:06 +08:00
Object.seal(this);
2014-11-06 08:48:23 +08:00
};
MenuBar.prototype.bind_events = function () {
var that = this;
var editor = that.editor;
2014-12-03 04:15:36 +08:00
// File
this.element.find('#new-file').click(function () {
var w = window.open();
// Create a new file in the current directory
var parent = utils.url_path_split(editor.file_path)[0];
editor.contents.new_untitled(parent, {type: "file"}).then(
function (data) {
w.location = utils.url_join_encode(
that.base_url, 'edit', data.path
);
},
function(error) {
w.close();
dialog.modal({
title : 'Creating New File Failed',
body : "The error was: " + error.message,
buttons : {'OK' : {'class' : 'btn-primary'}}
});
}
);
});
this.element.find('#save-file').click(function () {
editor.save();
});
2014-12-05 09:09:06 +08:00
this.element.find('#rename-file').click(function () {
that.save_widget.rename();
});
2015-02-03 08:35:24 +08:00
this.element.find('#download-file').click(function () {
window.open(utils.url_join_encode(
that.base_url, 'files', that.editor.file_path
) + '?download=1');
});
// Edit
this.element.find('#menu-find').click(function () {
editor.codemirror.execCommand("find");
});
this.element.find('#menu-replace').click(function () {
editor.codemirror.execCommand("replace");
});
this.element.find('#menu-keymap-default').click(function () {
editor.update_codemirror_options({
vimMode: false,
2014-12-03 04:55:49 +08:00
keyMap: 'default'
});
});
this.element.find('#menu-keymap-sublime').click(function () {
editor.update_codemirror_options({
vimMode: false,
keyMap: 'sublime'
});
});
this.element.find('#menu-keymap-emacs').click(function () {
editor.update_codemirror_options({
vimMode: false,
keyMap: 'emacs'
});
});
this.element.find('#menu-keymap-vim').click(function () {
editor.update_codemirror_options({
vimMode: true,
keyMap: 'vim'
});
});
// View
this.element.find('#menu-line-numbers').click(function () {
var current = editor.codemirror.getOption('lineNumbers');
var value = Boolean(1-current);
editor.update_codemirror_options({lineNumbers: value});
});
this.events.on("config_changed.Editor", function () {
var keyMap = editor.codemirror.getOption('keyMap') || "default";
that.element.find(".selected-keymap").removeClass("selected-keymap");
that.element.find("#menu-keymap-" + keyMap).addClass("selected-keymap");
2014-11-06 08:48:23 +08:00
});
2014-12-08 05:40:42 +08:00
this.events.on("mode_changed.Editor", function (evt, modeinfo) {
that.element.find("#current-mode")
.text(modeinfo.name)
.attr(
'title',
"The current language is " + modeinfo.name
2014-12-08 05:40:42 +08:00
);
});
};
MenuBar.prototype._load_mode_menu = function () {
var list = this.element.find("#mode-menu");
var editor = this.editor;
function make_set_mode(info) {
return function () {
editor.set_codemirror_mode(info);
};
}
for (var i = 0; i < CodeMirror.modeInfo.length; i++) {
var info = CodeMirror.modeInfo[i];
list.append($("<li>").append(
$("<a>").attr("href", "#")
.text(info.name)
.click(make_set_mode(info))
.attr('title',
"Set language to " + info.name
2014-12-08 05:40:42 +08:00
)
));
}
2014-11-06 08:48:23 +08:00
};
return {'MenuBar': MenuBar};
});