menubar.js
155 lines
| 5.2 KiB
| application/javascript
|
JavascriptLexer
Thomas Kluyver
|
r19012 | // Copyright (c) IPython Development Team. | ||
// Distributed under the terms of the Modified BSD License. | ||||
define([ | ||||
'jquery', | ||||
Min RK
|
r19319 | 'base/js/namespace', | ||
Thomas Kluyver
|
r19012 | 'base/js/utils', | ||
Min RK
|
r19308 | 'base/js/dialog', | ||
Min RK
|
r19319 | 'codemirror/lib/codemirror', | ||
'codemirror/mode/meta', | ||||
Thomas Kluyver
|
r19012 | 'bootstrap', | ||
Min RK
|
r19319 | ], function($, IPython, utils, dialog, CodeMirror) { | ||
Thomas Kluyver
|
r19012 | "use strict"; | ||
var MenuBar = function (selector, options) { | ||||
Jonathan Frederic
|
r19176 | /** | ||
* 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 | ||||
*/ | ||||
Thomas Kluyver
|
r19012 | options = options || {}; | ||
this.base_url = options.base_url || utils.get_body_data("baseUrl"); | ||||
this.selector = selector; | ||||
Thomas Kluyver
|
r19013 | this.editor = options.editor; | ||
Min RK
|
r19303 | this.events = options.events; | ||
Min RK
|
r19317 | this.save_widget = options.save_widget; | ||
Thomas Kluyver
|
r19012 | |||
if (this.selector !== undefined) { | ||||
this.element = $(selector); | ||||
this.bind_events(); | ||||
} | ||||
Min RK
|
r19319 | this._load_mode_menu(); | ||
Min RK
|
r19317 | Object.seal(this); | ||
Thomas Kluyver
|
r19012 | }; | ||
MenuBar.prototype.bind_events = function () { | ||||
var that = this; | ||||
Min RK
|
r19303 | var editor = that.editor; | ||
Min RK
|
r19308 | |||
// 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'}} | ||||
}); | ||||
} | ||||
); | ||||
}); | ||||
Min RK
|
r19303 | this.element.find('#save-file').click(function () { | ||
editor.save(); | ||||
}); | ||||
Min RK
|
r19317 | this.element.find('#rename-file').click(function () { | ||
that.save_widget.rename(); | ||||
}); | ||||
Min RK
|
r19303 | |||
// Edit | ||||
this.element.find('#menu-find').click(function () { | ||||
editor.codemirror.execCommand("find"); | ||||
}); | ||||
this.element.find('#menu-replace').click(function () { | ||||
editor.codemirror.execCommand("replace"); | ||||
}); | ||||
Min RK
|
r19306 | this.element.find('#menu-keymap-default').click(function () { | ||
editor.update_codemirror_options({ | ||||
vimMode: false, | ||||
Min RK
|
r19310 | keyMap: 'default' | ||
Min RK
|
r19306 | }); | ||
}); | ||||
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' | ||||
}); | ||||
}); | ||||
Min RK
|
r19303 | |||
// 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 () { | ||||
Min RK
|
r19306 | var keyMap = editor.codemirror.getOption('keyMap') || "default"; | ||
that.element.find(".selected-keymap").removeClass("selected-keymap"); | ||||
that.element.find("#menu-keymap-" + keyMap).addClass("selected-keymap"); | ||||
Thomas Kluyver
|
r19012 | }); | ||
Min RK
|
r19319 | |||
this.events.on("mode_changed.Editor", function (evt, modeinfo) { | ||||
that.element.find("#current-mode") | ||||
.text(modeinfo.name) | ||||
.attr( | ||||
'title', | ||||
Min RK
|
r19338 | "The current language is " + modeinfo.name | ||
Min RK
|
r19319 | ); | ||
}); | ||||
}; | ||||
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', | ||||
Min RK
|
r19338 | "Set language to " + info.name | ||
Min RK
|
r19319 | ) | ||
)); | ||||
} | ||||
Thomas Kluyver
|
r19012 | }; | ||
return {'MenuBar': MenuBar}; | ||||
}); | ||||