##// END OF EJS Templates
remove superfluous event.preventDefault...
Min RK -
Show More
@@ -1,233 +1,231 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 'jquery',
5 'jquery',
6 'base/js/namespace',
6 'base/js/namespace',
7 'base/js/dialog',
7 'base/js/dialog',
8 'base/js/utils',
8 'base/js/utils',
9 ], function($, IPython, dialog, utils) {
9 ], function($, IPython, dialog, utils) {
10 "use strict";
10 "use strict";
11
11
12 var KernelSelector = function(selector, notebook) {
12 var KernelSelector = function(selector, notebook) {
13 var that = this;
13 var that = this;
14 this.selector = selector;
14 this.selector = selector;
15 this.notebook = notebook;
15 this.notebook = notebook;
16 this.notebook.set_kernelselector(this);
16 this.notebook.set_kernelselector(this);
17 this.events = notebook.events;
17 this.events = notebook.events;
18 this.current_selection = null;
18 this.current_selection = null;
19 this.kernelspecs = {};
19 this.kernelspecs = {};
20 if (this.selector !== undefined) {
20 if (this.selector !== undefined) {
21 this.element = $(selector);
21 this.element = $(selector);
22 this.request_kernelspecs();
22 this.request_kernelspecs();
23 }
23 }
24 this.bind_events();
24 this.bind_events();
25 // Make the object globally available for user convenience & inspection
25 // Make the object globally available for user convenience & inspection
26 IPython.kernelselector = this;
26 IPython.kernelselector = this;
27 this._finish_load = null;
27 this._finish_load = null;
28 this.loaded = new Promise(function(resolve, reject) {
28 this.loaded = new Promise(function(resolve, reject) {
29 that._finish_load = resolve;
29 that._finish_load = resolve;
30 });
30 });
31
31
32 Object.seal(this);
32 Object.seal(this);
33 };
33 };
34
34
35 KernelSelector.prototype.request_kernelspecs = function() {
35 KernelSelector.prototype.request_kernelspecs = function() {
36 var url = utils.url_join_encode(this.notebook.base_url, 'api/kernelspecs');
36 var url = utils.url_join_encode(this.notebook.base_url, 'api/kernelspecs');
37 utils.promising_ajax(url).then($.proxy(this._got_kernelspecs, this));
37 utils.promising_ajax(url).then($.proxy(this._got_kernelspecs, this));
38 };
38 };
39
39
40 KernelSelector.prototype._got_kernelspecs = function(data) {
40 KernelSelector.prototype._got_kernelspecs = function(data) {
41 var that = this;
41 var that = this;
42 this.kernelspecs = data.kernelspecs;
42 this.kernelspecs = data.kernelspecs;
43 var change_kernel_submenu = $("#menu-change-kernel-submenu");
43 var change_kernel_submenu = $("#menu-change-kernel-submenu");
44 var new_notebook_submenu = $("#menu-new-notebook-submenu");
44 var new_notebook_submenu = $("#menu-new-notebook-submenu");
45
45
46 var keys = Object.keys(data.kernelspecs).sort(function (a, b) {
46 var keys = Object.keys(data.kernelspecs).sort(function (a, b) {
47 // sort by display_name
47 // sort by display_name
48 var da = data.kernelspecs[a].spec.display_name;
48 var da = data.kernelspecs[a].spec.display_name;
49 var db = data.kernelspecs[b].spec.display_name;
49 var db = data.kernelspecs[b].spec.display_name;
50 if (da === db) {
50 if (da === db) {
51 return 0;
51 return 0;
52 } else if (da > db) {
52 } else if (da > db) {
53 return 1;
53 return 1;
54 } else {
54 } else {
55 return -1;
55 return -1;
56 }
56 }
57 });
57 });
58
58
59 keys.map(function (key) {
59 keys.map(function (key) {
60 // Create the Kernel > Change kernel submenu
60 // Create the Kernel > Change kernel submenu
61 var ks = data.kernelspecs[key];
61 var ks = data.kernelspecs[key];
62 change_kernel_submenu.append(
62 change_kernel_submenu.append(
63 $("<li>").attr("id", "kernel-submenu-"+ks.name).append(
63 $("<li>").attr("id", "kernel-submenu-"+ks.name).append(
64 $('<a>')
64 $('<a>')
65 .attr('href', '#')
65 .attr('href', '#')
66 .click( function () {
66 .click( function () {
67 that.set_kernel(ks.name);
67 that.set_kernel(ks.name);
68 event.preventDefault();
69 })
68 })
70 .text(ks.spec.display_name)
69 .text(ks.spec.display_name)
71 )
70 )
72 );
71 );
73 // Create the File > New Notebook submenu
72 // Create the File > New Notebook submenu
74 new_notebook_submenu.append(
73 new_notebook_submenu.append(
75 $("<li>").attr("id", "new-notebook-submenu-"+ks.name).append(
74 $("<li>").attr("id", "new-notebook-submenu-"+ks.name).append(
76 $('<a>')
75 $('<a>')
77 .attr('href', '#')
76 .attr('href', '#')
78 .click( function () {
77 .click( function () {
79 that.new_notebook(ks.name);
78 that.new_notebook(ks.name);
80 event.preventDefault();
81 })
79 })
82 .text(ks.spec.display_name)
80 .text(ks.spec.display_name)
83 )
81 )
84 );
82 );
85
83
86 });
84 });
87 // trigger loaded promise
85 // trigger loaded promise
88 this._finish_load();
86 this._finish_load();
89 };
87 };
90
88
91 KernelSelector.prototype._spec_changed = function (event, ks) {
89 KernelSelector.prototype._spec_changed = function (event, ks) {
92 /** event handler for spec_changed */
90 /** event handler for spec_changed */
93
91
94 // update selection
92 // update selection
95 this.current_selection = ks.name;
93 this.current_selection = ks.name;
96
94
97 // put the current kernel at the top of File > New Notebook
95 // put the current kernel at the top of File > New Notebook
98 var cur_kernel_entry = $("#new-notebook-submenu-" + ks.name);
96 var cur_kernel_entry = $("#new-notebook-submenu-" + ks.name);
99 var parent = cur_kernel_entry.parent();
97 var parent = cur_kernel_entry.parent();
100 // do something only if there is more than one kernel
98 // do something only if there is more than one kernel
101 if (parent.children().length > 1) {
99 if (parent.children().length > 1) {
102 // first, sort back the submenu
100 // first, sort back the submenu
103 parent.append(
101 parent.append(
104 parent.children("li[class!='divider']").sort(
102 parent.children("li[class!='divider']").sort(
105 function (a,b) {
103 function (a,b) {
106 var da = $("a",a).text();
104 var da = $("a",a).text();
107 var db = $("a",b).text();
105 var db = $("a",b).text();
108 if (da === db) {
106 if (da === db) {
109 return 0;
107 return 0;
110 } else if (da > db) {
108 } else if (da > db) {
111 return 1;
109 return 1;
112 } else {
110 } else {
113 return -1;
111 return -1;
114 }}));
112 }}));
115 // then, if there is no divider yet, add one
113 // then, if there is no divider yet, add one
116 if (!parent.children("li[class='divider']").length) {
114 if (!parent.children("li[class='divider']").length) {
117 parent.prepend($("<li>").attr("class","divider"));
115 parent.prepend($("<li>").attr("class","divider"));
118 }
116 }
119 // finally, put the current kernel at the top
117 // finally, put the current kernel at the top
120 parent.prepend(cur_kernel_entry);
118 parent.prepend(cur_kernel_entry);
121 }
119 }
122
120
123 // load logo
121 // load logo
124 var logo_img = this.element.find("img.current_kernel_logo");
122 var logo_img = this.element.find("img.current_kernel_logo");
125 $("#kernel_indicator").find('.kernel_indicator_name').text(ks.spec.display_name);
123 $("#kernel_indicator").find('.kernel_indicator_name').text(ks.spec.display_name);
126 if (ks.resources['logo-64x64']) {
124 if (ks.resources['logo-64x64']) {
127 logo_img.attr("src", ks.resources['logo-64x64']);
125 logo_img.attr("src", ks.resources['logo-64x64']);
128 logo_img.show();
126 logo_img.show();
129 } else {
127 } else {
130 logo_img.hide();
128 logo_img.hide();
131 }
129 }
132
130
133 // load kernel css
131 // load kernel css
134 var css_url = ks.resources['kernel.css'];
132 var css_url = ks.resources['kernel.css'];
135 if (css_url) {
133 if (css_url) {
136 $('#kernel-css').attr('href', css_url);
134 $('#kernel-css').attr('href', css_url);
137 } else {
135 } else {
138 $('#kernel-css').attr('href', '');
136 $('#kernel-css').attr('href', '');
139 }
137 }
140
138
141 // load kernel js
139 // load kernel js
142 if (ks.resources['kernel.js']) {
140 if (ks.resources['kernel.js']) {
143 require([ks.resources['kernel.js']],
141 require([ks.resources['kernel.js']],
144 function (kernel_mod) {
142 function (kernel_mod) {
145 if (kernel_mod && kernel_mod.onload) {
143 if (kernel_mod && kernel_mod.onload) {
146 kernel_mod.onload();
144 kernel_mod.onload();
147 } else {
145 } else {
148 console.warn("Kernel " + ks.name + " has a kernel.js file that does not contain "+
146 console.warn("Kernel " + ks.name + " has a kernel.js file that does not contain "+
149 "any asynchronous module definition. This is undefined behavior "+
147 "any asynchronous module definition. This is undefined behavior "+
150 "and not recommended.");
148 "and not recommended.");
151 }
149 }
152 }, function (err) {
150 }, function (err) {
153 console.warn("Failed to load kernel.js from ", ks.resources['kernel.js'], err);
151 console.warn("Failed to load kernel.js from ", ks.resources['kernel.js'], err);
154 }
152 }
155 );
153 );
156 }
154 }
157 };
155 };
158
156
159 KernelSelector.prototype.set_kernel = function (kernel_name) {
157 KernelSelector.prototype.set_kernel = function (kernel_name) {
160 /** set the kernel by name, ensuring kernelspecs have been loaded, first */
158 /** set the kernel by name, ensuring kernelspecs have been loaded, first */
161 var that = this;
159 var that = this;
162 return this.loaded.then(function () {
160 return this.loaded.then(function () {
163 that._set_kernel(kernel_name);
161 that._set_kernel(kernel_name);
164 });
162 });
165 };
163 };
166
164
167 KernelSelector.prototype._set_kernel = function (kernel_name) {
165 KernelSelector.prototype._set_kernel = function (kernel_name) {
168 /** Actually set the kernel (kernelspecs have been loaded) */
166 /** Actually set the kernel (kernelspecs have been loaded) */
169 if (kernel_name === this.current_selection) {
167 if (kernel_name === this.current_selection) {
170 // only trigger event if value changed
168 // only trigger event if value changed
171 return;
169 return;
172 }
170 }
173 var ks = this.kernelspecs[kernel_name];
171 var ks = this.kernelspecs[kernel_name];
174 if (this.notebook._session_starting) {
172 if (this.notebook._session_starting) {
175 console.error("Cannot change kernel while waiting for pending session start.");
173 console.error("Cannot change kernel while waiting for pending session start.");
176 return;
174 return;
177 }
175 }
178 this.current_selection = kernel_name;
176 this.current_selection = kernel_name;
179 this.events.trigger('spec_changed.Kernel', ks);
177 this.events.trigger('spec_changed.Kernel', ks);
180 };
178 };
181
179
182 KernelSelector.prototype.new_notebook = function (kernel_name) {
180 KernelSelector.prototype.new_notebook = function (kernel_name) {
183
181
184 var w = window.open();
182 var w = window.open();
185 // Create a new notebook in the same path as the current
183 // Create a new notebook in the same path as the current
186 // notebook's path.
184 // notebook's path.
187 var that = this;
185 var that = this;
188 var parent = utils.url_path_split(that.notebook.notebook_path)[0];
186 var parent = utils.url_path_split(that.notebook.notebook_path)[0];
189 that.notebook.contents.new_untitled(parent, {type: "notebook"}).then(
187 that.notebook.contents.new_untitled(parent, {type: "notebook"}).then(
190 function (data) {
188 function (data) {
191 var url = utils.url_join_encode(
189 var url = utils.url_join_encode(
192 that.notebook.base_url, 'notebooks', data.path
190 that.notebook.base_url, 'notebooks', data.path
193 );
191 );
194 url += "?kernel_name=" + kernel_name;
192 url += "?kernel_name=" + kernel_name;
195 w.location = url;
193 w.location = url;
196 },
194 },
197 function(error) {
195 function(error) {
198 w.close();
196 w.close();
199 dialog.modal({
197 dialog.modal({
200 title : 'Creating Notebook Failed',
198 title : 'Creating Notebook Failed',
201 body : "The error was: " + error.message,
199 body : "The error was: " + error.message,
202 buttons : {'OK' : {'class' : 'btn-primary'}}
200 buttons : {'OK' : {'class' : 'btn-primary'}}
203 });
201 });
204 }
202 }
205 );
203 );
206 };
204 };
207
205
208 KernelSelector.prototype.lock_switch = function() {
206 KernelSelector.prototype.lock_switch = function() {
209 // should set a flag and display warning+reload if user want to
207 // should set a flag and display warning+reload if user want to
210 // re-change kernel. As UI discussion never finish
208 // re-change kernel. As UI discussion never finish
211 // making that a separate PR.
209 // making that a separate PR.
212 console.warn('switching kernel is not guaranteed to work !');
210 console.warn('switching kernel is not guaranteed to work !');
213 };
211 };
214
212
215 KernelSelector.prototype.bind_events = function() {
213 KernelSelector.prototype.bind_events = function() {
216 var that = this;
214 var that = this;
217 this.events.on('spec_changed.Kernel', $.proxy(this._spec_changed, this));
215 this.events.on('spec_changed.Kernel', $.proxy(this._spec_changed, this));
218
216
219 this.events.on('kernel_created.Session', function (event, data) {
217 this.events.on('kernel_created.Session', function (event, data) {
220 that.set_kernel(data.kernel.name);
218 that.set_kernel(data.kernel.name);
221 });
219 });
222
220
223 var logo_img = this.element.find("img.current_kernel_logo");
221 var logo_img = this.element.find("img.current_kernel_logo");
224 logo_img.on("load", function() {
222 logo_img.on("load", function() {
225 logo_img.show();
223 logo_img.show();
226 });
224 });
227 logo_img.on("error", function() {
225 logo_img.on("error", function() {
228 logo_img.hide();
226 logo_img.hide();
229 });
227 });
230 };
228 };
231
229
232 return {'KernelSelector': KernelSelector};
230 return {'KernelSelector': KernelSelector};
233 });
231 });
@@ -1,467 +1,413 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 'jquery',
5 'jquery',
6 'base/js/namespace',
6 'base/js/namespace',
7 'base/js/dialog',
7 'base/js/dialog',
8 'base/js/utils',
8 'base/js/utils',
9 'notebook/js/tour',
9 'notebook/js/tour',
10 'bootstrap',
10 'bootstrap',
11 'moment',
11 'moment',
12 ], function($, IPython, dialog, utils, tour, bootstrap, moment) {
12 ], function($, IPython, dialog, utils, tour, bootstrap, moment) {
13 "use strict";
13 "use strict";
14
14
15 var MenuBar = function (selector, options) {
15 var MenuBar = function (selector, options) {
16 /**
16 /**
17 * Constructor
17 * Constructor
18 *
18 *
19 * A MenuBar Class to generate the menubar of IPython notebook
19 * A MenuBar Class to generate the menubar of IPython notebook
20 *
20 *
21 * Parameters:
21 * Parameters:
22 * selector: string
22 * selector: string
23 * options: dictionary
23 * options: dictionary
24 * Dictionary of keyword arguments.
24 * Dictionary of keyword arguments.
25 * notebook: Notebook instance
25 * notebook: Notebook instance
26 * contents: ContentManager instance
26 * contents: ContentManager instance
27 * events: $(Events) instance
27 * events: $(Events) instance
28 * save_widget: SaveWidget instance
28 * save_widget: SaveWidget instance
29 * quick_help: QuickHelp instance
29 * quick_help: QuickHelp instance
30 * base_url : string
30 * base_url : string
31 * notebook_path : string
31 * notebook_path : string
32 * notebook_name : string
32 * notebook_name : string
33 */
33 */
34 options = options || {};
34 options = options || {};
35 this.base_url = options.base_url || utils.get_body_data("baseUrl");
35 this.base_url = options.base_url || utils.get_body_data("baseUrl");
36 this.selector = selector;
36 this.selector = selector;
37 this.notebook = options.notebook;
37 this.notebook = options.notebook;
38 this.contents = options.contents;
38 this.contents = options.contents;
39 this.events = options.events;
39 this.events = options.events;
40 this.save_widget = options.save_widget;
40 this.save_widget = options.save_widget;
41 this.quick_help = options.quick_help;
41 this.quick_help = options.quick_help;
42
42
43 try {
43 try {
44 this.tour = new tour.Tour(this.notebook, this.events);
44 this.tour = new tour.Tour(this.notebook, this.events);
45 } catch (e) {
45 } catch (e) {
46 this.tour = undefined;
46 this.tour = undefined;
47 console.log("Failed to instantiate Notebook Tour", e);
47 console.log("Failed to instantiate Notebook Tour", e);
48 }
48 }
49
49
50 if (this.selector !== undefined) {
50 if (this.selector !== undefined) {
51 this.element = $(selector);
51 this.element = $(selector);
52 this.style();
52 this.style();
53 this.bind_events();
53 this.bind_events();
54 }
54 }
55 };
55 };
56
56
57 // TODO: This has definitively nothing to do with style ...
57 // TODO: This has definitively nothing to do with style ...
58 MenuBar.prototype.style = function () {
58 MenuBar.prototype.style = function () {
59 var that = this;
59 var that = this;
60 this.element.find("li").click(function (event, ui) {
60 this.element.find("li").click(function (event, ui) {
61 // The selected cell loses focus when the menu is entered, so we
61 // The selected cell loses focus when the menu is entered, so we
62 // re-select it upon selection.
62 // re-select it upon selection.
63 var i = that.notebook.get_selected_index();
63 var i = that.notebook.get_selected_index();
64 that.notebook.select(i);
64 that.notebook.select(i);
65 event.preventDefault();
65 event.preventDefault();
66 }
66 }
67 );
67 );
68 };
68 };
69
69
70 MenuBar.prototype._nbconvert = function (format, download) {
70 MenuBar.prototype._nbconvert = function (format, download) {
71 download = download || false;
71 download = download || false;
72 var notebook_path = this.notebook.notebook_path;
72 var notebook_path = this.notebook.notebook_path;
73 var url = utils.url_join_encode(
73 var url = utils.url_join_encode(
74 this.base_url,
74 this.base_url,
75 'nbconvert',
75 'nbconvert',
76 format,
76 format,
77 notebook_path
77 notebook_path
78 ) + "?download=" + download.toString();
78 ) + "?download=" + download.toString();
79
79
80 var w = window.open();
80 var w = window.open();
81 if (this.notebook.dirty) {
81 if (this.notebook.dirty) {
82 this.notebook.save_notebook().then(function() {
82 this.notebook.save_notebook().then(function() {
83 w.location = url;
83 w.location = url;
84 });
84 });
85 } else {
85 } else {
86 w.location = url;
86 w.location = url;
87 }
87 }
88 };
88 };
89
89
90 MenuBar.prototype._size_header = function() {
90 MenuBar.prototype._size_header = function() {
91 /**
91 /**
92 * Update header spacer size.
92 * Update header spacer size.
93 */
93 */
94 this.events.trigger('resize-header.Page');
94 this.events.trigger('resize-header.Page');
95 };
95 };
96
96
97 MenuBar.prototype.bind_events = function () {
97 MenuBar.prototype.bind_events = function () {
98 /**
98 /**
99 * File
99 * File
100 */
100 */
101 var that = this;
101 var that = this;
102
102
103 this.element.find('#open_notebook').click(function () {
103 this.element.find('#open_notebook').click(function () {
104 var parent = utils.url_path_split(that.notebook.notebook_path)[0];
104 var parent = utils.url_path_split(that.notebook.notebook_path)[0];
105 window.open(utils.url_join_encode(that.base_url, 'tree', parent));
105 window.open(utils.url_join_encode(that.base_url, 'tree', parent));
106 // all if(event) are here to let test pass
107 // as evnet might not be devined when clicking with
108 // casper.js
109 if(event)event.preventDefault();
110 });
106 });
111 this.element.find('#copy_notebook').click(function () {
107 this.element.find('#copy_notebook').click(function () {
112 that.notebook.copy_notebook();
108 that.notebook.copy_notebook();
113 return false;
109 return false;
114 });
110 });
115 this.element.find('#download_ipynb').click(function () {
111 this.element.find('#download_ipynb').click(function () {
116 var base_url = that.notebook.base_url;
112 var base_url = that.notebook.base_url;
117 var notebook_path = that.notebook.notebook_path;
113 var notebook_path = that.notebook.notebook_path;
118 if (that.notebook.dirty) {
114 if (that.notebook.dirty) {
119 that.notebook.save_notebook({async : false});
115 that.notebook.save_notebook({async : false});
120 }
116 }
121
117
122 var url = utils.url_join_encode(base_url, 'files', notebook_path);
118 var url = utils.url_join_encode(base_url, 'files', notebook_path);
123 window.open(url + '?download=1');
119 window.open(url + '?download=1');
124 if(event)event.preventDefault();
125 });
120 });
126
121
127 this.element.find('#print_preview').click(function () {
122 this.element.find('#print_preview').click(function () {
128 that._nbconvert('html', false);
123 that._nbconvert('html', false);
129 if(event)event.preventDefault();
130 });
124 });
131
125
132 this.element.find('#download_html').click(function () {
126 this.element.find('#download_html').click(function () {
133 that._nbconvert('html', true);
127 that._nbconvert('html', true);
134 if(event)event.preventDefault();
135 });
128 });
136
129
137 this.element.find('#download_rst').click(function () {
130 this.element.find('#download_rst').click(function () {
138 that._nbconvert('rst', true);
131 that._nbconvert('rst', true);
139 if(event)event.preventDefault();
140 });
132 });
141
133
142 this.element.find('#download_pdf').click(function () {
134 this.element.find('#download_pdf').click(function () {
143 that._nbconvert('pdf', true);
135 that._nbconvert('pdf', true);
144 if(event)event.preventDefault();
145 });
136 });
146
137
147 this.element.find('#download_script').click(function () {
138 this.element.find('#download_script').click(function () {
148 that._nbconvert('script', true);
139 that._nbconvert('script', true);
149 if(event)event.preventDefault();
150 });
140 });
151
141
152 this.element.find('#rename_notebook').click(function () {
142 this.element.find('#rename_notebook').click(function () {
153 that.save_widget.rename_notebook({notebook: that.notebook});
143 that.save_widget.rename_notebook({notebook: that.notebook});
154 if(event)event.preventDefault();
155 });
144 });
156
145
157 this.element.find('#save_checkpoint').click(function () {
146 this.element.find('#save_checkpoint').click(function () {
158 that.notebook.save_checkpoint();
147 that.notebook.save_checkpoint();
159 if(event)event.preventDefault();
160 });
148 });
161
149
162 this.element.find('#restore_checkpoint').click(function () {
150 this.element.find('#restore_checkpoint').click(function () {
163 });
151 });
164
152
165 this.element.find('#trust_notebook').click(function () {
153 this.element.find('#trust_notebook').click(function () {
166 that.notebook.trust_notebook();
154 that.notebook.trust_notebook();
167 if(event)event.preventDefault();
168 });
155 });
169 this.events.on('trust_changed.Notebook', function (event, trusted) {
156 this.events.on('trust_changed.Notebook', function (event, trusted) {
170 if (trusted) {
157 if (trusted) {
171 that.element.find('#trust_notebook')
158 that.element.find('#trust_notebook')
172 .addClass("disabled").off('click')
159 .addClass("disabled").off('click')
173 .find("a").text("Trusted Notebook");
160 .find("a").text("Trusted Notebook");
174 } else {
161 } else {
175 that.element.find('#trust_notebook')
162 that.element.find('#trust_notebook')
176 .removeClass("disabled").on('click', function () {
163 .removeClass("disabled").on('click', function () {
177 that.notebook.trust_notebook();
164 that.notebook.trust_notebook();
178 })
165 })
179 .find("a").text("Trust Notebook");
166 .find("a").text("Trust Notebook");
180 }
167 }
181 if(event)event.preventDefault();
182 });
168 });
183
169
184 this.element.find('#kill_and_exit').click(function () {
170 this.element.find('#kill_and_exit').click(function () {
185 var close_window = function () {
171 var close_window = function () {
186 /**
172 /**
187 * allow closing of new tabs in Chromium, impossible in FF
173 * allow closing of new tabs in Chromium, impossible in FF
188 */
174 */
189 window.open('', '_self', '');
175 window.open('', '_self', '');
190 window.close();
176 window.close();
191 };
177 };
192 // finish with close on success or failure
178 // finish with close on success or failure
193 that.notebook.session.delete(close_window, close_window);
179 that.notebook.session.delete(close_window, close_window);
194 if(event)event.preventDefault();
195 });
180 });
196
181
197 // Edit
182 // Edit
198 this.element.find('#cut_cell').click(function () {
183 this.element.find('#cut_cell').click(function () {
199 that.notebook.cut_cell();
184 that.notebook.cut_cell();
200 if(event)event.preventDefault();
201 });
185 });
202 this.element.find('#copy_cell').click(function () {
186 this.element.find('#copy_cell').click(function () {
203 that.notebook.copy_cell();
187 that.notebook.copy_cell();
204 if(event)event.preventDefault();
205 });
188 });
206 this.element.find('#delete_cell').click(function () {
189 this.element.find('#delete_cell').click(function () {
207 that.notebook.delete_cell();
190 that.notebook.delete_cell();
208 if(event)event.preventDefault();
209 });
191 });
210 this.element.find('#undelete_cell').click(function () {
192 this.element.find('#undelete_cell').click(function () {
211 that.notebook.undelete_cell();
193 that.notebook.undelete_cell();
212 if(event)event.preventDefault();
213 });
194 });
214 this.element.find('#split_cell').click(function () {
195 this.element.find('#split_cell').click(function () {
215 that.notebook.split_cell();
196 that.notebook.split_cell();
216 if(event)event.preventDefault();
217 });
197 });
218 this.element.find('#merge_cell_above').click(function () {
198 this.element.find('#merge_cell_above').click(function () {
219 that.notebook.merge_cell_above();
199 that.notebook.merge_cell_above();
220 if(event)event.preventDefault();
221 });
200 });
222 this.element.find('#merge_cell_below').click(function () {
201 this.element.find('#merge_cell_below').click(function () {
223 that.notebook.merge_cell_below();
202 that.notebook.merge_cell_below();
224 if(event)event.preventDefault();
225 });
203 });
226 this.element.find('#move_cell_up').click(function () {
204 this.element.find('#move_cell_up').click(function () {
227 that.notebook.move_cell_up();
205 that.notebook.move_cell_up();
228 if(event)event.preventDefault();
229 });
206 });
230 this.element.find('#move_cell_down').click(function () {
207 this.element.find('#move_cell_down').click(function () {
231 that.notebook.move_cell_down();
208 that.notebook.move_cell_down();
232 if(event)event.preventDefault();
233 });
209 });
234 this.element.find('#edit_nb_metadata').click(function () {
210 this.element.find('#edit_nb_metadata').click(function () {
235 that.notebook.edit_metadata({
211 that.notebook.edit_metadata({
236 notebook: that.notebook,
212 notebook: that.notebook,
237 keyboard_manager: that.notebook.keyboard_manager});
213 keyboard_manager: that.notebook.keyboard_manager});
238 if(event)event.preventDefault();
239 });
214 });
240
215
241 // View
216 // View
242 this.element.find('#toggle_header').click(function () {
217 this.element.find('#toggle_header').click(function () {
243 $('#header-container').toggle();
218 $('#header-container').toggle();
244 $('.header-bar').toggle();
219 $('.header-bar').toggle();
245 that._size_header();
220 that._size_header();
246 if(event)event.preventDefault();
247 });
221 });
248 this.element.find('#toggle_toolbar').click(function () {
222 this.element.find('#toggle_toolbar').click(function () {
249 $('div#maintoolbar').toggle();
223 $('div#maintoolbar').toggle();
250 that._size_header();
224 that._size_header();
251 if(event)event.preventDefault();
252 });
225 });
253 // Insert
226 // Insert
254 this.element.find('#insert_cell_above').click(function () {
227 this.element.find('#insert_cell_above').click(function () {
255 that.notebook.insert_cell_above('code');
228 that.notebook.insert_cell_above('code');
256 that.notebook.select_prev();
229 that.notebook.select_prev();
257 if(event)event.preventDefault();
258 });
230 });
259 this.element.find('#insert_cell_below').click(function () {
231 this.element.find('#insert_cell_below').click(function () {
260 that.notebook.insert_cell_below('code');
232 that.notebook.insert_cell_below('code');
261 that.notebook.select_next();
233 that.notebook.select_next();
262 if(event)event.preventDefault();
263 });
234 });
264 // Cell
235 // Cell
265 this.element.find('#run_cell').click(function () {
236 this.element.find('#run_cell').click(function () {
266 that.notebook.execute_cell();
237 that.notebook.execute_cell();
267 if(event)event.preventDefault();
268 });
238 });
269 this.element.find('#run_cell_select_below').click(function () {
239 this.element.find('#run_cell_select_below').click(function () {
270 that.notebook.execute_cell_and_select_below();
240 that.notebook.execute_cell_and_select_below();
271 if(event)event.preventDefault();
272 });
241 });
273 this.element.find('#run_cell_insert_below').click(function () {
242 this.element.find('#run_cell_insert_below').click(function () {
274 that.notebook.execute_cell_and_insert_below();
243 that.notebook.execute_cell_and_insert_below();
275 if(event)event.preventDefault();
276 });
244 });
277 this.element.find('#run_all_cells').click(function () {
245 this.element.find('#run_all_cells').click(function () {
278 that.notebook.execute_all_cells();
246 that.notebook.execute_all_cells();
279 if(event)event.preventDefault();
280 });
247 });
281 this.element.find('#run_all_cells_above').click(function () {
248 this.element.find('#run_all_cells_above').click(function () {
282 that.notebook.execute_cells_above();
249 that.notebook.execute_cells_above();
283 if(event)event.preventDefault();
284 });
250 });
285 this.element.find('#run_all_cells_below').click(function () {
251 this.element.find('#run_all_cells_below').click(function () {
286 that.notebook.execute_cells_below();
252 that.notebook.execute_cells_below();
287 if(event)event.preventDefault();
288 });
253 });
289 this.element.find('#to_code').click(function () {
254 this.element.find('#to_code').click(function () {
290 that.notebook.to_code();
255 that.notebook.to_code();
291 if(event)event.preventDefault();
292 });
256 });
293 this.element.find('#to_markdown').click(function () {
257 this.element.find('#to_markdown').click(function () {
294 that.notebook.to_markdown();
258 that.notebook.to_markdown();
295 if(event)event.preventDefault();
296 });
259 });
297 this.element.find('#to_raw').click(function () {
260 this.element.find('#to_raw').click(function () {
298 that.notebook.to_raw();
261 that.notebook.to_raw();
299 if(event)event.preventDefault();
300 });
262 });
301
263
302 this.element.find('#toggle_current_output').click(function () {
264 this.element.find('#toggle_current_output').click(function () {
303 that.notebook.toggle_output();
265 that.notebook.toggle_output();
304 if(event)event.preventDefault();
305 });
266 });
306 this.element.find('#toggle_current_output_scroll').click(function () {
267 this.element.find('#toggle_current_output_scroll').click(function () {
307 that.notebook.toggle_output_scroll();
268 that.notebook.toggle_output_scroll();
308 if(event)event.preventDefault();
309 });
269 });
310 this.element.find('#clear_current_output').click(function () {
270 this.element.find('#clear_current_output').click(function () {
311 that.notebook.clear_output();
271 that.notebook.clear_output();
312 if(event)event.preventDefault();
313 });
272 });
314
273
315 this.element.find('#toggle_all_output').click(function () {
274 this.element.find('#toggle_all_output').click(function () {
316 that.notebook.toggle_all_output();
275 that.notebook.toggle_all_output();
317 if(event)event.preventDefault();
318 });
276 });
319 this.element.find('#toggle_all_output_scroll').click(function () {
277 this.element.find('#toggle_all_output_scroll').click(function () {
320 that.notebook.toggle_all_output_scroll();
278 that.notebook.toggle_all_output_scroll();
321 if(event)event.preventDefault();
322 });
279 });
323 this.element.find('#clear_all_output').click(function () {
280 this.element.find('#clear_all_output').click(function () {
324 that.notebook.clear_all_output();
281 that.notebook.clear_all_output();
325 if(event)event.preventDefault();
326 });
282 });
327
283
328 // Kernel
284 // Kernel
329 this.element.find('#int_kernel').click(function () {
285 this.element.find('#int_kernel').click(function () {
330 that.notebook.kernel.interrupt();
286 that.notebook.kernel.interrupt();
331 if(event)event.preventDefault();
332 });
287 });
333 this.element.find('#restart_kernel').click(function () {
288 this.element.find('#restart_kernel').click(function () {
334 that.notebook.restart_kernel();
289 that.notebook.restart_kernel();
335 if(event)event.preventDefault();
336 });
290 });
337 this.element.find('#reconnect_kernel').click(function () {
291 this.element.find('#reconnect_kernel').click(function () {
338 that.notebook.kernel.reconnect();
292 that.notebook.kernel.reconnect();
339 if(event)event.preventDefault();
340 });
293 });
341 // Help
294 // Help
342 if (this.tour) {
295 if (this.tour) {
343 this.element.find('#notebook_tour').click(function () {
296 this.element.find('#notebook_tour').click(function () {
344 that.tour.start();
297 that.tour.start();
345 if(event)event.preventDefault();
346 });
298 });
347 } else {
299 } else {
348 this.element.find('#notebook_tour').addClass("disabled");
300 this.element.find('#notebook_tour').addClass("disabled");
349 }
301 }
350 this.element.find('#keyboard_shortcuts').click(function () {
302 this.element.find('#keyboard_shortcuts').click(function () {
351 that.quick_help.show_keyboard_shortcuts();
303 that.quick_help.show_keyboard_shortcuts();
352 if(event)event.preventDefault();
353 });
304 });
354
305
355 this.update_restore_checkpoint(null);
306 this.update_restore_checkpoint(null);
356
307
357 this.events.on('checkpoints_listed.Notebook', function (event, data) {
308 this.events.on('checkpoints_listed.Notebook', function (event, data) {
358 that.update_restore_checkpoint(that.notebook.checkpoints);
309 that.update_restore_checkpoint(that.notebook.checkpoints);
359 if(event)event.preventDefault();
360 });
310 });
361
311
362 this.events.on('checkpoint_created.Notebook', function (event, data) {
312 this.events.on('checkpoint_created.Notebook', function (event, data) {
363 that.update_restore_checkpoint(that.notebook.checkpoints);
313 that.update_restore_checkpoint(that.notebook.checkpoints);
364 if(event)event.preventDefault();
365 });
314 });
366
315
367 this.events.on('notebook_loaded.Notebook', function() {
316 this.events.on('notebook_loaded.Notebook', function() {
368 var langinfo = that.notebook.metadata.language_info || {};
317 var langinfo = that.notebook.metadata.language_info || {};
369 that.update_nbconvert_script(langinfo);
318 that.update_nbconvert_script(langinfo);
370 if(event)event.preventDefault();
371 });
319 });
372
320
373 this.events.on('kernel_ready.Kernel', function(event, data) {
321 this.events.on('kernel_ready.Kernel', function(event, data) {
374 var langinfo = data.kernel.info_reply.language_info || {};
322 var langinfo = data.kernel.info_reply.language_info || {};
375 that.update_nbconvert_script(langinfo);
323 that.update_nbconvert_script(langinfo);
376 that.add_kernel_help_links(data.kernel.info_reply.help_links || []);
324 that.add_kernel_help_links(data.kernel.info_reply.help_links || []);
377 if(event)event.preventDefault();
378 });
325 });
379 };
326 };
380
327
381 MenuBar.prototype.update_restore_checkpoint = function(checkpoints) {
328 MenuBar.prototype.update_restore_checkpoint = function(checkpoints) {
382 var ul = this.element.find("#restore_checkpoint").find("ul");
329 var ul = this.element.find("#restore_checkpoint").find("ul");
383 ul.empty();
330 ul.empty();
384 if (!checkpoints || checkpoints.length === 0) {
331 if (!checkpoints || checkpoints.length === 0) {
385 ul.append(
332 ul.append(
386 $("<li/>")
333 $("<li/>")
387 .addClass("disabled")
334 .addClass("disabled")
388 .append(
335 .append(
389 $("<a/>")
336 $("<a/>")
390 .text("No checkpoints")
337 .text("No checkpoints")
391 )
338 )
392 );
339 );
393 return;
340 return;
394 }
341 }
395
342
396 var that = this;
343 var that = this;
397 checkpoints.map(function (checkpoint) {
344 checkpoints.map(function (checkpoint) {
398 var d = new Date(checkpoint.last_modified);
345 var d = new Date(checkpoint.last_modified);
399 ul.append(
346 ul.append(
400 $("<li/>").append(
347 $("<li/>").append(
401 $("<a/>")
348 $("<a/>")
402 .attr("href", "#")
349 .attr("href", "#")
403 .text(moment(d).format("LLLL"))
350 .text(moment(d).format("LLLL"))
404 .click(function () {
351 .click(function () {
405 that.notebook.restore_checkpoint_dialog(checkpoint);
352 that.notebook.restore_checkpoint_dialog(checkpoint);
406 if(event)event.preventDefault();
407 })
353 })
408 )
354 )
409 );
355 );
410 });
356 });
411 };
357 };
412
358
413 MenuBar.prototype.update_nbconvert_script = function(langinfo) {
359 MenuBar.prototype.update_nbconvert_script = function(langinfo) {
414 /**
360 /**
415 * Set the 'Download as foo' menu option for the relevant language.
361 * Set the 'Download as foo' menu option for the relevant language.
416 */
362 */
417 var el = this.element.find('#download_script');
363 var el = this.element.find('#download_script');
418
364
419 // Set menu entry text to e.g. "Python (.py)"
365 // Set menu entry text to e.g. "Python (.py)"
420 var langname = (langinfo.name || 'Script');
366 var langname = (langinfo.name || 'Script');
421 langname = langname.charAt(0).toUpperCase()+langname.substr(1); // Capitalise
367 langname = langname.charAt(0).toUpperCase()+langname.substr(1); // Capitalise
422 el.find('a').text(langname + ' ('+(langinfo.file_extension || 'txt')+')');
368 el.find('a').text(langname + ' ('+(langinfo.file_extension || 'txt')+')');
423 };
369 };
424
370
425 MenuBar.prototype.add_kernel_help_links = function(help_links) {
371 MenuBar.prototype.add_kernel_help_links = function(help_links) {
426 /** add links from kernel_info to the help menu */
372 /** add links from kernel_info to the help menu */
427 var divider = $("#kernel-help-links");
373 var divider = $("#kernel-help-links");
428 if (divider.length === 0) {
374 if (divider.length === 0) {
429 // insert kernel help section above about link
375 // insert kernel help section above about link
430 var about = $("#notebook_about").parent();
376 var about = $("#notebook_about").parent();
431 divider = $("<li>")
377 divider = $("<li>")
432 .attr('id', "kernel-help-links")
378 .attr('id', "kernel-help-links")
433 .addClass('divider');
379 .addClass('divider');
434 about.prev().before(divider);
380 about.prev().before(divider);
435 }
381 }
436 // remove previous entries
382 // remove previous entries
437 while (!divider.next().hasClass('divider')) {
383 while (!divider.next().hasClass('divider')) {
438 divider.next().remove();
384 divider.next().remove();
439 }
385 }
440 if (help_links.length === 0) {
386 if (help_links.length === 0) {
441 // no help links, remove the divider
387 // no help links, remove the divider
442 divider.remove();
388 divider.remove();
443 return;
389 return;
444 }
390 }
445 var cursor = divider;
391 var cursor = divider;
446 help_links.map(function (link) {
392 help_links.map(function (link) {
447 cursor.after($("<li>")
393 cursor.after($("<li>")
448 .append($("<a>")
394 .append($("<a>")
449 .attr('target', '_blank')
395 .attr('target', '_blank')
450 .attr('title', 'Opens in a new window')
396 .attr('title', 'Opens in a new window')
451 .attr('href', link.url)
397 .attr('href', link.url)
452 .text(link.text)
398 .text(link.text)
453 .append($("<i>")
399 .append($("<i>")
454 .addClass("fa fa-external-link menu-icon pull-right")
400 .addClass("fa fa-external-link menu-icon pull-right")
455 )
401 )
456 )
402 )
457 );
403 );
458 cursor = cursor.next();
404 cursor = cursor.next();
459 });
405 });
460
406
461 };
407 };
462
408
463 // Backwards compatability.
409 // Backwards compatability.
464 IPython.MenuBar = MenuBar;
410 IPython.MenuBar = MenuBar;
465
411
466 return {'MenuBar': MenuBar};
412 return {'MenuBar': MenuBar};
467 });
413 });
General Comments 0
You need to be logged in to leave comments. Login now