Show More
@@ -1,289 +1,289 b'' | |||||
1 | // Copyright (c) IPython Development Team. |
|
1 | // Copyright (c) IPython Development Team. | |
2 | // Distributed under the terms of the Modified BSD License. |
|
2 | // Distributed under the terms of the Modified BSD License. | |
3 |
|
3 | |||
4 | define([ |
|
4 | define([ | |
5 | 'base/js/namespace', |
|
5 | 'base/js/namespace', | |
6 | 'jquery', |
|
6 | 'jquery', | |
7 | 'base/js/utils', |
|
7 | 'base/js/utils', | |
8 | 'base/js/dialog', |
|
8 | '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) { | |
72 | var base_url = this.base_url; |
|
72 | var base_url = this.base_url; | |
73 | var settings = { |
|
73 | var settings = { | |
74 | processData : false, |
|
74 | processData : false, | |
75 | cache : false, |
|
75 | cache : false, | |
76 | type : "POST", |
|
76 | type : "POST", | |
77 | dataType : "json", |
|
77 | dataType : "json", | |
78 | async : false, |
|
78 | async : false, | |
79 | success : function (data, status, xhr){ |
|
79 | success : function (data, status, xhr){ | |
80 | var notebook_name = data.name; |
|
80 | var notebook_name = data.name; | |
81 | window.open( |
|
81 | window.open( | |
82 | utils.url_join_encode( |
|
82 | utils.url_join_encode( | |
83 | base_url, |
|
83 | base_url, | |
84 | 'contents', |
|
84 | 'contents', | |
85 | path, |
|
85 | path, | |
86 | notebook_name |
|
86 | notebook_name | |
87 | ), |
|
87 | ), | |
88 | '_blank' |
|
88 | '_blank' | |
89 | ); |
|
89 | ); | |
90 | }, |
|
90 | }, | |
91 | error : function(xhr, status, error) { |
|
91 | error : function(xhr, status, error) { | |
92 | utils.log_ajax_error(xhr, status, error); |
|
92 | utils.log_ajax_error(xhr, status, error); | |
93 | var msg; |
|
93 | var msg; | |
94 | if (xhr.responseJSON && xhr.responseJSON.message) { |
|
94 | if (xhr.responseJSON && xhr.responseJSON.message) { | |
95 | msg = xhr.responseJSON.message; |
|
95 | msg = xhr.responseJSON.message; | |
96 | } else { |
|
96 | } else { | |
97 | msg = xhr.statusText; |
|
97 | msg = xhr.statusText; | |
98 | } |
|
98 | } | |
99 | dialog.modal({ |
|
99 | dialog.modal({ | |
100 | title : 'Creating Notebook Failed', |
|
100 | title : 'Creating Notebook Failed', | |
101 | body : "The error was: " + msg, |
|
101 | body : "The error was: " + msg, | |
102 | buttons : {'OK' : {'class' : 'btn-primary'}} |
|
102 | buttons : {'OK' : {'class' : 'btn-primary'}} | |
103 | }); |
|
103 | }); | |
104 | } |
|
104 | } | |
105 | }; |
|
105 | }; | |
106 | var url = utils.url_join_encode( |
|
106 | var url = utils.url_join_encode( | |
107 | base_url, |
|
107 | base_url, | |
108 | 'api/contents', |
|
108 | 'api/contents', | |
109 | path |
|
109 | path | |
110 | ); |
|
110 | ); | |
111 | $.ajax(url,settings); |
|
111 | $.ajax(url,settings); | |
112 | }; |
|
112 | }; | |
113 |
|
113 | |||
114 | Contents.prototype.delete_notebook = function(name, path) { |
|
114 | Contents.prototype.delete_notebook = function(name, path) { | |
115 | var settings = { |
|
115 | var settings = { | |
116 | processData : false, |
|
116 | processData : false, | |
117 | cache : false, |
|
117 | cache : false, | |
118 | type : "DELETE", |
|
118 | type : "DELETE", | |
119 | dataType : "json", |
|
119 | dataType : "json", | |
120 | success : $.proxy(this.events.trigger, this.events, |
|
120 | success : $.proxy(this.events.trigger, this.events, | |
121 | 'notebook_deleted.Contents', |
|
121 | 'notebook_deleted.Contents', | |
122 | { |
|
122 | { | |
123 | name: name, |
|
123 | name: name, | |
124 | path: path |
|
124 | path: path | |
125 | }), |
|
125 | }), | |
126 | error : utils.log_ajax_error |
|
126 | error : utils.log_ajax_error | |
127 | }; |
|
127 | }; | |
128 | var url = utils.url_join_encode( |
|
128 | var url = utils.url_join_encode( | |
129 | this.base_url, |
|
129 | this.base_url, | |
130 | 'api/contents', |
|
130 | 'api/contents', | |
131 | path, |
|
131 | path, | |
132 | name |
|
132 | name | |
133 | ); |
|
133 | ); | |
134 | $.ajax(url, settings); |
|
134 | $.ajax(url, settings); | |
135 | }; |
|
135 | }; | |
136 |
|
136 | |||
137 | Contents.prototype.rename_notebook = function(path, name, new_name) { |
|
137 | Contents.prototype.rename_notebook = function(path, name, new_name) { | |
138 | var that = this; |
|
138 | var that = this; | |
139 | var data = {name: new_name}; |
|
139 | var data = {name: new_name}; | |
140 | var settings = { |
|
140 | var settings = { | |
141 | processData : false, |
|
141 | processData : false, | |
142 | cache : false, |
|
142 | cache : false, | |
143 | type : "PATCH", |
|
143 | type : "PATCH", | |
144 | data : JSON.stringify(data), |
|
144 | data : JSON.stringify(data), | |
145 | dataType: "json", |
|
145 | dataType: "json", | |
146 | contentType: 'application/json', |
|
146 | contentType: 'application/json', | |
147 | success : function (json, status, xhr) { |
|
147 | success : function (json, status, xhr) { | |
148 | that.events.trigger('notebook_rename_success.Contents', |
|
148 | that.events.trigger('notebook_rename_success.Contents', | |
149 | json); |
|
149 | json); | |
150 | }, |
|
150 | }, | |
151 | error : function (xhr, status, error) { |
|
151 | error : function (xhr, status, error) { | |
152 | that.events.trigger('notebook_rename_error.Contents', |
|
152 | that.events.trigger('notebook_rename_error.Contents', | |
153 | [xhr, status, error]); |
|
153 | [xhr, status, error]); | |
154 | } |
|
154 | } | |
155 | }; |
|
155 | }; | |
156 | var url = utils.url_join_encode( |
|
156 | var url = utils.url_join_encode( | |
157 | this.base_url, |
|
157 | this.base_url, | |
158 | 'api/contents', |
|
158 | 'api/contents', | |
159 | path, |
|
159 | path, | |
160 | name |
|
160 | name | |
161 | ); |
|
161 | ); | |
162 | $.ajax(url, settings); |
|
162 | $.ajax(url, settings); | |
163 | }; |
|
163 | }; | |
164 |
|
164 | |||
165 | Contents.prototype.save_notebook = function(path, name, content, |
|
165 | Contents.prototype.save_notebook = function(path, name, content, | |
166 | extra_settings) { |
|
166 | extra_settings) { | |
167 | var that = content; |
|
167 | var that = content; | |
168 | // Create a JSON model to be sent to the server. |
|
168 | // Create a JSON model to be sent to the server. | |
169 | var model = { |
|
169 | var model = { | |
170 | name : name, |
|
170 | name : name, | |
171 | path : path, |
|
171 | path : path, | |
172 | type : "notebook", |
|
172 | type : "notebook", | |
173 | content : content |
|
173 | content : content | |
174 | }; |
|
174 | }; | |
175 | // time the ajax call for autosave tuning purposes. |
|
175 | // time the ajax call for autosave tuning purposes. | |
176 | var start = new Date().getTime(); |
|
176 | var start = new Date().getTime(); | |
177 | // We do the call with settings so we can set cache to false. |
|
177 | // We do the call with settings so we can set cache to false. | |
178 | var settings = { |
|
178 | var settings = { | |
179 | processData : false, |
|
179 | processData : false, | |
180 | cache : false, |
|
180 | cache : false, | |
181 | type : "PUT", |
|
181 | type : "PUT", | |
182 | data : JSON.stringify(model), |
|
182 | data : JSON.stringify(model), | |
183 | contentType: 'application/json', |
|
183 | contentType: 'application/json', | |
184 | success : $.proxy(this.events.trigger, this.events, |
|
184 | success : $.proxy(this.events.trigger, this.events, | |
185 | 'notebook_save_success.Contents', |
|
185 | 'notebook_save_success.Contents', | |
186 | $.extend(model, { start : start })), |
|
186 | $.extend(model, { start : start })), | |
187 | error : function (xhr, status, error) { |
|
187 | error : function (xhr, status, error) { | |
188 | that.events.trigger('notebook_save_error.Contents', |
|
188 | that.events.trigger('notebook_save_error.Contents', | |
189 | [xhr, status, error, model]); |
|
189 | [xhr, status, error, model]); | |
190 | } |
|
190 | } | |
191 | }; |
|
191 | }; | |
192 | if (extra_settings) { |
|
192 | if (extra_settings) { | |
193 | for (var key in extra_settings) { |
|
193 | for (var key in extra_settings) { | |
194 | settings[key] = extra_settings[key]; |
|
194 | settings[key] = extra_settings[key]; | |
195 | } |
|
195 | } | |
196 | } |
|
196 | } | |
197 | var url = utils.url_join_encode( |
|
197 | var url = utils.url_join_encode( | |
198 | this.base_url, |
|
198 | this.base_url, | |
199 | 'api/contents', |
|
199 | 'api/contents', | |
200 | path, |
|
200 | path, | |
201 | name |
|
201 | name | |
202 | ); |
|
202 | ); | |
203 | $.ajax(url, settings); |
|
203 | $.ajax(url, settings); | |
204 | }; |
|
204 | }; | |
205 |
|
205 | |||
206 | /** |
|
206 | /** | |
207 | * Checkpointing Functions |
|
207 | * Checkpointing Functions | |
208 | */ |
|
208 | */ | |
209 |
|
209 | |||
210 | Contents.prototype.save_checkpoint = function() { |
|
210 | Contents.prototype.save_checkpoint = function() { | |
211 | // This is not necessary - integrated into save |
|
211 | // This is not necessary - integrated into save | |
212 | }; |
|
212 | }; | |
213 |
|
213 | |||
214 | Contents.prototype.restore_checkpoint = function(notebook, id) { |
|
214 | Contents.prototype.restore_checkpoint = function(notebook, id) { | |
215 | that = notebook; |
|
215 | that = notebook; | |
216 | this.events.trigger('notebook_restoring.Notebook', checkpoint); |
|
216 | this.events.trigger('notebook_restoring.Notebook', checkpoint); | |
217 | var url = utils.url_join_encode( |
|
217 | var url = utils.url_join_encode( | |
218 | this.base_url, |
|
218 | this.base_url, | |
219 | 'api/contents', |
|
219 | 'api/contents', | |
220 | this.notebook_path, |
|
220 | this.notebook_path, | |
221 | this.notebook_name, |
|
221 | this.notebook_name, | |
222 | 'checkpoints', |
|
222 | 'checkpoints', | |
223 | checkpoint |
|
223 | checkpoint | |
224 | ); |
|
224 | ); | |
225 | $.post(url).done( |
|
225 | $.post(url).done( | |
226 | $.proxy(that.restore_checkpoint_success, that) |
|
226 | $.proxy(that.restore_checkpoint_success, that) | |
227 | ).fail( |
|
227 | ).fail( | |
228 | $.proxy(that.restore_checkpoint_error, that) |
|
228 | $.proxy(that.restore_checkpoint_error, that) | |
229 | ); |
|
229 | ); | |
230 | }; |
|
230 | }; | |
231 |
|
231 | |||
232 | Contents.prototype.list_checkpoints = function(notebook) { |
|
232 | Contents.prototype.list_checkpoints = function(notebook) { | |
233 | that = notebook; |
|
233 | that = notebook; | |
234 | var url = utils.url_join_encode( |
|
234 | var url = utils.url_join_encode( | |
235 | that.base_url, |
|
235 | that.base_url, | |
236 | 'api/contents', |
|
236 | 'api/contents', | |
237 | that.notebook_path, |
|
237 | that.notebook_path, | |
238 | that.notebook_name, |
|
238 | that.notebook_name, | |
239 | 'checkpoints' |
|
239 | 'checkpoints' | |
240 | ); |
|
240 | ); | |
241 | $.get(url).done( |
|
241 | $.get(url).done( | |
242 | $.proxy(that.list_checkpoints_success, that) |
|
242 | $.proxy(that.list_checkpoints_success, that) | |
243 | ).fail( |
|
243 | ).fail( | |
244 | $.proxy(that.list_checkpoints_error, that) |
|
244 | $.proxy(that.list_checkpoints_error, that) | |
245 | ); |
|
245 | ); | |
246 | }; |
|
246 | }; | |
247 |
|
247 | |||
248 | /** |
|
248 | /** | |
249 | * File management functions |
|
249 | * File management functions | |
250 | */ |
|
250 | */ | |
251 |
|
251 | |||
252 | /** |
|
252 | /** | |
253 | * List notebooks and directories at a given path |
|
253 | * List notebooks and directories at a given path | |
254 | * |
|
254 | * | |
255 | * On success, load_callback is called with an array of dictionaries |
|
255 | * On success, load_callback is called with an array of dictionaries | |
256 | * representing individual files or directories. Each dictionary has |
|
256 | * representing individual files or directories. Each dictionary has | |
257 | * the keys: |
|
257 | * the keys: | |
258 | * type: "notebook" or "directory" |
|
258 | * type: "notebook" or "directory" | |
259 | * name: the name of the file or directory |
|
259 | * name: the name of the file or directory | |
260 | * created: created date |
|
260 | * created: created date | |
261 | * last_modified: last modified dat |
|
261 | * last_modified: last modified dat | |
262 | * path: the path |
|
262 | * path: the path | |
263 | * @method list_notebooks |
|
263 | * @method list_notebooks | |
264 | * @param {String} path The path to list notebooks in |
|
264 | * @param {String} path The path to list notebooks in | |
265 | * @param {Function} load_callback called with list of notebooks on success |
|
265 | * @param {Function} load_callback called with list of notebooks on success | |
266 | * @param {Function} error_callback called with ajax results on error |
|
266 | * @param {Function} error_callback called with ajax results on error | |
267 | */ |
|
267 | */ | |
268 | Contents.prototype.list_contents = function(path, load_callback, |
|
268 | Contents.prototype.list_contents = function(path, load_callback, | |
269 | error_callback) { |
|
269 | error_callback) { | |
270 | var that = this; |
|
270 | var that = this; | |
271 | var settings = { |
|
271 | var settings = { | |
272 | processData : false, |
|
272 | processData : false, | |
273 | cache : false, |
|
273 | cache : false, | |
274 | type : "GET", |
|
274 | type : "GET", | |
275 | dataType : "json", |
|
275 | dataType : "json", | |
276 | success : load_callback, |
|
276 | success : load_callback, | |
277 | error : error_callback |
|
277 | error : error_callback | |
278 | }; |
|
278 | }; | |
279 |
|
279 | |||
280 | var url = utils.url_join_encode(this.base_url, 'api', 'contents', |
|
280 | var url = utils.url_join_encode(this.base_url, 'api', 'contents', | |
281 | path); |
|
281 | path); | |
282 | $.ajax(url, settings); |
|
282 | $.ajax(url, settings); | |
283 | } |
|
283 | }; | |
284 |
|
284 | |||
285 |
|
285 | |||
286 | IPython.Contents = Contents; |
|
286 | IPython.Contents = Contents; | |
287 |
|
287 | |||
288 | return {'Contents': Contents}; |
|
288 | return {'Contents': Contents}; | |
289 | }); |
|
289 | }); |
General Comments 0
You need to be logged in to leave comments.
Login now