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