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