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