Show More
@@ -1,291 +1,290 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 | 'base/js/dialog', |
|
9 | 9 | ], function(IPython, $, utils, dialog) { |
|
10 | 10 | var ContentManager = function(options) { |
|
11 | 11 | // Constructor |
|
12 | 12 | // |
|
13 | 13 | // A contentmanager 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 | <<<<<<< HEAD | |
|
19 | 18 | // options: dictionary |
|
20 | 19 | // Dictionary of keyword arguments. |
|
21 | 20 | // events: $(Events) instance |
|
22 | 21 | // base_url: string |
|
23 | 22 | this.version = 0.1; |
|
24 | 23 | this.events = options.events; |
|
25 | 24 | this.base_url = options.base_url; |
|
26 | 25 | }; |
|
27 | 26 | |
|
28 | 27 | /** |
|
29 | 28 | * Notebook Functions |
|
30 | 29 | */ |
|
31 | 30 | |
|
32 | 31 | /** |
|
33 | 32 | * Load a notebook. |
|
34 | 33 | * |
|
35 | 34 | * Calls success_callback with notebook JSON object (as string), or |
|
36 | 35 | * error_callback with error. |
|
37 | 36 | * |
|
38 | 37 | * @method load_notebook |
|
39 | 38 | * @param {String} path |
|
40 | 39 | * @param {String} name |
|
41 | 40 | * @param {Function} success_callback |
|
42 | 41 | * @param {Function} error_callback |
|
43 | 42 | */ |
|
44 | 43 | ContentManager.prototype.load_notebook = function (path, name, success_callback, |
|
45 | 44 | error_callback) { |
|
46 | 45 | // We do the call with settings so we can set cache to false. |
|
47 | 46 | var settings = { |
|
48 | 47 | processData : false, |
|
49 | 48 | cache : false, |
|
50 | 49 | type : "GET", |
|
51 | 50 | dataType : "json", |
|
52 | 51 | success : success_callback, |
|
53 | 52 | error : error_callback, |
|
54 | 53 | }; |
|
55 | 54 | this.events.trigger('notebook_loading.Notebook'); |
|
56 | 55 | var url = utils.url_join_encode( |
|
57 | 56 | this.base_url, |
|
58 | 57 | 'api/contents', |
|
59 | 58 | path, |
|
60 | 59 | name |
|
61 | 60 | ); |
|
62 | 61 | $.ajax(url, settings); |
|
63 | 62 | }; |
|
64 | 63 | |
|
65 | 64 | |
|
66 | 65 | /** |
|
67 | 66 | * Creates a new notebook file at the specified path, and |
|
68 | 67 | * opens that notebook in a new window. |
|
69 | 68 | * |
|
70 | 69 | * @method scroll_to_cell |
|
71 | 70 | * @param {String} path The path to create the new notebook at |
|
72 | 71 | */ |
|
73 | 72 | ContentManager.prototype.new_notebook = function(path) { |
|
74 | 73 | var base_url = this.base_url; |
|
75 | 74 | var settings = { |
|
76 | 75 | processData : false, |
|
77 | 76 | cache : false, |
|
78 | 77 | type : "POST", |
|
79 | 78 | dataType : "json", |
|
80 | 79 | async : false, |
|
81 | 80 | success : function (data, status, xhr){ |
|
82 | 81 | var notebook_name = data.name; |
|
83 | 82 | window.open( |
|
84 | 83 | utils.url_join_encode( |
|
85 | 84 | base_url, |
|
86 | 85 | 'contents', |
|
87 | 86 | path, |
|
88 | 87 | notebook_name |
|
89 | 88 | ), |
|
90 | 89 | '_blank' |
|
91 | 90 | ); |
|
92 | 91 | }, |
|
93 | 92 | error : function(xhr, status, error) { |
|
94 | 93 | utils.log_ajax_error(xhr, status, error); |
|
95 | 94 | var msg; |
|
96 | 95 | if (xhr.responseJSON && xhr.responseJSON.message) { |
|
97 | 96 | msg = xhr.responseJSON.message; |
|
98 | 97 | } else { |
|
99 | 98 | msg = xhr.statusText; |
|
100 | 99 | } |
|
101 | 100 | dialog.modal({ |
|
102 | 101 | title : 'Creating Notebook Failed', |
|
103 | 102 | body : "The error was: " + msg, |
|
104 | 103 | buttons : {'OK' : {'class' : 'btn-primary'}} |
|
105 | 104 | }); |
|
106 | 105 | } |
|
107 | 106 | }; |
|
108 | 107 | var url = utils.url_join_encode( |
|
109 | 108 | base_url, |
|
110 | 109 | 'api/contents', |
|
111 | 110 | path |
|
112 | 111 | ); |
|
113 | 112 | $.ajax(url,settings); |
|
114 | 113 | }; |
|
115 | 114 | |
|
116 | 115 | ContentManager.prototype.delete_notebook = function(name, path) { |
|
117 | 116 | var settings = { |
|
118 | 117 | processData : false, |
|
119 | 118 | cache : false, |
|
120 | 119 | type : "DELETE", |
|
121 | 120 | dataType : "json", |
|
122 | 121 | success : $.proxy(this.events.trigger, this.events, |
|
123 | 122 | 'notebook_deleted.ContentManager', |
|
124 | 123 | { |
|
125 | 124 | name: name, |
|
126 | 125 | path: path |
|
127 | 126 | }), |
|
128 | 127 | error : utils.log_ajax_error |
|
129 | 128 | }; |
|
130 | 129 | var url = utils.url_join_encode( |
|
131 | 130 | this.base_url, |
|
132 | 131 | 'api/contents', |
|
133 | 132 | path, |
|
134 | 133 | name |
|
135 | 134 | ); |
|
136 | 135 | $.ajax(url, settings); |
|
137 | 136 | }; |
|
138 | 137 | |
|
139 | 138 | ContentManager.prototype.rename_notebook = function(path, name, new_name) { |
|
140 | 139 | var that = this; |
|
141 | 140 | var data = {name: new_name}; |
|
142 | 141 | var settings = { |
|
143 | 142 | processData : false, |
|
144 | 143 | cache : false, |
|
145 | 144 | type : "PATCH", |
|
146 | 145 | data : JSON.stringify(data), |
|
147 | 146 | dataType: "json", |
|
148 | 147 | contentType: 'application/json', |
|
149 | 148 | success : function (json, status, xhr) { |
|
150 | 149 | that.events.trigger('notebook_rename_success.ContentManager', |
|
151 | 150 | json); |
|
152 | 151 | }, |
|
153 | 152 | error : function (xhr, status, error) { |
|
154 | 153 | that.events.trigger('notebook_rename_error.ContentManager', |
|
155 | 154 | [xhr, status, error]); |
|
156 | 155 | } |
|
157 | 156 | }; |
|
158 | 157 | var url = utils.url_join_encode( |
|
159 | 158 | this.base_url, |
|
160 | 159 | 'api/contents', |
|
161 | 160 | path, |
|
162 | 161 | name |
|
163 | 162 | ); |
|
164 | 163 | $.ajax(url, settings); |
|
165 | 164 | }; |
|
166 | 165 | |
|
167 | 166 | ContentManager.prototype.save_notebook = function(path, name, content, |
|
168 | 167 | extra_settings) { |
|
169 | 168 | var that = content; |
|
170 | 169 | // Create a JSON model to be sent to the server. |
|
171 | 170 | var model = { |
|
172 | 171 | name : name, |
|
173 | 172 | path : path, |
|
174 | 173 | type : "notebook", |
|
175 | 174 | content : content |
|
176 | 175 | }; |
|
177 | 176 | // time the ajax call for autosave tuning purposes. |
|
178 | 177 | var start = new Date().getTime(); |
|
179 | 178 | // We do the call with settings so we can set cache to false. |
|
180 | 179 | var settings = { |
|
181 | 180 | processData : false, |
|
182 | 181 | cache : false, |
|
183 | 182 | type : "PUT", |
|
184 | 183 | data : JSON.stringify(model), |
|
185 | 184 | contentType: 'application/json', |
|
186 | 185 | success : $.proxy(this.events.trigger, this.events, |
|
187 | 186 | 'notebook_save_success.ContentManager', |
|
188 | 187 | $.extend(model, { start : start })), |
|
189 | 188 | error : function (xhr, status, error) { |
|
190 | 189 | that.events.trigger('notebook_save_error.ContentManager', |
|
191 | 190 | [xhr, status, error, model]); |
|
192 | 191 | } |
|
193 | 192 | }; |
|
194 | 193 | if (extra_settings) { |
|
195 | 194 | for (var key in extra_settings) { |
|
196 | 195 | settings[key] = extra_settings[key]; |
|
197 | 196 | } |
|
198 | 197 | } |
|
199 | 198 | var url = utils.url_join_encode( |
|
200 | 199 | this.base_url, |
|
201 | 200 | 'api/contents', |
|
202 | 201 | path, |
|
203 | 202 | name |
|
204 | 203 | ); |
|
205 | 204 | $.ajax(url, settings); |
|
206 | 205 | }; |
|
207 | 206 | |
|
208 | 207 | /** |
|
209 | 208 | * Checkpointing Functions |
|
210 | 209 | */ |
|
211 | 210 | |
|
212 | 211 | ContentManager.prototype.save_checkpoint = function() { |
|
213 | 212 | // This is not necessary - integrated into save |
|
214 | 213 | }; |
|
215 | 214 | |
|
216 | 215 | ContentManager.prototype.restore_checkpoint = function(notebook, id) { |
|
217 | 216 | that = notebook; |
|
218 | 217 | this.events.trigger('notebook_restoring.Notebook', checkpoint); |
|
219 | 218 | var url = utils.url_join_encode( |
|
220 | 219 | this.base_url, |
|
221 | 220 | 'api/contents', |
|
222 | 221 | this.notebook_path, |
|
223 | 222 | this.notebook_name, |
|
224 | 223 | 'checkpoints', |
|
225 | 224 | checkpoint |
|
226 | 225 | ); |
|
227 | 226 | $.post(url).done( |
|
228 | 227 | $.proxy(that.restore_checkpoint_success, that) |
|
229 | 228 | ).fail( |
|
230 | 229 | $.proxy(that.restore_checkpoint_error, that) |
|
231 | 230 | ); |
|
232 | 231 | }; |
|
233 | 232 | |
|
234 | 233 | ContentManager.prototype.list_checkpoints = function(notebook) { |
|
235 | 234 | that = notebook; |
|
236 | 235 | var url = utils.url_join_encode( |
|
237 | 236 | that.base_url, |
|
238 | 237 | 'api/contents', |
|
239 | 238 | that.notebook_path, |
|
240 | 239 | that.notebook_name, |
|
241 | 240 | 'checkpoints' |
|
242 | 241 | ); |
|
243 | 242 | $.get(url).done( |
|
244 | 243 | $.proxy(that.list_checkpoints_success, that) |
|
245 | 244 | ).fail( |
|
246 | 245 | $.proxy(that.list_checkpoints_error, that) |
|
247 | 246 | ); |
|
248 | 247 | }; |
|
249 | 248 | |
|
250 | 249 | /** |
|
251 | 250 | * File management functions |
|
252 | 251 | */ |
|
253 | 252 | |
|
254 | 253 | /** |
|
255 | 254 | * List notebooks and directories at a given path |
|
256 | 255 | * |
|
257 | 256 | * On success, load_callback is called with an array of dictionaries |
|
258 | 257 | * representing individual files or directories. Each dictionary has |
|
259 | 258 | * the keys: |
|
260 | 259 | * type: "notebook" or "directory" |
|
261 | 260 | * name: the name of the file or directory |
|
262 | 261 | * created: created date |
|
263 | 262 | * last_modified: last modified dat |
|
264 | 263 | * path: the path |
|
265 | 264 | * @method list_notebooks |
|
266 | 265 | * @param {String} path The path to list notebooks in |
|
267 | 266 | * @param {Function} load_callback called with list of notebooks on success |
|
268 | 267 | * @param {Function} error_callback called with ajax results on error |
|
269 | 268 | */ |
|
270 | 269 | ContentManager.prototype.list_contents = function(path, load_callback, |
|
271 | 270 | error_callback) { |
|
272 | 271 | var that = this; |
|
273 | 272 | var settings = { |
|
274 | 273 | processData : false, |
|
275 | 274 | cache : false, |
|
276 | 275 | type : "GET", |
|
277 | 276 | dataType : "json", |
|
278 | 277 | success : load_callback, |
|
279 | 278 | error : error_callback |
|
280 | 279 | }; |
|
281 | 280 | |
|
282 | 281 | var url = utils.url_join_encode(this.base_url, 'api', 'contents', |
|
283 | 282 | path); |
|
284 | 283 | $.ajax(url, settings); |
|
285 | 284 | } |
|
286 | 285 | |
|
287 | 286 | |
|
288 | 287 | IPython.ContentManager = ContentManager; |
|
289 | 288 | |
|
290 | 289 | return {'ContentManager': ContentManager}; |
|
291 | 290 | }); |
General Comments 0
You need to be logged in to leave comments.
Login now