##// END OF EJS Templates
Toggle line numbers in menu...
Min RK -
Show More
@@ -1,159 +1,155
1 1 // Copyright (c) IPython Development Team.
2 2 // Distributed under the terms of the Modified BSD License.
3 3
4 4 define([
5 5 'jquery',
6 6 'base/js/namespace',
7 7 'base/js/utils',
8 8 'base/js/dialog',
9 9 'codemirror/lib/codemirror',
10 10 'codemirror/mode/meta',
11 11 'bootstrap',
12 12 ], function($, IPython, utils, dialog, CodeMirror) {
13 13 "use strict";
14 14
15 15 var MenuBar = function (selector, options) {
16 16 /**
17 17 * Constructor
18 18 *
19 19 * A MenuBar Class to generate the menubar of IPython notebook
20 20 *
21 21 * Parameters:
22 22 * selector: string
23 23 * options: dictionary
24 24 * Dictionary of keyword arguments.
25 25 * codemirror: CodeMirror instance
26 26 * contents: ContentManager instance
27 27 * events: $(Events) instance
28 28 * base_url : string
29 29 * file_path : string
30 30 */
31 31 options = options || {};
32 32 this.base_url = options.base_url || utils.get_body_data("baseUrl");
33 33 this.selector = selector;
34 34 this.editor = options.editor;
35 35 this.events = options.events;
36 36 this.save_widget = options.save_widget;
37 37
38 38 if (this.selector !== undefined) {
39 39 this.element = $(selector);
40 40 this.bind_events();
41 41 }
42 42 this._load_mode_menu();
43 43 Object.seal(this);
44 44 };
45 45
46 46 MenuBar.prototype.bind_events = function () {
47 47 var that = this;
48 48 var editor = that.editor;
49 49
50 50 // File
51 51 this.element.find('#new-file').click(function () {
52 52 var w = window.open();
53 53 // Create a new file in the current directory
54 54 var parent = utils.url_path_split(editor.file_path)[0];
55 55 editor.contents.new_untitled(parent, {type: "file"}).then(
56 56 function (data) {
57 57 w.location = utils.url_join_encode(
58 58 that.base_url, 'edit', data.path
59 59 );
60 60 },
61 61 function(error) {
62 62 w.close();
63 63 dialog.modal({
64 64 title : 'Creating New File Failed',
65 65 body : "The error was: " + error.message,
66 66 buttons : {'OK' : {'class' : 'btn-primary'}}
67 67 });
68 68 }
69 69 );
70 70 });
71 71 this.element.find('#save-file').click(function () {
72 72 editor.save();
73 73 });
74 74 this.element.find('#rename-file').click(function () {
75 75 that.save_widget.rename();
76 76 });
77 77
78 78 // Edit
79 79 this.element.find('#menu-find').click(function () {
80 80 editor.codemirror.execCommand("find");
81 81 });
82 82 this.element.find('#menu-replace').click(function () {
83 83 editor.codemirror.execCommand("replace");
84 84 });
85 85 this.element.find('#menu-keymap-default').click(function () {
86 86 editor.update_codemirror_options({
87 87 vimMode: false,
88 88 keyMap: 'default'
89 89 });
90 90 });
91 91 this.element.find('#menu-keymap-sublime').click(function () {
92 92 editor.update_codemirror_options({
93 93 vimMode: false,
94 94 keyMap: 'sublime'
95 95 });
96 96 });
97 97 this.element.find('#menu-keymap-emacs').click(function () {
98 98 editor.update_codemirror_options({
99 99 vimMode: false,
100 100 keyMap: 'emacs'
101 101 });
102 102 });
103 103 this.element.find('#menu-keymap-vim').click(function () {
104 104 editor.update_codemirror_options({
105 105 vimMode: true,
106 106 keyMap: 'vim'
107 107 });
108 108 });
109 109
110 110 // View
111 111 this.element.find('#menu-line-numbers').click(function () {
112 112 var current = editor.codemirror.getOption('lineNumbers');
113 113 var value = Boolean(1-current);
114 114 editor.update_codemirror_options({lineNumbers: value});
115 115 });
116 116
117 117 this.events.on("config_changed.Editor", function () {
118 var lineNumbers = editor.codemirror.getOption('lineNumbers');
119 var text = lineNumbers ? "Hide" : "Show";
120 text = text + " Line Numbers";
121 that.element.find('#menu-line-numbers').find("a").text(text);
122 118 var keyMap = editor.codemirror.getOption('keyMap') || "default";
123 119 that.element.find(".selected-keymap").removeClass("selected-keymap");
124 120 that.element.find("#menu-keymap-" + keyMap).addClass("selected-keymap");
125 121 });
126 122
127 123 this.events.on("mode_changed.Editor", function (evt, modeinfo) {
128 124 that.element.find("#current-mode")
129 125 .text(modeinfo.name)
130 126 .attr(
131 127 'title',
132 128 "The current highlighting mode is " + modeinfo.name
133 129 );
134 130 });
135 131 };
136 132
137 133 MenuBar.prototype._load_mode_menu = function () {
138 134 var list = this.element.find("#mode-menu");
139 135 var editor = this.editor;
140 136 function make_set_mode(info) {
141 137 return function () {
142 138 editor.set_codemirror_mode(info);
143 139 };
144 140 }
145 141 for (var i = 0; i < CodeMirror.modeInfo.length; i++) {
146 142 var info = CodeMirror.modeInfo[i];
147 143 list.append($("<li>").append(
148 144 $("<a>").attr("href", "#")
149 145 .text(info.name)
150 146 .click(make_set_mode(info))
151 147 .attr('title',
152 148 "Set highlighting mode to " + info.name
153 149 )
154 150 ));
155 151 }
156 152 };
157 153
158 154 return {'MenuBar': MenuBar};
159 155 });
@@ -1,92 +1,92
1 1 {% extends "page.html" %}
2 2
3 3 {% block title %}{{page_title}}{% endblock %}
4 4
5 5 {% block stylesheet %}
6 6 <link rel="stylesheet" href="{{ static_url('components/codemirror/lib/codemirror.css') }}">
7 7 <link rel="stylesheet" href="{{ static_url('components/codemirror/addon/dialog/dialog.css') }}">
8 8 {{super()}}
9 9 {% endblock %}
10 10
11 11 {% block params %}
12 12
13 13 data-base-url="{{base_url}}"
14 14 data-file-path="{{file_path}}"
15 15
16 16 {% endblock %}
17 17
18 18 {% block headercontainer %}
19 19
20 20 <span id="save_widget" class="pull-left save_widget">
21 21 <span class="filename"></span>
22 22 <span class="last_modified"></span>
23 23 </span>
24 24
25 25 {% endblock %}
26 26
27 27 {% block header %}
28 28
29 29 <div id="menubar-container" class="container">
30 30 <div id="menubar">
31 31 <div id="menus" class="navbar navbar-default" role="navigation">
32 32 <div class="container-fluid">
33 33 <button type="button" class="btn btn-default navbar-toggle" data-toggle="collapse" data-target=".navbar-collapse">
34 34 <i class="fa fa-bars"></i>
35 35 <span class="navbar-text">Menu</span>
36 36 </button>
37 37 <ul class="nav navbar-nav navbar-right">
38 38 <li id="notification_area"></li>
39 39 </ul>
40 40 <div class="navbar-collapse collapse">
41 41 <ul class="nav navbar-nav">
42 42 <li class="dropdown"><a href="#" class="dropdown-toggle" data-toggle="dropdown">File</a>
43 43 <ul id="file-menu" class="dropdown-menu">
44 44 <li id="new-file"><a href="#">New</a></li>
45 45 <li id="save-file"><a href="#">Save</a></li>
46 46 <li id="rename-file"><a href="#">Rename</a></li>
47 47 </ul>
48 48 </li>
49 49 <li class="dropdown"><a href="#" class="dropdown-toggle" data-toggle="dropdown">Edit</a>
50 50 <ul id="edit-menu" class="dropdown-menu">
51 51 <li id="menu-find"><a href="#">Find</a></li>
52 52 <li id="menu-replace"><a href="#">Find &amp; Replace</a></li>
53 53 <li class="divider"></li>
54 54 <li class="dropdown-header">Key Map</li>
55 55 <li id="menu-keymap-default"><a href="#">Default<i class="fa"></i></a></li>
56 56 <li id="menu-keymap-sublime"><a href="#">Sublime Text<i class="fa"></i></a></li>
57 57 <li id="menu-keymap-vim"><a href="#">Vim<i class="fa"></i></a></li>
58 58 <li id="menu-keymap-emacs"><a href="#">emacs<i class="fa"></i></a></li>
59 59 </ul>
60 60 </li>
61 61 <li class="dropdown"><a href="#" class="dropdown-toggle" data-toggle="dropdown">View</a>
62 62 <ul id="view-menu" class="dropdown-menu">
63 <li id="menu-line-numbers"><a href="#">Hide Line Numbers</a></li>
63 <li id="menu-line-numbers"><a href="#">Toggle Line Numbers</a></li>
64 64 </ul>
65 65 </li>
66 66 <li class="dropdown"><a href="#" class="dropdown-toggle" data-toggle="dropdown">Mode</a>
67 67 <ul id="mode-menu" class="dropdown-menu">
68 68 </ul>
69 69 </li>
70 70 </ul>
71 71 <p id="current-mode" class="navbar-text navbar-right">current mode</p>
72 72 </div>
73 73 </div>
74 74 </div>
75 75 </div>
76 76 </div>
77 77
78 78 {% endblock %}
79 79
80 80 {% block site %}
81 81
82 82
83 83 <div id="texteditor-container" class="container"></div>
84 84
85 85 {% endblock %}
86 86
87 87 {% block script %}
88 88
89 89 {{super()}}
90 90
91 91 <script src="{{ static_url("edit/js/main.js") }}" type="text/javascript" charset="utf-8"></script>
92 92 {% endblock %}
General Comments 0
You need to be logged in to leave comments. Login now