Show More
@@ -1,204 +1,203 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 | // 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.version = 0.1; |
|
22 | this.version = 0.1; | |
23 | this.events = options.events; |
|
23 | this.events = options.events; | |
24 | this.base_url = options.base_url; |
|
24 | this.base_url = options.base_url; | |
25 | }; |
|
25 | }; | |
26 |
|
26 | |||
27 | /** |
|
27 | /** | |
28 | * Creates a new notebook file at the specified path, and |
|
28 | * Creates a new notebook file at the specified path, and | |
29 | * opens that notebook in a new window. |
|
29 | * opens that notebook in a new window. | |
30 | * |
|
30 | * | |
31 | * @method scroll_to_cell |
|
31 | * @method scroll_to_cell | |
32 | * @param {String} path The path to create the new notebook at |
|
32 | * @param {String} path The path to create the new notebook at | |
33 | */ |
|
33 | */ | |
34 | ContentManager.prototype.new_notebook = function(path) { |
|
34 | ContentManager.prototype.new_notebook = function(path) { | |
35 | var base_url = this.base_url; |
|
35 | var base_url = this.base_url; | |
36 | var settings = { |
|
36 | var settings = { | |
37 | processData : false, |
|
37 | processData : false, | |
38 | cache : false, |
|
38 | cache : false, | |
39 | type : "POST", |
|
39 | type : "POST", | |
40 | dataType : "json", |
|
40 | dataType : "json", | |
41 | async : false, |
|
41 | async : false, | |
42 | success : function (data, status, xhr){ |
|
42 | success : function (data, status, xhr){ | |
43 | var notebook_name = data.name; |
|
43 | var notebook_name = data.name; | |
44 | window.open( |
|
44 | window.open( | |
45 | utils.url_join_encode( |
|
45 | utils.url_join_encode( | |
46 | base_url, |
|
46 | base_url, | |
47 | 'notebooks', |
|
47 | 'notebooks', | |
48 | path, |
|
48 | path, | |
49 | notebook_name |
|
49 | notebook_name | |
50 | ), |
|
50 | ), | |
51 | '_blank' |
|
51 | '_blank' | |
52 | ); |
|
52 | ); | |
53 | }, |
|
53 | }, | |
54 | error : function(xhr, status, error) { |
|
54 | error : function(xhr, status, error) { | |
55 | utils.log_ajax_error(xhr, status, error); |
|
55 | utils.log_ajax_error(xhr, status, error); | |
56 | var msg; |
|
56 | var msg; | |
57 | if (xhr.responseJSON && xhr.responseJSON.message) { |
|
57 | if (xhr.responseJSON && xhr.responseJSON.message) { | |
58 | msg = xhr.responseJSON.message; |
|
58 | msg = xhr.responseJSON.message; | |
59 | } else { |
|
59 | } else { | |
60 | msg = xhr.statusText; |
|
60 | msg = xhr.statusText; | |
61 | } |
|
61 | } | |
62 | dialog.modal({ |
|
62 | dialog.modal({ | |
63 | title : 'Creating Notebook Failed', |
|
63 | title : 'Creating Notebook Failed', | |
64 | body : "The error was: " + msg, |
|
64 | body : "The error was: " + msg, | |
65 | buttons : {'OK' : {'class' : 'btn-primary'}} |
|
65 | buttons : {'OK' : {'class' : 'btn-primary'}} | |
66 | }); |
|
66 | }); | |
67 | } |
|
67 | } | |
68 | }; |
|
68 | }; | |
69 | var url = utils.url_join_encode( |
|
69 | var url = utils.url_join_encode( | |
70 | base_url, |
|
70 | base_url, | |
71 | 'api/notebooks', |
|
71 | 'api/notebooks', | |
72 | path |
|
72 | path | |
73 | ); |
|
73 | ); | |
74 | $.ajax(url,settings); |
|
74 | $.ajax(url,settings); | |
75 | }; |
|
75 | }; | |
76 |
|
76 | |||
77 | ContentManager.prototype.delete_notebook = function(name, path) { |
|
77 | ContentManager.prototype.delete_notebook = function(name, path) { | |
78 | var that = this; |
|
78 | var that = this; | |
79 | var settings = { |
|
79 | var settings = { | |
80 | processData : false, |
|
80 | processData : false, | |
81 | cache : false, |
|
81 | cache : false, | |
82 | type : "DELETE", |
|
82 | type : "DELETE", | |
83 | dataType : "json", |
|
83 | dataType : "json", | |
84 | success : function (data, status, xhr) { |
|
84 | success : function (data, status, xhr) { | |
85 | that.events.trigger('notebook_deleted.ContentManager', { |
|
85 | that.events.trigger('notebook_deleted.ContentManager', { | |
86 | name: name, |
|
86 | name: name, | |
87 | path: path |
|
87 | path: path | |
88 | }); |
|
88 | }); | |
89 | }, |
|
89 | }, | |
90 | error : utils.log_ajax_error |
|
90 | error : utils.log_ajax_error | |
91 | }; |
|
91 | }; | |
92 | var url = utils.url_join_encode( |
|
92 | var url = utils.url_join_encode( | |
93 | this.base_url, |
|
93 | this.base_url, | |
94 | 'api/notebooks', |
|
94 | 'api/notebooks', | |
95 | path, |
|
95 | path, | |
96 | name |
|
96 | name | |
97 | ); |
|
97 | ); | |
98 | $.ajax(url, settings); |
|
98 | $.ajax(url, settings); | |
99 | }; |
|
99 | }; | |
100 |
|
100 | |||
101 | ContentManager.prototype.rename_notebook = function(notebook, nbname) { |
|
101 | ContentManager.prototype.rename_notebook = function(notebook, nbname) { | |
102 | var that = notebook; |
|
102 | var that = notebook; | |
103 | if (!nbname.match(/\.ipynb$/)) { |
|
103 | if (!nbname.match(/\.ipynb$/)) { | |
104 | nbname = nbname + ".ipynb"; |
|
104 | nbname = nbname + ".ipynb"; | |
105 | } |
|
105 | } | |
106 | var data = {name: nbname}; |
|
106 | var data = {name: nbname}; | |
107 | var settings = { |
|
107 | var settings = { | |
108 | processData : false, |
|
108 | processData : false, | |
109 | cache : false, |
|
109 | cache : false, | |
110 | type : "PATCH", |
|
110 | type : "PATCH", | |
111 | data : JSON.stringify(data), |
|
111 | data : JSON.stringify(data), | |
112 | dataType: "json", |
|
112 | dataType: "json", | |
113 | contentType: 'application/json', |
|
113 | contentType: 'application/json', | |
114 | success : $.proxy(that.rename_success, that), |
|
114 | success : $.proxy(that.rename_success, that), | |
115 | error : $.proxy(that.rename_error, that) |
|
115 | error : $.proxy(that.rename_error, that) | |
116 | }; |
|
116 | }; | |
117 | this.events.trigger('rename_notebook.Notebook', data); |
|
|||
118 | var url = utils.url_join_encode( |
|
117 | var url = utils.url_join_encode( | |
119 | that.base_url, |
|
118 | that.base_url, | |
120 | 'api/notebooks', |
|
119 | 'api/notebooks', | |
121 | that.notebook_path, |
|
120 | that.notebook_path, | |
122 | that.notebook_name |
|
121 | that.notebook_name | |
123 | ); |
|
122 | ); | |
124 | $.ajax(url, settings); |
|
123 | $.ajax(url, settings); | |
125 | }; |
|
124 | }; | |
126 |
|
125 | |||
127 | ContentManager.prototype.save_notebook = function(notebook, extra_settings) { |
|
126 | ContentManager.prototype.save_notebook = function(notebook, extra_settings) { | |
128 | var that = notebook; |
|
127 | var that = notebook; | |
129 | // Create a JSON model to be sent to the server. |
|
128 | // Create a JSON model to be sent to the server. | |
130 | var model = {}; |
|
129 | var model = {}; | |
131 | model.name = notebook.notebook_name; |
|
130 | model.name = notebook.notebook_name; | |
132 | model.path = notebook.notebook_path; |
|
131 | model.path = notebook.notebook_path; | |
133 | model.content = notebook.toJSON(); |
|
132 | model.content = notebook.toJSON(); | |
134 | model.content.nbformat = notebook.nbformat; |
|
133 | model.content.nbformat = notebook.nbformat; | |
135 | model.content.nbformat_minor = notebook.nbformat_minor; |
|
134 | model.content.nbformat_minor = notebook.nbformat_minor; | |
136 | // time the ajax call for autosave tuning purposes. |
|
135 | // time the ajax call for autosave tuning purposes. | |
137 | var start = new Date().getTime(); |
|
136 | var start = new Date().getTime(); | |
138 | // We do the call with settings so we can set cache to false. |
|
137 | // We do the call with settings so we can set cache to false. | |
139 | var settings = { |
|
138 | var settings = { | |
140 | processData : false, |
|
139 | processData : false, | |
141 | cache : false, |
|
140 | cache : false, | |
142 | type : "PUT", |
|
141 | type : "PUT", | |
143 | data : JSON.stringify(model), |
|
142 | data : JSON.stringify(model), | |
144 | contentType: 'application/json', |
|
143 | contentType: 'application/json', | |
145 | success : $.proxy(notebook.save_notebook_success, that, start), |
|
144 | success : $.proxy(notebook.save_notebook_success, that, start), | |
146 | error : $.proxy(notebook.save_notebook_error, that) |
|
145 | error : $.proxy(notebook.save_notebook_error, that) | |
147 | }; |
|
146 | }; | |
148 | if (extra_settings) { |
|
147 | if (extra_settings) { | |
149 | for (var key in extra_settings) { |
|
148 | for (var key in extra_settings) { | |
150 | settings[key] = extra_settings[key]; |
|
149 | settings[key] = extra_settings[key]; | |
151 | } |
|
150 | } | |
152 | } |
|
151 | } | |
153 | notebook.events.trigger('notebook_saving.Notebook'); |
|
152 | notebook.events.trigger('notebook_saving.Notebook'); | |
154 | var url = utils.url_join_encode( |
|
153 | var url = utils.url_join_encode( | |
155 | notebook.base_url, |
|
154 | notebook.base_url, | |
156 | 'api/notebooks', |
|
155 | 'api/notebooks', | |
157 | notebook.notebook_path, |
|
156 | notebook.notebook_path, | |
158 | notebook.notebook_name |
|
157 | notebook.notebook_name | |
159 | ); |
|
158 | ); | |
160 | $.ajax(url, settings); |
|
159 | $.ajax(url, settings); | |
161 | }; |
|
160 | }; | |
162 |
|
161 | |||
163 | ContentManager.prototype.save_checkpoint = function() { |
|
162 | ContentManager.prototype.save_checkpoint = function() { | |
164 | // This is not necessary - integrated into save |
|
163 | // This is not necessary - integrated into save | |
165 | }; |
|
164 | }; | |
166 |
|
165 | |||
167 | ContentManager.prototype.restore_checkpoint = function(notebook, id) { |
|
166 | ContentManager.prototype.restore_checkpoint = function(notebook, id) { | |
168 | that = notebook; |
|
167 | that = notebook; | |
169 | this.events.trigger('notebook_restoring.Notebook', checkpoint); |
|
168 | this.events.trigger('notebook_restoring.Notebook', checkpoint); | |
170 | var url = utils.url_join_encode( |
|
169 | var url = utils.url_join_encode( | |
171 | this.base_url, |
|
170 | this.base_url, | |
172 | 'api/notebooks', |
|
171 | 'api/notebooks', | |
173 | this.notebook_path, |
|
172 | this.notebook_path, | |
174 | this.notebook_name, |
|
173 | this.notebook_name, | |
175 | 'checkpoints', |
|
174 | 'checkpoints', | |
176 | checkpoint |
|
175 | checkpoint | |
177 | ); |
|
176 | ); | |
178 | $.post(url).done( |
|
177 | $.post(url).done( | |
179 | $.proxy(that.restore_checkpoint_success, that) |
|
178 | $.proxy(that.restore_checkpoint_success, that) | |
180 | ).fail( |
|
179 | ).fail( | |
181 | $.proxy(that.restore_checkpoint_error, that) |
|
180 | $.proxy(that.restore_checkpoint_error, that) | |
182 | ); |
|
181 | ); | |
183 | }; |
|
182 | }; | |
184 |
|
183 | |||
185 | ContentManager.prototype.list_checkpoints = function(notebook) { |
|
184 | ContentManager.prototype.list_checkpoints = function(notebook) { | |
186 | that = notebook; |
|
185 | that = notebook; | |
187 | var url = utils.url_join_encode( |
|
186 | var url = utils.url_join_encode( | |
188 | that.base_url, |
|
187 | that.base_url, | |
189 | 'api/notebooks', |
|
188 | 'api/notebooks', | |
190 | that.notebook_path, |
|
189 | that.notebook_path, | |
191 | that.notebook_name, |
|
190 | that.notebook_name, | |
192 | 'checkpoints' |
|
191 | 'checkpoints' | |
193 | ); |
|
192 | ); | |
194 | $.get(url).done( |
|
193 | $.get(url).done( | |
195 | $.proxy(that.list_checkpoints_success, that) |
|
194 | $.proxy(that.list_checkpoints_success, that) | |
196 | ).fail( |
|
195 | ).fail( | |
197 | $.proxy(that.list_checkpoints_error, that) |
|
196 | $.proxy(that.list_checkpoints_error, that) | |
198 | ); |
|
197 | ); | |
199 | }; |
|
198 | }; | |
200 |
|
199 | |||
201 | IPython.ContentManager = ContentManager; |
|
200 | IPython.ContentManager = ContentManager; | |
202 |
|
201 | |||
203 | return {'ContentManager': ContentManager}; |
|
202 | return {'ContentManager': ContentManager}; | |
204 | }); |
|
203 | }); |
General Comments 0
You need to be logged in to leave comments.
Login now