Show More
@@ -1,289 +1,270 | |||||
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 | 'base/js/dialog', | |
9 | ], function(IPython, $, utils, dialog) { |
|
9 | ], function(IPython, $, utils, dialog) { | |
10 | var Contents = function(options) { |
|
10 | var Contents = function(options) { | |
11 | // Constructor |
|
11 | // Constructor | |
12 | // |
|
12 | // | |
13 | // A contents handles passing file operations |
|
13 | // A contents handles passing file operations | |
14 | // to the back-end. This includes checkpointing |
|
14 | // to the back-end. This includes checkpointing | |
15 | // with the normal file operations. |
|
15 | // with the normal file operations. | |
16 | // |
|
16 | // | |
17 | // Parameters: |
|
17 | // Parameters: | |
18 | // options: dictionary |
|
18 | // options: dictionary | |
19 | // Dictionary of keyword arguments. |
|
19 | // Dictionary of keyword arguments. | |
20 | // events: $(Events) instance |
|
20 | // events: $(Events) instance | |
21 | // base_url: string |
|
21 | // base_url: string | |
22 | this.events = options.events; |
|
22 | this.events = options.events; | |
23 | this.base_url = options.base_url; |
|
23 | this.base_url = options.base_url; | |
24 | }; |
|
24 | }; | |
25 |
|
25 | |||
26 | /** |
|
26 | /** | |
27 | * Notebook Functions |
|
27 | * Notebook Functions | |
28 | */ |
|
28 | */ | |
29 |
|
29 | |||
30 | /** |
|
30 | /** | |
31 | * Load a notebook. |
|
31 | * Load a notebook. | |
32 | * |
|
32 | * | |
33 | * Calls success_callback with notebook JSON object (as string), or |
|
33 | * Calls success_callback with notebook JSON object (as string), or | |
34 | * error_callback with error. |
|
34 | * error_callback with error. | |
35 | * |
|
35 | * | |
36 | * @method load_notebook |
|
36 | * @method load_notebook | |
37 | * @param {String} path |
|
37 | * @param {String} path | |
38 | * @param {String} name |
|
38 | * @param {String} name | |
39 | * @param {Function} success_callback |
|
39 | * @param {Function} success_callback | |
40 | * @param {Function} error_callback |
|
40 | * @param {Function} error_callback | |
41 | */ |
|
41 | */ | |
42 | Contents.prototype.load_notebook = function (path, name, success_callback, |
|
42 | Contents.prototype.load_notebook = function (path, name, success_callback, | |
43 | error_callback) { |
|
43 | error_callback) { | |
44 | // We do the call with settings so we can set cache to false. |
|
44 | // We do the call with settings so we can set cache to false. | |
45 | var settings = { |
|
45 | var settings = { | |
46 | processData : false, |
|
46 | processData : false, | |
47 | cache : false, |
|
47 | cache : false, | |
48 | type : "GET", |
|
48 | type : "GET", | |
49 | dataType : "json", |
|
49 | dataType : "json", | |
50 | success : success_callback, |
|
50 | success : success_callback, | |
51 | error : error_callback, |
|
51 | error : error_callback, | |
52 | }; |
|
52 | }; | |
53 | this.events.trigger('notebook_loading.Notebook'); |
|
53 | this.events.trigger('notebook_loading.Notebook'); | |
54 | var url = utils.url_join_encode( |
|
54 | var url = utils.url_join_encode( | |
55 | this.base_url, |
|
55 | this.base_url, | |
56 | 'api/contents', |
|
56 | 'api/contents', | |
57 | path, |
|
57 | path, | |
58 | name |
|
58 | name | |
59 | ); |
|
59 | ); | |
60 | $.ajax(url, settings); |
|
60 | $.ajax(url, settings); | |
61 | }; |
|
61 | }; | |
62 |
|
62 | |||
63 |
|
63 | |||
64 | /** |
|
64 | /** | |
65 | * Creates a new notebook file at the specified path, and |
|
65 | * Creates a new notebook file at the specified path, and | |
66 | * opens that notebook in a new window. |
|
66 | * opens that notebook in a new window. | |
67 | * |
|
67 | * | |
68 | * @method scroll_to_cell |
|
68 | * @method scroll_to_cell | |
69 | * @param {String} path The path to create the new notebook at |
|
69 | * @param {String} path The path to create the new notebook at | |
70 | */ |
|
70 | */ | |
71 | Contents.prototype.new_notebook = function(path) { |
|
71 | Contents.prototype.new_notebook = function(path, options) { | |
72 | var base_url = this.base_url; |
|
72 | var base_url = this.base_url; | |
|
73 | var success_callback = options.success_callback || function(data, status, xhr) {}; | |||
|
74 | var error_callback = options.error_callback || function(xhr, status, error) {}; | |||
73 | var settings = { |
|
75 | var settings = { | |
74 | processData : false, |
|
76 | processData : false, | |
75 | cache : false, |
|
77 | cache : false, | |
76 | type : "POST", |
|
78 | type : "POST", | |
77 | dataType : "json", |
|
79 | dataType : "json", | |
78 | async : false, |
|
80 | async : false, | |
79 |
success : |
|
81 | success : success_callback, | |
80 | var notebook_name = data.name; |
|
|||
81 | window.open( |
|
|||
82 | utils.url_join_encode( |
|
|||
83 | base_url, |
|
|||
84 | 'notebooks', |
|
|||
85 | path, |
|
|||
86 | notebook_name |
|
|||
87 | ), |
|
|||
88 | '_blank' |
|
|||
89 | ); |
|
|||
90 | }, |
|
|||
91 | error : function(xhr, status, error) { |
|
82 | error : function(xhr, status, error) { | |
92 | utils.log_ajax_error(xhr, status, error); |
|
83 | utils.log_ajax_error(xhr, status, error); | |
93 | var msg; |
|
84 | error_callback(xhr, status, error); | |
94 | if (xhr.responseJSON && xhr.responseJSON.message) { |
|
|||
95 | msg = xhr.responseJSON.message; |
|
|||
96 | } else { |
|
|||
97 | msg = xhr.statusText; |
|
|||
98 | } |
|
|||
99 | dialog.modal({ |
|
|||
100 | title : 'Creating Notebook Failed', |
|
|||
101 | body : "The error was: " + msg, |
|
|||
102 | buttons : {'OK' : {'class' : 'btn-primary'}} |
|
|||
103 | }); |
|
|||
104 | } |
|
85 | } | |
105 | }; |
|
86 | }; | |
106 | var url = utils.url_join_encode( |
|
87 | var url = utils.url_join_encode( | |
107 | base_url, |
|
88 | base_url, | |
108 | 'api/contents', |
|
89 | 'api/contents', | |
109 | path |
|
90 | path | |
110 | ); |
|
91 | ); | |
111 | $.ajax(url,settings); |
|
92 | $.ajax(url,settings); | |
112 | }; |
|
93 | }; | |
113 |
|
94 | |||
114 | Contents.prototype.delete_notebook = function(name, path) { |
|
95 | Contents.prototype.delete_notebook = function(name, path) { | |
115 | var settings = { |
|
96 | var settings = { | |
116 | processData : false, |
|
97 | processData : false, | |
117 | cache : false, |
|
98 | cache : false, | |
118 | type : "DELETE", |
|
99 | type : "DELETE", | |
119 | dataType : "json", |
|
100 | dataType : "json", | |
120 | success : $.proxy(this.events.trigger, this.events, |
|
101 | success : $.proxy(this.events.trigger, this.events, | |
121 | 'notebook_deleted.Contents', |
|
102 | 'notebook_deleted.Contents', | |
122 | { |
|
103 | { | |
123 | name: name, |
|
104 | name: name, | |
124 | path: path |
|
105 | path: path | |
125 | }), |
|
106 | }), | |
126 | error : utils.log_ajax_error |
|
107 | error : utils.log_ajax_error | |
127 | }; |
|
108 | }; | |
128 | var url = utils.url_join_encode( |
|
109 | var url = utils.url_join_encode( | |
129 | this.base_url, |
|
110 | this.base_url, | |
130 | 'api/contents', |
|
111 | 'api/contents', | |
131 | path, |
|
112 | path, | |
132 | name |
|
113 | name | |
133 | ); |
|
114 | ); | |
134 | $.ajax(url, settings); |
|
115 | $.ajax(url, settings); | |
135 | }; |
|
116 | }; | |
136 |
|
117 | |||
137 | Contents.prototype.rename_notebook = function(path, name, new_name) { |
|
118 | Contents.prototype.rename_notebook = function(path, name, new_name) { | |
138 | var that = this; |
|
119 | var that = this; | |
139 | var data = {name: new_name}; |
|
120 | var data = {name: new_name}; | |
140 | var settings = { |
|
121 | var settings = { | |
141 | processData : false, |
|
122 | processData : false, | |
142 | cache : false, |
|
123 | cache : false, | |
143 | type : "PATCH", |
|
124 | type : "PATCH", | |
144 | data : JSON.stringify(data), |
|
125 | data : JSON.stringify(data), | |
145 | dataType: "json", |
|
126 | dataType: "json", | |
146 | contentType: 'application/json', |
|
127 | contentType: 'application/json', | |
147 | success : function (json, status, xhr) { |
|
128 | success : function (json, status, xhr) { | |
148 | that.events.trigger('notebook_rename_success.Contents', |
|
129 | that.events.trigger('notebook_rename_success.Contents', | |
149 | json); |
|
130 | json); | |
150 | }, |
|
131 | }, | |
151 | error : function (xhr, status, error) { |
|
132 | error : function (xhr, status, error) { | |
152 | that.events.trigger('notebook_rename_error.Contents', |
|
133 | that.events.trigger('notebook_rename_error.Contents', | |
153 | [xhr, status, error]); |
|
134 | [xhr, status, error]); | |
154 | } |
|
135 | } | |
155 | }; |
|
136 | }; | |
156 | var url = utils.url_join_encode( |
|
137 | var url = utils.url_join_encode( | |
157 | this.base_url, |
|
138 | this.base_url, | |
158 | 'api/contents', |
|
139 | 'api/contents', | |
159 | path, |
|
140 | path, | |
160 | name |
|
141 | name | |
161 | ); |
|
142 | ); | |
162 | $.ajax(url, settings); |
|
143 | $.ajax(url, settings); | |
163 | }; |
|
144 | }; | |
164 |
|
145 | |||
165 | Contents.prototype.save_notebook = function(path, name, content, |
|
146 | Contents.prototype.save_notebook = function(path, name, content, | |
166 | extra_settings) { |
|
147 | extra_settings) { | |
167 | var that = content; |
|
148 | var that = content; | |
168 | // Create a JSON model to be sent to the server. |
|
149 | // Create a JSON model to be sent to the server. | |
169 | var model = { |
|
150 | var model = { | |
170 | name : name, |
|
151 | name : name, | |
171 | path : path, |
|
152 | path : path, | |
172 | type : "notebook", |
|
153 | type : "notebook", | |
173 | content : content |
|
154 | content : content | |
174 | }; |
|
155 | }; | |
175 | // time the ajax call for autosave tuning purposes. |
|
156 | // time the ajax call for autosave tuning purposes. | |
176 | var start = new Date().getTime(); |
|
157 | var start = new Date().getTime(); | |
177 | // We do the call with settings so we can set cache to false. |
|
158 | // We do the call with settings so we can set cache to false. | |
178 | var settings = { |
|
159 | var settings = { | |
179 | processData : false, |
|
160 | processData : false, | |
180 | cache : false, |
|
161 | cache : false, | |
181 | type : "PUT", |
|
162 | type : "PUT", | |
182 | data : JSON.stringify(model), |
|
163 | data : JSON.stringify(model), | |
183 | contentType: 'application/json', |
|
164 | contentType: 'application/json', | |
184 | success : $.proxy(this.events.trigger, this.events, |
|
165 | success : $.proxy(this.events.trigger, this.events, | |
185 | 'notebook_save_success.Contents', |
|
166 | 'notebook_save_success.Contents', | |
186 | $.extend(model, { start : start })), |
|
167 | $.extend(model, { start : start })), | |
187 | error : function (xhr, status, error) { |
|
168 | error : function (xhr, status, error) { | |
188 | that.events.trigger('notebook_save_error.Contents', |
|
169 | that.events.trigger('notebook_save_error.Contents', | |
189 | [xhr, status, error, model]); |
|
170 | [xhr, status, error, model]); | |
190 | } |
|
171 | } | |
191 | }; |
|
172 | }; | |
192 | if (extra_settings) { |
|
173 | if (extra_settings) { | |
193 | for (var key in extra_settings) { |
|
174 | for (var key in extra_settings) { | |
194 | settings[key] = extra_settings[key]; |
|
175 | settings[key] = extra_settings[key]; | |
195 | } |
|
176 | } | |
196 | } |
|
177 | } | |
197 | var url = utils.url_join_encode( |
|
178 | var url = utils.url_join_encode( | |
198 | this.base_url, |
|
179 | this.base_url, | |
199 | 'api/contents', |
|
180 | 'api/contents', | |
200 | path, |
|
181 | path, | |
201 | name |
|
182 | name | |
202 | ); |
|
183 | ); | |
203 | $.ajax(url, settings); |
|
184 | $.ajax(url, settings); | |
204 | }; |
|
185 | }; | |
205 |
|
186 | |||
206 | /** |
|
187 | /** | |
207 | * Checkpointing Functions |
|
188 | * Checkpointing Functions | |
208 | */ |
|
189 | */ | |
209 |
|
190 | |||
210 | Contents.prototype.save_checkpoint = function() { |
|
191 | Contents.prototype.save_checkpoint = function() { | |
211 | // This is not necessary - integrated into save |
|
192 | // This is not necessary - integrated into save | |
212 | }; |
|
193 | }; | |
213 |
|
194 | |||
214 | Contents.prototype.restore_checkpoint = function(notebook, id) { |
|
195 | Contents.prototype.restore_checkpoint = function(notebook, id) { | |
215 | that = notebook; |
|
196 | that = notebook; | |
216 | this.events.trigger('notebook_restoring.Notebook', checkpoint); |
|
197 | this.events.trigger('notebook_restoring.Notebook', checkpoint); | |
217 | var url = utils.url_join_encode( |
|
198 | var url = utils.url_join_encode( | |
218 | this.base_url, |
|
199 | this.base_url, | |
219 | 'api/contents', |
|
200 | 'api/contents', | |
220 | this.notebook_path, |
|
201 | this.notebook_path, | |
221 | this.notebook_name, |
|
202 | this.notebook_name, | |
222 | 'checkpoints', |
|
203 | 'checkpoints', | |
223 | checkpoint |
|
204 | checkpoint | |
224 | ); |
|
205 | ); | |
225 | $.post(url).done( |
|
206 | $.post(url).done( | |
226 | $.proxy(that.restore_checkpoint_success, that) |
|
207 | $.proxy(that.restore_checkpoint_success, that) | |
227 | ).fail( |
|
208 | ).fail( | |
228 | $.proxy(that.restore_checkpoint_error, that) |
|
209 | $.proxy(that.restore_checkpoint_error, that) | |
229 | ); |
|
210 | ); | |
230 | }; |
|
211 | }; | |
231 |
|
212 | |||
232 | Contents.prototype.list_checkpoints = function(notebook) { |
|
213 | Contents.prototype.list_checkpoints = function(notebook) { | |
233 | that = notebook; |
|
214 | that = notebook; | |
234 | var url = utils.url_join_encode( |
|
215 | var url = utils.url_join_encode( | |
235 | that.base_url, |
|
216 | that.base_url, | |
236 | 'api/contents', |
|
217 | 'api/contents', | |
237 | that.notebook_path, |
|
218 | that.notebook_path, | |
238 | that.notebook_name, |
|
219 | that.notebook_name, | |
239 | 'checkpoints' |
|
220 | 'checkpoints' | |
240 | ); |
|
221 | ); | |
241 | $.get(url).done( |
|
222 | $.get(url).done( | |
242 | $.proxy(that.list_checkpoints_success, that) |
|
223 | $.proxy(that.list_checkpoints_success, that) | |
243 | ).fail( |
|
224 | ).fail( | |
244 | $.proxy(that.list_checkpoints_error, that) |
|
225 | $.proxy(that.list_checkpoints_error, that) | |
245 | ); |
|
226 | ); | |
246 | }; |
|
227 | }; | |
247 |
|
228 | |||
248 | /** |
|
229 | /** | |
249 | * File management functions |
|
230 | * File management functions | |
250 | */ |
|
231 | */ | |
251 |
|
232 | |||
252 | /** |
|
233 | /** | |
253 | * List notebooks and directories at a given path |
|
234 | * List notebooks and directories at a given path | |
254 | * |
|
235 | * | |
255 | * On success, load_callback is called with an array of dictionaries |
|
236 | * On success, load_callback is called with an array of dictionaries | |
256 | * representing individual files or directories. Each dictionary has |
|
237 | * representing individual files or directories. Each dictionary has | |
257 | * the keys: |
|
238 | * the keys: | |
258 | * type: "notebook" or "directory" |
|
239 | * type: "notebook" or "directory" | |
259 | * name: the name of the file or directory |
|
240 | * name: the name of the file or directory | |
260 | * created: created date |
|
241 | * created: created date | |
261 | * last_modified: last modified dat |
|
242 | * last_modified: last modified dat | |
262 | * path: the path |
|
243 | * path: the path | |
263 | * @method list_notebooks |
|
244 | * @method list_notebooks | |
264 | * @param {String} path The path to list notebooks in |
|
245 | * @param {String} path The path to list notebooks in | |
265 | * @param {Function} load_callback called with list of notebooks on success |
|
246 | * @param {Function} load_callback called with list of notebooks on success | |
266 | * @param {Function} error_callback called with ajax results on error |
|
247 | * @param {Function} error_callback called with ajax results on error | |
267 | */ |
|
248 | */ | |
268 | Contents.prototype.list_contents = function(path, load_callback, |
|
249 | Contents.prototype.list_contents = function(path, load_callback, | |
269 | error_callback) { |
|
250 | error_callback) { | |
270 | var that = this; |
|
251 | var that = this; | |
271 | var settings = { |
|
252 | var settings = { | |
272 | processData : false, |
|
253 | processData : false, | |
273 | cache : false, |
|
254 | cache : false, | |
274 | type : "GET", |
|
255 | type : "GET", | |
275 | dataType : "json", |
|
256 | dataType : "json", | |
276 | success : load_callback, |
|
257 | success : load_callback, | |
277 | error : error_callback |
|
258 | error : error_callback | |
278 | }; |
|
259 | }; | |
279 |
|
260 | |||
280 | var url = utils.url_join_encode(this.base_url, 'api', 'contents', |
|
261 | var url = utils.url_join_encode(this.base_url, 'api', 'contents', | |
281 | path); |
|
262 | path); | |
282 | $.ajax(url, settings); |
|
263 | $.ajax(url, settings); | |
283 | }; |
|
264 | }; | |
284 |
|
265 | |||
285 |
|
266 | |||
286 | IPython.Contents = Contents; |
|
267 | IPython.Contents = Contents; | |
287 |
|
268 | |||
288 | return {'Contents': Contents}; |
|
269 | return {'Contents': Contents}; | |
289 | }); |
|
270 | }); |
@@ -1,357 +1,379 | |||||
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 | 'notebook/js/tour', |
|
8 | 'notebook/js/tour', | |
9 | 'bootstrap', |
|
9 | 'bootstrap', | |
10 | 'moment', |
|
10 | 'moment', | |
11 | ], function(IPython, $, utils, tour, bootstrap, moment) { |
|
11 | ], function(IPython, $, utils, tour, bootstrap, moment) { | |
12 | "use strict"; |
|
12 | "use strict"; | |
13 |
|
13 | |||
14 | var MenuBar = function (selector, options) { |
|
14 | var MenuBar = function (selector, options) { | |
15 | // Constructor |
|
15 | // Constructor | |
16 | // |
|
16 | // | |
17 | // A MenuBar Class to generate the menubar of IPython notebook |
|
17 | // A MenuBar Class to generate the menubar of IPython notebook | |
18 | // |
|
18 | // | |
19 | // Parameters: |
|
19 | // Parameters: | |
20 | // selector: string |
|
20 | // selector: string | |
21 | // options: dictionary |
|
21 | // options: dictionary | |
22 | // Dictionary of keyword arguments. |
|
22 | // Dictionary of keyword arguments. | |
23 | // notebook: Notebook instance |
|
23 | // notebook: Notebook instance | |
24 | // contents: ContentManager instance |
|
24 | // contents: ContentManager instance | |
25 | // layout_manager: LayoutManager instance |
|
25 | // layout_manager: LayoutManager instance | |
26 | // events: $(Events) instance |
|
26 | // events: $(Events) instance | |
27 | // save_widget: SaveWidget instance |
|
27 | // save_widget: SaveWidget instance | |
28 | // quick_help: QuickHelp instance |
|
28 | // quick_help: QuickHelp instance | |
29 | // base_url : string |
|
29 | // base_url : string | |
30 | // notebook_path : string |
|
30 | // notebook_path : string | |
31 | // notebook_name : string |
|
31 | // notebook_name : string | |
32 | options = options || {}; |
|
32 | options = options || {}; | |
33 | this.base_url = options.base_url || utils.get_body_data("baseUrl"); |
|
33 | this.base_url = options.base_url || utils.get_body_data("baseUrl"); | |
34 | this.selector = selector; |
|
34 | this.selector = selector; | |
35 | this.notebook = options.notebook; |
|
35 | this.notebook = options.notebook; | |
36 | this.contents = options.contents; |
|
36 | this.contents = options.contents; | |
37 | this.layout_manager = options.layout_manager; |
|
37 | this.layout_manager = options.layout_manager; | |
38 | this.events = options.events; |
|
38 | this.events = options.events; | |
39 | this.save_widget = options.save_widget; |
|
39 | this.save_widget = options.save_widget; | |
40 | this.quick_help = options.quick_help; |
|
40 | this.quick_help = options.quick_help; | |
41 |
|
41 | |||
42 | try { |
|
42 | try { | |
43 | this.tour = new tour.Tour(this.notebook, this.events); |
|
43 | this.tour = new tour.Tour(this.notebook, this.events); | |
44 | } catch (e) { |
|
44 | } catch (e) { | |
45 | this.tour = undefined; |
|
45 | this.tour = undefined; | |
46 | console.log("Failed to instantiate Notebook Tour", e); |
|
46 | console.log("Failed to instantiate Notebook Tour", e); | |
47 | } |
|
47 | } | |
48 |
|
48 | |||
49 | if (this.selector !== undefined) { |
|
49 | if (this.selector !== undefined) { | |
50 | this.element = $(selector); |
|
50 | this.element = $(selector); | |
51 | this.style(); |
|
51 | this.style(); | |
52 | this.bind_events(); |
|
52 | this.bind_events(); | |
53 | } |
|
53 | } | |
54 | }; |
|
54 | }; | |
55 |
|
55 | |||
56 | // TODO: This has definitively nothing to do with style ... |
|
56 | // TODO: This has definitively nothing to do with style ... | |
57 | MenuBar.prototype.style = function () { |
|
57 | MenuBar.prototype.style = function () { | |
58 | var that = this; |
|
58 | var that = this; | |
59 | this.element.find("li").click(function (event, ui) { |
|
59 | this.element.find("li").click(function (event, ui) { | |
60 | // The selected cell loses focus when the menu is entered, so we |
|
60 | // The selected cell loses focus when the menu is entered, so we | |
61 | // re-select it upon selection. |
|
61 | // re-select it upon selection. | |
62 | var i = that.notebook.get_selected_index(); |
|
62 | var i = that.notebook.get_selected_index(); | |
63 | that.notebook.select(i); |
|
63 | that.notebook.select(i); | |
64 | } |
|
64 | } | |
65 | ); |
|
65 | ); | |
66 | }; |
|
66 | }; | |
67 |
|
67 | |||
68 | MenuBar.prototype._nbconvert = function (format, download) { |
|
68 | MenuBar.prototype._nbconvert = function (format, download) { | |
69 | download = download || false; |
|
69 | download = download || false; | |
70 | var notebook_path = this.notebook.notebook_path; |
|
70 | var notebook_path = this.notebook.notebook_path; | |
71 | var notebook_name = this.notebook.notebook_name; |
|
71 | var notebook_name = this.notebook.notebook_name; | |
72 | if (this.notebook.dirty) { |
|
72 | if (this.notebook.dirty) { | |
73 | this.notebook.save_notebook({async : false}); |
|
73 | this.notebook.save_notebook({async : false}); | |
74 | } |
|
74 | } | |
75 | var url = utils.url_join_encode( |
|
75 | var url = utils.url_join_encode( | |
76 | this.base_url, |
|
76 | this.base_url, | |
77 | 'nbconvert', |
|
77 | 'nbconvert', | |
78 | format, |
|
78 | format, | |
79 | notebook_path, |
|
79 | notebook_path, | |
80 | notebook_name |
|
80 | notebook_name | |
81 | ) + "?download=" + download.toString(); |
|
81 | ) + "?download=" + download.toString(); | |
82 |
|
82 | |||
83 | window.open(url); |
|
83 | window.open(url); | |
84 | }; |
|
84 | }; | |
85 |
|
85 | |||
86 | MenuBar.prototype.bind_events = function () { |
|
86 | MenuBar.prototype.bind_events = function () { | |
87 | // File |
|
87 | // File | |
88 | var that = this; |
|
88 | var that = this; | |
89 | this.element.find('#new_notebook').click(function () { |
|
89 | this.element.find('#new_notebook').click(function () { | |
90 | // Create a new notebook in the same path as the current |
|
90 | // Create a new notebook in the same path as the current | |
91 | // notebook's path. |
|
91 | // notebook's path. | |
92 |
that.contents.new_notebook(that.notebook.notebook_path |
|
92 | that.contents.new_notebook(that.notebook.notebook_path, | |
|
93 | { | |||
|
94 | success_callback: function (data, status, xhr) { | |||
|
95 | window.open( | |||
|
96 | utils.url_join_encode( | |||
|
97 | common_options.base_url, 'notebooks', | |||
|
98 | data.path, data.name | |||
|
99 | ), '_blank'); | |||
|
100 | }, | |||
|
101 | error_callback: function(xhr, status, error) { | |||
|
102 | var msg; | |||
|
103 | if (xhr.responseJSON && xhr.responseJSON.message) { | |||
|
104 | msg = xhr.responseJSON.message; | |||
|
105 | } else { | |||
|
106 | msg = xhr.statusText; | |||
|
107 | } | |||
|
108 | dialog.modal({ | |||
|
109 | title : 'Creating Notebook Failed', | |||
|
110 | body : "The error was: " + msg, | |||
|
111 | buttons : {'OK' : {'class' : 'btn-primary'}} | |||
|
112 | }); | |||
|
113 | } | |||
|
114 | }); | |||
93 | }); |
|
115 | }); | |
94 | this.element.find('#open_notebook').click(function () { |
|
116 | this.element.find('#open_notebook').click(function () { | |
95 | window.open(utils.url_join_encode( |
|
117 | window.open(utils.url_join_encode( | |
96 | that.notebook.base_url, |
|
118 | that.notebook.base_url, | |
97 | 'tree', |
|
119 | 'tree', | |
98 | that.notebook.notebook_path |
|
120 | that.notebook.notebook_path | |
99 | )); |
|
121 | )); | |
100 | }); |
|
122 | }); | |
101 | this.element.find('#copy_notebook').click(function () { |
|
123 | this.element.find('#copy_notebook').click(function () { | |
102 | that.notebook.copy_notebook(); |
|
124 | that.notebook.copy_notebook(); | |
103 | return false; |
|
125 | return false; | |
104 | }); |
|
126 | }); | |
105 | this.element.find('#download_ipynb').click(function () { |
|
127 | this.element.find('#download_ipynb').click(function () { | |
106 | var base_url = that.notebook.base_url; |
|
128 | var base_url = that.notebook.base_url; | |
107 | var notebook_path = that.notebook.notebook_path; |
|
129 | var notebook_path = that.notebook.notebook_path; | |
108 | var notebook_name = that.notebook.notebook_name; |
|
130 | var notebook_name = that.notebook.notebook_name; | |
109 | if (that.notebook.dirty) { |
|
131 | if (that.notebook.dirty) { | |
110 | that.notebook.save_notebook({async : false}); |
|
132 | that.notebook.save_notebook({async : false}); | |
111 | } |
|
133 | } | |
112 |
|
134 | |||
113 | var url = utils.url_join_encode( |
|
135 | var url = utils.url_join_encode( | |
114 | base_url, |
|
136 | base_url, | |
115 | 'files', |
|
137 | 'files', | |
116 | notebook_path, |
|
138 | notebook_path, | |
117 | notebook_name |
|
139 | notebook_name | |
118 | ); |
|
140 | ); | |
119 | window.open(url + '?download=1'); |
|
141 | window.open(url + '?download=1'); | |
120 | }); |
|
142 | }); | |
121 |
|
143 | |||
122 | this.element.find('#print_preview').click(function () { |
|
144 | this.element.find('#print_preview').click(function () { | |
123 | that._nbconvert('html', false); |
|
145 | that._nbconvert('html', false); | |
124 | }); |
|
146 | }); | |
125 |
|
147 | |||
126 | this.element.find('#download_py').click(function () { |
|
148 | this.element.find('#download_py').click(function () { | |
127 | that._nbconvert('python', true); |
|
149 | that._nbconvert('python', true); | |
128 | }); |
|
150 | }); | |
129 |
|
151 | |||
130 | this.element.find('#download_html').click(function () { |
|
152 | this.element.find('#download_html').click(function () { | |
131 | that._nbconvert('html', true); |
|
153 | that._nbconvert('html', true); | |
132 | }); |
|
154 | }); | |
133 |
|
155 | |||
134 | this.element.find('#download_rst').click(function () { |
|
156 | this.element.find('#download_rst').click(function () { | |
135 | that._nbconvert('rst', true); |
|
157 | that._nbconvert('rst', true); | |
136 | }); |
|
158 | }); | |
137 |
|
159 | |||
138 | this.element.find('#download_pdf').click(function () { |
|
160 | this.element.find('#download_pdf').click(function () { | |
139 | that._nbconvert('pdf', true); |
|
161 | that._nbconvert('pdf', true); | |
140 | }); |
|
162 | }); | |
141 |
|
163 | |||
142 | this.element.find('#rename_notebook').click(function () { |
|
164 | this.element.find('#rename_notebook').click(function () { | |
143 | that.save_widget.rename_notebook({notebook: that.notebook}); |
|
165 | that.save_widget.rename_notebook({notebook: that.notebook}); | |
144 | }); |
|
166 | }); | |
145 | this.element.find('#save_checkpoint').click(function () { |
|
167 | this.element.find('#save_checkpoint').click(function () { | |
146 | that.notebook.save_checkpoint(); |
|
168 | that.notebook.save_checkpoint(); | |
147 | }); |
|
169 | }); | |
148 | this.element.find('#restore_checkpoint').click(function () { |
|
170 | this.element.find('#restore_checkpoint').click(function () { | |
149 | }); |
|
171 | }); | |
150 | this.element.find('#trust_notebook').click(function () { |
|
172 | this.element.find('#trust_notebook').click(function () { | |
151 | that.notebook.trust_notebook(); |
|
173 | that.notebook.trust_notebook(); | |
152 | }); |
|
174 | }); | |
153 | this.events.on('trust_changed.Notebook', function (event, trusted) { |
|
175 | this.events.on('trust_changed.Notebook', function (event, trusted) { | |
154 | if (trusted) { |
|
176 | if (trusted) { | |
155 | that.element.find('#trust_notebook') |
|
177 | that.element.find('#trust_notebook') | |
156 | .addClass("disabled") |
|
178 | .addClass("disabled") | |
157 | .find("a").text("Trusted Notebook"); |
|
179 | .find("a").text("Trusted Notebook"); | |
158 | } else { |
|
180 | } else { | |
159 | that.element.find('#trust_notebook') |
|
181 | that.element.find('#trust_notebook') | |
160 | .removeClass("disabled") |
|
182 | .removeClass("disabled") | |
161 | .find("a").text("Trust Notebook"); |
|
183 | .find("a").text("Trust Notebook"); | |
162 | } |
|
184 | } | |
163 | }); |
|
185 | }); | |
164 | this.element.find('#kill_and_exit').click(function () { |
|
186 | this.element.find('#kill_and_exit').click(function () { | |
165 | var close_window = function () { |
|
187 | var close_window = function () { | |
166 | // allow closing of new tabs in Chromium, impossible in FF |
|
188 | // allow closing of new tabs in Chromium, impossible in FF | |
167 | window.open('', '_self', ''); |
|
189 | window.open('', '_self', ''); | |
168 | window.close(); |
|
190 | window.close(); | |
169 | }; |
|
191 | }; | |
170 | // finish with close on success or failure |
|
192 | // finish with close on success or failure | |
171 | that.notebook.session.delete(close_window, close_window); |
|
193 | that.notebook.session.delete(close_window, close_window); | |
172 | }); |
|
194 | }); | |
173 | // Edit |
|
195 | // Edit | |
174 | this.element.find('#cut_cell').click(function () { |
|
196 | this.element.find('#cut_cell').click(function () { | |
175 | that.notebook.cut_cell(); |
|
197 | that.notebook.cut_cell(); | |
176 | }); |
|
198 | }); | |
177 | this.element.find('#copy_cell').click(function () { |
|
199 | this.element.find('#copy_cell').click(function () { | |
178 | that.notebook.copy_cell(); |
|
200 | that.notebook.copy_cell(); | |
179 | }); |
|
201 | }); | |
180 | this.element.find('#delete_cell').click(function () { |
|
202 | this.element.find('#delete_cell').click(function () { | |
181 | that.notebook.delete_cell(); |
|
203 | that.notebook.delete_cell(); | |
182 | }); |
|
204 | }); | |
183 | this.element.find('#undelete_cell').click(function () { |
|
205 | this.element.find('#undelete_cell').click(function () { | |
184 | that.notebook.undelete_cell(); |
|
206 | that.notebook.undelete_cell(); | |
185 | }); |
|
207 | }); | |
186 | this.element.find('#split_cell').click(function () { |
|
208 | this.element.find('#split_cell').click(function () { | |
187 | that.notebook.split_cell(); |
|
209 | that.notebook.split_cell(); | |
188 | }); |
|
210 | }); | |
189 | this.element.find('#merge_cell_above').click(function () { |
|
211 | this.element.find('#merge_cell_above').click(function () { | |
190 | that.notebook.merge_cell_above(); |
|
212 | that.notebook.merge_cell_above(); | |
191 | }); |
|
213 | }); | |
192 | this.element.find('#merge_cell_below').click(function () { |
|
214 | this.element.find('#merge_cell_below').click(function () { | |
193 | that.notebook.merge_cell_below(); |
|
215 | that.notebook.merge_cell_below(); | |
194 | }); |
|
216 | }); | |
195 | this.element.find('#move_cell_up').click(function () { |
|
217 | this.element.find('#move_cell_up').click(function () { | |
196 | that.notebook.move_cell_up(); |
|
218 | that.notebook.move_cell_up(); | |
197 | }); |
|
219 | }); | |
198 | this.element.find('#move_cell_down').click(function () { |
|
220 | this.element.find('#move_cell_down').click(function () { | |
199 | that.notebook.move_cell_down(); |
|
221 | that.notebook.move_cell_down(); | |
200 | }); |
|
222 | }); | |
201 | this.element.find('#edit_nb_metadata').click(function () { |
|
223 | this.element.find('#edit_nb_metadata').click(function () { | |
202 | that.notebook.edit_metadata({ |
|
224 | that.notebook.edit_metadata({ | |
203 | notebook: that.notebook, |
|
225 | notebook: that.notebook, | |
204 | keyboard_manager: that.notebook.keyboard_manager}); |
|
226 | keyboard_manager: that.notebook.keyboard_manager}); | |
205 | }); |
|
227 | }); | |
206 |
|
228 | |||
207 | // View |
|
229 | // View | |
208 | this.element.find('#toggle_header').click(function () { |
|
230 | this.element.find('#toggle_header').click(function () { | |
209 | $('div#header').toggle(); |
|
231 | $('div#header').toggle(); | |
210 | that.layout_manager.do_resize(); |
|
232 | that.layout_manager.do_resize(); | |
211 | }); |
|
233 | }); | |
212 | this.element.find('#toggle_toolbar').click(function () { |
|
234 | this.element.find('#toggle_toolbar').click(function () { | |
213 | $('div#maintoolbar').toggle(); |
|
235 | $('div#maintoolbar').toggle(); | |
214 | that.layout_manager.do_resize(); |
|
236 | that.layout_manager.do_resize(); | |
215 | }); |
|
237 | }); | |
216 | // Insert |
|
238 | // Insert | |
217 | this.element.find('#insert_cell_above').click(function () { |
|
239 | this.element.find('#insert_cell_above').click(function () { | |
218 | that.notebook.insert_cell_above('code'); |
|
240 | that.notebook.insert_cell_above('code'); | |
219 | that.notebook.select_prev(); |
|
241 | that.notebook.select_prev(); | |
220 | }); |
|
242 | }); | |
221 | this.element.find('#insert_cell_below').click(function () { |
|
243 | this.element.find('#insert_cell_below').click(function () { | |
222 | that.notebook.insert_cell_below('code'); |
|
244 | that.notebook.insert_cell_below('code'); | |
223 | that.notebook.select_next(); |
|
245 | that.notebook.select_next(); | |
224 | }); |
|
246 | }); | |
225 | // Cell |
|
247 | // Cell | |
226 | this.element.find('#run_cell').click(function () { |
|
248 | this.element.find('#run_cell').click(function () { | |
227 | that.notebook.execute_cell(); |
|
249 | that.notebook.execute_cell(); | |
228 | }); |
|
250 | }); | |
229 | this.element.find('#run_cell_select_below').click(function () { |
|
251 | this.element.find('#run_cell_select_below').click(function () { | |
230 | that.notebook.execute_cell_and_select_below(); |
|
252 | that.notebook.execute_cell_and_select_below(); | |
231 | }); |
|
253 | }); | |
232 | this.element.find('#run_cell_insert_below').click(function () { |
|
254 | this.element.find('#run_cell_insert_below').click(function () { | |
233 | that.notebook.execute_cell_and_insert_below(); |
|
255 | that.notebook.execute_cell_and_insert_below(); | |
234 | }); |
|
256 | }); | |
235 | this.element.find('#run_all_cells').click(function () { |
|
257 | this.element.find('#run_all_cells').click(function () { | |
236 | that.notebook.execute_all_cells(); |
|
258 | that.notebook.execute_all_cells(); | |
237 | }); |
|
259 | }); | |
238 | this.element.find('#run_all_cells_above').click(function () { |
|
260 | this.element.find('#run_all_cells_above').click(function () { | |
239 | that.notebook.execute_cells_above(); |
|
261 | that.notebook.execute_cells_above(); | |
240 | }); |
|
262 | }); | |
241 | this.element.find('#run_all_cells_below').click(function () { |
|
263 | this.element.find('#run_all_cells_below').click(function () { | |
242 | that.notebook.execute_cells_below(); |
|
264 | that.notebook.execute_cells_below(); | |
243 | }); |
|
265 | }); | |
244 | this.element.find('#to_code').click(function () { |
|
266 | this.element.find('#to_code').click(function () { | |
245 | that.notebook.to_code(); |
|
267 | that.notebook.to_code(); | |
246 | }); |
|
268 | }); | |
247 | this.element.find('#to_markdown').click(function () { |
|
269 | this.element.find('#to_markdown').click(function () { | |
248 | that.notebook.to_markdown(); |
|
270 | that.notebook.to_markdown(); | |
249 | }); |
|
271 | }); | |
250 | this.element.find('#to_raw').click(function () { |
|
272 | this.element.find('#to_raw').click(function () { | |
251 | that.notebook.to_raw(); |
|
273 | that.notebook.to_raw(); | |
252 | }); |
|
274 | }); | |
253 | this.element.find('#to_heading1').click(function () { |
|
275 | this.element.find('#to_heading1').click(function () { | |
254 | that.notebook.to_heading(undefined, 1); |
|
276 | that.notebook.to_heading(undefined, 1); | |
255 | }); |
|
277 | }); | |
256 | this.element.find('#to_heading2').click(function () { |
|
278 | this.element.find('#to_heading2').click(function () { | |
257 | that.notebook.to_heading(undefined, 2); |
|
279 | that.notebook.to_heading(undefined, 2); | |
258 | }); |
|
280 | }); | |
259 | this.element.find('#to_heading3').click(function () { |
|
281 | this.element.find('#to_heading3').click(function () { | |
260 | that.notebook.to_heading(undefined, 3); |
|
282 | that.notebook.to_heading(undefined, 3); | |
261 | }); |
|
283 | }); | |
262 | this.element.find('#to_heading4').click(function () { |
|
284 | this.element.find('#to_heading4').click(function () { | |
263 | that.notebook.to_heading(undefined, 4); |
|
285 | that.notebook.to_heading(undefined, 4); | |
264 | }); |
|
286 | }); | |
265 | this.element.find('#to_heading5').click(function () { |
|
287 | this.element.find('#to_heading5').click(function () { | |
266 | that.notebook.to_heading(undefined, 5); |
|
288 | that.notebook.to_heading(undefined, 5); | |
267 | }); |
|
289 | }); | |
268 | this.element.find('#to_heading6').click(function () { |
|
290 | this.element.find('#to_heading6').click(function () { | |
269 | that.notebook.to_heading(undefined, 6); |
|
291 | that.notebook.to_heading(undefined, 6); | |
270 | }); |
|
292 | }); | |
271 |
|
293 | |||
272 | this.element.find('#toggle_current_output').click(function () { |
|
294 | this.element.find('#toggle_current_output').click(function () { | |
273 | that.notebook.toggle_output(); |
|
295 | that.notebook.toggle_output(); | |
274 | }); |
|
296 | }); | |
275 | this.element.find('#toggle_current_output_scroll').click(function () { |
|
297 | this.element.find('#toggle_current_output_scroll').click(function () { | |
276 | that.notebook.toggle_output_scroll(); |
|
298 | that.notebook.toggle_output_scroll(); | |
277 | }); |
|
299 | }); | |
278 | this.element.find('#clear_current_output').click(function () { |
|
300 | this.element.find('#clear_current_output').click(function () { | |
279 | that.notebook.clear_output(); |
|
301 | that.notebook.clear_output(); | |
280 | }); |
|
302 | }); | |
281 |
|
303 | |||
282 | this.element.find('#toggle_all_output').click(function () { |
|
304 | this.element.find('#toggle_all_output').click(function () { | |
283 | that.notebook.toggle_all_output(); |
|
305 | that.notebook.toggle_all_output(); | |
284 | }); |
|
306 | }); | |
285 | this.element.find('#toggle_all_output_scroll').click(function () { |
|
307 | this.element.find('#toggle_all_output_scroll').click(function () { | |
286 | that.notebook.toggle_all_output_scroll(); |
|
308 | that.notebook.toggle_all_output_scroll(); | |
287 | }); |
|
309 | }); | |
288 | this.element.find('#clear_all_output').click(function () { |
|
310 | this.element.find('#clear_all_output').click(function () { | |
289 | that.notebook.clear_all_output(); |
|
311 | that.notebook.clear_all_output(); | |
290 | }); |
|
312 | }); | |
291 |
|
313 | |||
292 | // Kernel |
|
314 | // Kernel | |
293 | this.element.find('#int_kernel').click(function () { |
|
315 | this.element.find('#int_kernel').click(function () { | |
294 | that.notebook.kernel.interrupt(); |
|
316 | that.notebook.kernel.interrupt(); | |
295 | }); |
|
317 | }); | |
296 | this.element.find('#restart_kernel').click(function () { |
|
318 | this.element.find('#restart_kernel').click(function () { | |
297 | that.notebook.restart_kernel(); |
|
319 | that.notebook.restart_kernel(); | |
298 | }); |
|
320 | }); | |
299 | // Help |
|
321 | // Help | |
300 | if (this.tour) { |
|
322 | if (this.tour) { | |
301 | this.element.find('#notebook_tour').click(function () { |
|
323 | this.element.find('#notebook_tour').click(function () { | |
302 | that.tour.start(); |
|
324 | that.tour.start(); | |
303 | }); |
|
325 | }); | |
304 | } else { |
|
326 | } else { | |
305 | this.element.find('#notebook_tour').addClass("disabled"); |
|
327 | this.element.find('#notebook_tour').addClass("disabled"); | |
306 | } |
|
328 | } | |
307 | this.element.find('#keyboard_shortcuts').click(function () { |
|
329 | this.element.find('#keyboard_shortcuts').click(function () { | |
308 | that.quick_help.show_keyboard_shortcuts(); |
|
330 | that.quick_help.show_keyboard_shortcuts(); | |
309 | }); |
|
331 | }); | |
310 |
|
332 | |||
311 | this.update_restore_checkpoint(null); |
|
333 | this.update_restore_checkpoint(null); | |
312 |
|
334 | |||
313 | this.events.on('checkpoints_listed.Notebook', function (event, data) { |
|
335 | this.events.on('checkpoints_listed.Notebook', function (event, data) { | |
314 | that.update_restore_checkpoint(that.notebook.checkpoints); |
|
336 | that.update_restore_checkpoint(that.notebook.checkpoints); | |
315 | }); |
|
337 | }); | |
316 |
|
338 | |||
317 | this.events.on('checkpoint_created.Notebook', function (event, data) { |
|
339 | this.events.on('checkpoint_created.Notebook', function (event, data) { | |
318 | that.update_restore_checkpoint(that.notebook.checkpoints); |
|
340 | that.update_restore_checkpoint(that.notebook.checkpoints); | |
319 | }); |
|
341 | }); | |
320 | }; |
|
342 | }; | |
321 |
|
343 | |||
322 | MenuBar.prototype.update_restore_checkpoint = function(checkpoints) { |
|
344 | MenuBar.prototype.update_restore_checkpoint = function(checkpoints) { | |
323 | var ul = this.element.find("#restore_checkpoint").find("ul"); |
|
345 | var ul = this.element.find("#restore_checkpoint").find("ul"); | |
324 | ul.empty(); |
|
346 | ul.empty(); | |
325 | if (!checkpoints || checkpoints.length === 0) { |
|
347 | if (!checkpoints || checkpoints.length === 0) { | |
326 | ul.append( |
|
348 | ul.append( | |
327 | $("<li/>") |
|
349 | $("<li/>") | |
328 | .addClass("disabled") |
|
350 | .addClass("disabled") | |
329 | .append( |
|
351 | .append( | |
330 | $("<a/>") |
|
352 | $("<a/>") | |
331 | .text("No checkpoints") |
|
353 | .text("No checkpoints") | |
332 | ) |
|
354 | ) | |
333 | ); |
|
355 | ); | |
334 | return; |
|
356 | return; | |
335 | } |
|
357 | } | |
336 |
|
358 | |||
337 | var that = this; |
|
359 | var that = this; | |
338 | checkpoints.map(function (checkpoint) { |
|
360 | checkpoints.map(function (checkpoint) { | |
339 | var d = new Date(checkpoint.last_modified); |
|
361 | var d = new Date(checkpoint.last_modified); | |
340 | ul.append( |
|
362 | ul.append( | |
341 | $("<li/>").append( |
|
363 | $("<li/>").append( | |
342 | $("<a/>") |
|
364 | $("<a/>") | |
343 | .attr("href", "#") |
|
365 | .attr("href", "#") | |
344 | .text(moment(d).format("LLLL")) |
|
366 | .text(moment(d).format("LLLL")) | |
345 | .click(function () { |
|
367 | .click(function () { | |
346 | that.notebook.restore_checkpoint_dialog(checkpoint); |
|
368 | that.notebook.restore_checkpoint_dialog(checkpoint); | |
347 | }) |
|
369 | }) | |
348 | ) |
|
370 | ) | |
349 | ); |
|
371 | ); | |
350 | }); |
|
372 | }); | |
351 | }; |
|
373 | }; | |
352 |
|
374 | |||
353 | // Backwards compatability. |
|
375 | // Backwards compatability. | |
354 | IPython.MenuBar = MenuBar; |
|
376 | IPython.MenuBar = MenuBar; | |
355 |
|
377 | |||
356 | return {'MenuBar': MenuBar}; |
|
378 | return {'MenuBar': MenuBar}; | |
357 | }); |
|
379 | }); |
@@ -1,127 +1,149 | |||||
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 | require([ |
|
4 | require([ | |
5 | 'base/js/namespace', |
|
5 | 'base/js/namespace', | |
6 | 'jquery', |
|
6 | 'jquery', | |
7 | 'base/js/events', |
|
7 | 'base/js/events', | |
8 | 'base/js/page', |
|
8 | 'base/js/page', | |
9 | 'base/js/utils', |
|
9 | 'base/js/utils', | |
10 | 'contents', |
|
10 | 'contents', | |
11 | 'tree/js/notebooklist', |
|
11 | 'tree/js/notebooklist', | |
12 | 'tree/js/clusterlist', |
|
12 | 'tree/js/clusterlist', | |
13 | 'tree/js/sessionlist', |
|
13 | 'tree/js/sessionlist', | |
14 | 'tree/js/kernellist', |
|
14 | 'tree/js/kernellist', | |
15 | 'tree/js/terminallist', |
|
15 | 'tree/js/terminallist', | |
16 | 'auth/js/loginwidget', |
|
16 | 'auth/js/loginwidget', | |
17 | // only loaded, not used: |
|
17 | // only loaded, not used: | |
18 | 'jqueryui', |
|
18 | 'jqueryui', | |
19 | 'bootstrap', |
|
19 | 'bootstrap', | |
20 | 'custom/custom', |
|
20 | 'custom/custom', | |
21 | ], function( |
|
21 | ], function( | |
22 | IPython, |
|
22 | IPython, | |
23 | $, |
|
23 | $, | |
24 | events, |
|
24 | events, | |
25 | page, |
|
25 | page, | |
26 | utils, |
|
26 | utils, | |
27 | contents, |
|
27 | contents, | |
28 | notebooklist, |
|
28 | notebooklist, | |
29 | clusterlist, |
|
29 | clusterlist, | |
30 | sesssionlist, |
|
30 | sesssionlist, | |
31 | kernellist, |
|
31 | kernellist, | |
32 | terminallist, |
|
32 | terminallist, | |
33 | loginwidget){ |
|
33 | loginwidget){ | |
34 |
|
34 | |||
35 | page = new page.Page(); |
|
35 | page = new page.Page(); | |
36 |
|
36 | |||
37 | var common_options = { |
|
37 | var common_options = { | |
38 | base_url: utils.get_body_data("baseUrl"), |
|
38 | base_url: utils.get_body_data("baseUrl"), | |
39 | notebook_path: utils.get_body_data("notebookPath"), |
|
39 | notebook_path: utils.get_body_data("notebookPath"), | |
40 | }; |
|
40 | }; | |
41 | session_list = new sesssionlist.SesssionList($.extend({ |
|
41 | session_list = new sesssionlist.SesssionList($.extend({ | |
42 | events: events}, |
|
42 | events: events}, | |
43 | common_options)); |
|
43 | common_options)); | |
44 | contents = new contents.Contents($.extend({ |
|
44 | contents = new contents.Contents($.extend({ | |
45 | events: events}, |
|
45 | events: events}, | |
46 | common_options)); |
|
46 | common_options)); | |
47 | notebook_list = new notebooklist.NotebookList('#notebook_list', $.extend({ |
|
47 | notebook_list = new notebooklist.NotebookList('#notebook_list', $.extend({ | |
48 | contents: contents, |
|
48 | contents: contents, | |
49 | session_list: session_list}, |
|
49 | session_list: session_list}, | |
50 | common_options)); |
|
50 | common_options)); | |
51 | cluster_list = new clusterlist.ClusterList('#cluster_list', common_options); |
|
51 | cluster_list = new clusterlist.ClusterList('#cluster_list', common_options); | |
52 | kernel_list = new kernellist.KernelList('#running_list', $.extend({ |
|
52 | kernel_list = new kernellist.KernelList('#running_list', $.extend({ | |
53 | session_list: session_list}, |
|
53 | session_list: session_list}, | |
54 | common_options)); |
|
54 | common_options)); | |
55 |
|
55 | |||
56 | if (utils.get_body_data("terminalsAvailable") === "True") { |
|
56 | if (utils.get_body_data("terminalsAvailable") === "True") { | |
57 | terminal_list = new terminallist.TerminalList('#terminal_list', common_options); |
|
57 | terminal_list = new terminallist.TerminalList('#terminal_list', common_options); | |
58 | } |
|
58 | } | |
59 |
|
59 | |||
60 | login_widget = new loginwidget.LoginWidget('#login_widget', common_options); |
|
60 | login_widget = new loginwidget.LoginWidget('#login_widget', common_options); | |
61 |
|
61 | |||
62 | $('#new_notebook').button().click(function (e) { |
|
62 | $('#new_notebook').button().click(function (e) { | |
63 |
contents.new_notebook(common_options.notebook_path |
|
63 | contents.new_notebook(common_options.notebook_path, | |
|
64 | { | |||
|
65 | success_callback: function (data, status, xhr) { | |||
|
66 | window.open( | |||
|
67 | utils.url_join_encode( | |||
|
68 | common_options.base_url, 'notebooks', | |||
|
69 | data.path, data.name | |||
|
70 | ), '_blank'); | |||
|
71 | }, | |||
|
72 | error_callback: function(xhr, status, error) { | |||
|
73 | var msg; | |||
|
74 | if (xhr.responseJSON && xhr.responseJSON.message) { | |||
|
75 | msg = xhr.responseJSON.message; | |||
|
76 | } else { | |||
|
77 | msg = xhr.statusText; | |||
|
78 | } | |||
|
79 | dialog.modal({ | |||
|
80 | title : 'Creating Notebook Failed', | |||
|
81 | body : "The error was: " + msg, | |||
|
82 | buttons : {'OK' : {'class' : 'btn-primary'}} | |||
|
83 | }); | |||
|
84 | } | |||
|
85 | }); | |||
64 | }); |
|
86 | }); | |
65 |
|
87 | |||
66 | var interval_id=0; |
|
88 | var interval_id=0; | |
67 | // auto refresh every xx secondes, no need to be fast, |
|
89 | // auto refresh every xx secondes, no need to be fast, | |
68 | // update is done at least when page get focus |
|
90 | // update is done at least when page get focus | |
69 | var time_refresh = 60; // in sec |
|
91 | var time_refresh = 60; // in sec | |
70 |
|
92 | |||
71 | var enable_autorefresh = function(){ |
|
93 | var enable_autorefresh = function(){ | |
72 | //refresh immediately , then start interval |
|
94 | //refresh immediately , then start interval | |
73 | session_list.load_sessions(); |
|
95 | session_list.load_sessions(); | |
74 | cluster_list.load_list(); |
|
96 | cluster_list.load_list(); | |
75 | if (!interval_id){ |
|
97 | if (!interval_id){ | |
76 | interval_id = setInterval(function(){ |
|
98 | interval_id = setInterval(function(){ | |
77 | session_list.load_sessions(); |
|
99 | session_list.load_sessions(); | |
78 | cluster_list.load_list(); |
|
100 | cluster_list.load_list(); | |
79 | }, time_refresh*1000); |
|
101 | }, time_refresh*1000); | |
80 | } |
|
102 | } | |
81 | }; |
|
103 | }; | |
82 |
|
104 | |||
83 | var disable_autorefresh = function(){ |
|
105 | var disable_autorefresh = function(){ | |
84 | clearInterval(interval_id); |
|
106 | clearInterval(interval_id); | |
85 | interval_id = 0; |
|
107 | interval_id = 0; | |
86 | }; |
|
108 | }; | |
87 |
|
109 | |||
88 | // stop autorefresh when page lose focus |
|
110 | // stop autorefresh when page lose focus | |
89 | $(window).blur(function() { |
|
111 | $(window).blur(function() { | |
90 | disable_autorefresh(); |
|
112 | disable_autorefresh(); | |
91 | }); |
|
113 | }); | |
92 |
|
114 | |||
93 | //re-enable when page get focus back |
|
115 | //re-enable when page get focus back | |
94 | $(window).focus(function() { |
|
116 | $(window).focus(function() { | |
95 | enable_autorefresh(); |
|
117 | enable_autorefresh(); | |
96 | }); |
|
118 | }); | |
97 |
|
119 | |||
98 | // finally start it, it will refresh immediately |
|
120 | // finally start it, it will refresh immediately | |
99 | enable_autorefresh(); |
|
121 | enable_autorefresh(); | |
100 |
|
122 | |||
101 | page.show(); |
|
123 | page.show(); | |
102 |
|
124 | |||
103 | // For backwards compatability. |
|
125 | // For backwards compatability. | |
104 | IPython.page = page; |
|
126 | IPython.page = page; | |
105 | IPython.notebook_list = notebook_list; |
|
127 | IPython.notebook_list = notebook_list; | |
106 | IPython.cluster_list = cluster_list; |
|
128 | IPython.cluster_list = cluster_list; | |
107 | IPython.session_list = session_list; |
|
129 | IPython.session_list = session_list; | |
108 | IPython.kernel_list = kernel_list; |
|
130 | IPython.kernel_list = kernel_list; | |
109 | IPython.login_widget = login_widget; |
|
131 | IPython.login_widget = login_widget; | |
110 |
|
132 | |||
111 | events.trigger('app_initialized.DashboardApp'); |
|
133 | events.trigger('app_initialized.DashboardApp'); | |
112 |
|
134 | |||
113 | // bound the upload method to the on change of the file select list |
|
135 | // bound the upload method to the on change of the file select list | |
114 | $("#alternate_upload").change(function (event){ |
|
136 | $("#alternate_upload").change(function (event){ | |
115 | notebook_list.handleFilesUpload(event,'form'); |
|
137 | notebook_list.handleFilesUpload(event,'form'); | |
116 | }); |
|
138 | }); | |
117 |
|
139 | |||
118 | // set hash on tab click |
|
140 | // set hash on tab click | |
119 | $("#tabs").find("a").click(function() { |
|
141 | $("#tabs").find("a").click(function() { | |
120 | window.location.hash = $(this).attr("href"); |
|
142 | window.location.hash = $(this).attr("href"); | |
121 | }); |
|
143 | }); | |
122 |
|
144 | |||
123 | // load tab if url hash |
|
145 | // load tab if url hash | |
124 | if (window.location.hash) { |
|
146 | if (window.location.hash) { | |
125 | $("#tabs").find("a[href=" + window.location.hash + "]").click(); |
|
147 | $("#tabs").find("a[href=" + window.location.hash + "]").click(); | |
126 | } |
|
148 | } | |
127 | }); |
|
149 | }); |
General Comments 0
You need to be logged in to leave comments.
Login now