##// END OF EJS Templates
sort dashbord in frontend
Bussonnier Matthias -
Show More
@@ -1,508 +1,523 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 NotebookList.prototype.draw_notebook_list = function (list, error_msg) {
165 NotebookList.prototype.draw_notebook_list = function (list, error_msg) {
166 list.content.sort(function(a, b) {
167 if (a['type'] < b['type']) {
168 return -1;
169 }
170 if (a['type'] > b['type']) {
171 return 1;
172 }
173 if (a['name'] < b['name']) {
174 return -1;
175 }
176 if (a['name'] > b['name']) {
177 return 1;
178 }
179 return 0;
180 });
166 var message = error_msg || 'Notebook list empty.';
181 var message = error_msg || 'Notebook list empty.';
167 var item = null;
182 var item = null;
168 var model = null;
183 var model = null;
169 var len = list.content.length;
184 var len = list.content.length;
170 this.clear_list();
185 this.clear_list();
171 var n_uploads = this.element.children('.list_item').length;
186 var n_uploads = this.element.children('.list_item').length;
172 if (len === 0) {
187 if (len === 0) {
173 item = this.new_item(0);
188 item = this.new_item(0);
174 var span12 = item.children().first();
189 var span12 = item.children().first();
175 span12.empty();
190 span12.empty();
176 span12.append($('<div style="margin:auto;text-align:center;color:grey"/>').text(message));
191 span12.append($('<div style="margin:auto;text-align:center;color:grey"/>').text(message));
177 }
192 }
178 var path = this.notebook_path;
193 var path = this.notebook_path;
179 var offset = n_uploads;
194 var offset = n_uploads;
180 if (path !== '') {
195 if (path !== '') {
181 item = this.new_item(offset);
196 item = this.new_item(offset);
182 model = {
197 model = {
183 type: 'directory',
198 type: 'directory',
184 name: '..',
199 name: '..',
185 path: utils.url_path_split(path)[0],
200 path: utils.url_path_split(path)[0],
186 };
201 };
187 this.add_link(model, item);
202 this.add_link(model, item);
188 offset += 1;
203 offset += 1;
189 }
204 }
190 for (var i=0; i<len; i++) {
205 for (var i=0; i<len; i++) {
191 model = list.content[i];
206 model = list.content[i];
192 item = this.new_item(i+offset);
207 item = this.new_item(i+offset);
193 this.add_link(model, item);
208 this.add_link(model, item);
194 }
209 }
195 // Trigger an event when we've finished drawing the notebook list.
210 // Trigger an event when we've finished drawing the notebook list.
196 events.trigger('draw_notebook_list.NotebookList');
211 events.trigger('draw_notebook_list.NotebookList');
197 };
212 };
198
213
199
214
200 NotebookList.prototype.new_item = function (index) {
215 NotebookList.prototype.new_item = function (index) {
201 var item = $('<div/>').addClass("list_item").addClass("row");
216 var item = $('<div/>').addClass("list_item").addClass("row");
202 // item.addClass('list_item ui-widget ui-widget-content ui-helper-clearfix');
217 // item.addClass('list_item ui-widget ui-widget-content ui-helper-clearfix');
203 // item.css('border-top-style','none');
218 // item.css('border-top-style','none');
204 item.append($("<div/>").addClass("col-md-12").append(
219 item.append($("<div/>").addClass("col-md-12").append(
205 $('<i/>').addClass('item_icon')
220 $('<i/>').addClass('item_icon')
206 ).append(
221 ).append(
207 $("<a/>").addClass("item_link").append(
222 $("<a/>").addClass("item_link").append(
208 $("<span/>").addClass("item_name")
223 $("<span/>").addClass("item_name")
209 )
224 )
210 ).append(
225 ).append(
211 $('<div/>').addClass("item_buttons btn-group pull-right")
226 $('<div/>').addClass("item_buttons btn-group pull-right")
212 ));
227 ));
213
228
214 if (index === -1) {
229 if (index === -1) {
215 this.element.append(item);
230 this.element.append(item);
216 } else {
231 } else {
217 this.element.children().eq(index).after(item);
232 this.element.children().eq(index).after(item);
218 }
233 }
219 return item;
234 return item;
220 };
235 };
221
236
222
237
223 NotebookList.icons = {
238 NotebookList.icons = {
224 directory: 'folder_icon',
239 directory: 'folder_icon',
225 notebook: 'notebook_icon',
240 notebook: 'notebook_icon',
226 file: 'file_icon',
241 file: 'file_icon',
227 };
242 };
228
243
229 NotebookList.uri_prefixes = {
244 NotebookList.uri_prefixes = {
230 directory: 'tree',
245 directory: 'tree',
231 notebook: 'notebooks',
246 notebook: 'notebooks',
232 file: 'files',
247 file: 'files',
233 };
248 };
234
249
235
250
236 NotebookList.prototype.add_link = function (model, item) {
251 NotebookList.prototype.add_link = function (model, item) {
237 var path = model.path,
252 var path = model.path,
238 name = model.name;
253 name = model.name;
239 item.data('name', name);
254 item.data('name', name);
240 item.data('path', path);
255 item.data('path', path);
241 item.find(".item_name").text(name);
256 item.find(".item_name").text(name);
242 var icon = NotebookList.icons[model.type];
257 var icon = NotebookList.icons[model.type];
243 var uri_prefix = NotebookList.uri_prefixes[model.type];
258 var uri_prefix = NotebookList.uri_prefixes[model.type];
244 item.find(".item_icon").addClass(icon).addClass('icon-fixed-width');
259 item.find(".item_icon").addClass(icon).addClass('icon-fixed-width');
245 var link = item.find("a.item_link")
260 var link = item.find("a.item_link")
246 .attr('href',
261 .attr('href',
247 utils.url_join_encode(
262 utils.url_join_encode(
248 this.base_url,
263 this.base_url,
249 uri_prefix,
264 uri_prefix,
250 path
265 path
251 )
266 )
252 );
267 );
253 // directory nav doesn't open new tabs
268 // directory nav doesn't open new tabs
254 // files, notebooks do
269 // files, notebooks do
255 if (model.type !== "directory") {
270 if (model.type !== "directory") {
256 link.attr('target','_blank');
271 link.attr('target','_blank');
257 }
272 }
258 if (model.type !== 'directory') {
273 if (model.type !== 'directory') {
259 this.add_duplicate_button(item);
274 this.add_duplicate_button(item);
260 }
275 }
261 if (model.type == 'file') {
276 if (model.type == 'file') {
262 this.add_delete_button(item);
277 this.add_delete_button(item);
263 } else if (model.type == 'notebook') {
278 } else if (model.type == 'notebook') {
264 if (this.sessions[path] === undefined){
279 if (this.sessions[path] === undefined){
265 this.add_delete_button(item);
280 this.add_delete_button(item);
266 } else {
281 } else {
267 this.add_shutdown_button(item, this.sessions[path]);
282 this.add_shutdown_button(item, this.sessions[path]);
268 }
283 }
269 }
284 }
270 };
285 };
271
286
272
287
273 NotebookList.prototype.add_name_input = function (name, item, icon_type) {
288 NotebookList.prototype.add_name_input = function (name, item, icon_type) {
274 item.data('name', name);
289 item.data('name', name);
275 item.find(".item_icon").addClass(NotebookList.icons[icon_type]).addClass('icon-fixed-width');
290 item.find(".item_icon").addClass(NotebookList.icons[icon_type]).addClass('icon-fixed-width');
276 item.find(".item_name").empty().append(
291 item.find(".item_name").empty().append(
277 $('<input/>')
292 $('<input/>')
278 .addClass("filename_input")
293 .addClass("filename_input")
279 .attr('value', name)
294 .attr('value', name)
280 .attr('size', '30')
295 .attr('size', '30')
281 .attr('type', 'text')
296 .attr('type', 'text')
282 .keyup(function(event){
297 .keyup(function(event){
283 if(event.keyCode == 13){item.find('.upload_button').click();}
298 if(event.keyCode == 13){item.find('.upload_button').click();}
284 else if(event.keyCode == 27){item.remove();}
299 else if(event.keyCode == 27){item.remove();}
285 })
300 })
286 );
301 );
287 };
302 };
288
303
289
304
290 NotebookList.prototype.add_file_data = function (data, item) {
305 NotebookList.prototype.add_file_data = function (data, item) {
291 item.data('filedata', data);
306 item.data('filedata', data);
292 };
307 };
293
308
294
309
295 NotebookList.prototype.add_shutdown_button = function (item, session) {
310 NotebookList.prototype.add_shutdown_button = function (item, session) {
296 var that = this;
311 var that = this;
297 var shutdown_button = $("<button/>").text("Shutdown").addClass("btn btn-xs btn-danger").
312 var shutdown_button = $("<button/>").text("Shutdown").addClass("btn btn-xs btn-danger").
298 click(function (e) {
313 click(function (e) {
299 var settings = {
314 var settings = {
300 processData : false,
315 processData : false,
301 cache : false,
316 cache : false,
302 type : "DELETE",
317 type : "DELETE",
303 dataType : "json",
318 dataType : "json",
304 success : function () {
319 success : function () {
305 that.load_sessions();
320 that.load_sessions();
306 },
321 },
307 error : utils.log_ajax_error,
322 error : utils.log_ajax_error,
308 };
323 };
309 var url = utils.url_join_encode(
324 var url = utils.url_join_encode(
310 that.base_url,
325 that.base_url,
311 'api/sessions',
326 'api/sessions',
312 session
327 session
313 );
328 );
314 $.ajax(url, settings);
329 $.ajax(url, settings);
315 return false;
330 return false;
316 });
331 });
317 item.find(".item_buttons").append(shutdown_button);
332 item.find(".item_buttons").append(shutdown_button);
318 };
333 };
319
334
320 NotebookList.prototype.add_duplicate_button = function (item) {
335 NotebookList.prototype.add_duplicate_button = function (item) {
321 var notebooklist = this;
336 var notebooklist = this;
322 var duplicate_button = $("<button/>").text("Duplicate").addClass("btn btn-default btn-xs").
337 var duplicate_button = $("<button/>").text("Duplicate").addClass("btn btn-default btn-xs").
323 click(function (e) {
338 click(function (e) {
324 // $(this) is the button that was clicked.
339 // $(this) is the button that was clicked.
325 var that = $(this);
340 var that = $(this);
326 var name = item.data('name');
341 var name = item.data('name');
327 var path = item.data('path');
342 var path = item.data('path');
328 var message = 'Are you sure you want to duplicate ' + name + '?';
343 var message = 'Are you sure you want to duplicate ' + name + '?';
329 var copy_from = {copy_from : path};
344 var copy_from = {copy_from : path};
330 IPython.dialog.modal({
345 IPython.dialog.modal({
331 title : "Duplicate " + name,
346 title : "Duplicate " + name,
332 body : message,
347 body : message,
333 buttons : {
348 buttons : {
334 Duplicate : {
349 Duplicate : {
335 class: "btn-primary",
350 class: "btn-primary",
336 click: function() {
351 click: function() {
337 notebooklist.contents.copy(path, notebooklist.notebook_path).then(function () {
352 notebooklist.contents.copy(path, notebooklist.notebook_path).then(function () {
338 notebooklist.load_list();
353 notebooklist.load_list();
339 });
354 });
340 }
355 }
341 },
356 },
342 Cancel : {}
357 Cancel : {}
343 }
358 }
344 });
359 });
345 return false;
360 return false;
346 });
361 });
347 item.find(".item_buttons").append(duplicate_button);
362 item.find(".item_buttons").append(duplicate_button);
348 };
363 };
349
364
350 NotebookList.prototype.add_delete_button = function (item) {
365 NotebookList.prototype.add_delete_button = function (item) {
351 var notebooklist = this;
366 var notebooklist = this;
352 var delete_button = $("<button/>").text("Delete").addClass("btn btn-default btn-xs").
367 var delete_button = $("<button/>").text("Delete").addClass("btn btn-default btn-xs").
353 click(function (e) {
368 click(function (e) {
354 // $(this) is the button that was clicked.
369 // $(this) is the button that was clicked.
355 var that = $(this);
370 var that = $(this);
356 // We use the filename from the parent list_item element's
371 // We use the filename from the parent list_item element's
357 // data because the outer scope's values change as we iterate through the loop.
372 // data because the outer scope's values change as we iterate through the loop.
358 var parent_item = that.parents('div.list_item');
373 var parent_item = that.parents('div.list_item');
359 var name = parent_item.data('name');
374 var name = parent_item.data('name');
360 var path = parent_item.data('path');
375 var path = parent_item.data('path');
361 var message = 'Are you sure you want to permanently delete the file: ' + name + '?';
376 var message = 'Are you sure you want to permanently delete the file: ' + name + '?';
362 dialog.modal({
377 dialog.modal({
363 title : "Delete file",
378 title : "Delete file",
364 body : message,
379 body : message,
365 buttons : {
380 buttons : {
366 Delete : {
381 Delete : {
367 class: "btn-danger",
382 class: "btn-danger",
368 click: function() {
383 click: function() {
369 notebooklist.contents.delete(path).then(
384 notebooklist.contents.delete(path).then(
370 function() {
385 function() {
371 notebooklist.notebook_deleted(path);
386 notebooklist.notebook_deleted(path);
372 }
387 }
373 );
388 );
374 }
389 }
375 },
390 },
376 Cancel : {}
391 Cancel : {}
377 }
392 }
378 });
393 });
379 return false;
394 return false;
380 });
395 });
381 item.find(".item_buttons").append(delete_button);
396 item.find(".item_buttons").append(delete_button);
382 };
397 };
383
398
384 NotebookList.prototype.notebook_deleted = function(path) {
399 NotebookList.prototype.notebook_deleted = function(path) {
385 /**
400 /**
386 * Remove the deleted notebook.
401 * Remove the deleted notebook.
387 */
402 */
388 $( ":data(path)" ).each(function() {
403 $( ":data(path)" ).each(function() {
389 var element = $(this);
404 var element = $(this);
390 if (element.data("path") == path) {
405 if (element.data("path") == path) {
391 element.remove();
406 element.remove();
392 events.trigger('notebook_deleted.NotebookList');
407 events.trigger('notebook_deleted.NotebookList');
393 }
408 }
394 });
409 });
395 };
410 };
396
411
397
412
398 NotebookList.prototype.add_upload_button = function (item) {
413 NotebookList.prototype.add_upload_button = function (item) {
399 var that = this;
414 var that = this;
400 var upload_button = $('<button/>').text("Upload")
415 var upload_button = $('<button/>').text("Upload")
401 .addClass('btn btn-primary btn-xs upload_button')
416 .addClass('btn btn-primary btn-xs upload_button')
402 .click(function (e) {
417 .click(function (e) {
403 var filename = item.find('.item_name > input').val();
418 var filename = item.find('.item_name > input').val();
404 var path = utils.url_path_join(that.notebook_path, filename);
419 var path = utils.url_path_join(that.notebook_path, filename);
405 var filedata = item.data('filedata');
420 var filedata = item.data('filedata');
406 var format = 'text';
421 var format = 'text';
407 if (filename.length === 0 || filename[0] === '.') {
422 if (filename.length === 0 || filename[0] === '.') {
408 dialog.modal({
423 dialog.modal({
409 title : 'Invalid file name',
424 title : 'Invalid file name',
410 body : "File names must be at least one character and not start with a dot",
425 body : "File names must be at least one character and not start with a dot",
411 buttons : {'OK' : { 'class' : 'btn-primary' }}
426 buttons : {'OK' : { 'class' : 'btn-primary' }}
412 });
427 });
413 return false;
428 return false;
414 }
429 }
415 if (filedata instanceof ArrayBuffer) {
430 if (filedata instanceof ArrayBuffer) {
416 // base64-encode binary file data
431 // base64-encode binary file data
417 var bytes = '';
432 var bytes = '';
418 var buf = new Uint8Array(filedata);
433 var buf = new Uint8Array(filedata);
419 var nbytes = buf.byteLength;
434 var nbytes = buf.byteLength;
420 for (var i=0; i<nbytes; i++) {
435 for (var i=0; i<nbytes; i++) {
421 bytes += String.fromCharCode(buf[i]);
436 bytes += String.fromCharCode(buf[i]);
422 }
437 }
423 filedata = btoa(bytes);
438 filedata = btoa(bytes);
424 format = 'base64';
439 format = 'base64';
425 }
440 }
426 var model = {};
441 var model = {};
427
442
428 var name_and_ext = utils.splitext(filename);
443 var name_and_ext = utils.splitext(filename);
429 var file_ext = name_and_ext[1];
444 var file_ext = name_and_ext[1];
430 var content_type;
445 var content_type;
431 if (file_ext === '.ipynb') {
446 if (file_ext === '.ipynb') {
432 model.type = 'notebook';
447 model.type = 'notebook';
433 model.format = 'json';
448 model.format = 'json';
434 try {
449 try {
435 model.content = JSON.parse(filedata);
450 model.content = JSON.parse(filedata);
436 } catch (e) {
451 } catch (e) {
437 dialog.modal({
452 dialog.modal({
438 title : 'Cannot upload invalid Notebook',
453 title : 'Cannot upload invalid Notebook',
439 body : "The error was: " + e,
454 body : "The error was: " + e,
440 buttons : {'OK' : {
455 buttons : {'OK' : {
441 'class' : 'btn-primary',
456 'class' : 'btn-primary',
442 click: function () {
457 click: function () {
443 item.remove();
458 item.remove();
444 }
459 }
445 }}
460 }}
446 });
461 });
447 return false;
462 return false;
448 }
463 }
449 content_type = 'application/json';
464 content_type = 'application/json';
450 } else {
465 } else {
451 model.type = 'file';
466 model.type = 'file';
452 model.format = format;
467 model.format = format;
453 model.content = filedata;
468 model.content = filedata;
454 content_type = 'application/octet-stream';
469 content_type = 'application/octet-stream';
455 }
470 }
456 filedata = item.data('filedata');
471 filedata = item.data('filedata');
457
472
458 var on_success = function () {
473 var on_success = function () {
459 item.removeClass('new-file');
474 item.removeClass('new-file');
460 that.add_link(model, item);
475 that.add_link(model, item);
461 that.add_delete_button(item);
476 that.add_delete_button(item);
462 that.session_list.load_sessions();
477 that.session_list.load_sessions();
463 };
478 };
464
479
465 var exists = false;
480 var exists = false;
466 $.each(that.element.find('.list_item:not(.new-file)'), function(k,v){
481 $.each(that.element.find('.list_item:not(.new-file)'), function(k,v){
467 if ($(v).data('name') === filename) { exists = true; return false; }
482 if ($(v).data('name') === filename) { exists = true; return false; }
468 });
483 });
469
484
470 if (exists) {
485 if (exists) {
471 dialog.modal({
486 dialog.modal({
472 title : "Replace file",
487 title : "Replace file",
473 body : 'There is already a file named ' + filename + ', do you want to replace it?',
488 body : 'There is already a file named ' + filename + ', do you want to replace it?',
474 buttons : {
489 buttons : {
475 Overwrite : {
490 Overwrite : {
476 class: "btn-danger",
491 class: "btn-danger",
477 click: function () {
492 click: function () {
478 that.contents.save(path, model).then(on_success);
493 that.contents.save(path, model).then(on_success);
479 }
494 }
480 },
495 },
481 Cancel : {
496 Cancel : {
482 click: function() { item.remove(); }
497 click: function() { item.remove(); }
483 }
498 }
484 }
499 }
485 });
500 });
486 } else {
501 } else {
487 that.contents.save(path, model).then(on_success);
502 that.contents.save(path, model).then(on_success);
488 }
503 }
489
504
490 return false;
505 return false;
491 });
506 });
492 var cancel_button = $('<button/>').text("Cancel")
507 var cancel_button = $('<button/>').text("Cancel")
493 .addClass("btn btn-default btn-xs")
508 .addClass("btn btn-default btn-xs")
494 .click(function (e) {
509 .click(function (e) {
495 item.remove();
510 item.remove();
496 return false;
511 return false;
497 });
512 });
498 item.find(".item_buttons").empty()
513 item.find(".item_buttons").empty()
499 .append(upload_button)
514 .append(upload_button)
500 .append(cancel_button);
515 .append(cancel_button);
501 };
516 };
502
517
503
518
504 // Backwards compatability.
519 // Backwards compatability.
505 IPython.NotebookList = NotebookList;
520 IPython.NotebookList = NotebookList;
506
521
507 return {'NotebookList': NotebookList};
522 return {'NotebookList': NotebookList};
508 });
523 });
General Comments 0
You need to be logged in to leave comments. Login now