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