Show More
@@ -1,486 +1,486 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 | "use strict"; |
|
10 | "use strict"; | |
11 |
|
11 | |||
12 | var NotebookList = function (selector, options) { |
|
12 | var NotebookList = function (selector, options) { | |
13 | // Constructor |
|
13 | // Constructor | |
14 | // |
|
14 | // | |
15 | // Parameters: |
|
15 | // Parameters: | |
16 | // selector: string |
|
16 | // selector: string | |
17 | // options: dictionary |
|
17 | // options: dictionary | |
18 | // Dictionary of keyword arguments. |
|
18 | // Dictionary of keyword arguments. | |
19 | // session_list: SessionList instance |
|
19 | // session_list: SessionList instance | |
20 | // element_name: string |
|
20 | // element_name: string | |
21 | // base_url: string |
|
21 | // base_url: string | |
22 | // notebook_path: string |
|
22 | // notebook_path: string | |
23 | // contents: Contents instance |
|
23 | // contents: Contents instance | |
24 | var that = this; |
|
24 | var that = this; | |
25 | this.session_list = options.session_list; |
|
25 | this.session_list = options.session_list; | |
26 | // allow code re-use by just changing element_name in kernellist.js |
|
26 | // allow code re-use by just changing element_name in kernellist.js | |
27 | this.element_name = options.element_name || 'notebook'; |
|
27 | this.element_name = options.element_name || 'notebook'; | |
28 | this.selector = selector; |
|
28 | this.selector = selector; | |
29 | if (this.selector !== undefined) { |
|
29 | if (this.selector !== undefined) { | |
30 | this.element = $(selector); |
|
30 | this.element = $(selector); | |
31 | this.style(); |
|
31 | this.style(); | |
32 | this.bind_events(); |
|
32 | this.bind_events(); | |
33 | } |
|
33 | } | |
34 | this.notebooks_list = []; |
|
34 | this.notebooks_list = []; | |
35 | this.sessions = {}; |
|
35 | this.sessions = {}; | |
36 | this.base_url = options.base_url || utils.get_body_data("baseUrl"); |
|
36 | this.base_url = options.base_url || utils.get_body_data("baseUrl"); | |
37 | this.notebook_path = options.notebook_path || utils.get_body_data("notebookPath"); |
|
37 | this.notebook_path = options.notebook_path || utils.get_body_data("notebookPath"); | |
38 | this.contents = options.contents; |
|
38 | this.contents = options.contents; | |
39 | if (this.session_list && this.session_list.events) { |
|
39 | if (this.session_list && this.session_list.events) { | |
40 | this.session_list.events.on('sessions_loaded.Dashboard', |
|
40 | this.session_list.events.on('sessions_loaded.Dashboard', | |
41 | function(e, d) { that.sessions_loaded(d); }); |
|
41 | function(e, d) { that.sessions_loaded(d); }); | |
42 | } |
|
42 | } | |
43 | }; |
|
43 | }; | |
44 |
|
44 | |||
45 | NotebookList.prototype.style = function () { |
|
45 | NotebookList.prototype.style = function () { | |
46 | var prefix = '#' + this.element_name; |
|
46 | var prefix = '#' + this.element_name; | |
47 | $(prefix + '_toolbar').addClass('list_toolbar'); |
|
47 | $(prefix + '_toolbar').addClass('list_toolbar'); | |
48 | $(prefix + '_list_info').addClass('toolbar_info'); |
|
48 | $(prefix + '_list_info').addClass('toolbar_info'); | |
49 | $(prefix + '_buttons').addClass('toolbar_buttons'); |
|
49 | $(prefix + '_buttons').addClass('toolbar_buttons'); | |
50 | $(prefix + '_list_header').addClass('list_header'); |
|
50 | $(prefix + '_list_header').addClass('list_header'); | |
51 | this.element.addClass("list_container"); |
|
51 | this.element.addClass("list_container"); | |
52 | }; |
|
52 | }; | |
53 |
|
53 | |||
54 |
|
54 | |||
55 | NotebookList.prototype.bind_events = function () { |
|
55 | NotebookList.prototype.bind_events = function () { | |
56 | var that = this; |
|
56 | var that = this; | |
57 | $('#refresh_' + this.element_name + '_list').click(function () { |
|
57 | $('#refresh_' + this.element_name + '_list').click(function () { | |
58 | that.load_sessions(); |
|
58 | that.load_sessions(); | |
59 | }); |
|
59 | }); | |
60 | this.element.bind('dragover', function () { |
|
60 | this.element.bind('dragover', function () { | |
61 | return false; |
|
61 | return false; | |
62 | }); |
|
62 | }); | |
63 | this.element.bind('drop', function(event){ |
|
63 | this.element.bind('drop', function(event){ | |
64 | that.handleFilesUpload(event,'drop'); |
|
64 | that.handleFilesUpload(event,'drop'); | |
65 | return false; |
|
65 | return false; | |
66 | }); |
|
66 | }); | |
67 | }; |
|
67 | }; | |
68 |
|
68 | |||
69 | NotebookList.prototype.handleFilesUpload = function(event, dropOrForm) { |
|
69 | NotebookList.prototype.handleFilesUpload = function(event, dropOrForm) { | |
70 | var that = this; |
|
70 | var that = this; | |
71 | var files; |
|
71 | var files; | |
72 | if(dropOrForm =='drop'){ |
|
72 | if(dropOrForm =='drop'){ | |
73 | files = event.originalEvent.dataTransfer.files; |
|
73 | files = event.originalEvent.dataTransfer.files; | |
74 | } else |
|
74 | } else | |
75 | { |
|
75 | { | |
76 | files = event.originalEvent.target.files; |
|
76 | files = event.originalEvent.target.files; | |
77 | } |
|
77 | } | |
78 | for (var i = 0; i < files.length; i++) { |
|
78 | for (var i = 0; i < files.length; i++) { | |
79 | var f = files[i]; |
|
79 | var f = files[i]; | |
80 | var name_and_ext = utils.splitext(f.name); |
|
80 | var name_and_ext = utils.splitext(f.name); | |
81 | var file_ext = name_and_ext[1]; |
|
81 | var file_ext = name_and_ext[1]; | |
82 |
|
82 | |||
83 | var reader = new FileReader(); |
|
83 | var reader = new FileReader(); | |
84 | if (file_ext === '.ipynb') { |
|
84 | if (file_ext === '.ipynb') { | |
85 | reader.readAsText(f); |
|
85 | reader.readAsText(f); | |
86 | } else { |
|
86 | } else { | |
87 | // read non-notebook files as binary |
|
87 | // read non-notebook files as binary | |
88 | reader.readAsArrayBuffer(f); |
|
88 | reader.readAsArrayBuffer(f); | |
89 | } |
|
89 | } | |
90 | var item = that.new_item(0); |
|
90 | var item = that.new_item(0); | |
91 | item.addClass('new-file'); |
|
91 | item.addClass('new-file'); | |
92 | that.add_name_input(f.name, item, file_ext == '.ipynb' ? 'notebook' : 'file'); |
|
92 | that.add_name_input(f.name, item, file_ext == '.ipynb' ? 'notebook' : 'file'); | |
93 | // Store the list item in the reader so we can use it later |
|
93 | // Store the list item in the reader so we can use it later | |
94 | // to know which item it belongs to. |
|
94 | // to know which item it belongs to. | |
95 | $(reader).data('item', item); |
|
95 | $(reader).data('item', item); | |
96 | reader.onload = function (event) { |
|
96 | reader.onload = function (event) { | |
97 | var item = $(event.target).data('item'); |
|
97 | var item = $(event.target).data('item'); | |
98 | that.add_file_data(event.target.result, item); |
|
98 | that.add_file_data(event.target.result, item); | |
99 | that.add_upload_button(item); |
|
99 | that.add_upload_button(item); | |
100 | }; |
|
100 | }; | |
101 | reader.onerror = function (event) { |
|
101 | reader.onerror = function (event) { | |
102 | var item = $(event.target).data('item'); |
|
102 | var item = $(event.target).data('item'); | |
103 | var name = item.data('name') |
|
103 | var name = item.data('name') | |
104 | item.remove(); |
|
104 | item.remove(); | |
105 | dialog.modal({ |
|
105 | dialog.modal({ | |
106 | title : 'Failed to read file', |
|
106 | title : 'Failed to read file', | |
107 | body : "Failed to read file '" + name + "'", |
|
107 | body : "Failed to read file '" + name + "'", | |
108 | buttons : {'OK' : { 'class' : 'btn-primary' }} |
|
108 | buttons : {'OK' : { 'class' : 'btn-primary' }} | |
109 | }); |
|
109 | }); | |
110 | }; |
|
110 | }; | |
111 | } |
|
111 | } | |
112 | // Replace the file input form wth a clone of itself. This is required to |
|
112 | // Replace the file input form wth a clone of itself. This is required to | |
113 | // reset the form. Otherwise, if you upload a file, delete it and try to |
|
113 | // reset the form. Otherwise, if you upload a file, delete it and try to | |
114 | // upload it again, the changed event won't fire. |
|
114 | // upload it again, the changed event won't fire. | |
115 | var form = $('input.fileinput'); |
|
115 | var form = $('input.fileinput'); | |
116 | form.replaceWith(form.clone(true)); |
|
116 | form.replaceWith(form.clone(true)); | |
117 | return false; |
|
117 | return false; | |
118 | }; |
|
118 | }; | |
119 |
|
119 | |||
120 | NotebookList.prototype.clear_list = function (remove_uploads) { |
|
120 | NotebookList.prototype.clear_list = function (remove_uploads) { | |
121 | // Clears the navigation tree. |
|
121 | // Clears the navigation tree. | |
122 | // |
|
122 | // | |
123 | // Parameters |
|
123 | // Parameters | |
124 | // remove_uploads: bool=False |
|
124 | // remove_uploads: bool=False | |
125 | // Should upload prompts also be removed from the tree. |
|
125 | // Should upload prompts also be removed from the tree. | |
126 | if (remove_uploads) { |
|
126 | if (remove_uploads) { | |
127 | this.element.children('.list_item').remove(); |
|
127 | this.element.children('.list_item').remove(); | |
128 | } else { |
|
128 | } else { | |
129 | this.element.children('.list_item:not(.new-file)').remove(); |
|
129 | this.element.children('.list_item:not(.new-file)').remove(); | |
130 | } |
|
130 | } | |
131 | }; |
|
131 | }; | |
132 |
|
132 | |||
133 | NotebookList.prototype.load_sessions = function(){ |
|
133 | NotebookList.prototype.load_sessions = function(){ | |
134 | this.session_list.load_sessions(); |
|
134 | this.session_list.load_sessions(); | |
135 | }; |
|
135 | }; | |
136 |
|
136 | |||
137 |
|
137 | |||
138 | NotebookList.prototype.sessions_loaded = function(data){ |
|
138 | NotebookList.prototype.sessions_loaded = function(data){ | |
139 | this.sessions = data; |
|
139 | this.sessions = data; | |
140 | this.load_list(); |
|
140 | this.load_list(); | |
141 | }; |
|
141 | }; | |
142 |
|
142 | |||
143 | NotebookList.prototype.load_list = function () { |
|
143 | NotebookList.prototype.load_list = function () { | |
144 | var that = this |
|
144 | var that = this | |
145 | this.contents.list_contents(that.notebook_path, { |
|
145 | this.contents.list_contents(that.notebook_path, { | |
146 | success: $.proxy(this.draw_notebook_list, this), |
|
146 | success: $.proxy(this.draw_notebook_list, this), | |
147 | error: function(xhr, status, error) { |
|
147 | error: function(xhr, status, error) { | |
148 | utils.log_ajax_error(xhr, status, error); |
|
148 | utils.log_ajax_error(xhr, status, error); | |
149 | that.draw_notebook_list([], "Error connecting to server."); |
|
149 | that.draw_notebook_list([], "Error connecting to server."); | |
150 | } |
|
150 | } | |
151 | }); |
|
151 | }); | |
152 | }; |
|
152 | }; | |
153 |
|
153 | |||
154 | /** |
|
154 | /** | |
155 | * Draw the list of notebooks |
|
155 | * Draw the list of notebooks | |
156 | * @method draw_notebook_list |
|
156 | * @method draw_notebook_list | |
157 | * @param {Array} list An array of dictionaries representing files or |
|
157 | * @param {Array} list An array of dictionaries representing files or | |
158 |
* direc |
|
158 | * directories. | |
159 | * @param {String} error_msg An error message |
|
159 | * @param {String} error_msg An error message | |
160 | */ |
|
160 | */ | |
161 | NotebookList.prototype.draw_notebook_list = function (list, error_msg) { |
|
161 | NotebookList.prototype.draw_notebook_list = function (list, error_msg) { | |
162 | var message = error_msg || 'Notebook list empty.'; |
|
162 | var message = error_msg || 'Notebook list empty.'; | |
163 | var item = null; |
|
163 | var item = null; | |
164 | var model = null; |
|
164 | var model = null; | |
165 | var len = list.content.length; |
|
165 | var len = list.content.length; | |
166 | this.clear_list(); |
|
166 | this.clear_list(); | |
167 | var n_uploads = this.element.children('.list_item').length; |
|
167 | var n_uploads = this.element.children('.list_item').length; | |
168 | if (len === 0) { |
|
168 | if (len === 0) { | |
169 | item = this.new_item(0); |
|
169 | item = this.new_item(0); | |
170 | var span12 = item.children().first(); |
|
170 | var span12 = item.children().first(); | |
171 | span12.empty(); |
|
171 | span12.empty(); | |
172 | span12.append($('<div style="margin:auto;text-align:center;color:grey"/>').text(message)); |
|
172 | span12.append($('<div style="margin:auto;text-align:center;color:grey"/>').text(message)); | |
173 | } |
|
173 | } | |
174 | var path = this.notebook_path; |
|
174 | var path = this.notebook_path; | |
175 | var offset = n_uploads; |
|
175 | var offset = n_uploads; | |
176 | if (path !== '') { |
|
176 | if (path !== '') { | |
177 | item = this.new_item(offset); |
|
177 | item = this.new_item(offset); | |
178 | model = { |
|
178 | model = { | |
179 | type: 'directory', |
|
179 | type: 'directory', | |
180 | name: '..', |
|
180 | name: '..', | |
181 | path: path, |
|
181 | path: path, | |
182 | }; |
|
182 | }; | |
183 | this.add_link(model, item); |
|
183 | this.add_link(model, item); | |
184 | offset += 1; |
|
184 | offset += 1; | |
185 | } |
|
185 | } | |
186 | for (var i=0; i<len; i++) { |
|
186 | for (var i=0; i<len; i++) { | |
187 | model = list.content[i]; |
|
187 | model = list.content[i]; | |
188 | item = this.new_item(i+offset); |
|
188 | item = this.new_item(i+offset); | |
189 | this.add_link(model, item); |
|
189 | this.add_link(model, item); | |
190 | } |
|
190 | } | |
191 | }; |
|
191 | }; | |
192 |
|
192 | |||
193 |
|
193 | |||
194 | NotebookList.prototype.new_item = function (index) { |
|
194 | NotebookList.prototype.new_item = function (index) { | |
195 | var item = $('<div/>').addClass("list_item").addClass("row"); |
|
195 | var item = $('<div/>').addClass("list_item").addClass("row"); | |
196 | // item.addClass('list_item ui-widget ui-widget-content ui-helper-clearfix'); |
|
196 | // item.addClass('list_item ui-widget ui-widget-content ui-helper-clearfix'); | |
197 | // item.css('border-top-style','none'); |
|
197 | // item.css('border-top-style','none'); | |
198 | item.append($("<div/>").addClass("col-md-12").append( |
|
198 | item.append($("<div/>").addClass("col-md-12").append( | |
199 | $('<i/>').addClass('item_icon') |
|
199 | $('<i/>').addClass('item_icon') | |
200 | ).append( |
|
200 | ).append( | |
201 | $("<a/>").addClass("item_link").append( |
|
201 | $("<a/>").addClass("item_link").append( | |
202 | $("<span/>").addClass("item_name") |
|
202 | $("<span/>").addClass("item_name") | |
203 | ) |
|
203 | ) | |
204 | ).append( |
|
204 | ).append( | |
205 | $('<div/>').addClass("item_buttons btn-group pull-right") |
|
205 | $('<div/>').addClass("item_buttons btn-group pull-right") | |
206 | )); |
|
206 | )); | |
207 |
|
207 | |||
208 | if (index === -1) { |
|
208 | if (index === -1) { | |
209 | this.element.append(item); |
|
209 | this.element.append(item); | |
210 | } else { |
|
210 | } else { | |
211 | this.element.children().eq(index).after(item); |
|
211 | this.element.children().eq(index).after(item); | |
212 | } |
|
212 | } | |
213 | return item; |
|
213 | return item; | |
214 | }; |
|
214 | }; | |
215 |
|
215 | |||
216 |
|
216 | |||
217 | NotebookList.icons = { |
|
217 | NotebookList.icons = { | |
218 | directory: 'folder_icon', |
|
218 | directory: 'folder_icon', | |
219 | notebook: 'notebook_icon', |
|
219 | notebook: 'notebook_icon', | |
220 | file: 'file_icon', |
|
220 | file: 'file_icon', | |
221 | }; |
|
221 | }; | |
222 |
|
222 | |||
223 | NotebookList.uri_prefixes = { |
|
223 | NotebookList.uri_prefixes = { | |
224 | directory: 'tree', |
|
224 | directory: 'tree', | |
225 | notebook: 'notebooks', |
|
225 | notebook: 'notebooks', | |
226 | file: 'files', |
|
226 | file: 'files', | |
227 | }; |
|
227 | }; | |
228 |
|
228 | |||
229 |
|
229 | |||
230 | NotebookList.prototype.add_link = function (model, item) { |
|
230 | NotebookList.prototype.add_link = function (model, item) { | |
231 | var path = model.path, |
|
231 | var path = model.path, | |
232 | name = model.name; |
|
232 | name = model.name; | |
233 | item.data('name', name); |
|
233 | item.data('name', name); | |
234 | item.data('path', path); |
|
234 | item.data('path', path); | |
235 | item.find(".item_name").text(name); |
|
235 | item.find(".item_name").text(name); | |
236 | var icon = NotebookList.icons[model.type]; |
|
236 | var icon = NotebookList.icons[model.type]; | |
237 | var uri_prefix = NotebookList.uri_prefixes[model.type]; |
|
237 | var uri_prefix = NotebookList.uri_prefixes[model.type]; | |
238 | item.find(".item_icon").addClass(icon).addClass('icon-fixed-width'); |
|
238 | item.find(".item_icon").addClass(icon).addClass('icon-fixed-width'); | |
239 | var link = item.find("a.item_link") |
|
239 | var link = item.find("a.item_link") | |
240 | .attr('href', |
|
240 | .attr('href', | |
241 | utils.url_join_encode( |
|
241 | utils.url_join_encode( | |
242 | this.base_url, |
|
242 | this.base_url, | |
243 | uri_prefix, |
|
243 | uri_prefix, | |
244 | path, |
|
244 | path, | |
245 | name |
|
245 | name | |
246 | ) |
|
246 | ) | |
247 | ); |
|
247 | ); | |
248 | // directory nav doesn't open new tabs |
|
248 | // directory nav doesn't open new tabs | |
249 | // files, notebooks do |
|
249 | // files, notebooks do | |
250 | if (model.type !== "directory") { |
|
250 | if (model.type !== "directory") { | |
251 | link.attr('target','_blank'); |
|
251 | link.attr('target','_blank'); | |
252 | } |
|
252 | } | |
253 | var path_name = utils.url_path_join(path, name); |
|
253 | var path_name = utils.url_path_join(path, name); | |
254 | if (model.type == 'file') { |
|
254 | if (model.type == 'file') { | |
255 | this.add_delete_button(item); |
|
255 | this.add_delete_button(item); | |
256 | } else if (model.type == 'notebook') { |
|
256 | } else if (model.type == 'notebook') { | |
257 | if(this.sessions[path_name] === undefined){ |
|
257 | if(this.sessions[path_name] === undefined){ | |
258 | this.add_delete_button(item); |
|
258 | this.add_delete_button(item); | |
259 | } else { |
|
259 | } else { | |
260 | this.add_shutdown_button(item, this.sessions[path_name]); |
|
260 | this.add_shutdown_button(item, this.sessions[path_name]); | |
261 | } |
|
261 | } | |
262 | } |
|
262 | } | |
263 | }; |
|
263 | }; | |
264 |
|
264 | |||
265 |
|
265 | |||
266 | NotebookList.prototype.add_name_input = function (name, item, icon_type) { |
|
266 | NotebookList.prototype.add_name_input = function (name, item, icon_type) { | |
267 | item.data('name', name); |
|
267 | item.data('name', name); | |
268 | item.find(".item_icon").addClass(NotebookList.icons[icon_type]).addClass('icon-fixed-width'); |
|
268 | item.find(".item_icon").addClass(NotebookList.icons[icon_type]).addClass('icon-fixed-width'); | |
269 | item.find(".item_name").empty().append( |
|
269 | item.find(".item_name").empty().append( | |
270 | $('<input/>') |
|
270 | $('<input/>') | |
271 | .addClass("filename_input") |
|
271 | .addClass("filename_input") | |
272 | .attr('value', name) |
|
272 | .attr('value', name) | |
273 | .attr('size', '30') |
|
273 | .attr('size', '30') | |
274 | .attr('type', 'text') |
|
274 | .attr('type', 'text') | |
275 | .keyup(function(event){ |
|
275 | .keyup(function(event){ | |
276 | if(event.keyCode == 13){item.find('.upload_button').click();} |
|
276 | if(event.keyCode == 13){item.find('.upload_button').click();} | |
277 | else if(event.keyCode == 27){item.remove();} |
|
277 | else if(event.keyCode == 27){item.remove();} | |
278 | }) |
|
278 | }) | |
279 | ); |
|
279 | ); | |
280 | }; |
|
280 | }; | |
281 |
|
281 | |||
282 |
|
282 | |||
283 | NotebookList.prototype.add_file_data = function (data, item) { |
|
283 | NotebookList.prototype.add_file_data = function (data, item) { | |
284 | item.data('filedata', data); |
|
284 | item.data('filedata', data); | |
285 | }; |
|
285 | }; | |
286 |
|
286 | |||
287 |
|
287 | |||
288 | NotebookList.prototype.add_shutdown_button = function (item, session) { |
|
288 | NotebookList.prototype.add_shutdown_button = function (item, session) { | |
289 | var that = this; |
|
289 | var that = this; | |
290 | var shutdown_button = $("<button/>").text("Shutdown").addClass("btn btn-xs btn-danger"). |
|
290 | var shutdown_button = $("<button/>").text("Shutdown").addClass("btn btn-xs btn-danger"). | |
291 | click(function (e) { |
|
291 | click(function (e) { | |
292 | var settings = { |
|
292 | var settings = { | |
293 | processData : false, |
|
293 | processData : false, | |
294 | cache : false, |
|
294 | cache : false, | |
295 | type : "DELETE", |
|
295 | type : "DELETE", | |
296 | dataType : "json", |
|
296 | dataType : "json", | |
297 | success : function () { |
|
297 | success : function () { | |
298 | that.load_sessions(); |
|
298 | that.load_sessions(); | |
299 | }, |
|
299 | }, | |
300 | error : utils.log_ajax_error, |
|
300 | error : utils.log_ajax_error, | |
301 | }; |
|
301 | }; | |
302 | var url = utils.url_join_encode( |
|
302 | var url = utils.url_join_encode( | |
303 | that.base_url, |
|
303 | that.base_url, | |
304 | 'api/sessions', |
|
304 | 'api/sessions', | |
305 | session |
|
305 | session | |
306 | ); |
|
306 | ); | |
307 | $.ajax(url, settings); |
|
307 | $.ajax(url, settings); | |
308 | return false; |
|
308 | return false; | |
309 | }); |
|
309 | }); | |
310 | // var new_buttons = item.find('a'); // shutdown_button; |
|
310 | // var new_buttons = item.find('a'); // shutdown_button; | |
311 | item.find(".item_buttons").text("").append(shutdown_button); |
|
311 | item.find(".item_buttons").text("").append(shutdown_button); | |
312 | }; |
|
312 | }; | |
313 |
|
313 | |||
314 | NotebookList.prototype.add_delete_button = function (item) { |
|
314 | NotebookList.prototype.add_delete_button = function (item) { | |
315 | var new_buttons = $('<span/>').addClass("btn-group pull-right"); |
|
315 | var new_buttons = $('<span/>').addClass("btn-group pull-right"); | |
316 | var notebooklist = this; |
|
316 | var notebooklist = this; | |
317 | var delete_button = $("<button/>").text("Delete").addClass("btn btn-default btn-xs"). |
|
317 | var delete_button = $("<button/>").text("Delete").addClass("btn btn-default btn-xs"). | |
318 | click(function (e) { |
|
318 | click(function (e) { | |
319 | // $(this) is the button that was clicked. |
|
319 | // $(this) is the button that was clicked. | |
320 | var that = $(this); |
|
320 | var that = $(this); | |
321 | // We use the filename from the parent list_item element's |
|
321 | // We use the filename from the parent list_item element's | |
322 | // data because the outer scope's values change as we iterate through the loop. |
|
322 | // data because the outer scope's values change as we iterate through the loop. | |
323 | var parent_item = that.parents('div.list_item'); |
|
323 | var parent_item = that.parents('div.list_item'); | |
324 | var name = parent_item.data('nbname'); |
|
324 | var name = parent_item.data('nbname'); | |
325 | var path = parent_item.data('path'); |
|
325 | var path = parent_item.data('path'); | |
326 | var message = 'Are you sure you want to permanently delete the file: ' + nbname + '?'; |
|
326 | var message = 'Are you sure you want to permanently delete the file: ' + nbname + '?'; | |
327 | dialog.modal({ |
|
327 | dialog.modal({ | |
328 | title : "Delete file", |
|
328 | title : "Delete file", | |
329 | body : message, |
|
329 | body : message, | |
330 | buttons : { |
|
330 | buttons : { | |
331 | Delete : { |
|
331 | Delete : { | |
332 | class: "btn-danger", |
|
332 | class: "btn-danger", | |
333 | click: function() { |
|
333 | click: function() { | |
334 | notebooklist.contents.delete_file(name, path, { |
|
334 | notebooklist.contents.delete_file(name, path, { | |
335 | success: function() { |
|
335 | success: function() { | |
336 | notebooklist.notebook_deleted(path, name); |
|
336 | notebooklist.notebook_deleted(path, name); | |
337 | } |
|
337 | } | |
338 | }); |
|
338 | }); | |
339 | } |
|
339 | } | |
340 | }, |
|
340 | }, | |
341 | Cancel : {} |
|
341 | Cancel : {} | |
342 | } |
|
342 | } | |
343 | }); |
|
343 | }); | |
344 | return false; |
|
344 | return false; | |
345 | }); |
|
345 | }); | |
346 | item.find(".item_buttons").text("").append(delete_button); |
|
346 | item.find(".item_buttons").text("").append(delete_button); | |
347 | }; |
|
347 | }; | |
348 |
|
348 | |||
349 | NotebookList.prototype.notebook_deleted = function(path, name) { |
|
349 | NotebookList.prototype.notebook_deleted = function(path, name) { | |
350 | // Remove the deleted notebook. |
|
350 | // Remove the deleted notebook. | |
351 | $( ":data(nbname)" ).each(function() { |
|
351 | $( ":data(nbname)" ).each(function() { | |
352 | var element = $( this ); |
|
352 | var element = $( this ); | |
353 | if (element.data( "nbname" ) == d.name && |
|
353 | if (element.data( "nbname" ) == d.name && | |
354 | element.data( "path" ) == d.path) { |
|
354 | element.data( "path" ) == d.path) { | |
355 | element.remove(); |
|
355 | element.remove(); | |
356 | } |
|
356 | } | |
357 | }); |
|
357 | }); | |
358 | } |
|
358 | } | |
359 |
|
359 | |||
360 |
|
360 | |||
361 | NotebookList.prototype.add_upload_button = function (item, type) { |
|
361 | NotebookList.prototype.add_upload_button = function (item, type) { | |
362 | var that = this; |
|
362 | var that = this; | |
363 | var upload_button = $('<button/>').text("Upload") |
|
363 | var upload_button = $('<button/>').text("Upload") | |
364 | .addClass('btn btn-primary btn-xs upload_button') |
|
364 | .addClass('btn btn-primary btn-xs upload_button') | |
365 | .click(function (e) { |
|
365 | .click(function (e) { | |
366 | var path = that.notebook_path; |
|
366 | var path = that.notebook_path; | |
367 | var filename = item.find('.item_name > input').val(); |
|
367 | var filename = item.find('.item_name > input').val(); | |
368 | var filedata = item.data('filedata'); |
|
368 | var filedata = item.data('filedata'); | |
369 | var format = 'text'; |
|
369 | var format = 'text'; | |
370 | if (filename.length === 0 || filename[0] === '.') { |
|
370 | if (filename.length === 0 || filename[0] === '.') { | |
371 | dialog.modal({ |
|
371 | dialog.modal({ | |
372 | title : 'Invalid file name', |
|
372 | title : 'Invalid file name', | |
373 | body : "File names must be at least one character and not start with a dot", |
|
373 | body : "File names must be at least one character and not start with a dot", | |
374 | buttons : {'OK' : { 'class' : 'btn-primary' }} |
|
374 | buttons : {'OK' : { 'class' : 'btn-primary' }} | |
375 | }); |
|
375 | }); | |
376 | return false; |
|
376 | return false; | |
377 | } |
|
377 | } | |
378 | if (filedata instanceof ArrayBuffer) { |
|
378 | if (filedata instanceof ArrayBuffer) { | |
379 | // base64-encode binary file data |
|
379 | // base64-encode binary file data | |
380 | var bytes = ''; |
|
380 | var bytes = ''; | |
381 | var buf = new Uint8Array(filedata); |
|
381 | var buf = new Uint8Array(filedata); | |
382 | var nbytes = buf.byteLength; |
|
382 | var nbytes = buf.byteLength; | |
383 | for (var i=0; i<nbytes; i++) { |
|
383 | for (var i=0; i<nbytes; i++) { | |
384 | bytes += String.fromCharCode(buf[i]); |
|
384 | bytes += String.fromCharCode(buf[i]); | |
385 | } |
|
385 | } | |
386 | filedata = btoa(bytes); |
|
386 | filedata = btoa(bytes); | |
387 | format = 'base64'; |
|
387 | format = 'base64'; | |
388 | } |
|
388 | } | |
389 | var model = { |
|
389 | var model = { | |
390 | path: path, |
|
390 | path: path, | |
391 | name: filename |
|
391 | name: filename | |
392 | }; |
|
392 | }; | |
393 |
|
393 | |||
394 | var name_and_ext = utils.splitext(filename); |
|
394 | var name_and_ext = utils.splitext(filename); | |
395 | var file_ext = name_and_ext[1]; |
|
395 | var file_ext = name_and_ext[1]; | |
396 | var content_type; |
|
396 | var content_type; | |
397 | if (file_ext === '.ipynb') { |
|
397 | if (file_ext === '.ipynb') { | |
398 | model.type = 'notebook'; |
|
398 | model.type = 'notebook'; | |
399 | model.format = 'json'; |
|
399 | model.format = 'json'; | |
400 | try { |
|
400 | try { | |
401 | model.content = JSON.parse(filedata); |
|
401 | model.content = JSON.parse(filedata); | |
402 | } catch (e) { |
|
402 | } catch (e) { | |
403 | dialog.modal({ |
|
403 | dialog.modal({ | |
404 | title : 'Cannot upload invalid Notebook', |
|
404 | title : 'Cannot upload invalid Notebook', | |
405 | body : "The error was: " + e, |
|
405 | body : "The error was: " + e, | |
406 | buttons : {'OK' : { |
|
406 | buttons : {'OK' : { | |
407 | 'class' : 'btn-primary', |
|
407 | 'class' : 'btn-primary', | |
408 | click: function () { |
|
408 | click: function () { | |
409 | item.remove(); |
|
409 | item.remove(); | |
410 | } |
|
410 | } | |
411 | }} |
|
411 | }} | |
412 | }); |
|
412 | }); | |
413 | return false; |
|
413 | return false; | |
414 | } |
|
414 | } | |
415 | content_type = 'application/json'; |
|
415 | content_type = 'application/json'; | |
416 | } else { |
|
416 | } else { | |
417 | model.type = 'file'; |
|
417 | model.type = 'file'; | |
418 | model.format = format; |
|
418 | model.format = format; | |
419 | model.content = filedata; |
|
419 | model.content = filedata; | |
420 | content_type = 'application/octet-stream'; |
|
420 | content_type = 'application/octet-stream'; | |
421 | } |
|
421 | } | |
422 | var filedata = item.data('filedata'); |
|
422 | var filedata = item.data('filedata'); | |
423 |
|
423 | |||
424 | var settings = { |
|
424 | var settings = { | |
425 | processData : false, |
|
425 | processData : false, | |
426 | cache : false, |
|
426 | cache : false, | |
427 | type : 'PUT', |
|
427 | type : 'PUT', | |
428 | data : JSON.stringify(model), |
|
428 | data : JSON.stringify(model), | |
429 | contentType: content_type, |
|
429 | contentType: content_type, | |
430 | success : function (data, status, xhr) { |
|
430 | success : function (data, status, xhr) { | |
431 | item.removeClass('new-file'); |
|
431 | item.removeClass('new-file'); | |
432 | that.add_link(model, item); |
|
432 | that.add_link(model, item); | |
433 | that.add_delete_button(item); |
|
433 | that.add_delete_button(item); | |
434 | that.session_list.load_sessions(); |
|
434 | that.session_list.load_sessions(); | |
435 | }, |
|
435 | }, | |
436 | error : utils.log_ajax_error, |
|
436 | error : utils.log_ajax_error, | |
437 | }; |
|
437 | }; | |
438 |
|
438 | |||
439 | var url = utils.url_join_encode( |
|
439 | var url = utils.url_join_encode( | |
440 | that.base_url, |
|
440 | that.base_url, | |
441 | 'api/contents', |
|
441 | 'api/contents', | |
442 | that.notebook_path, |
|
442 | that.notebook_path, | |
443 | filename |
|
443 | filename | |
444 | ); |
|
444 | ); | |
445 |
|
445 | |||
446 | var exists = false; |
|
446 | var exists = false; | |
447 | $.each(that.element.find('.list_item:not(.new-file)'), function(k,v){ |
|
447 | $.each(that.element.find('.list_item:not(.new-file)'), function(k,v){ | |
448 | if ($(v).data('name') === filename) { exists = true; return false; } |
|
448 | if ($(v).data('name') === filename) { exists = true; return false; } | |
449 | }); |
|
449 | }); | |
450 | if (exists) { |
|
450 | if (exists) { | |
451 | dialog.modal({ |
|
451 | dialog.modal({ | |
452 | title : "Replace file", |
|
452 | title : "Replace file", | |
453 | body : 'There is already a file named ' + filename + ', do you want to replace it?', |
|
453 | body : 'There is already a file named ' + filename + ', do you want to replace it?', | |
454 | buttons : { |
|
454 | buttons : { | |
455 | Overwrite : { |
|
455 | Overwrite : { | |
456 | class: "btn-danger", |
|
456 | class: "btn-danger", | |
457 | click: function() { $.ajax(url, settings); } |
|
457 | click: function() { $.ajax(url, settings); } | |
458 | }, |
|
458 | }, | |
459 | Cancel : { |
|
459 | Cancel : { | |
460 | click: function() { item.remove(); } |
|
460 | click: function() { item.remove(); } | |
461 | } |
|
461 | } | |
462 | } |
|
462 | } | |
463 | }); |
|
463 | }); | |
464 | } else { |
|
464 | } else { | |
465 | $.ajax(url, settings); |
|
465 | $.ajax(url, settings); | |
466 | } |
|
466 | } | |
467 |
|
467 | |||
468 | return false; |
|
468 | return false; | |
469 | }); |
|
469 | }); | |
470 | var cancel_button = $('<button/>').text("Cancel") |
|
470 | var cancel_button = $('<button/>').text("Cancel") | |
471 | .addClass("btn btn-default btn-xs") |
|
471 | .addClass("btn btn-default btn-xs") | |
472 | .click(function (e) { |
|
472 | .click(function (e) { | |
473 | item.remove(); |
|
473 | item.remove(); | |
474 | return false; |
|
474 | return false; | |
475 | }); |
|
475 | }); | |
476 | item.find(".item_buttons").empty() |
|
476 | item.find(".item_buttons").empty() | |
477 | .append(upload_button) |
|
477 | .append(upload_button) | |
478 | .append(cancel_button); |
|
478 | .append(cancel_button); | |
479 | }; |
|
479 | }; | |
480 |
|
480 | |||
481 |
|
481 | |||
482 | // Backwards compatability. |
|
482 | // Backwards compatability. | |
483 | IPython.NotebookList = NotebookList; |
|
483 | IPython.NotebookList = NotebookList; | |
484 |
|
484 | |||
485 | return {'NotebookList': NotebookList}; |
|
485 | return {'NotebookList': NotebookList}; | |
486 | }); |
|
486 | }); |
General Comments 0
You need to be logged in to leave comments.
Login now