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