Show More
@@ -1,251 +1,251 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 | // base_url: string |
|
20 | // base_url: string | |
21 | this.base_url = options.base_url; |
|
21 | this.base_url = options.base_url; | |
22 | }; |
|
22 | }; | |
23 |
|
23 | |||
24 | /** Error type */ |
|
24 | /** Error type */ | |
25 | Contents.DIRECTORY_NOT_EMPTY_ERROR = 'DirectoryNotEmptyError'; |
|
25 | Contents.DIRECTORY_NOT_EMPTY_ERROR = 'DirectoryNotEmptyError'; | |
26 |
|
26 | |||
27 | Contents.DirectoryNotEmptyError = function() { |
|
27 | Contents.DirectoryNotEmptyError = function() { | |
28 | // Constructor |
|
28 | // Constructor | |
29 | // |
|
29 | // | |
30 | // An error representing the result of attempting to delete a non-empty |
|
30 | // An error representing the result of attempting to delete a non-empty | |
31 | // directory. |
|
31 | // directory. | |
32 | this.message = 'A directory must be empty before being deleted.'; |
|
32 | this.message = 'A directory must be empty before being deleted.'; | |
33 | } |
|
33 | } | |
34 | Contents.DirectoryNotEmptyError.prototype = new Error; |
|
34 | Contents.DirectoryNotEmptyError.prototype = new Error; | |
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 function(xhr, status, error) { }; |
|
58 | return function(xhr, status, 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 | * Load a file. |
|
70 | * Load 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 load_notebook |
|
74 | * @method load_notebook | |
75 | * @param {String} path |
|
75 | * @param {String} path | |
76 | * @param {String} name |
|
76 | * @param {String} name | |
77 | * @param {Function} success |
|
77 | * @param {Function} success | |
78 | * @param {Function} error |
|
78 | * @param {Function} error | |
79 | */ |
|
79 | */ | |
80 | Contents.prototype.load_file = function (path, name, options) { |
|
80 | Contents.prototype.load_file = function (path, name, options) { | |
81 | // We do the call with settings so we can set cache to false. |
|
81 | // We do the call with settings so we can set cache to false. | |
82 | var settings = { |
|
82 | var settings = { | |
83 | processData : false, |
|
83 | processData : false, | |
84 | cache : false, |
|
84 | cache : false, | |
85 | type : "GET", |
|
85 | type : "GET", | |
86 | dataType : "json", |
|
86 | dataType : "json", | |
87 | success : options.success, |
|
87 | success : options.success, | |
88 | error : this.create_basic_error_handler(options.error) |
|
88 | error : this.create_basic_error_handler(options.error) | |
89 | }; |
|
89 | }; | |
90 | var url = this.api_url(path, name); |
|
90 | var url = this.api_url(path, name); | |
91 | $.ajax(url, settings); |
|
91 | $.ajax(url, settings); | |
92 | }; |
|
92 | }; | |
93 |
|
93 | |||
94 |
|
94 | |||
95 | /** |
|
95 | /** | |
96 | * Creates a new notebook file at the specified directory path. |
|
96 | * Creates a new notebook file at the specified directory path. | |
97 | * |
|
97 | * | |
98 | * @method scroll_to_cell |
|
98 | * @method scroll_to_cell | |
99 | * @param {String} path The path to create the new notebook at |
|
99 | * @param {String} path The path to create the new notebook at | |
100 | */ |
|
100 | */ | |
101 | Contents.prototype.new_notebook = function(path, options) { |
|
101 | Contents.prototype.new_notebook = function(path, options) { | |
102 | var error = options.error || function() {}; |
|
102 | var error = options.error || function() {}; | |
103 | var settings = { |
|
103 | var settings = { | |
104 | processData : false, |
|
104 | processData : false, | |
105 | cache : false, |
|
105 | cache : false, | |
106 | type : "POST", |
|
106 | type : "POST", | |
107 | dataType : "json", |
|
107 | dataType : "json", | |
108 | success : options.success || function() {}, |
|
108 | success : options.success || function() {}, | |
109 | error : this.create_basic_error_handler(options.error) |
|
109 | error : this.create_basic_error_handler(options.error) | |
110 | }; |
|
110 | }; | |
111 | $.ajax(this.api_url(path), settings); |
|
111 | $.ajax(this.api_url(path), settings); | |
112 | }; |
|
112 | }; | |
113 |
|
113 | |||
114 | Contents.prototype.delete_file = function(name, path, options) { |
|
114 | Contents.prototype.delete_file = function(name, path, options) { | |
115 | var error = options.error || function() {}; |
|
115 | var error_callback = options.error || function() {}; | |
116 | var that = this; |
|
116 | var that = this; | |
117 | var settings = { |
|
117 | var settings = { | |
118 | processData : false, |
|
118 | processData : false, | |
119 | cache : false, |
|
119 | cache : false, | |
120 | type : "DELETE", |
|
120 | type : "DELETE", | |
121 | dataType : "json", |
|
121 | dataType : "json", | |
122 | success : options.success || function() {}, |
|
122 | success : options.success || function() {}, | |
123 | error : function(xhr, status, error) { |
|
123 | error : function(xhr, status, error) { | |
124 | // TODO: update IPEP27 to specify errors more precisely, so |
|
124 | // TODO: update IPEP27 to specify errors more precisely, so | |
125 | // that error types can be detected here with certainty. |
|
125 | // that error types can be detected here with certainty. | |
126 | if (xhr.status === 400) { |
|
126 | if (xhr.status === 400) { | |
127 | error(new Contents.DirectoryNotEmptyError()); |
|
127 | error_callback(new Contents.DirectoryNotEmptyError()); | |
128 | } |
|
128 | } | |
129 | error(utils.wrap_ajax_error(xhr, status, error)); |
|
129 | error_callback(utils.wrap_ajax_error(xhr, status, error)); | |
130 | } |
|
130 | } | |
131 | }; |
|
131 | }; | |
132 | var url = this.api_url(path, name); |
|
132 | var url = this.api_url(path, name); | |
133 | $.ajax(url, settings); |
|
133 | $.ajax(url, settings); | |
134 | }; |
|
134 | }; | |
135 |
|
135 | |||
136 | Contents.prototype.rename_file = function(path, name, new_path, new_name, options) { |
|
136 | Contents.prototype.rename_file = function(path, name, new_path, new_name, options) { | |
137 | var data = {name: new_name, path: new_path}; |
|
137 | var data = {name: new_name, path: new_path}; | |
138 | var settings = { |
|
138 | var settings = { | |
139 | processData : false, |
|
139 | processData : false, | |
140 | cache : false, |
|
140 | cache : false, | |
141 | type : "PATCH", |
|
141 | type : "PATCH", | |
142 | data : JSON.stringify(data), |
|
142 | data : JSON.stringify(data), | |
143 | dataType: "json", |
|
143 | dataType: "json", | |
144 | contentType: 'application/json', |
|
144 | contentType: 'application/json', | |
145 | success : options.success || function() {}, |
|
145 | success : options.success || function() {}, | |
146 | error : this.create_basic_error_handler(options.error) |
|
146 | error : this.create_basic_error_handler(options.error) | |
147 | }; |
|
147 | }; | |
148 | var url = this.api_url(path, name); |
|
148 | var url = this.api_url(path, name); | |
149 | $.ajax(url, settings); |
|
149 | $.ajax(url, settings); | |
150 | }; |
|
150 | }; | |
151 |
|
151 | |||
152 | Contents.prototype.save_file = function(path, name, model, options) { |
|
152 | Contents.prototype.save_file = function(path, name, model, options) { | |
153 | // We do the call with settings so we can set cache to false. |
|
153 | // We do the call with settings so we can set cache to false. | |
154 | var settings = { |
|
154 | var settings = { | |
155 | processData : false, |
|
155 | processData : false, | |
156 | cache : false, |
|
156 | cache : false, | |
157 | type : "PUT", |
|
157 | type : "PUT", | |
158 | data : JSON.stringify(model), |
|
158 | data : JSON.stringify(model), | |
159 | contentType: 'application/json', |
|
159 | contentType: 'application/json', | |
160 | success : options.success || function() {}, |
|
160 | success : options.success || function() {}, | |
161 | error : this.create_basic_error_handler(options.error) |
|
161 | error : this.create_basic_error_handler(options.error) | |
162 | }; |
|
162 | }; | |
163 | if (options.extra_settings) { |
|
163 | if (options.extra_settings) { | |
164 | $.extend(settings, options.extra_settings); |
|
164 | $.extend(settings, options.extra_settings); | |
165 | } |
|
165 | } | |
166 | var url = this.api_url(path, name); |
|
166 | var url = this.api_url(path, name); | |
167 | $.ajax(url, settings); |
|
167 | $.ajax(url, settings); | |
168 | }; |
|
168 | }; | |
169 |
|
169 | |||
170 | /** |
|
170 | /** | |
171 | * Checkpointing Functions |
|
171 | * Checkpointing Functions | |
172 | */ |
|
172 | */ | |
173 |
|
173 | |||
174 | Contents.prototype.create_checkpoint = function(path, name, options) { |
|
174 | Contents.prototype.create_checkpoint = function(path, name, options) { | |
175 | var url = this.api_url(path, name, 'checkpoints'); |
|
175 | var url = this.api_url(path, name, 'checkpoints'); | |
176 | var settings = { |
|
176 | var settings = { | |
177 | type : "POST", |
|
177 | type : "POST", | |
178 | success: options.success || function() {}, |
|
178 | success: options.success || function() {}, | |
179 | error : this.create_basic_error_handler(options.error) |
|
179 | error : this.create_basic_error_handler(options.error) | |
180 | }; |
|
180 | }; | |
181 | $.ajax(url, settings); |
|
181 | $.ajax(url, settings); | |
182 | }; |
|
182 | }; | |
183 |
|
183 | |||
184 | Contents.prototype.list_checkpoints = function(path, name, options) { |
|
184 | Contents.prototype.list_checkpoints = function(path, name, options) { | |
185 | var url = this.api_url(path, name, 'checkpoints'); |
|
185 | var url = this.api_url(path, name, 'checkpoints'); | |
186 | var settings = { |
|
186 | var settings = { | |
187 | type : "GET", |
|
187 | type : "GET", | |
188 | success: options.success, |
|
188 | success: options.success, | |
189 | error : this.create_basic_error_handler(options.error) |
|
189 | error : this.create_basic_error_handler(options.error) | |
190 | }; |
|
190 | }; | |
191 | $.ajax(url, settings); |
|
191 | $.ajax(url, settings); | |
192 | }; |
|
192 | }; | |
193 |
|
193 | |||
194 | Contents.prototype.restore_checkpoint = function(path, name, checkpoint_id, options) { |
|
194 | Contents.prototype.restore_checkpoint = function(path, name, checkpoint_id, options) { | |
195 | var url = this.api_url(path, name, 'checkpoints', checkpoint_id); |
|
195 | var url = this.api_url(path, name, 'checkpoints', checkpoint_id); | |
196 | var settings = { |
|
196 | var settings = { | |
197 | type : "POST", |
|
197 | type : "POST", | |
198 | success: options.success || function() {}, |
|
198 | success: options.success || function() {}, | |
199 | error : this.create_basic_error_handler(options.error) |
|
199 | error : this.create_basic_error_handler(options.error) | |
200 | }; |
|
200 | }; | |
201 | $.ajax(url, settings); |
|
201 | $.ajax(url, settings); | |
202 | }; |
|
202 | }; | |
203 |
|
203 | |||
204 | Contents.prototype.delete_checkpoint = function(path, name, checkpoint_id, options) { |
|
204 | Contents.prototype.delete_checkpoint = function(path, name, checkpoint_id, options) { | |
205 | var url = this.api_url(path, name, 'checkpoints', checkpoint_id); |
|
205 | var url = this.api_url(path, name, 'checkpoints', checkpoint_id); | |
206 | var settings = { |
|
206 | var settings = { | |
207 | type : "DELETE", |
|
207 | type : "DELETE", | |
208 | success: options.success || function() {}, |
|
208 | success: options.success || function() {}, | |
209 | error : this.create_basic_error_handler(options.error) |
|
209 | error : this.create_basic_error_handler(options.error) | |
210 | }; |
|
210 | }; | |
211 | $.ajax(url, settings); |
|
211 | $.ajax(url, settings); | |
212 | }; |
|
212 | }; | |
213 |
|
213 | |||
214 | /** |
|
214 | /** | |
215 | * File management functions |
|
215 | * File management functions | |
216 | */ |
|
216 | */ | |
217 |
|
217 | |||
218 | /** |
|
218 | /** | |
219 | * List notebooks and directories at a given path |
|
219 | * List notebooks and directories at a given path | |
220 | * |
|
220 | * | |
221 | * On success, load_callback is called with an array of dictionaries |
|
221 | * On success, load_callback is called with an array of dictionaries | |
222 | * representing individual files or directories. Each dictionary has |
|
222 | * representing individual files or directories. Each dictionary has | |
223 | * the keys: |
|
223 | * the keys: | |
224 | * type: "notebook" or "directory" |
|
224 | * type: "notebook" or "directory" | |
225 | * name: the name of the file or directory |
|
225 | * name: the name of the file or directory | |
226 | * created: created date |
|
226 | * created: created date | |
227 | * last_modified: last modified dat |
|
227 | * last_modified: last modified dat | |
228 | * path: the path |
|
228 | * path: the path | |
229 | * @method list_notebooks |
|
229 | * @method list_notebooks | |
230 | * @param {String} path The path to list notebooks in |
|
230 | * @param {String} path The path to list notebooks in | |
231 | * @param {Function} load_callback called with list of notebooks on success |
|
231 | * @param {Function} load_callback called with list of notebooks on success | |
232 | * @param {Function} error called with ajax results on error |
|
232 | * @param {Function} error called with ajax results on error | |
233 | */ |
|
233 | */ | |
234 | Contents.prototype.list_contents = function(path, options) { |
|
234 | Contents.prototype.list_contents = function(path, options) { | |
235 | var settings = { |
|
235 | var settings = { | |
236 | processData : false, |
|
236 | processData : false, | |
237 | cache : false, |
|
237 | cache : false, | |
238 | type : "GET", |
|
238 | type : "GET", | |
239 | dataType : "json", |
|
239 | dataType : "json", | |
240 | success : options.success, |
|
240 | success : options.success, | |
241 | error : this.create_basic_error_handler(options.error) |
|
241 | error : this.create_basic_error_handler(options.error) | |
242 | }; |
|
242 | }; | |
243 |
|
243 | |||
244 | $.ajax(this.api_url(path), settings); |
|
244 | $.ajax(this.api_url(path), settings); | |
245 | }; |
|
245 | }; | |
246 |
|
246 | |||
247 |
|
247 | |||
248 | IPython.Contents = Contents; |
|
248 | IPython.Contents = Contents; | |
249 |
|
249 | |||
250 | return {'Contents': Contents}; |
|
250 | return {'Contents': Contents}; | |
251 | }); |
|
251 | }); |
General Comments 0
You need to be logged in to leave comments.
Login now