##// END OF EJS Templates
All aboard the promise train
Thomas Kluyver -
Show More
@@ -92,14 +92,13 b' define(['
92 // Create a new notebook in the same path as the current
92 // Create a new notebook in the same path as the current
93 // notebook's path.
93 // notebook's path.
94 var parent = utils.url_path_split(that.notebook.notebook_path)[0];
94 var parent = utils.url_path_split(that.notebook.notebook_path)[0];
95 that.contents.new_untitled(parent, {
95 that.contents.new_untitled(parent, {type: "notebook"}).then(
96 type: "notebook",
96 function (data) {
97 success: function (data) {
98 w.location = utils.url_join_encode(
97 w.location = utils.url_join_encode(
99 that.base_url, 'notebooks', data.path
98 that.base_url, 'notebooks', data.path
100 );
99 );
101 },
100 },
102 error: function(error) {
101 function(error) {
103 w.close();
102 w.close();
104 dialog.modal({
103 dialog.modal({
105 title : 'Creating Notebook Failed',
104 title : 'Creating Notebook Failed',
@@ -107,7 +106,7 b' define(['
107 buttons : {'OK' : {'class' : 'btn-primary'}}
106 buttons : {'OK' : {'class' : 'btn-primary'}}
108 });
107 });
109 }
108 }
110 });
109 );
111 });
110 });
112 this.element.find('#open_notebook').click(function () {
111 this.element.find('#open_notebook').click(function () {
113 var parent = utils.url_path_split(that.notebook.notebook_path)[0];
112 var parent = utils.url_path_split(that.notebook.notebook_path)[0];
@@ -1902,12 +1902,12 b' define(['
1902 var start = new Date().getTime();
1902 var start = new Date().getTime();
1903
1903
1904 var that = this;
1904 var that = this;
1905 this.contents.save(this.notebook_path, model, {
1905 this.contents.save(this.notebook_path, model).then(
1906 success: $.proxy(this.save_notebook_success, this, start),
1906 $.proxy(this.save_notebook_success, this, start),
1907 error: function (error) {
1907 function (error) {
1908 that.events.trigger('notebook_save_failed.Notebook', error);
1908 that.events.trigger('notebook_save_failed.Notebook', error);
1909 }
1909 }
1910 });
1910 );
1911 };
1911 };
1912
1912
1913 /**
1913 /**
@@ -2025,17 +2025,17 b' define(['
2025 var base_url = this.base_url;
2025 var base_url = this.base_url;
2026 var w = window.open();
2026 var w = window.open();
2027 var parent = utils.url_path_split(this.notebook_path)[0];
2027 var parent = utils.url_path_split(this.notebook_path)[0];
2028 this.contents.copy(this.notebook_path, parent, {
2028 this.contents.copy(this.notebook_path, parent).then(
2029 success: function (data) {
2029 function (data) {
2030 w.location = utils.url_join_encode(
2030 w.location = utils.url_join_encode(
2031 base_url, 'notebooks', data.path
2031 base_url, 'notebooks', data.path
2032 );
2032 );
2033 },
2033 },
2034 error : function(error) {
2034 function(error) {
2035 w.close();
2035 w.close();
2036 console.log(error);
2036 console.log(error);
2037 },
2037 }
2038 });
2038 );
2039 };
2039 };
2040
2040
2041 Notebook.prototype.rename = function (new_name) {
2041 Notebook.prototype.rename = function (new_name) {
@@ -2046,15 +2046,15 b' define(['
2046 var that = this;
2046 var that = this;
2047 var parent = utils.url_path_split(this.notebook_path)[0];
2047 var parent = utils.url_path_split(this.notebook_path)[0];
2048 var new_path = utils.url_path_join(parent, new_name);
2048 var new_path = utils.url_path_join(parent, new_name);
2049 this.contents.rename(this.notebook_path, new_path, {
2049 this.contents.rename(this.notebook_path, new_path).then(
2050 success: function (json) {
2050 function (json) {
2051 that.notebook_name = json.name;
2051 that.notebook_name = json.name;
2052 that.notebook_path = json.path;
2052 that.notebook_path = json.path;
2053 that.session.rename_notebook(json.path);
2053 that.session.rename_notebook(json.path);
2054 that.events.trigger('notebook_renamed.Notebook', json);
2054 that.events.trigger('notebook_renamed.Notebook', json);
2055 },
2055 },
2056 error: $.proxy(this.rename_error, this)
2056 $.proxy(this.rename_error, this)
2057 });
2057 );
2058 };
2058 };
2059
2059
2060 Notebook.prototype.delete = function () {
2060 Notebook.prototype.delete = function () {
@@ -2326,12 +2326,12 b' define(['
2326 */
2326 */
2327 Notebook.prototype.list_checkpoints = function () {
2327 Notebook.prototype.list_checkpoints = function () {
2328 var that = this;
2328 var that = this;
2329 this.contents.list_checkpoints(this.notebook_path, {
2329 this.contents.list_checkpoints(this.notebook_path).then(
2330 success: $.proxy(this.list_checkpoints_success, this),
2330 $.proxy(this.list_checkpoints_success, this),
2331 error: function(error) {
2331 function(error) {
2332 that.events.trigger('list_checkpoints_failed.Notebook', error);
2332 that.events.trigger('list_checkpoints_failed.Notebook', error);
2333 }
2333 }
2334 });
2334 );
2335 };
2335 };
2336
2336
2337 /**
2337 /**
@@ -2358,12 +2358,12 b' define(['
2358 */
2358 */
2359 Notebook.prototype.create_checkpoint = function () {
2359 Notebook.prototype.create_checkpoint = function () {
2360 var that = this;
2360 var that = this;
2361 this.contents.create_checkpoint(this.notebook_path, {
2361 this.contents.create_checkpoint(this.notebook_path).then(
2362 success: $.proxy(this.create_checkpoint_success, this),
2362 $.proxy(this.create_checkpoint_success, this),
2363 error: function (error) {
2363 function (error) {
2364 that.events.trigger('checkpoint_failed.Notebook', error);
2364 that.events.trigger('checkpoint_failed.Notebook', error);
2365 }
2365 }
2366 });
2366 );
2367 };
2367 };
2368
2368
2369 /**
2369 /**
@@ -2428,13 +2428,12 b' define(['
2428 Notebook.prototype.restore_checkpoint = function (checkpoint) {
2428 Notebook.prototype.restore_checkpoint = function (checkpoint) {
2429 this.events.trigger('notebook_restoring.Notebook', checkpoint);
2429 this.events.trigger('notebook_restoring.Notebook', checkpoint);
2430 var that = this;
2430 var that = this;
2431 this.contents.restore_checkpoint(this.notebook_path,
2431 this.contents.restore_checkpoint(this.notebook_path, checkpoint).then(
2432 checkpoint, {
2432 $.proxy(this.restore_checkpoint_success, this),
2433 success: $.proxy(this.restore_checkpoint_success, this),
2433 function (error) {
2434 error: function (error) {
2435 that.events.trigger('checkpoint_restore_failed.Notebook', error);
2434 that.events.trigger('checkpoint_restore_failed.Notebook', error);
2436 }
2435 }
2437 });
2436 );
2438 };
2437 };
2439
2438
2440 /**
2439 /**
@@ -2456,13 +2455,12 b' define(['
2456 Notebook.prototype.delete_checkpoint = function (checkpoint) {
2455 Notebook.prototype.delete_checkpoint = function (checkpoint) {
2457 this.events.trigger('notebook_restoring.Notebook', checkpoint);
2456 this.events.trigger('notebook_restoring.Notebook', checkpoint);
2458 var that = this;
2457 var that = this;
2459 this.contents.delete_checkpoint(this.notebook_path,
2458 this.contents.delete_checkpoint(this.notebook_path, checkpoint).then(
2460 checkpoint, {
2459 $.proxy(this.delete_checkpoint_success, this),
2461 success: $.proxy(this.delete_checkpoint_success, this),
2460 function (error) {
2462 error: function (error) {
2463 that.events.trigger('checkpoint_delete_failed.Notebook', error);
2461 that.events.trigger('checkpoint_delete_failed.Notebook', error);
2464 }
2462 }
2465 });
2463 );
2466 };
2464 };
2467
2465
2468 /**
2466 /**
@@ -112,33 +112,31 b' define(['
112 type : "POST",
112 type : "POST",
113 data: data,
113 data: data,
114 dataType : "json",
114 dataType : "json",
115 success : options.success || function() {},
116 error : this.create_basic_error_handler(options.error)
117 };
115 };
118 $.ajax(this.api_url(path), settings);
116 return utils.promising_ajax(this.api_url(path), settings);
119 };
117 };
120
118
121 Contents.prototype.delete = function(path, options) {
119 Contents.prototype.delete = function(path) {
122 var error_callback = options.error || function() {};
123 var settings = {
120 var settings = {
124 processData : false,
121 processData : false,
125 type : "DELETE",
122 type : "DELETE",
126 dataType : "json",
123 dataType : "json",
127 success : options.success || function() {},
124 };
128 error : function(xhr, status, error) {
125 var url = this.api_url(path);
126 return utils.promising_ajax(url, settings).catch(
127 // Translate certain errors to more specific ones.
128 function(error) {
129 // TODO: update IPEP27 to specify errors more precisely, so
129 // TODO: update IPEP27 to specify errors more precisely, so
130 // that error types can be detected here with certainty.
130 // that error types can be detected here with certainty.
131 if (xhr.status === 400) {
131 if (error.xhr.status === 400) {
132 error_callback(new Contents.DirectoryNotEmptyError());
132 return Promise.reject(new Contents.DirectoryNotEmptyError());
133 }
133 }
134 error_callback(utils.wrap_ajax_error(xhr, status, error));
134 return Promise.reject(error);
135 }
135 }
136 };
136 );
137 var url = this.api_url(path);
138 $.ajax(url, settings);
139 };
137 };
140
138
141 Contents.prototype.rename = function(path, new_path, options) {
139 Contents.prototype.rename = function(path, new_path) {
142 var data = {path: new_path};
140 var data = {path: new_path};
143 var settings = {
141 var settings = {
144 processData : false,
142 processData : false,
@@ -146,28 +144,24 b' define(['
146 data : JSON.stringify(data),
144 data : JSON.stringify(data),
147 dataType: "json",
145 dataType: "json",
148 contentType: 'application/json',
146 contentType: 'application/json',
149 success : options.success || function() {},
150 error : this.create_basic_error_handler(options.error)
151 };
147 };
152 var url = this.api_url(path);
148 var url = this.api_url(path);
153 $.ajax(url, settings);
149 return utils.promising_ajax(url, settings);
154 };
150 };
155
151
156 Contents.prototype.save = function(path, model, options) {
152 Contents.prototype.save = function(path, model) {
157 // We do the call with settings so we can set cache to false.
153 // We do the call with settings so we can set cache to false.
158 var settings = {
154 var settings = {
159 processData : false,
155 processData : false,
160 type : "PUT",
156 type : "PUT",
161 data : JSON.stringify(model),
157 data : JSON.stringify(model),
162 contentType: 'application/json',
158 contentType: 'application/json',
163 success : options.success || function() {},
164 error : this.create_basic_error_handler(options.error)
165 };
159 };
166 var url = this.api_url(path);
160 var url = this.api_url(path);
167 $.ajax(url, settings);
161 return utils.promising_ajax(url, settings);
168 };
162 };
169
163
170 Contents.prototype.copy = function(from_file, to_dir, options) {
164 Contents.prototype.copy = function(from_file, to_dir) {
171 // Copy a file into a given directory via POST
165 // Copy a file into a given directory via POST
172 // The server will select the name of the copied file
166 // The server will select the name of the copied file
173 var url = this.api_url(to_dir);
167 var url = this.api_url(to_dir);
@@ -177,54 +171,44 b' define(['
177 type: "POST",
171 type: "POST",
178 data: JSON.stringify({copy_from: from_file}),
172 data: JSON.stringify({copy_from: from_file}),
179 dataType : "json",
173 dataType : "json",
180 success: options.success || function() {},
181 error: this.create_basic_error_handler(options.error)
182 };
174 };
183 $.ajax(url, settings);
175 return utils.promising_ajax(url, settings);
184 };
176 };
185
177
186 /**
178 /**
187 * Checkpointing Functions
179 * Checkpointing Functions
188 */
180 */
189
181
190 Contents.prototype.create_checkpoint = function(path, options) {
182 Contents.prototype.create_checkpoint = function(path) {
191 var url = this.api_url(path, 'checkpoints');
183 var url = this.api_url(path, 'checkpoints');
192 var settings = {
184 var settings = {
193 type : "POST",
185 type : "POST",
194 success: options.success || function() {},
195 error : this.create_basic_error_handler(options.error)
196 };
186 };
197 $.ajax(url, settings);
187 return utils.promising_ajax(url, settings);
198 };
188 };
199
189
200 Contents.prototype.list_checkpoints = function(path, options) {
190 Contents.prototype.list_checkpoints = function(path) {
201 var url = this.api_url(path, 'checkpoints');
191 var url = this.api_url(path, 'checkpoints');
202 var settings = {
192 var settings = {
203 type : "GET",
193 type : "GET",
204 success: options.success,
205 error : this.create_basic_error_handler(options.error)
206 };
194 };
207 $.ajax(url, settings);
195 return utils.promising_ajax(url, settings);
208 };
196 };
209
197
210 Contents.prototype.restore_checkpoint = function(path, checkpoint_id, options) {
198 Contents.prototype.restore_checkpoint = function(path, checkpoint_id) {
211 var url = this.api_url(path, 'checkpoints', checkpoint_id);
199 var url = this.api_url(path, 'checkpoints', checkpoint_id);
212 var settings = {
200 var settings = {
213 type : "POST",
201 type : "POST",
214 success: options.success || function() {},
215 error : this.create_basic_error_handler(options.error)
216 };
202 };
217 $.ajax(url, settings);
203 return utils.promising_ajax(url, settings);
218 };
204 };
219
205
220 Contents.prototype.delete_checkpoint = function(path, checkpoint_id, options) {
206 Contents.prototype.delete_checkpoint = function(path, checkpoint_id) {
221 var url = this.api_url(path, 'checkpoints', checkpoint_id);
207 var url = this.api_url(path, 'checkpoints', checkpoint_id);
222 var settings = {
208 var settings = {
223 type : "DELETE",
209 type : "DELETE",
224 success: options.success || function() {},
225 error : this.create_basic_error_handler(options.error)
226 };
210 };
227 $.ajax(url, settings);
211 return utils.promising_ajax(url, settings);
228 };
212 };
229
213
230 /**
214 /**
@@ -65,14 +65,13 b' require(['
65
65
66 $('#new_notebook').click(function (e) {
66 $('#new_notebook').click(function (e) {
67 var w = window.open();
67 var w = window.open();
68 contents.new_untitled(common_options.notebook_path, {
68 contents.new_untitled(common_options.notebook_path, {type: "notebook"}).then(
69 type: "notebook",
69 function (data) {
70 success: function (data) {
71 w.location = utils.url_join_encode(
70 w.location = utils.url_join_encode(
72 common_options.base_url, 'notebooks', data.path
71 common_options.base_url, 'notebooks', data.path
73 );
72 );
74 },
73 },
75 error: function(error) {
74 function(error) {
76 w.close();
75 w.close();
77 dialog.modal({
76 dialog.modal({
78 title : 'Creating Notebook Failed',
77 title : 'Creating Notebook Failed',
@@ -80,7 +79,7 b' require(['
80 buttons : {'OK' : {'class' : 'btn-primary'}}
79 buttons : {'OK' : {'class' : 'btn-primary'}}
81 });
80 });
82 }
81 }
83 });
82 );
84 });
83 });
85
84
86 var interval_id=0;
85 var interval_id=0;
@@ -328,11 +328,11 b' define(['
328 Delete : {
328 Delete : {
329 class: "btn-danger",
329 class: "btn-danger",
330 click: function() {
330 click: function() {
331 notebooklist.contents.delete(path, {
331 notebooklist.contents.delete(path).then(
332 success: function() {
332 function() {
333 notebooklist.notebook_deleted(path);
333 notebooklist.notebook_deleted(path);
334 }
334 }
335 });
335 );
336 }
336 }
337 },
337 },
338 Cancel : {}
338 Cancel : {}
@@ -414,13 +414,11 b' define(['
414 }
414 }
415 filedata = item.data('filedata');
415 filedata = item.data('filedata');
416
416
417 var settings = {
417 var on_success = function () {
418 success : function () {
418 item.removeClass('new-file');
419 item.removeClass('new-file');
419 that.add_link(model, item);
420 that.add_link(model, item);
420 that.add_delete_button(item);
421 that.add_delete_button(item);
421 that.session_list.load_sessions();
422 that.session_list.load_sessions();
423 },
424 };
422 };
425
423
426 var exists = false;
424 var exists = false;
@@ -436,8 +434,8 b' define(['
436 Overwrite : {
434 Overwrite : {
437 class: "btn-danger",
435 class: "btn-danger",
438 click: function () {
436 click: function () {
439 that.contents.save(path, model, settings);
437 that.contents.save(path, model).then(on_success);
440 }
438 }
441 },
439 },
442 Cancel : {
440 Cancel : {
443 click: function() { item.remove(); }
441 click: function() { item.remove(); }
@@ -445,7 +443,7 b' define(['
445 }
443 }
446 });
444 });
447 } else {
445 } else {
448 that.contents.save(path, model, settings);
446 that.contents.save(path, model).then(on_success);
449 }
447 }
450
448
451 return false;
449 return false;
General Comments 0
You need to be logged in to leave comments. Login now