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