Show More
@@ -1,273 +1,270 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 | ], function(IPython, $, utils) { |
|
8 | ], function(IPython, $, utils) { | |
9 | var Contents = function(options) { |
|
9 | var Contents = function(options) { | |
10 | // Constructor |
|
10 | // Constructor | |
11 | // |
|
11 | // | |
12 | // A contents handles passing file operations |
|
12 | // A contents handles passing file operations | |
13 | // to the back-end. This includes checkpointing |
|
13 | // to the back-end. This includes checkpointing | |
14 | // with the normal file operations. |
|
14 | // with the normal file operations. | |
15 | // |
|
15 | // | |
16 | // Parameters: |
|
16 | // Parameters: | |
17 | // options: dictionary |
|
17 | // options: dictionary | |
18 | // Dictionary of keyword arguments. |
|
18 | // Dictionary of keyword arguments. | |
19 | // base_url: string |
|
19 | // base_url: string | |
20 | this.base_url = options.base_url; |
|
20 | this.base_url = options.base_url; | |
21 | }; |
|
21 | }; | |
22 |
|
22 | |||
23 | /** Error type */ |
|
23 | /** Error type */ | |
24 | Contents.DIRECTORY_NOT_EMPTY_ERROR = 'DirectoryNotEmptyError'; |
|
24 | Contents.DIRECTORY_NOT_EMPTY_ERROR = 'DirectoryNotEmptyError'; | |
25 |
|
25 | |||
26 | Contents.DirectoryNotEmptyError = function() { |
|
26 | Contents.DirectoryNotEmptyError = function() { | |
27 | // Constructor |
|
27 | // Constructor | |
28 | // |
|
28 | // | |
29 | // An error representing the result of attempting to delete a non-empty |
|
29 | // An error representing the result of attempting to delete a non-empty | |
30 | // directory. |
|
30 | // directory. | |
31 | this.message = 'A directory must be empty before being deleted.'; |
|
31 | this.message = 'A directory must be empty before being deleted.'; | |
32 | }; |
|
32 | }; | |
33 |
|
33 | |||
34 | Contents.DirectoryNotEmptyError.prototype = Object.create(Error.prototype); |
|
34 | Contents.DirectoryNotEmptyError.prototype = Object.create(Error.prototype); | |
35 | Contents.DirectoryNotEmptyError.prototype.name = |
|
35 | Contents.DirectoryNotEmptyError.prototype.name = | |
36 | Contents.DIRECTORY_NOT_EMPTY_ERROR; |
|
36 | Contents.DIRECTORY_NOT_EMPTY_ERROR; | |
37 |
|
37 | |||
38 |
|
38 | |||
39 | Contents.prototype.api_url = function() { |
|
39 | Contents.prototype.api_url = function() { | |
40 | var url_parts = [this.base_url, 'api/contents'].concat( |
|
40 | var url_parts = [this.base_url, 'api/contents'].concat( | |
41 | Array.prototype.slice.apply(arguments)); |
|
41 | Array.prototype.slice.apply(arguments)); | |
42 | return utils.url_join_encode.apply(null, url_parts); |
|
42 | return utils.url_join_encode.apply(null, url_parts); | |
43 | }; |
|
43 | }; | |
44 |
|
44 | |||
45 | /** |
|
45 | /** | |
46 | * Creates a basic error handler that wraps a jqXHR error as an Error. |
|
46 | * Creates a basic error handler that wraps a jqXHR error as an Error. | |
47 | * |
|
47 | * | |
48 | * Takes a callback that accepts an Error, and returns a callback that can |
|
48 | * Takes a callback that accepts an Error, and returns a callback that can | |
49 | * be passed directly to $.ajax, which will wrap the error from jQuery |
|
49 | * be passed directly to $.ajax, which will wrap the error from jQuery | |
50 | * as an Error, and pass that to the original callback. |
|
50 | * as an Error, and pass that to the original callback. | |
51 | * |
|
51 | * | |
52 | * @method create_basic_error_handler |
|
52 | * @method create_basic_error_handler | |
53 | * @param{Function} callback |
|
53 | * @param{Function} callback | |
54 | * @return{Function} |
|
54 | * @return{Function} | |
55 | */ |
|
55 | */ | |
56 | Contents.prototype.create_basic_error_handler = function(callback) { |
|
56 | Contents.prototype.create_basic_error_handler = function(callback) { | |
57 | if (!callback) { |
|
57 | if (!callback) { | |
58 | return utils.log_ajax_error; |
|
58 | return utils.log_ajax_error; | |
59 | } |
|
59 | } | |
60 | return function(xhr, status, error) { |
|
60 | return function(xhr, status, error) { | |
61 | callback(utils.wrap_ajax_error(xhr, status, error)); |
|
61 | callback(utils.wrap_ajax_error(xhr, status, error)); | |
62 | }; |
|
62 | }; | |
63 | }; |
|
63 | }; | |
64 |
|
64 | |||
65 | /** |
|
65 | /** | |
66 | * File Functions (including notebook operations) |
|
66 | * File Functions (including notebook operations) | |
67 | */ |
|
67 | */ | |
68 |
|
68 | |||
69 | /** |
|
69 | /** | |
70 | * Get a file. |
|
70 | * Get a file. | |
71 | * |
|
71 | * | |
72 | * Calls success with file JSON model, or error with error. |
|
72 | * Calls success with file JSON model, or error with error. | |
73 | * |
|
73 | * | |
74 | * @method get |
|
74 | * @method get | |
75 | * @param {String} path |
|
75 | * @param {String} path | |
76 | * @param {Function} success |
|
76 | * @param {Function} success | |
77 | * @param {Function} error |
|
77 | * @param {Function} error | |
78 | */ |
|
78 | */ | |
79 | Contents.prototype.get = function (path, options) { |
|
79 | Contents.prototype.get = function (path, options) { | |
80 | // We do the call with settings so we can set cache to false. |
|
80 | // We do the call with settings so we can set cache to false. | |
81 | var settings = { |
|
81 | var settings = { | |
82 | processData : false, |
|
82 | processData : false, | |
83 | cache : false, |
|
83 | cache : false, | |
84 | type : "GET", |
|
84 | type : "GET", | |
85 | dataType : "json", |
|
85 | dataType : "json", | |
86 | success : options.success, |
|
86 | success : options.success, | |
87 | error : this.create_basic_error_handler(options.error) |
|
87 | error : this.create_basic_error_handler(options.error) | |
88 | }; |
|
88 | }; | |
89 | var url = this.api_url(path); |
|
89 | var url = this.api_url(path); | |
90 | $.ajax(url, settings); |
|
90 | $.ajax(url, settings); | |
91 | }; |
|
91 | }; | |
92 |
|
92 | |||
93 |
|
93 | |||
94 | /** |
|
94 | /** | |
95 | * Creates a new untitled file or directory in the specified directory path. |
|
95 | * Creates a new untitled file or directory in the specified directory path. | |
96 | * |
|
96 | * | |
97 | * @method new |
|
97 | * @method new | |
98 | * @param {String} path: the directory in which to create the new file/directory |
|
98 | * @param {String} path: the directory in which to create the new file/directory | |
99 | * @param {Object} options: |
|
99 | * @param {Object} options: | |
100 | * ext: file extension to use |
|
100 | * ext: file extension to use | |
101 | * type: model type to create ('notebook', 'file', or 'directory') |
|
101 | * type: model type to create ('notebook', 'file', or 'directory') | |
102 | */ |
|
102 | */ | |
103 | Contents.prototype.new_untitled = function(path, options) { |
|
103 | Contents.prototype.new_untitled = function(path, options) { | |
104 | var data = JSON.stringify({ |
|
104 | var data = JSON.stringify({ | |
105 | ext: options.ext, |
|
105 | ext: options.ext, | |
106 | type: options.type |
|
106 | type: options.type | |
107 | }); |
|
107 | }); | |
108 |
|
108 | |||
109 | var settings = { |
|
109 | var settings = { | |
110 | processData : false, |
|
110 | processData : false, | |
111 | type : "POST", |
|
111 | type : "POST", | |
112 | data: data, |
|
112 | data: data, | |
113 | dataType : "json", |
|
113 | dataType : "json", | |
114 | success : options.success || function() {}, |
|
114 | success : options.success || function() {}, | |
115 | error : this.create_basic_error_handler(options.error) |
|
115 | error : this.create_basic_error_handler(options.error) | |
116 | }; |
|
116 | }; | |
117 | if (options.extra_settings) { |
|
|||
118 | $.extend(settings, options.extra_settings); |
|
|||
119 | } |
|
|||
120 | $.ajax(this.api_url(path), settings); |
|
117 | $.ajax(this.api_url(path), settings); | |
121 | }; |
|
118 | }; | |
122 |
|
119 | |||
123 | Contents.prototype.delete = function(path, options) { |
|
120 | Contents.prototype.delete = function(path, options) { | |
124 | var error_callback = options.error || function() {}; |
|
121 | var error_callback = options.error || function() {}; | |
125 | var settings = { |
|
122 | var settings = { | |
126 | processData : false, |
|
123 | processData : false, | |
127 | type : "DELETE", |
|
124 | type : "DELETE", | |
128 | dataType : "json", |
|
125 | dataType : "json", | |
129 | success : options.success || function() {}, |
|
126 | success : options.success || function() {}, | |
130 | error : function(xhr, status, error) { |
|
127 | error : function(xhr, status, error) { | |
131 | // TODO: update IPEP27 to specify errors more precisely, so |
|
128 | // TODO: update IPEP27 to specify errors more precisely, so | |
132 | // that error types can be detected here with certainty. |
|
129 | // that error types can be detected here with certainty. | |
133 | if (xhr.status === 400) { |
|
130 | if (xhr.status === 400) { | |
134 | error_callback(new Contents.DirectoryNotEmptyError()); |
|
131 | error_callback(new Contents.DirectoryNotEmptyError()); | |
135 | } |
|
132 | } | |
136 | error_callback(utils.wrap_ajax_error(xhr, status, error)); |
|
133 | error_callback(utils.wrap_ajax_error(xhr, status, error)); | |
137 | } |
|
134 | } | |
138 | }; |
|
135 | }; | |
139 | var url = this.api_url(path); |
|
136 | var url = this.api_url(path); | |
140 | $.ajax(url, settings); |
|
137 | $.ajax(url, settings); | |
141 | }; |
|
138 | }; | |
142 |
|
139 | |||
143 | Contents.prototype.rename = function(path, new_path, options) { |
|
140 | Contents.prototype.rename = function(path, new_path, options) { | |
144 | var data = {path: new_path}; |
|
141 | var data = {path: new_path}; | |
145 | var settings = { |
|
142 | var settings = { | |
146 | processData : false, |
|
143 | processData : false, | |
147 | type : "PATCH", |
|
144 | type : "PATCH", | |
148 | data : JSON.stringify(data), |
|
145 | data : JSON.stringify(data), | |
149 | dataType: "json", |
|
146 | dataType: "json", | |
150 | contentType: 'application/json', |
|
147 | contentType: 'application/json', | |
151 | success : options.success || function() {}, |
|
148 | success : options.success || function() {}, | |
152 | error : this.create_basic_error_handler(options.error) |
|
149 | error : this.create_basic_error_handler(options.error) | |
153 | }; |
|
150 | }; | |
154 | var url = this.api_url(path); |
|
151 | var url = this.api_url(path); | |
155 | $.ajax(url, settings); |
|
152 | $.ajax(url, settings); | |
156 | }; |
|
153 | }; | |
157 |
|
154 | |||
158 | Contents.prototype.save = function(path, model, options) { |
|
155 | Contents.prototype.save = function(path, model, options) { | |
159 | // We do the call with settings so we can set cache to false. |
|
156 | // We do the call with settings so we can set cache to false. | |
160 | var settings = { |
|
157 | var settings = { | |
161 | processData : false, |
|
158 | processData : false, | |
162 | type : "PUT", |
|
159 | type : "PUT", | |
163 | data : JSON.stringify(model), |
|
160 | data : JSON.stringify(model), | |
164 | contentType: 'application/json', |
|
161 | contentType: 'application/json', | |
165 | success : options.success || function() {}, |
|
162 | success : options.success || function() {}, | |
166 | error : this.create_basic_error_handler(options.error) |
|
163 | error : this.create_basic_error_handler(options.error) | |
167 | }; |
|
164 | }; | |
168 | if (options.extra_settings) { |
|
165 | if (options.extra_settings) { | |
169 | $.extend(settings, options.extra_settings); |
|
166 | $.extend(settings, options.extra_settings); | |
170 | } |
|
167 | } | |
171 | var url = this.api_url(path); |
|
168 | var url = this.api_url(path); | |
172 | $.ajax(url, settings); |
|
169 | $.ajax(url, settings); | |
173 | }; |
|
170 | }; | |
174 |
|
171 | |||
175 | Contents.prototype.copy = function(from_file, to_dir, options) { |
|
172 | Contents.prototype.copy = function(from_file, to_dir, options) { | |
176 | // Copy a file into a given directory via POST |
|
173 | // Copy a file into a given directory via POST | |
177 | // The server will select the name of the copied file |
|
174 | // The server will select the name of the copied file | |
178 | var url = this.api_url(to_dir); |
|
175 | var url = this.api_url(to_dir); | |
179 |
|
176 | |||
180 | var settings = { |
|
177 | var settings = { | |
181 | processData : false, |
|
178 | processData : false, | |
182 | type: "POST", |
|
179 | type: "POST", | |
183 | data: JSON.stringify({copy_from: from_file}), |
|
180 | data: JSON.stringify({copy_from: from_file}), | |
184 | dataType : "json", |
|
181 | dataType : "json", | |
185 | success: options.success || function() {}, |
|
182 | success: options.success || function() {}, | |
186 | error: this.create_basic_error_handler(options.error) |
|
183 | error: this.create_basic_error_handler(options.error) | |
187 | }; |
|
184 | }; | |
188 | if (options.extra_settings) { |
|
185 | if (options.extra_settings) { | |
189 | $.extend(settings, options.extra_settings); |
|
186 | $.extend(settings, options.extra_settings); | |
190 | } |
|
187 | } | |
191 | $.ajax(url, settings); |
|
188 | $.ajax(url, settings); | |
192 | }; |
|
189 | }; | |
193 |
|
190 | |||
194 | /** |
|
191 | /** | |
195 | * Checkpointing Functions |
|
192 | * Checkpointing Functions | |
196 | */ |
|
193 | */ | |
197 |
|
194 | |||
198 | Contents.prototype.create_checkpoint = function(path, options) { |
|
195 | Contents.prototype.create_checkpoint = function(path, options) { | |
199 | var url = this.api_url(path, 'checkpoints'); |
|
196 | var url = this.api_url(path, 'checkpoints'); | |
200 | var settings = { |
|
197 | var settings = { | |
201 | type : "POST", |
|
198 | type : "POST", | |
202 | success: options.success || function() {}, |
|
199 | success: options.success || function() {}, | |
203 | error : this.create_basic_error_handler(options.error) |
|
200 | error : this.create_basic_error_handler(options.error) | |
204 | }; |
|
201 | }; | |
205 | $.ajax(url, settings); |
|
202 | $.ajax(url, settings); | |
206 | }; |
|
203 | }; | |
207 |
|
204 | |||
208 | Contents.prototype.list_checkpoints = function(path, options) { |
|
205 | Contents.prototype.list_checkpoints = function(path, options) { | |
209 | var url = this.api_url(path, 'checkpoints'); |
|
206 | var url = this.api_url(path, 'checkpoints'); | |
210 | var settings = { |
|
207 | var settings = { | |
211 | type : "GET", |
|
208 | type : "GET", | |
212 | success: options.success, |
|
209 | success: options.success, | |
213 | error : this.create_basic_error_handler(options.error) |
|
210 | error : this.create_basic_error_handler(options.error) | |
214 | }; |
|
211 | }; | |
215 | $.ajax(url, settings); |
|
212 | $.ajax(url, settings); | |
216 | }; |
|
213 | }; | |
217 |
|
214 | |||
218 | Contents.prototype.restore_checkpoint = function(path, checkpoint_id, options) { |
|
215 | Contents.prototype.restore_checkpoint = function(path, checkpoint_id, options) { | |
219 | var url = this.api_url(path, 'checkpoints', checkpoint_id); |
|
216 | var url = this.api_url(path, 'checkpoints', checkpoint_id); | |
220 | var settings = { |
|
217 | var settings = { | |
221 | type : "POST", |
|
218 | type : "POST", | |
222 | success: options.success || function() {}, |
|
219 | success: options.success || function() {}, | |
223 | error : this.create_basic_error_handler(options.error) |
|
220 | error : this.create_basic_error_handler(options.error) | |
224 | }; |
|
221 | }; | |
225 | $.ajax(url, settings); |
|
222 | $.ajax(url, settings); | |
226 | }; |
|
223 | }; | |
227 |
|
224 | |||
228 | Contents.prototype.delete_checkpoint = function(path, checkpoint_id, options) { |
|
225 | Contents.prototype.delete_checkpoint = function(path, checkpoint_id, options) { | |
229 | var url = this.api_url(path, 'checkpoints', checkpoint_id); |
|
226 | var url = this.api_url(path, 'checkpoints', checkpoint_id); | |
230 | var settings = { |
|
227 | var settings = { | |
231 | type : "DELETE", |
|
228 | type : "DELETE", | |
232 | success: options.success || function() {}, |
|
229 | success: options.success || function() {}, | |
233 | error : this.create_basic_error_handler(options.error) |
|
230 | error : this.create_basic_error_handler(options.error) | |
234 | }; |
|
231 | }; | |
235 | $.ajax(url, settings); |
|
232 | $.ajax(url, settings); | |
236 | }; |
|
233 | }; | |
237 |
|
234 | |||
238 | /** |
|
235 | /** | |
239 | * File management functions |
|
236 | * File management functions | |
240 | */ |
|
237 | */ | |
241 |
|
238 | |||
242 | /** |
|
239 | /** | |
243 | * List notebooks and directories at a given path |
|
240 | * List notebooks and directories at a given path | |
244 | * |
|
241 | * | |
245 | * On success, load_callback is called with an array of dictionaries |
|
242 | * On success, load_callback is called with an array of dictionaries | |
246 | * representing individual files or directories. Each dictionary has |
|
243 | * representing individual files or directories. Each dictionary has | |
247 | * the keys: |
|
244 | * the keys: | |
248 | * type: "notebook" or "directory" |
|
245 | * type: "notebook" or "directory" | |
249 | * created: created date |
|
246 | * created: created date | |
250 | * last_modified: last modified dat |
|
247 | * last_modified: last modified dat | |
251 | * @method list_notebooks |
|
248 | * @method list_notebooks | |
252 | * @param {String} path The path to list notebooks in |
|
249 | * @param {String} path The path to list notebooks in | |
253 | * @param {Function} load_callback called with list of notebooks on success |
|
250 | * @param {Function} load_callback called with list of notebooks on success | |
254 | * @param {Function} error called with ajax results on error |
|
251 | * @param {Function} error called with ajax results on error | |
255 | */ |
|
252 | */ | |
256 | Contents.prototype.list_contents = function(path, options) { |
|
253 | Contents.prototype.list_contents = function(path, options) { | |
257 | var settings = { |
|
254 | var settings = { | |
258 | processData : false, |
|
255 | processData : false, | |
259 | cache : false, |
|
256 | cache : false, | |
260 | type : "GET", |
|
257 | type : "GET", | |
261 | dataType : "json", |
|
258 | dataType : "json", | |
262 | success : options.success, |
|
259 | success : options.success, | |
263 | error : this.create_basic_error_handler(options.error) |
|
260 | error : this.create_basic_error_handler(options.error) | |
264 | }; |
|
261 | }; | |
265 |
|
262 | |||
266 | $.ajax(this.api_url(path), settings); |
|
263 | $.ajax(this.api_url(path), settings); | |
267 | }; |
|
264 | }; | |
268 |
|
265 | |||
269 |
|
266 | |||
270 | IPython.Contents = Contents; |
|
267 | IPython.Contents = Contents; | |
271 |
|
268 | |||
272 | return {'Contents': Contents}; |
|
269 | return {'Contents': Contents}; | |
273 | }); |
|
270 | }); |
General Comments 0
You need to be logged in to leave comments.
Login now