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