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