##// END OF EJS Templates
Merge pull request #8059 from minrk/deselect-click...
Matthias Bussonnier -
r20782:b60c7135 merge
parent child Browse files
Show More
@@ -1,853 +1,869 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 'base/js/keyboard',
10 'base/js/keyboard',
11 ], function(IPython, $, utils, dialog, events, keyboard) {
11 ], function(IPython, $, utils, dialog, events, keyboard) {
12 "use strict";
12 "use strict";
13
13
14 var NotebookList = function (selector, options) {
14 var NotebookList = function (selector, options) {
15 /**
15 /**
16 * Constructor
16 * Constructor
17 *
17 *
18 * Parameters:
18 * Parameters:
19 * selector: string
19 * selector: string
20 * options: dictionary
20 * options: dictionary
21 * Dictionary of keyword arguments.
21 * Dictionary of keyword arguments.
22 * session_list: SessionList instance
22 * session_list: SessionList instance
23 * element_name: string
23 * element_name: string
24 * base_url: string
24 * base_url: string
25 * notebook_path: string
25 * notebook_path: string
26 * contents: Contents instance
26 * contents: Contents instance
27 */
27 */
28 var that = this;
28 var that = this;
29 this.session_list = options.session_list;
29 this.session_list = options.session_list;
30 // allow code re-use by just changing element_name in kernellist.js
30 // allow code re-use by just changing element_name in kernellist.js
31 this.element_name = options.element_name || 'notebook';
31 this.element_name = options.element_name || 'notebook';
32 this.selector = selector;
32 this.selector = selector;
33 if (this.selector !== undefined) {
33 if (this.selector !== undefined) {
34 this.element = $(selector);
34 this.element = $(selector);
35 this.style();
35 this.style();
36 this.bind_events();
36 this.bind_events();
37 }
37 }
38 this.notebooks_list = [];
38 this.notebooks_list = [];
39 this.sessions = {};
39 this.sessions = {};
40 this.base_url = options.base_url || utils.get_body_data("baseUrl");
40 this.base_url = options.base_url || utils.get_body_data("baseUrl");
41 this.notebook_path = options.notebook_path || utils.get_body_data("notebookPath");
41 this.notebook_path = options.notebook_path || utils.get_body_data("notebookPath");
42 this.contents = options.contents;
42 this.contents = options.contents;
43 if (this.session_list && this.session_list.events) {
43 if (this.session_list && this.session_list.events) {
44 this.session_list.events.on('sessions_loaded.Dashboard',
44 this.session_list.events.on('sessions_loaded.Dashboard',
45 function(e, d) { that.sessions_loaded(d); });
45 function(e, d) { that.sessions_loaded(d); });
46 }
46 }
47 this.selected = [];
47 this.selected = [];
48 };
48 };
49
49
50 NotebookList.prototype.style = function () {
50 NotebookList.prototype.style = function () {
51 var prefix = '#' + this.element_name;
51 var prefix = '#' + this.element_name;
52 $(prefix + '_toolbar').addClass('list_toolbar');
52 $(prefix + '_toolbar').addClass('list_toolbar');
53 $(prefix + '_list_info').addClass('toolbar_info');
53 $(prefix + '_list_info').addClass('toolbar_info');
54 $(prefix + '_buttons').addClass('toolbar_buttons');
54 $(prefix + '_buttons').addClass('toolbar_buttons');
55 $(prefix + '_list_header').addClass('list_header');
55 $(prefix + '_list_header').addClass('list_header');
56 this.element.addClass("list_container");
56 this.element.addClass("list_container");
57 };
57 };
58
58
59 NotebookList.prototype.bind_events = function () {
59 NotebookList.prototype.bind_events = function () {
60 var that = this;
60 var that = this;
61 $('#refresh_' + this.element_name + '_list').click(function () {
61 $('#refresh_' + this.element_name + '_list').click(function () {
62 that.load_sessions();
62 that.load_sessions();
63 });
63 });
64 this.element.bind('dragover', function () {
64 this.element.bind('dragover', function () {
65 return false;
65 return false;
66 });
66 });
67 this.element.bind('drop', function(event){
67 this.element.bind('drop', function(event){
68 that.handleFilesUpload(event,'drop');
68 that.handleFilesUpload(event,'drop');
69 return false;
69 return false;
70 });
70 });
71
71
72 // Bind events for singleton controls.
72 // Bind events for singleton controls.
73 if (!NotebookList._bound_singletons) {
73 if (!NotebookList._bound_singletons) {
74 NotebookList._bound_singletons = true;
74 NotebookList._bound_singletons = true;
75 $('#new-file').click(function(e) {
75 $('#new-file').click(function(e) {
76 var w = window.open(undefined, IPython._target);
76 var w = window.open(undefined, IPython._target);
77 that.contents.new_untitled(that.notebook_path || '', {type: 'file', ext: '.txt'}).then(function(data) {
77 that.contents.new_untitled(that.notebook_path || '', {type: 'file', ext: '.txt'}).then(function(data) {
78 var url = utils.url_join_encode(
78 var url = utils.url_join_encode(
79 that.base_url, 'edit', data.path
79 that.base_url, 'edit', data.path
80 );
80 );
81 w.location = url;
81 w.location = url;
82 }).catch(function (e) {
82 }).catch(function (e) {
83 w.close();
83 w.close();
84 dialog.modal({
84 dialog.modal({
85 title: 'Creating File Failed',
85 title: 'Creating File Failed',
86 body: $('<div/>')
86 body: $('<div/>')
87 .text("An error occurred while creating a new file.")
87 .text("An error occurred while creating a new file.")
88 .append($('<div/>')
88 .append($('<div/>')
89 .addClass('alert alert-danger')
89 .addClass('alert alert-danger')
90 .text(e.message || e)),
90 .text(e.message || e)),
91 buttons: {
91 buttons: {
92 OK: {'class': 'btn-primary'}
92 OK: {'class': 'btn-primary'}
93 }
93 }
94 });
94 });
95 console.warn('Error durring New file creation', e);
95 console.warn('Error durring New file creation', e);
96 });
96 });
97 that.load_sessions();
97 that.load_sessions();
98 });
98 });
99 $('#new-folder').click(function(e) {
99 $('#new-folder').click(function(e) {
100 that.contents.new_untitled(that.notebook_path || '', {type: 'directory'})
100 that.contents.new_untitled(that.notebook_path || '', {type: 'directory'})
101 .then(function(){
101 .then(function(){
102 that.load_list();
102 that.load_list();
103 }).catch(function (e) {
103 }).catch(function (e) {
104 dialog.modal({
104 dialog.modal({
105 title: 'Creating Folder Failed',
105 title: 'Creating Folder Failed',
106 body: $('<div/>')
106 body: $('<div/>')
107 .text("An error occurred while creating a new folder.")
107 .text("An error occurred while creating a new folder.")
108 .append($('<div/>')
108 .append($('<div/>')
109 .addClass('alert alert-danger')
109 .addClass('alert alert-danger')
110 .text(e.message || e)),
110 .text(e.message || e)),
111 buttons: {
111 buttons: {
112 OK: {'class': 'btn-primary'}
112 OK: {'class': 'btn-primary'}
113 }
113 }
114 });
114 });
115 console.warn('Error durring New directory creation', e);
115 console.warn('Error durring New directory creation', e);
116 });
116 });
117 that.load_sessions();
117 that.load_sessions();
118 });
118 });
119
119
120 // Bind events for action buttons.
120 // Bind events for action buttons.
121 $('.rename-button').click($.proxy(this.rename_selected, this));
121 $('.rename-button').click($.proxy(this.rename_selected, this));
122 $('.shutdown-button').click($.proxy(this.shutdown_selected, this));
122 $('.shutdown-button').click($.proxy(this.shutdown_selected, this));
123 $('.duplicate-button').click($.proxy(this.duplicate_selected, this));
123 $('.duplicate-button').click($.proxy(this.duplicate_selected, this));
124 $('.delete-button').click($.proxy(this.delete_selected, this));
124 $('.delete-button').click($.proxy(this.delete_selected, this));
125
125
126 // Bind events for selection menu buttons.
126 // Bind events for selection menu buttons.
127 $('#selector-menu').click(function(event){that.select($(event.target).attr('id'))});
127 $('#selector-menu').click(function (event) {
128 $('#select-all').change(function(){that.select($(this).is(':checked') ? 'select-all' : 'select-none')});
128 that.select($(event.target).attr('id'));
129 $('#button-select-all').click(function(e) {
129 });
130 var select_all = $('#select-all');
131 select_all.change(function () {
132 if (!select_all.prop('checked') || select_all.data('indeterminate')) {
133 that.select('select-none');
134 } else {
135 that.select('select-all');
136 }
137 });
138 $('#button-select-all').click(function (e) {
130 // toggle checkbox if the click doesn't come from the checkbox already
139 // toggle checkbox if the click doesn't come from the checkbox already
131 if (!$(e.target).is('input[type=checkbox]')) {
140 if (!$(e.target).is('input[type=checkbox]')) {
132 var checkbox = $('#select-all');
141 if (select_all.prop('checked') || select_all.data('indeterminate')) {
133 checkbox.prop('checked', !checkbox.prop('checked'));
142 that.select('select-none');
134 that.select(checkbox.prop('checked') ? 'select-all' : 'select-none');
143 } else {
144 that.select('select-all');
145 }
135 }
146 }
136 });
147 });
137 }
148 }
138 };
149 };
139
150
140 NotebookList.prototype.handleFilesUpload = function(event, dropOrForm) {
151 NotebookList.prototype.handleFilesUpload = function(event, dropOrForm) {
141 var that = this;
152 var that = this;
142 var files;
153 var files;
143 if(dropOrForm =='drop'){
154 if(dropOrForm =='drop'){
144 files = event.originalEvent.dataTransfer.files;
155 files = event.originalEvent.dataTransfer.files;
145 } else
156 } else
146 {
157 {
147 files = event.originalEvent.target.files;
158 files = event.originalEvent.target.files;
148 }
159 }
149 for (var i = 0; i < files.length; i++) {
160 for (var i = 0; i < files.length; i++) {
150 var f = files[i];
161 var f = files[i];
151 var name_and_ext = utils.splitext(f.name);
162 var name_and_ext = utils.splitext(f.name);
152 var file_ext = name_and_ext[1];
163 var file_ext = name_and_ext[1];
153
164
154 var reader = new FileReader();
165 var reader = new FileReader();
155 if (file_ext === '.ipynb') {
166 if (file_ext === '.ipynb') {
156 reader.readAsText(f);
167 reader.readAsText(f);
157 } else {
168 } else {
158 // read non-notebook files as binary
169 // read non-notebook files as binary
159 reader.readAsArrayBuffer(f);
170 reader.readAsArrayBuffer(f);
160 }
171 }
161 var item = that.new_item(0, true);
172 var item = that.new_item(0, true);
162 item.addClass('new-file');
173 item.addClass('new-file');
163 that.add_name_input(f.name, item, file_ext == '.ipynb' ? 'notebook' : 'file');
174 that.add_name_input(f.name, item, file_ext == '.ipynb' ? 'notebook' : 'file');
164 // Store the list item in the reader so we can use it later
175 // Store the list item in the reader so we can use it later
165 // to know which item it belongs to.
176 // to know which item it belongs to.
166 $(reader).data('item', item);
177 $(reader).data('item', item);
167 reader.onload = function (event) {
178 reader.onload = function (event) {
168 var item = $(event.target).data('item');
179 var item = $(event.target).data('item');
169 that.add_file_data(event.target.result, item);
180 that.add_file_data(event.target.result, item);
170 that.add_upload_button(item);
181 that.add_upload_button(item);
171 };
182 };
172 reader.onerror = function (event) {
183 reader.onerror = function (event) {
173 var item = $(event.target).data('item');
184 var item = $(event.target).data('item');
174 var name = item.data('name');
185 var name = item.data('name');
175 item.remove();
186 item.remove();
176 dialog.modal({
187 dialog.modal({
177 title : 'Failed to read file',
188 title : 'Failed to read file',
178 body : "Failed to read file '" + name + "'",
189 body : "Failed to read file '" + name + "'",
179 buttons : {'OK' : { 'class' : 'btn-primary' }}
190 buttons : {'OK' : { 'class' : 'btn-primary' }}
180 });
191 });
181 };
192 };
182 }
193 }
183 // Replace the file input form wth a clone of itself. This is required to
194 // Replace the file input form wth a clone of itself. This is required to
184 // reset the form. Otherwise, if you upload a file, delete it and try to
195 // reset the form. Otherwise, if you upload a file, delete it and try to
185 // upload it again, the changed event won't fire.
196 // upload it again, the changed event won't fire.
186 var form = $('input.fileinput');
197 var form = $('input.fileinput');
187 form.replaceWith(form.clone(true));
198 form.replaceWith(form.clone(true));
188 return false;
199 return false;
189 };
200 };
190
201
191 NotebookList.prototype.clear_list = function (remove_uploads) {
202 NotebookList.prototype.clear_list = function (remove_uploads) {
192 /**
203 /**
193 * Clears the navigation tree.
204 * Clears the navigation tree.
194 *
205 *
195 * Parameters
206 * Parameters
196 * remove_uploads: bool=False
207 * remove_uploads: bool=False
197 * Should upload prompts also be removed from the tree.
208 * Should upload prompts also be removed from the tree.
198 */
209 */
199 if (remove_uploads) {
210 if (remove_uploads) {
200 this.element.children('.list_item').remove();
211 this.element.children('.list_item').remove();
201 } else {
212 } else {
202 this.element.children('.list_item:not(.new-file)').remove();
213 this.element.children('.list_item:not(.new-file)').remove();
203 }
214 }
204 };
215 };
205
216
206 NotebookList.prototype.load_sessions = function(){
217 NotebookList.prototype.load_sessions = function(){
207 this.session_list.load_sessions();
218 this.session_list.load_sessions();
208 };
219 };
209
220
210
221
211 NotebookList.prototype.sessions_loaded = function(data){
222 NotebookList.prototype.sessions_loaded = function(data){
212 this.sessions = data;
223 this.sessions = data;
213 this.load_list();
224 this.load_list();
214 };
225 };
215
226
216 NotebookList.prototype.load_list = function () {
227 NotebookList.prototype.load_list = function () {
217 var that = this;
228 var that = this;
218 this.contents.list_contents(that.notebook_path).then(
229 this.contents.list_contents(that.notebook_path).then(
219 $.proxy(this.draw_notebook_list, this),
230 $.proxy(this.draw_notebook_list, this),
220 function(error) {
231 function(error) {
221 that.draw_notebook_list({content: []}, "Server error: " + error.message);
232 that.draw_notebook_list({content: []}, "Server error: " + error.message);
222 }
233 }
223 );
234 );
224 };
235 };
225
236
226 /**
237 /**
227 * Draw the list of notebooks
238 * Draw the list of notebooks
228 * @method draw_notebook_list
239 * @method draw_notebook_list
229 * @param {Array} list An array of dictionaries representing files or
240 * @param {Array} list An array of dictionaries representing files or
230 * directories.
241 * directories.
231 * @param {String} error_msg An error message
242 * @param {String} error_msg An error message
232 */
243 */
233
244
234
245
235 var type_order = {'directory':0,'notebook':1,'file':2};
246 var type_order = {'directory':0,'notebook':1,'file':2};
236
247
237 NotebookList.prototype.draw_notebook_list = function (list, error_msg) {
248 NotebookList.prototype.draw_notebook_list = function (list, error_msg) {
238 // Remember what was selected before the refresh.
249 // Remember what was selected before the refresh.
239 var selected_before = this.selected;
250 var selected_before = this.selected;
240
251
241 list.content.sort(function(a, b) {
252 list.content.sort(function(a, b) {
242 if (type_order[a['type']] < type_order[b['type']]) {
253 if (type_order[a['type']] < type_order[b['type']]) {
243 return -1;
254 return -1;
244 }
255 }
245 if (type_order[a['type']] > type_order[b['type']]) {
256 if (type_order[a['type']] > type_order[b['type']]) {
246 return 1;
257 return 1;
247 }
258 }
248 if (a['name'] < b['name']) {
259 if (a['name'] < b['name']) {
249 return -1;
260 return -1;
250 }
261 }
251 if (a['name'] > b['name']) {
262 if (a['name'] > b['name']) {
252 return 1;
263 return 1;
253 }
264 }
254 return 0;
265 return 0;
255 });
266 });
256 var message = error_msg || 'Notebook list empty.';
267 var message = error_msg || 'Notebook list empty.';
257 var item = null;
268 var item = null;
258 var model = null;
269 var model = null;
259 var len = list.content.length;
270 var len = list.content.length;
260 this.clear_list();
271 this.clear_list();
261 var n_uploads = this.element.children('.list_item').length;
272 var n_uploads = this.element.children('.list_item').length;
262 if (len === 0) {
273 if (len === 0) {
263 item = this.new_item(0);
274 item = this.new_item(0);
264 var span12 = item.children().first();
275 var span12 = item.children().first();
265 span12.empty();
276 span12.empty();
266 span12.append($('<div style="margin:auto;text-align:center;color:grey"/>').text(message));
277 span12.append($('<div style="margin:auto;text-align:center;color:grey"/>').text(message));
267 }
278 }
268 var path = this.notebook_path;
279 var path = this.notebook_path;
269 var offset = n_uploads;
280 var offset = n_uploads;
270 if (path !== '') {
281 if (path !== '') {
271 item = this.new_item(offset, false);
282 item = this.new_item(offset, false);
272 model = {
283 model = {
273 type: 'directory',
284 type: 'directory',
274 name: '..',
285 name: '..',
275 path: utils.url_path_split(path)[0],
286 path: utils.url_path_split(path)[0],
276 };
287 };
277 this.add_link(model, item);
288 this.add_link(model, item);
278 offset += 1;
289 offset += 1;
279 }
290 }
280 for (var i=0; i<len; i++) {
291 for (var i=0; i<len; i++) {
281 model = list.content[i];
292 model = list.content[i];
282 item = this.new_item(i+offset, true);
293 item = this.new_item(i+offset, true);
283 this.add_link(model, item);
294 this.add_link(model, item);
284 }
295 }
285 // Trigger an event when we've finished drawing the notebook list.
296 // Trigger an event when we've finished drawing the notebook list.
286 events.trigger('draw_notebook_list.NotebookList');
297 events.trigger('draw_notebook_list.NotebookList');
287
298
288 // Reselect the items that were selected before. Notify listeners
299 // Reselect the items that were selected before. Notify listeners
289 // that the selected items may have changed. O(n^2) operation.
300 // that the selected items may have changed. O(n^2) operation.
290 selected_before.forEach(function(item) {
301 selected_before.forEach(function(item) {
291 var list_items = $('.list_item');
302 var list_items = $('.list_item');
292 for (var i=0; i<list_items.length; i++) {
303 for (var i=0; i<list_items.length; i++) {
293 var $list_item = $(list_items[i]);
304 var $list_item = $(list_items[i]);
294 if ($list_item.data('path') == item.path) {
305 if ($list_item.data('path') == item.path) {
295 $list_item.find('input[type=checkbox]').prop('checked', true);
306 $list_item.find('input[type=checkbox]').prop('checked', true);
296 break;
307 break;
297 }
308 }
298 }
309 }
299 });
310 });
300 this._selection_changed();
311 this._selection_changed();
301 };
312 };
302
313
303
314
304 /**
315 /**
305 * Creates a new item.
316 * Creates a new item.
306 * @param {integer} index
317 * @param {integer} index
307 * @param {boolean} [selectable] - tristate, undefined: don't draw checkbox,
318 * @param {boolean} [selectable] - tristate, undefined: don't draw checkbox,
308 * false: don't draw checkbox but pad
319 * false: don't draw checkbox but pad
309 * where it should be, true: draw checkbox.
320 * where it should be, true: draw checkbox.
310 * @return {JQuery} row
321 * @return {JQuery} row
311 */
322 */
312 NotebookList.prototype.new_item = function (index, selectable) {
323 NotebookList.prototype.new_item = function (index, selectable) {
313 var row = $('<div/>')
324 var row = $('<div/>')
314 .addClass("list_item")
325 .addClass("list_item")
315 .addClass("row");
326 .addClass("row");
316
327
317 var item = $("<div/>")
328 var item = $("<div/>")
318 .addClass("col-md-12")
329 .addClass("col-md-12")
319 .appendTo(row);
330 .appendTo(row);
320
331
321 var checkbox;
332 var checkbox;
322 if (selectable !== undefined) {
333 if (selectable !== undefined) {
323 checkbox = $('<input/>')
334 checkbox = $('<input/>')
324 .attr('type', 'checkbox')
335 .attr('type', 'checkbox')
325 .attr('title', 'Click here to rename, delete, etc.')
336 .attr('title', 'Click here to rename, delete, etc.')
326 .appendTo(item);
337 .appendTo(item);
327 }
338 }
328
339
329 $('<i/>')
340 $('<i/>')
330 .addClass('item_icon')
341 .addClass('item_icon')
331 .appendTo(item);
342 .appendTo(item);
332
343
333 var link = $("<a/>")
344 var link = $("<a/>")
334 .addClass("item_link")
345 .addClass("item_link")
335 .appendTo(item);
346 .appendTo(item);
336
347
337 $("<span/>")
348 $("<span/>")
338 .addClass("item_name")
349 .addClass("item_name")
339 .appendTo(link);
350 .appendTo(link);
340
351
341 if (selectable === false) {
352 if (selectable === false) {
342 checkbox.css('visibility', 'hidden');
353 checkbox.css('visibility', 'hidden');
343 } else if (selectable === true) {
354 } else if (selectable === true) {
344 var that = this;
355 var that = this;
345 row.click(function(e) {
356 row.click(function(e) {
346 // toggle checkbox only if the click doesn't come from the checkbox or the link
357 // toggle checkbox only if the click doesn't come from the checkbox or the link
347 if (!$(e.target).is('span[class=item_name]') && !$(e.target).is('input[type=checkbox]')) {
358 if (!$(e.target).is('span[class=item_name]') && !$(e.target).is('input[type=checkbox]')) {
348 checkbox.prop('checked', !checkbox.prop('checked'));
359 checkbox.prop('checked', !checkbox.prop('checked'));
349 }
360 }
350 that._selection_changed();
361 that._selection_changed();
351 });
362 });
352 }
363 }
353
364
354 var buttons = $('<div/>')
365 var buttons = $('<div/>')
355 .addClass("item_buttons pull-right")
366 .addClass("item_buttons pull-right")
356 .appendTo(item);
367 .appendTo(item);
357
368
358 $('<div/>')
369 $('<div/>')
359 .addClass('running-indicator')
370 .addClass('running-indicator')
360 .text('Running')
371 .text('Running')
361 .css('visibility', 'hidden')
372 .css('visibility', 'hidden')
362 .appendTo(buttons);
373 .appendTo(buttons);
363
374
364 if (index === -1) {
375 if (index === -1) {
365 this.element.append(row);
376 this.element.append(row);
366 } else {
377 } else {
367 this.element.children().eq(index).after(row);
378 this.element.children().eq(index).after(row);
368 }
379 }
369 return row;
380 return row;
370 };
381 };
371
382
372
383
373 NotebookList.icons = {
384 NotebookList.icons = {
374 directory: 'folder_icon',
385 directory: 'folder_icon',
375 notebook: 'notebook_icon',
386 notebook: 'notebook_icon',
376 file: 'file_icon',
387 file: 'file_icon',
377 };
388 };
378
389
379 NotebookList.uri_prefixes = {
390 NotebookList.uri_prefixes = {
380 directory: 'tree',
391 directory: 'tree',
381 notebook: 'notebooks',
392 notebook: 'notebooks',
382 file: 'edit',
393 file: 'edit',
383 };
394 };
384
395
385 /**
396 /**
386 * Select all items in the tree of specified type.
397 * Select all items in the tree of specified type.
387 * selection_type : string among "select-all", "select-folders", "select-notebooks", "select-running-notebooks", "select-files"
398 * selection_type : string among "select-all", "select-folders", "select-notebooks", "select-running-notebooks", "select-files"
388 * any other string (like "select-none") deselects all items
399 * any other string (like "select-none") deselects all items
389 */
400 */
390 NotebookList.prototype.select = function(selection_type) {
401 NotebookList.prototype.select = function(selection_type) {
391 var that = this;
402 var that = this;
392 $('.list_item').each(function(index, item) {
403 $('.list_item').each(function(index, item) {
393 var item_type = $(item).data('type');
404 var item_type = $(item).data('type');
394 var state = false;
405 var state = false;
395 state = state || (selection_type === "select-all");
406 state = state || (selection_type === "select-all");
396 state = state || (selection_type === "select-folders" && item_type === 'directory');
407 state = state || (selection_type === "select-folders" && item_type === 'directory');
397 state = state || (selection_type === "select-notebooks" && item_type === 'notebook');
408 state = state || (selection_type === "select-notebooks" && item_type === 'notebook');
398 state = state || (selection_type === "select-running-notebooks" && item_type === 'notebook' && that.sessions[$(item).data('path')] !== undefined);
409 state = state || (selection_type === "select-running-notebooks" && item_type === 'notebook' && that.sessions[$(item).data('path')] !== undefined);
399 state = state || (selection_type === "select-files" && item_type === 'file');
410 state = state || (selection_type === "select-files" && item_type === 'file');
400 $(item).find('input[type=checkbox]').prop('checked', state);
411 $(item).find('input[type=checkbox]').prop('checked', state);
401 });
412 });
402 this._selection_changed();
413 this._selection_changed();
403 };
414 };
404
415
405
416
406 /**
417 /**
407 * Handles when any row selector checkbox is toggled.
418 * Handles when any row selector checkbox is toggled.
408 */
419 */
409 NotebookList.prototype._selection_changed = function() {
420 NotebookList.prototype._selection_changed = function() {
410 // Use a JQuery selector to find each row with a checked checkbox. If
421 // Use a JQuery selector to find each row with a checked checkbox. If
411 // we decide to add more checkboxes in the future, this code will need
422 // we decide to add more checkboxes in the future, this code will need
412 // to be changed to distinguish which checkbox is the row selector.
423 // to be changed to distinguish which checkbox is the row selector.
413 var selected = [];
424 var selected = [];
414 var has_running_notebook = false;
425 var has_running_notebook = false;
415 var has_directory = false;
426 var has_directory = false;
416 var has_file = false;
427 var has_file = false;
417 var that = this;
428 var that = this;
418 var checked = 0;
429 var checked = 0;
419 $('.list_item :checked').each(function(index, item) {
430 $('.list_item :checked').each(function(index, item) {
420 var parent = $(item).parent().parent();
431 var parent = $(item).parent().parent();
421
432
422 // If the item doesn't have an upload button, isn't the
433 // If the item doesn't have an upload button, isn't the
423 // breadcrumbs and isn't the parent folder '..', then it can be selected.
434 // breadcrumbs and isn't the parent folder '..', then it can be selected.
424 // Breadcrumbs path == ''.
435 // Breadcrumbs path == ''.
425 if (parent.find('.upload_button').length === 0 && parent.data('path') !== '' && parent.data('path') !== utils.url_path_split(that.notebook_path)[0]) {
436 if (parent.find('.upload_button').length === 0 && parent.data('path') !== '' && parent.data('path') !== utils.url_path_split(that.notebook_path)[0]) {
426 checked++;
437 checked++;
427 selected.push({
438 selected.push({
428 name: parent.data('name'),
439 name: parent.data('name'),
429 path: parent.data('path'),
440 path: parent.data('path'),
430 type: parent.data('type')
441 type: parent.data('type')
431 });
442 });
432
443
433 // Set flags according to what is selected. Flags are later
444 // Set flags according to what is selected. Flags are later
434 // used to decide which action buttons are visible.
445 // used to decide which action buttons are visible.
435 has_running_notebook = has_running_notebook ||
446 has_running_notebook = has_running_notebook ||
436 (parent.data('type') == 'notebook' && that.sessions[parent.data('path')] !== undefined);
447 (parent.data('type') == 'notebook' && that.sessions[parent.data('path')] !== undefined);
437 has_file = has_file || parent.data('type') == 'file';
448 has_file = has_file || parent.data('type') == 'file';
438 has_directory = has_directory || parent.data('type') == 'directory';
449 has_directory = has_directory || parent.data('type') == 'directory';
439 }
450 }
440 });
451 });
441 this.selected = selected;
452 this.selected = selected;
442
453
443 // Rename is only visible when one item is selected, and it is not a running notebook
454 // Rename is only visible when one item is selected, and it is not a running notebook
444 if (selected.length==1 && !has_running_notebook) {
455 if (selected.length==1 && !has_running_notebook) {
445 $('.rename-button').css('display', 'inline-block');
456 $('.rename-button').css('display', 'inline-block');
446 } else {
457 } else {
447 $('.rename-button').css('display', 'none');
458 $('.rename-button').css('display', 'none');
448 }
459 }
449
460
450 // Shutdown is only visible when one or more notebooks running notebooks
461 // Shutdown is only visible when one or more notebooks running notebooks
451 // are selected and no non-notebook items are selected.
462 // are selected and no non-notebook items are selected.
452 if (has_running_notebook && !(has_file || has_directory)) {
463 if (has_running_notebook && !(has_file || has_directory)) {
453 $('.shutdown-button').css('display', 'inline-block');
464 $('.shutdown-button').css('display', 'inline-block');
454 } else {
465 } else {
455 $('.shutdown-button').css('display', 'none');
466 $('.shutdown-button').css('display', 'none');
456 }
467 }
457
468
458 // Duplicate isn't visible when a directory is selected.
469 // Duplicate isn't visible when a directory is selected.
459 if (selected.length > 0 && !has_directory) {
470 if (selected.length > 0 && !has_directory) {
460 $('.duplicate-button').css('display', 'inline-block');
471 $('.duplicate-button').css('display', 'inline-block');
461 } else {
472 } else {
462 $('.duplicate-button').css('display', 'none');
473 $('.duplicate-button').css('display', 'none');
463 }
474 }
464
475
465 // Delete is visible if one or more items are selected.
476 // Delete is visible if one or more items are selected.
466 if (selected.length > 0) {
477 if (selected.length > 0) {
467 $('.delete-button').css('display', 'inline-block');
478 $('.delete-button').css('display', 'inline-block');
468 } else {
479 } else {
469 $('.delete-button').css('display', 'none');
480 $('.delete-button').css('display', 'none');
470 }
481 }
471
482
472 // If all of the items are selected, show the selector as checked. If
483 // If all of the items are selected, show the selector as checked. If
473 // some of the items are selected, show it as checked. Otherwise,
484 // some of the items are selected, show it as checked. Otherwise,
474 // uncheck it.
485 // uncheck it.
475 var total = 0;
486 var total = 0;
476 $('.list_item input[type=checkbox]').each(function(index, item) {
487 $('.list_item input[type=checkbox]').each(function(index, item) {
477 var parent = $(item).parent().parent();
488 var parent = $(item).parent().parent();
478 // If the item doesn't have an upload button and it's not the
489 // If the item doesn't have an upload button and it's not the
479 // breadcrumbs, it can be selected. Breadcrumbs path == ''.
490 // breadcrumbs, it can be selected. Breadcrumbs path == ''.
480 if (parent.find('.upload_button').length === 0 && parent.data('path') !== '' && parent.data('path') !== utils.url_path_split(that.notebook_path)[0]) {
491 if (parent.find('.upload_button').length === 0 && parent.data('path') !== '' && parent.data('path') !== utils.url_path_split(that.notebook_path)[0]) {
481 total++;
492 total++;
482 }
493 }
483 });
494 });
495
496 var select_all = $("#select-all");
484 if (checked === 0) {
497 if (checked === 0) {
485 $('#tree-selector input[type=checkbox]')[0].indeterminate = false;
498 select_all.prop('checked', false);
486 $('#tree-selector input[type=checkbox]').prop('checked', false);
499 select_all.prop('indeterminate', false);
500 select_all.data('indeterminate', false);
487 } else if (checked === total) {
501 } else if (checked === total) {
488 $('#tree-selector input[type=checkbox]')[0].indeterminate = false;
502 select_all.prop('checked', true);
489 $('#tree-selector input[type=checkbox]').prop('checked', true);
503 select_all.prop('indeterminate', false);
504 select_all.data('indeterminate', false);
490 } else {
505 } else {
491 $('#tree-selector input[type=checkbox]').prop('checked', false);
506 select_all.prop('checked', false);
492 $('#tree-selector input[type=checkbox]')[0].indeterminate = true;
507 select_all.prop('indeterminate', true);
508 select_all.data('indeterminate', true);
493 }
509 }
494 // Update total counter
510 // Update total counter
495 $('#counter-select-all').html(checked===0 ? '&nbsp;' : checked);
511 $('#counter-select-all').html(checked===0 ? '&nbsp;' : checked);
496 };
512 };
497
513
498 NotebookList.prototype.add_link = function (model, item) {
514 NotebookList.prototype.add_link = function (model, item) {
499 var path = model.path,
515 var path = model.path,
500 name = model.name;
516 name = model.name;
501 var running = (model.type == 'notebook' && this.sessions[path] !== undefined);
517 var running = (model.type == 'notebook' && this.sessions[path] !== undefined);
502
518
503 item.data('name', name);
519 item.data('name', name);
504 item.data('path', path);
520 item.data('path', path);
505 item.data('type', model.type);
521 item.data('type', model.type);
506 item.find(".item_name").text(name);
522 item.find(".item_name").text(name);
507 var icon = NotebookList.icons[model.type];
523 var icon = NotebookList.icons[model.type];
508 if (running) {
524 if (running) {
509 icon = 'running_' + icon;
525 icon = 'running_' + icon;
510 }
526 }
511 var uri_prefix = NotebookList.uri_prefixes[model.type];
527 var uri_prefix = NotebookList.uri_prefixes[model.type];
512 item.find(".item_icon").addClass(icon).addClass('icon-fixed-width');
528 item.find(".item_icon").addClass(icon).addClass('icon-fixed-width');
513 var link = item.find("a.item_link")
529 var link = item.find("a.item_link")
514 .attr('href',
530 .attr('href',
515 utils.url_join_encode(
531 utils.url_join_encode(
516 this.base_url,
532 this.base_url,
517 uri_prefix,
533 uri_prefix,
518 path
534 path
519 )
535 )
520 );
536 );
521
537
522 item.find(".item_buttons .running-indicator").css('visibility', running ? '' : 'hidden');
538 item.find(".item_buttons .running-indicator").css('visibility', running ? '' : 'hidden');
523
539
524 // directory nav doesn't open new tabs
540 // directory nav doesn't open new tabs
525 // files, notebooks do
541 // files, notebooks do
526 if (model.type !== "directory") {
542 if (model.type !== "directory") {
527 link.attr('target',IPython._target);
543 link.attr('target',IPython._target);
528 }
544 }
529 };
545 };
530
546
531
547
532 NotebookList.prototype.add_name_input = function (name, item, icon_type) {
548 NotebookList.prototype.add_name_input = function (name, item, icon_type) {
533 item.data('name', name);
549 item.data('name', name);
534 item.find(".item_icon").addClass(NotebookList.icons[icon_type]).addClass('icon-fixed-width');
550 item.find(".item_icon").addClass(NotebookList.icons[icon_type]).addClass('icon-fixed-width');
535 item.find(".item_name").empty().append(
551 item.find(".item_name").empty().append(
536 $('<input/>')
552 $('<input/>')
537 .addClass("filename_input")
553 .addClass("filename_input")
538 .attr('value', name)
554 .attr('value', name)
539 .attr('size', '30')
555 .attr('size', '30')
540 .attr('type', 'text')
556 .attr('type', 'text')
541 .keyup(function(event){
557 .keyup(function(event){
542 if(event.keyCode == 13){item.find('.upload_button').click();}
558 if(event.keyCode == 13){item.find('.upload_button').click();}
543 else if(event.keyCode == 27){item.remove();}
559 else if(event.keyCode == 27){item.remove();}
544 })
560 })
545 );
561 );
546 };
562 };
547
563
548
564
549 NotebookList.prototype.add_file_data = function (data, item) {
565 NotebookList.prototype.add_file_data = function (data, item) {
550 item.data('filedata', data);
566 item.data('filedata', data);
551 };
567 };
552
568
553
569
554 NotebookList.prototype.shutdown_selected = function() {
570 NotebookList.prototype.shutdown_selected = function() {
555 var that = this;
571 var that = this;
556 this.selected.forEach(function(item) {
572 this.selected.forEach(function(item) {
557 if (item.type == 'notebook') {
573 if (item.type == 'notebook') {
558 that.shutdown_notebook(item.path);
574 that.shutdown_notebook(item.path);
559 }
575 }
560 });
576 });
561 };
577 };
562
578
563 NotebookList.prototype.shutdown_notebook = function(path) {
579 NotebookList.prototype.shutdown_notebook = function(path) {
564 var that = this;
580 var that = this;
565 var settings = {
581 var settings = {
566 processData : false,
582 processData : false,
567 cache : false,
583 cache : false,
568 type : "DELETE",
584 type : "DELETE",
569 dataType : "json",
585 dataType : "json",
570 success : function () {
586 success : function () {
571 that.load_sessions();
587 that.load_sessions();
572 },
588 },
573 error : utils.log_ajax_error,
589 error : utils.log_ajax_error,
574 };
590 };
575
591
576 var session = this.sessions[path];
592 var session = this.sessions[path];
577 if (session) {
593 if (session) {
578 var url = utils.url_join_encode(
594 var url = utils.url_join_encode(
579 this.base_url,
595 this.base_url,
580 'api/sessions',
596 'api/sessions',
581 session
597 session
582 );
598 );
583 $.ajax(url, settings);
599 $.ajax(url, settings);
584 }
600 }
585 };
601 };
586
602
587 NotebookList.prototype.rename_selected = function() {
603 NotebookList.prototype.rename_selected = function() {
588 if (this.selected.length != 1) return;
604 if (this.selected.length != 1) return;
589
605
590 var that = this;
606 var that = this;
591 var item_path = this.selected[0].path;
607 var item_path = this.selected[0].path;
592 var item_name = this.selected[0].name;
608 var item_name = this.selected[0].name;
593 var item_type = this.selected[0].type;
609 var item_type = this.selected[0].type;
594 var input = $('<input/>').attr('type','text').attr('size','25').addClass('form-control')
610 var input = $('<input/>').attr('type','text').attr('size','25').addClass('form-control')
595 .val(item_name);
611 .val(item_name);
596 var dialog_body = $('<div/>').append(
612 var dialog_body = $('<div/>').append(
597 $("<p/>").addClass("rename-message")
613 $("<p/>").addClass("rename-message")
598 .text('Enter a new '+ item_type + ' name:')
614 .text('Enter a new '+ item_type + ' name:')
599 ).append(
615 ).append(
600 $("<br/>")
616 $("<br/>")
601 ).append(input);
617 ).append(input);
602 var d = dialog.modal({
618 var d = dialog.modal({
603 title : "Rename "+ item_type,
619 title : "Rename "+ item_type,
604 body : dialog_body,
620 body : dialog_body,
605 buttons : {
621 buttons : {
606 OK : {
622 OK : {
607 class: "btn-primary",
623 class: "btn-primary",
608 click: function() {
624 click: function() {
609 that.contents.rename(item_path, utils.url_path_join(that.notebook_path, input.val())).then(function() {
625 that.contents.rename(item_path, utils.url_path_join(that.notebook_path, input.val())).then(function() {
610 that.load_list();
626 that.load_list();
611 }).catch(function(e) {
627 }).catch(function(e) {
612 dialog.modal({
628 dialog.modal({
613 title: "Rename Failed",
629 title: "Rename Failed",
614 body: $('<div/>')
630 body: $('<div/>')
615 .text("An error occurred while renaming \"" + item_name + "\" to \"" + input.val() + "\".")
631 .text("An error occurred while renaming \"" + item_name + "\" to \"" + input.val() + "\".")
616 .append($('<div/>')
632 .append($('<div/>')
617 .addClass('alert alert-danger')
633 .addClass('alert alert-danger')
618 .text(e.message || e)),
634 .text(e.message || e)),
619 buttons: {
635 buttons: {
620 OK: {'class': 'btn-primary'}
636 OK: {'class': 'btn-primary'}
621 }
637 }
622 });
638 });
623 console.warn('Error durring renaming :', e);
639 console.warn('Error durring renaming :', e);
624 });
640 });
625 }
641 }
626 },
642 },
627 Cancel : {}
643 Cancel : {}
628 },
644 },
629 open : function () {
645 open : function () {
630 // Upon ENTER, click the OK button.
646 // Upon ENTER, click the OK button.
631 input.keydown(function (event) {
647 input.keydown(function (event) {
632 if (event.which === keyboard.keycodes.enter) {
648 if (event.which === keyboard.keycodes.enter) {
633 d.find('.btn-primary').first().click();
649 d.find('.btn-primary').first().click();
634 return false;
650 return false;
635 }
651 }
636 });
652 });
637 input.focus().select();
653 input.focus().select();
638 }
654 }
639 });
655 });
640 };
656 };
641
657
642 NotebookList.prototype.delete_selected = function() {
658 NotebookList.prototype.delete_selected = function() {
643 var message;
659 var message;
644 if (this.selected.length == 1) {
660 if (this.selected.length == 1) {
645 message = 'Are you sure you want to permanently delete: ' + this.selected[0].name + '?';
661 message = 'Are you sure you want to permanently delete: ' + this.selected[0].name + '?';
646 } else {
662 } else {
647 message = 'Are you sure you want to permanently delete the ' + this.selected.length + ' files/folders selected?';
663 message = 'Are you sure you want to permanently delete the ' + this.selected.length + ' files/folders selected?';
648 }
664 }
649 var that = this;
665 var that = this;
650 dialog.modal({
666 dialog.modal({
651 title : "Delete",
667 title : "Delete",
652 body : message,
668 body : message,
653 buttons : {
669 buttons : {
654 Delete : {
670 Delete : {
655 class: "btn-danger",
671 class: "btn-danger",
656 click: function() {
672 click: function() {
657 // Shutdown any/all selected notebooks before deleting
673 // Shutdown any/all selected notebooks before deleting
658 // the files.
674 // the files.
659 that.shutdown_selected();
675 that.shutdown_selected();
660
676
661 // Delete selected.
677 // Delete selected.
662 that.selected.forEach(function(item) {
678 that.selected.forEach(function(item) {
663 that.contents.delete(item.path).then(function() {
679 that.contents.delete(item.path).then(function() {
664 that.notebook_deleted(item.path);
680 that.notebook_deleted(item.path);
665 }).catch(function(e) {
681 }).catch(function(e) {
666 dialog.modal({
682 dialog.modal({
667 title: "Delete Failed",
683 title: "Delete Failed",
668 body: $('<div/>')
684 body: $('<div/>')
669 .text("An error occurred while deleting \"" + item.path + "\".")
685 .text("An error occurred while deleting \"" + item.path + "\".")
670 .append($('<div/>')
686 .append($('<div/>')
671 .addClass('alert alert-danger')
687 .addClass('alert alert-danger')
672 .text(e.message || e)),
688 .text(e.message || e)),
673 buttons: {
689 buttons: {
674 OK: {'class': 'btn-primary'}
690 OK: {'class': 'btn-primary'}
675 }
691 }
676 });
692 });
677 console.warn('Error durring content deletion:', e);
693 console.warn('Error durring content deletion:', e);
678 });
694 });
679 });
695 });
680 }
696 }
681 },
697 },
682 Cancel : {}
698 Cancel : {}
683 }
699 }
684 });
700 });
685 };
701 };
686
702
687 NotebookList.prototype.duplicate_selected = function() {
703 NotebookList.prototype.duplicate_selected = function() {
688 var message;
704 var message;
689 if (this.selected.length == 1) {
705 if (this.selected.length == 1) {
690 message = 'Are you sure you want to duplicate: ' + this.selected[0].name + '?';
706 message = 'Are you sure you want to duplicate: ' + this.selected[0].name + '?';
691 } else {
707 } else {
692 message = 'Are you sure you want to duplicate the ' + this.selected.length + ' files selected?';
708 message = 'Are you sure you want to duplicate the ' + this.selected.length + ' files selected?';
693 }
709 }
694 var that = this;
710 var that = this;
695 dialog.modal({
711 dialog.modal({
696 title : "Duplicate",
712 title : "Duplicate",
697 body : message,
713 body : message,
698 buttons : {
714 buttons : {
699 Duplicate : {
715 Duplicate : {
700 class: "btn-primary",
716 class: "btn-primary",
701 click: function() {
717 click: function() {
702 that.selected.forEach(function(item) {
718 that.selected.forEach(function(item) {
703 that.contents.copy(item.path, that.notebook_path).then(function () {
719 that.contents.copy(item.path, that.notebook_path).then(function () {
704 that.load_list();
720 that.load_list();
705 }).catch(function(e) {
721 }).catch(function(e) {
706 dialog.modal({
722 dialog.modal({
707 title: "Duplicate Failed",
723 title: "Duplicate Failed",
708 body: $('<div/>')
724 body: $('<div/>')
709 .text("An error occurred while duplicating \"" + item.path + "\".")
725 .text("An error occurred while duplicating \"" + item.path + "\".")
710 .append($('<div/>')
726 .append($('<div/>')
711 .addClass('alert alert-danger')
727 .addClass('alert alert-danger')
712 .text(e.message || e)),
728 .text(e.message || e)),
713 buttons: {
729 buttons: {
714 OK: {'class': 'btn-primary'}
730 OK: {'class': 'btn-primary'}
715 }
731 }
716 });
732 });
717 console.warn('Error durring content duplication', e);
733 console.warn('Error durring content duplication', e);
718 });
734 });
719 });
735 });
720 }
736 }
721 },
737 },
722 Cancel : {}
738 Cancel : {}
723 }
739 }
724 });
740 });
725 };
741 };
726
742
727 NotebookList.prototype.notebook_deleted = function(path) {
743 NotebookList.prototype.notebook_deleted = function(path) {
728 /**
744 /**
729 * Remove the deleted notebook.
745 * Remove the deleted notebook.
730 */
746 */
731 var that = this;
747 var that = this;
732 $( ":data(path)" ).each(function() {
748 $( ":data(path)" ).each(function() {
733 var element = $(this);
749 var element = $(this);
734 if (element.data("path") === path) {
750 if (element.data("path") === path) {
735 element.remove();
751 element.remove();
736 events.trigger('notebook_deleted.NotebookList');
752 events.trigger('notebook_deleted.NotebookList');
737 that._selection_changed();
753 that._selection_changed();
738 }
754 }
739 });
755 });
740 };
756 };
741
757
742
758
743 NotebookList.prototype.add_upload_button = function (item) {
759 NotebookList.prototype.add_upload_button = function (item) {
744 var that = this;
760 var that = this;
745 var upload_button = $('<button/>').text("Upload")
761 var upload_button = $('<button/>').text("Upload")
746 .addClass('btn btn-primary btn-xs upload_button')
762 .addClass('btn btn-primary btn-xs upload_button')
747 .click(function (e) {
763 .click(function (e) {
748 var filename = item.find('.item_name > input').val();
764 var filename = item.find('.item_name > input').val();
749 var path = utils.url_path_join(that.notebook_path, filename);
765 var path = utils.url_path_join(that.notebook_path, filename);
750 var filedata = item.data('filedata');
766 var filedata = item.data('filedata');
751 var format = 'text';
767 var format = 'text';
752 if (filename.length === 0 || filename[0] === '.') {
768 if (filename.length === 0 || filename[0] === '.') {
753 dialog.modal({
769 dialog.modal({
754 title : 'Invalid file name',
770 title : 'Invalid file name',
755 body : "File names must be at least one character and not start with a dot",
771 body : "File names must be at least one character and not start with a dot",
756 buttons : {'OK' : { 'class' : 'btn-primary' }}
772 buttons : {'OK' : { 'class' : 'btn-primary' }}
757 });
773 });
758 return false;
774 return false;
759 }
775 }
760 if (filedata instanceof ArrayBuffer) {
776 if (filedata instanceof ArrayBuffer) {
761 // base64-encode binary file data
777 // base64-encode binary file data
762 var bytes = '';
778 var bytes = '';
763 var buf = new Uint8Array(filedata);
779 var buf = new Uint8Array(filedata);
764 var nbytes = buf.byteLength;
780 var nbytes = buf.byteLength;
765 for (var i=0; i<nbytes; i++) {
781 for (var i=0; i<nbytes; i++) {
766 bytes += String.fromCharCode(buf[i]);
782 bytes += String.fromCharCode(buf[i]);
767 }
783 }
768 filedata = btoa(bytes);
784 filedata = btoa(bytes);
769 format = 'base64';
785 format = 'base64';
770 }
786 }
771 var model = {};
787 var model = {};
772
788
773 var name_and_ext = utils.splitext(filename);
789 var name_and_ext = utils.splitext(filename);
774 var file_ext = name_and_ext[1];
790 var file_ext = name_and_ext[1];
775 var content_type;
791 var content_type;
776 if (file_ext === '.ipynb') {
792 if (file_ext === '.ipynb') {
777 model.type = 'notebook';
793 model.type = 'notebook';
778 model.format = 'json';
794 model.format = 'json';
779 try {
795 try {
780 model.content = JSON.parse(filedata);
796 model.content = JSON.parse(filedata);
781 } catch (e) {
797 } catch (e) {
782 dialog.modal({
798 dialog.modal({
783 title : 'Cannot upload invalid Notebook',
799 title : 'Cannot upload invalid Notebook',
784 body : "The error was: " + e,
800 body : "The error was: " + e,
785 buttons : {'OK' : {
801 buttons : {'OK' : {
786 'class' : 'btn-primary',
802 'class' : 'btn-primary',
787 click: function () {
803 click: function () {
788 item.remove();
804 item.remove();
789 }
805 }
790 }}
806 }}
791 });
807 });
792 console.warn('Error durring notebook uploading', e);
808 console.warn('Error durring notebook uploading', e);
793 return false;
809 return false;
794 }
810 }
795 content_type = 'application/json';
811 content_type = 'application/json';
796 } else {
812 } else {
797 model.type = 'file';
813 model.type = 'file';
798 model.format = format;
814 model.format = format;
799 model.content = filedata;
815 model.content = filedata;
800 content_type = 'application/octet-stream';
816 content_type = 'application/octet-stream';
801 }
817 }
802 filedata = item.data('filedata');
818 filedata = item.data('filedata');
803
819
804 var on_success = function () {
820 var on_success = function () {
805 item.removeClass('new-file');
821 item.removeClass('new-file');
806 that.add_link(model, item);
822 that.add_link(model, item);
807 that.session_list.load_sessions();
823 that.session_list.load_sessions();
808 };
824 };
809
825
810 var exists = false;
826 var exists = false;
811 $.each(that.element.find('.list_item:not(.new-file)'), function(k,v){
827 $.each(that.element.find('.list_item:not(.new-file)'), function(k,v){
812 if ($(v).data('name') === filename) { exists = true; return false; }
828 if ($(v).data('name') === filename) { exists = true; return false; }
813 });
829 });
814
830
815 if (exists) {
831 if (exists) {
816 dialog.modal({
832 dialog.modal({
817 title : "Replace file",
833 title : "Replace file",
818 body : 'There is already a file named ' + filename + ', do you want to replace it?',
834 body : 'There is already a file named ' + filename + ', do you want to replace it?',
819 buttons : {
835 buttons : {
820 Overwrite : {
836 Overwrite : {
821 class: "btn-danger",
837 class: "btn-danger",
822 click: function () {
838 click: function () {
823 that.contents.save(path, model).then(on_success);
839 that.contents.save(path, model).then(on_success);
824 }
840 }
825 },
841 },
826 Cancel : {
842 Cancel : {
827 click: function() { item.remove(); }
843 click: function() { item.remove(); }
828 }
844 }
829 }
845 }
830 });
846 });
831 } else {
847 } else {
832 that.contents.save(path, model).then(on_success);
848 that.contents.save(path, model).then(on_success);
833 }
849 }
834
850
835 return false;
851 return false;
836 });
852 });
837 var cancel_button = $('<button/>').text("Cancel")
853 var cancel_button = $('<button/>').text("Cancel")
838 .addClass("btn btn-default btn-xs")
854 .addClass("btn btn-default btn-xs")
839 .click(function (e) {
855 .click(function (e) {
840 item.remove();
856 item.remove();
841 return false;
857 return false;
842 });
858 });
843 item.find(".item_buttons").empty()
859 item.find(".item_buttons").empty()
844 .append(upload_button)
860 .append(upload_button)
845 .append(cancel_button);
861 .append(cancel_button);
846 };
862 };
847
863
848
864
849 // Backwards compatability.
865 // Backwards compatability.
850 IPython.NotebookList = NotebookList;
866 IPython.NotebookList = NotebookList;
851
867
852 return {'NotebookList': NotebookList};
868 return {'NotebookList': NotebookList};
853 });
869 });
General Comments 0
You need to be logged in to leave comments. Login now