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