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