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