Show More
@@ -1,101 +1,121 b'' | |||||
1 | // Copyright (c) IPython Development Team. |
|
1 | // Copyright (c) IPython Development Team. | |
2 | // Distributed under the terms of the Modified BSD License. |
|
2 | // Distributed under the terms of the Modified BSD License. | |
3 |
|
3 | |||
4 | define([ |
|
4 | define([ | |
5 | 'base/js/namespace', |
|
5 | 'base/js/namespace', | |
6 | 'jquery', |
|
6 | 'jquery', | |
7 | 'base/js/utils', |
|
7 | 'base/js/utils', | |
|
8 | 'base/js/dialog', | |||
8 | 'bootstrap', |
|
9 | 'bootstrap', | |
9 | ], function(IPython, $, utils, bootstrap) { |
|
10 | ], function(IPython, $, utils, dialog, bootstrap) { | |
10 | "use strict"; |
|
11 | "use strict"; | |
11 |
|
12 | |||
12 | var MenuBar = function (selector, options) { |
|
13 | var MenuBar = function (selector, options) { | |
13 | /** |
|
14 | /** | |
14 | * Constructor |
|
15 | * Constructor | |
15 | * |
|
16 | * | |
16 | * A MenuBar Class to generate the menubar of IPython notebook |
|
17 | * A MenuBar Class to generate the menubar of IPython notebook | |
17 | * |
|
18 | * | |
18 | * Parameters: |
|
19 | * Parameters: | |
19 | * selector: string |
|
20 | * selector: string | |
20 | * options: dictionary |
|
21 | * options: dictionary | |
21 | * Dictionary of keyword arguments. |
|
22 | * Dictionary of keyword arguments. | |
22 | * codemirror: CodeMirror instance |
|
23 | * codemirror: CodeMirror instance | |
23 | * contents: ContentManager instance |
|
24 | * contents: ContentManager instance | |
24 | * events: $(Events) instance |
|
25 | * events: $(Events) instance | |
25 | * base_url : string |
|
26 | * base_url : string | |
26 | * file_path : string |
|
27 | * file_path : string | |
27 | */ |
|
28 | */ | |
28 | options = options || {}; |
|
29 | options = options || {}; | |
29 | this.base_url = options.base_url || utils.get_body_data("baseUrl"); |
|
30 | this.base_url = options.base_url || utils.get_body_data("baseUrl"); | |
30 | this.selector = selector; |
|
31 | this.selector = selector; | |
31 | this.editor = options.editor; |
|
32 | this.editor = options.editor; | |
32 | this.events = options.events; |
|
33 | this.events = options.events; | |
33 |
|
34 | |||
34 | if (this.selector !== undefined) { |
|
35 | if (this.selector !== undefined) { | |
35 | this.element = $(selector); |
|
36 | this.element = $(selector); | |
36 | this.bind_events(); |
|
37 | this.bind_events(); | |
37 | } |
|
38 | } | |
38 | }; |
|
39 | }; | |
39 |
|
40 | |||
40 | MenuBar.prototype.bind_events = function () { |
|
41 | MenuBar.prototype.bind_events = function () { | |
41 | /** |
|
|||
42 | * File |
|
|||
43 | */ |
|
|||
44 | var that = this; |
|
42 | var that = this; | |
45 | var editor = that.editor; |
|
43 | var editor = that.editor; | |
|
44 | ||||
|
45 | // File | |||
|
46 | this.element.find('#new-file').click(function () { | |||
|
47 | var w = window.open(); | |||
|
48 | // Create a new file in the current directory | |||
|
49 | var parent = utils.url_path_split(editor.file_path)[0]; | |||
|
50 | editor.contents.new_untitled(parent, {type: "file"}).then( | |||
|
51 | function (data) { | |||
|
52 | w.location = utils.url_join_encode( | |||
|
53 | that.base_url, 'edit', data.path | |||
|
54 | ); | |||
|
55 | }, | |||
|
56 | function(error) { | |||
|
57 | w.close(); | |||
|
58 | dialog.modal({ | |||
|
59 | title : 'Creating New File Failed', | |||
|
60 | body : "The error was: " + error.message, | |||
|
61 | buttons : {'OK' : {'class' : 'btn-primary'}} | |||
|
62 | }); | |||
|
63 | } | |||
|
64 | ); | |||
|
65 | }); | |||
46 | this.element.find('#save-file').click(function () { |
|
66 | this.element.find('#save-file').click(function () { | |
47 | editor.save(); |
|
67 | editor.save(); | |
48 | }); |
|
68 | }); | |
49 |
|
69 | |||
50 | // Edit |
|
70 | // Edit | |
51 | this.element.find('#menu-find').click(function () { |
|
71 | this.element.find('#menu-find').click(function () { | |
52 | editor.codemirror.execCommand("find"); |
|
72 | editor.codemirror.execCommand("find"); | |
53 | }); |
|
73 | }); | |
54 | this.element.find('#menu-replace').click(function () { |
|
74 | this.element.find('#menu-replace').click(function () { | |
55 | editor.codemirror.execCommand("replace"); |
|
75 | editor.codemirror.execCommand("replace"); | |
56 | }); |
|
76 | }); | |
57 | this.element.find('#menu-keymap-default').click(function () { |
|
77 | this.element.find('#menu-keymap-default').click(function () { | |
58 | editor.update_codemirror_options({ |
|
78 | editor.update_codemirror_options({ | |
59 | vimMode: false, |
|
79 | vimMode: false, | |
60 | keyMap: null |
|
80 | keyMap: null | |
61 | }); |
|
81 | }); | |
62 | }); |
|
82 | }); | |
63 | this.element.find('#menu-keymap-sublime').click(function () { |
|
83 | this.element.find('#menu-keymap-sublime').click(function () { | |
64 | editor.update_codemirror_options({ |
|
84 | editor.update_codemirror_options({ | |
65 | vimMode: false, |
|
85 | vimMode: false, | |
66 | keyMap: 'sublime' |
|
86 | keyMap: 'sublime' | |
67 | }); |
|
87 | }); | |
68 | }); |
|
88 | }); | |
69 | this.element.find('#menu-keymap-emacs').click(function () { |
|
89 | this.element.find('#menu-keymap-emacs').click(function () { | |
70 | editor.update_codemirror_options({ |
|
90 | editor.update_codemirror_options({ | |
71 | vimMode: false, |
|
91 | vimMode: false, | |
72 | keyMap: 'emacs' |
|
92 | keyMap: 'emacs' | |
73 | }); |
|
93 | }); | |
74 | }); |
|
94 | }); | |
75 | this.element.find('#menu-keymap-vim').click(function () { |
|
95 | this.element.find('#menu-keymap-vim').click(function () { | |
76 | editor.update_codemirror_options({ |
|
96 | editor.update_codemirror_options({ | |
77 | vimMode: true, |
|
97 | vimMode: true, | |
78 | keyMap: 'vim' |
|
98 | keyMap: 'vim' | |
79 | }); |
|
99 | }); | |
80 | }); |
|
100 | }); | |
81 |
|
101 | |||
82 | // View |
|
102 | // View | |
83 | this.element.find('#menu-line-numbers').click(function () { |
|
103 | this.element.find('#menu-line-numbers').click(function () { | |
84 | var current = editor.codemirror.getOption('lineNumbers'); |
|
104 | var current = editor.codemirror.getOption('lineNumbers'); | |
85 | var value = Boolean(1-current); |
|
105 | var value = Boolean(1-current); | |
86 | editor.update_codemirror_options({lineNumbers: value}); |
|
106 | editor.update_codemirror_options({lineNumbers: value}); | |
87 | }); |
|
107 | }); | |
88 |
|
108 | |||
89 | this.events.on("config_changed.Editor", function () { |
|
109 | this.events.on("config_changed.Editor", function () { | |
90 | var lineNumbers = editor.codemirror.getOption('lineNumbers'); |
|
110 | var lineNumbers = editor.codemirror.getOption('lineNumbers'); | |
91 | var text = lineNumbers ? "Hide" : "Show"; |
|
111 | var text = lineNumbers ? "Hide" : "Show"; | |
92 | text = text + " Line Numbers"; |
|
112 | text = text + " Line Numbers"; | |
93 | that.element.find('#menu-line-numbers').find("a").text(text); |
|
113 | that.element.find('#menu-line-numbers').find("a").text(text); | |
94 | var keyMap = editor.codemirror.getOption('keyMap') || "default"; |
|
114 | var keyMap = editor.codemirror.getOption('keyMap') || "default"; | |
95 | that.element.find(".selected-keymap").removeClass("selected-keymap"); |
|
115 | that.element.find(".selected-keymap").removeClass("selected-keymap"); | |
96 | that.element.find("#menu-keymap-" + keyMap).addClass("selected-keymap"); |
|
116 | that.element.find("#menu-keymap-" + keyMap).addClass("selected-keymap"); | |
97 | }); |
|
117 | }); | |
98 | }; |
|
118 | }; | |
99 |
|
119 | |||
100 | return {'MenuBar': MenuBar}; |
|
120 | return {'MenuBar': MenuBar}; | |
101 | }); |
|
121 | }); |
General Comments 0
You need to be logged in to leave comments.
Login now