##// END OF EJS Templates
Merge pull request #4410 from ivanov/close-new-tabs-chrome...
Paul Ivanov -
r13457:d5d3bf51 merge
parent child Browse files
Show More
@@ -1,305 +1,309
1 //----------------------------------------------------------------------------
1 //----------------------------------------------------------------------------
2 // Copyright (C) 2008-2011 The IPython Development Team
2 // Copyright (C) 2008-2011 The IPython Development Team
3 //
3 //
4 // Distributed under the terms of the BSD License. The full license is in
4 // Distributed under the terms of the BSD License. The full license is in
5 // the file COPYING, distributed as part of this software.
5 // the file COPYING, distributed as part of this software.
6 //----------------------------------------------------------------------------
6 //----------------------------------------------------------------------------
7
7
8 //============================================================================
8 //============================================================================
9 // MenuBar
9 // MenuBar
10 //============================================================================
10 //============================================================================
11
11
12 /**
12 /**
13 * @module IPython
13 * @module IPython
14 * @namespace IPython
14 * @namespace IPython
15 * @submodule MenuBar
15 * @submodule MenuBar
16 */
16 */
17
17
18
18
19 var IPython = (function (IPython) {
19 var IPython = (function (IPython) {
20 "use strict";
20 "use strict";
21
21
22 var utils = IPython.utils;
22 var utils = IPython.utils;
23
23
24 /**
24 /**
25 * A MenuBar Class to generate the menubar of IPython notebook
25 * A MenuBar Class to generate the menubar of IPython notebook
26 * @Class MenuBar
26 * @Class MenuBar
27 *
27 *
28 * @constructor
28 * @constructor
29 *
29 *
30 *
30 *
31 * @param selector {string} selector for the menubar element in DOM
31 * @param selector {string} selector for the menubar element in DOM
32 * @param {object} [options]
32 * @param {object} [options]
33 * @param [options.baseProjectUrl] {String} String to use for the
33 * @param [options.baseProjectUrl] {String} String to use for the
34 * Base Project url, default would be to inspect
34 * Base Project url, default would be to inspect
35 * $('body').data('baseProjectUrl');
35 * $('body').data('baseProjectUrl');
36 * does not support change for now is set through this option
36 * does not support change for now is set through this option
37 */
37 */
38 var MenuBar = function (selector, options) {
38 var MenuBar = function (selector, options) {
39 options = options || {};
39 options = options || {};
40 if (options.baseProjectUrl !== undefined) {
40 if (options.baseProjectUrl !== undefined) {
41 this._baseProjectUrl = options.baseProjectUrl;
41 this._baseProjectUrl = options.baseProjectUrl;
42 }
42 }
43 this.selector = selector;
43 this.selector = selector;
44 if (this.selector !== undefined) {
44 if (this.selector !== undefined) {
45 this.element = $(selector);
45 this.element = $(selector);
46 this.style();
46 this.style();
47 this.bind_events();
47 this.bind_events();
48 }
48 }
49 };
49 };
50
50
51 MenuBar.prototype.baseProjectUrl = function(){
51 MenuBar.prototype.baseProjectUrl = function(){
52 return this._baseProjectUrl || $('body').data('baseProjectUrl');
52 return this._baseProjectUrl || $('body').data('baseProjectUrl');
53 };
53 };
54
54
55 MenuBar.prototype.notebookPath = function() {
55 MenuBar.prototype.notebookPath = function() {
56 var path = $('body').data('notebookPath');
56 var path = $('body').data('notebookPath');
57 path = decodeURIComponent(path);
57 path = decodeURIComponent(path);
58 return path;
58 return path;
59 };
59 };
60
60
61 MenuBar.prototype.style = function () {
61 MenuBar.prototype.style = function () {
62 this.element.addClass('border-box-sizing');
62 this.element.addClass('border-box-sizing');
63 this.element.find("li").click(function (event, ui) {
63 this.element.find("li").click(function (event, ui) {
64 // The selected cell loses focus when the menu is entered, so we
64 // The selected cell loses focus when the menu is entered, so we
65 // re-select it upon selection.
65 // re-select it upon selection.
66 var i = IPython.notebook.get_selected_index();
66 var i = IPython.notebook.get_selected_index();
67 IPython.notebook.select(i);
67 IPython.notebook.select(i);
68 }
68 }
69 );
69 );
70 };
70 };
71
71
72
72
73 MenuBar.prototype.bind_events = function () {
73 MenuBar.prototype.bind_events = function () {
74 // File
74 // File
75 var that = this;
75 var that = this;
76 this.element.find('#new_notebook').click(function () {
76 this.element.find('#new_notebook').click(function () {
77 IPython.notebook.new_notebook();
77 IPython.notebook.new_notebook();
78 });
78 });
79 this.element.find('#open_notebook').click(function () {
79 this.element.find('#open_notebook').click(function () {
80 window.open(utils.url_path_join(
80 window.open(utils.url_path_join(
81 that.baseProjectUrl(),
81 that.baseProjectUrl(),
82 'tree',
82 'tree',
83 that.notebookPath()
83 that.notebookPath()
84 ));
84 ));
85 });
85 });
86 this.element.find('#copy_notebook').click(function () {
86 this.element.find('#copy_notebook').click(function () {
87 IPython.notebook.copy_notebook();
87 IPython.notebook.copy_notebook();
88 return false;
88 return false;
89 });
89 });
90 this.element.find('#download_ipynb').click(function () {
90 this.element.find('#download_ipynb').click(function () {
91 var notebook_name = IPython.notebook.get_notebook_name();
91 var notebook_name = IPython.notebook.get_notebook_name();
92 if (IPython.notebook.dirty) {
92 if (IPython.notebook.dirty) {
93 IPython.notebook.save_notebook({async : false});
93 IPython.notebook.save_notebook({async : false});
94 }
94 }
95
95
96 var url = utils.url_path_join(
96 var url = utils.url_path_join(
97 that.baseProjectUrl(),
97 that.baseProjectUrl(),
98 'files',
98 'files',
99 that.notebookPath(),
99 that.notebookPath(),
100 notebook_name + '.ipynb'
100 notebook_name + '.ipynb'
101 );
101 );
102 window.location.assign(url);
102 window.location.assign(url);
103 });
103 });
104
104
105 /* FIXME: download-as-py doesn't work right now
105 /* FIXME: download-as-py doesn't work right now
106 * We will need nbconvert hooked up to get this back
106 * We will need nbconvert hooked up to get this back
107
107
108 this.element.find('#download_py').click(function () {
108 this.element.find('#download_py').click(function () {
109 var notebook_name = IPython.notebook.get_notebook_name();
109 var notebook_name = IPython.notebook.get_notebook_name();
110 if (IPython.notebook.dirty) {
110 if (IPython.notebook.dirty) {
111 IPython.notebook.save_notebook({async : false});
111 IPython.notebook.save_notebook({async : false});
112 }
112 }
113 var url = utils.url_path_join(
113 var url = utils.url_path_join(
114 that.baseProjectUrl(),
114 that.baseProjectUrl(),
115 'api/notebooks',
115 'api/notebooks',
116 that.notebookPath(),
116 that.notebookPath(),
117 notebook_name + '.ipynb?format=py&download=True'
117 notebook_name + '.ipynb?format=py&download=True'
118 );
118 );
119 window.location.assign(url);
119 window.location.assign(url);
120 });
120 });
121
121
122 */
122 */
123
123
124 this.element.find('#rename_notebook').click(function () {
124 this.element.find('#rename_notebook').click(function () {
125 IPython.save_widget.rename_notebook();
125 IPython.save_widget.rename_notebook();
126 });
126 });
127 this.element.find('#save_checkpoint').click(function () {
127 this.element.find('#save_checkpoint').click(function () {
128 IPython.notebook.save_checkpoint();
128 IPython.notebook.save_checkpoint();
129 });
129 });
130 this.element.find('#restore_checkpoint').click(function () {
130 this.element.find('#restore_checkpoint').click(function () {
131 });
131 });
132 this.element.find('#kill_and_exit').click(function () {
132 this.element.find('#kill_and_exit').click(function () {
133 IPython.notebook.session.delete();
133 IPython.notebook.session.delete();
134 setTimeout(function(){window.close();}, 500);
134 setTimeout(function(){
135 // allow closing of new tabs in Chromium, impossible in FF
136 window.open('', '_self', '');
137 window.close();
138 }, 500);
135 });
139 });
136 // Edit
140 // Edit
137 this.element.find('#cut_cell').click(function () {
141 this.element.find('#cut_cell').click(function () {
138 IPython.notebook.cut_cell();
142 IPython.notebook.cut_cell();
139 });
143 });
140 this.element.find('#copy_cell').click(function () {
144 this.element.find('#copy_cell').click(function () {
141 IPython.notebook.copy_cell();
145 IPython.notebook.copy_cell();
142 });
146 });
143 this.element.find('#delete_cell').click(function () {
147 this.element.find('#delete_cell').click(function () {
144 IPython.notebook.delete_cell();
148 IPython.notebook.delete_cell();
145 });
149 });
146 this.element.find('#undelete_cell').click(function () {
150 this.element.find('#undelete_cell').click(function () {
147 IPython.notebook.undelete();
151 IPython.notebook.undelete();
148 });
152 });
149 this.element.find('#split_cell').click(function () {
153 this.element.find('#split_cell').click(function () {
150 IPython.notebook.split_cell();
154 IPython.notebook.split_cell();
151 });
155 });
152 this.element.find('#merge_cell_above').click(function () {
156 this.element.find('#merge_cell_above').click(function () {
153 IPython.notebook.merge_cell_above();
157 IPython.notebook.merge_cell_above();
154 });
158 });
155 this.element.find('#merge_cell_below').click(function () {
159 this.element.find('#merge_cell_below').click(function () {
156 IPython.notebook.merge_cell_below();
160 IPython.notebook.merge_cell_below();
157 });
161 });
158 this.element.find('#move_cell_up').click(function () {
162 this.element.find('#move_cell_up').click(function () {
159 IPython.notebook.move_cell_up();
163 IPython.notebook.move_cell_up();
160 });
164 });
161 this.element.find('#move_cell_down').click(function () {
165 this.element.find('#move_cell_down').click(function () {
162 IPython.notebook.move_cell_down();
166 IPython.notebook.move_cell_down();
163 });
167 });
164 this.element.find('#select_previous').click(function () {
168 this.element.find('#select_previous').click(function () {
165 IPython.notebook.select_prev();
169 IPython.notebook.select_prev();
166 });
170 });
167 this.element.find('#select_next').click(function () {
171 this.element.find('#select_next').click(function () {
168 IPython.notebook.select_next();
172 IPython.notebook.select_next();
169 });
173 });
170 this.element.find('#edit_nb_metadata').click(function () {
174 this.element.find('#edit_nb_metadata').click(function () {
171 IPython.notebook.edit_metadata();
175 IPython.notebook.edit_metadata();
172 });
176 });
173
177
174 // View
178 // View
175 this.element.find('#toggle_header').click(function () {
179 this.element.find('#toggle_header').click(function () {
176 $('div#header').toggle();
180 $('div#header').toggle();
177 IPython.layout_manager.do_resize();
181 IPython.layout_manager.do_resize();
178 });
182 });
179 this.element.find('#toggle_toolbar').click(function () {
183 this.element.find('#toggle_toolbar').click(function () {
180 $('div#maintoolbar').toggle();
184 $('div#maintoolbar').toggle();
181 IPython.layout_manager.do_resize();
185 IPython.layout_manager.do_resize();
182 });
186 });
183 // Insert
187 // Insert
184 this.element.find('#insert_cell_above').click(function () {
188 this.element.find('#insert_cell_above').click(function () {
185 IPython.notebook.insert_cell_above('code');
189 IPython.notebook.insert_cell_above('code');
186 });
190 });
187 this.element.find('#insert_cell_below').click(function () {
191 this.element.find('#insert_cell_below').click(function () {
188 IPython.notebook.insert_cell_below('code');
192 IPython.notebook.insert_cell_below('code');
189 });
193 });
190 // Cell
194 // Cell
191 this.element.find('#run_cell').click(function () {
195 this.element.find('#run_cell').click(function () {
192 IPython.notebook.execute_selected_cell();
196 IPython.notebook.execute_selected_cell();
193 });
197 });
194 this.element.find('#run_cell_in_place').click(function () {
198 this.element.find('#run_cell_in_place').click(function () {
195 IPython.notebook.execute_selected_cell({terminal:true});
199 IPython.notebook.execute_selected_cell({terminal:true});
196 });
200 });
197 this.element.find('#run_all_cells').click(function () {
201 this.element.find('#run_all_cells').click(function () {
198 IPython.notebook.execute_all_cells();
202 IPython.notebook.execute_all_cells();
199 });
203 });
200 this.element.find('#run_all_cells_above').click(function () {
204 this.element.find('#run_all_cells_above').click(function () {
201 IPython.notebook.execute_cells_above();
205 IPython.notebook.execute_cells_above();
202 });
206 });
203 this.element.find('#run_all_cells_below').click(function () {
207 this.element.find('#run_all_cells_below').click(function () {
204 IPython.notebook.execute_cells_below();
208 IPython.notebook.execute_cells_below();
205 });
209 });
206 this.element.find('#to_code').click(function () {
210 this.element.find('#to_code').click(function () {
207 IPython.notebook.to_code();
211 IPython.notebook.to_code();
208 });
212 });
209 this.element.find('#to_markdown').click(function () {
213 this.element.find('#to_markdown').click(function () {
210 IPython.notebook.to_markdown();
214 IPython.notebook.to_markdown();
211 });
215 });
212 this.element.find('#to_raw').click(function () {
216 this.element.find('#to_raw').click(function () {
213 IPython.notebook.to_raw();
217 IPython.notebook.to_raw();
214 });
218 });
215 this.element.find('#to_heading1').click(function () {
219 this.element.find('#to_heading1').click(function () {
216 IPython.notebook.to_heading(undefined, 1);
220 IPython.notebook.to_heading(undefined, 1);
217 });
221 });
218 this.element.find('#to_heading2').click(function () {
222 this.element.find('#to_heading2').click(function () {
219 IPython.notebook.to_heading(undefined, 2);
223 IPython.notebook.to_heading(undefined, 2);
220 });
224 });
221 this.element.find('#to_heading3').click(function () {
225 this.element.find('#to_heading3').click(function () {
222 IPython.notebook.to_heading(undefined, 3);
226 IPython.notebook.to_heading(undefined, 3);
223 });
227 });
224 this.element.find('#to_heading4').click(function () {
228 this.element.find('#to_heading4').click(function () {
225 IPython.notebook.to_heading(undefined, 4);
229 IPython.notebook.to_heading(undefined, 4);
226 });
230 });
227 this.element.find('#to_heading5').click(function () {
231 this.element.find('#to_heading5').click(function () {
228 IPython.notebook.to_heading(undefined, 5);
232 IPython.notebook.to_heading(undefined, 5);
229 });
233 });
230 this.element.find('#to_heading6').click(function () {
234 this.element.find('#to_heading6').click(function () {
231 IPython.notebook.to_heading(undefined, 6);
235 IPython.notebook.to_heading(undefined, 6);
232 });
236 });
233 this.element.find('#toggle_output').click(function () {
237 this.element.find('#toggle_output').click(function () {
234 IPython.notebook.toggle_output();
238 IPython.notebook.toggle_output();
235 });
239 });
236 this.element.find('#collapse_all_output').click(function () {
240 this.element.find('#collapse_all_output').click(function () {
237 IPython.notebook.collapse_all_output();
241 IPython.notebook.collapse_all_output();
238 });
242 });
239 this.element.find('#scroll_all_output').click(function () {
243 this.element.find('#scroll_all_output').click(function () {
240 IPython.notebook.scroll_all_output();
244 IPython.notebook.scroll_all_output();
241 });
245 });
242 this.element.find('#expand_all_output').click(function () {
246 this.element.find('#expand_all_output').click(function () {
243 IPython.notebook.expand_all_output();
247 IPython.notebook.expand_all_output();
244 });
248 });
245 this.element.find('#clear_all_output').click(function () {
249 this.element.find('#clear_all_output').click(function () {
246 IPython.notebook.clear_all_output();
250 IPython.notebook.clear_all_output();
247 });
251 });
248 // Kernel
252 // Kernel
249 this.element.find('#int_kernel').click(function () {
253 this.element.find('#int_kernel').click(function () {
250 IPython.notebook.session.interrupt_kernel();
254 IPython.notebook.session.interrupt_kernel();
251 });
255 });
252 this.element.find('#restart_kernel').click(function () {
256 this.element.find('#restart_kernel').click(function () {
253 IPython.notebook.restart_kernel();
257 IPython.notebook.restart_kernel();
254 });
258 });
255 // Help
259 // Help
256 this.element.find('#keyboard_shortcuts').click(function () {
260 this.element.find('#keyboard_shortcuts').click(function () {
257 IPython.quick_help.show_keyboard_shortcuts();
261 IPython.quick_help.show_keyboard_shortcuts();
258 });
262 });
259
263
260 this.update_restore_checkpoint(null);
264 this.update_restore_checkpoint(null);
261
265
262 $([IPython.events]).on('checkpoints_listed.Notebook', function (event, data) {
266 $([IPython.events]).on('checkpoints_listed.Notebook', function (event, data) {
263 that.update_restore_checkpoint(IPython.notebook.checkpoints);
267 that.update_restore_checkpoint(IPython.notebook.checkpoints);
264 });
268 });
265
269
266 $([IPython.events]).on('checkpoint_created.Notebook', function (event, data) {
270 $([IPython.events]).on('checkpoint_created.Notebook', function (event, data) {
267 that.update_restore_checkpoint(IPython.notebook.checkpoints);
271 that.update_restore_checkpoint(IPython.notebook.checkpoints);
268 });
272 });
269 };
273 };
270
274
271 MenuBar.prototype.update_restore_checkpoint = function(checkpoints) {
275 MenuBar.prototype.update_restore_checkpoint = function(checkpoints) {
272 var ul = this.element.find("#restore_checkpoint").find("ul");
276 var ul = this.element.find("#restore_checkpoint").find("ul");
273 ul.empty();
277 ul.empty();
274 if (!checkpoints || checkpoints.length === 0) {
278 if (!checkpoints || checkpoints.length === 0) {
275 ul.append(
279 ul.append(
276 $("<li/>")
280 $("<li/>")
277 .addClass("disabled")
281 .addClass("disabled")
278 .append(
282 .append(
279 $("<a/>")
283 $("<a/>")
280 .text("No checkpoints")
284 .text("No checkpoints")
281 )
285 )
282 );
286 );
283 return;
287 return;
284 }
288 }
285
289
286 checkpoints.map(function (checkpoint) {
290 checkpoints.map(function (checkpoint) {
287 var d = new Date(checkpoint.last_modified);
291 var d = new Date(checkpoint.last_modified);
288 ul.append(
292 ul.append(
289 $("<li/>").append(
293 $("<li/>").append(
290 $("<a/>")
294 $("<a/>")
291 .attr("href", "#")
295 .attr("href", "#")
292 .text(d.format("mmm dd HH:MM:ss"))
296 .text(d.format("mmm dd HH:MM:ss"))
293 .click(function () {
297 .click(function () {
294 IPython.notebook.restore_checkpoint_dialog(checkpoint);
298 IPython.notebook.restore_checkpoint_dialog(checkpoint);
295 })
299 })
296 )
300 )
297 );
301 );
298 });
302 });
299 };
303 };
300
304
301 IPython.MenuBar = MenuBar;
305 IPython.MenuBar = MenuBar;
302
306
303 return IPython;
307 return IPython;
304
308
305 }(IPython));
309 }(IPython));
General Comments 0
You need to be logged in to leave comments. Login now