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