##// END OF EJS Templates
Shutdown the selected notebooks before deleting them
Jonathan Frederic -
Show More
@@ -1,705 +1,710
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 };
47 };
48
48
49 NotebookList.prototype.style = function () {
49 NotebookList.prototype.style = function () {
50 var prefix = '#' + this.element_name;
50 var prefix = '#' + this.element_name;
51 $(prefix + '_toolbar').addClass('list_toolbar');
51 $(prefix + '_toolbar').addClass('list_toolbar');
52 $(prefix + '_list_info').addClass('toolbar_info');
52 $(prefix + '_list_info').addClass('toolbar_info');
53 $(prefix + '_buttons').addClass('toolbar_buttons');
53 $(prefix + '_buttons').addClass('toolbar_buttons');
54 $(prefix + '_list_header').addClass('list_header');
54 $(prefix + '_list_header').addClass('list_header');
55 this.element.addClass("list_container");
55 this.element.addClass("list_container");
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 // Bind events for singleton controls.
71 // Bind events for singleton controls.
72 if (!NotebookList._bound_singletons) {
72 if (!NotebookList._bound_singletons) {
73 NotebookList._bound_singletons = true;
73 NotebookList._bound_singletons = true;
74 $('#new-file').click(function(e) {
74 $('#new-file').click(function(e) {
75 var w = window.open();
75 var w = window.open();
76 that.contents.new_untitled(that.notebook_path || '', {type: 'file', ext: '.txt'}).then(function(data) {
76 that.contents.new_untitled(that.notebook_path || '', {type: 'file', ext: '.txt'}).then(function(data) {
77 var url = utils.url_join_encode(
77 var url = utils.url_join_encode(
78 that.base_url, 'edit', data.path
78 that.base_url, 'edit', data.path
79 );
79 );
80 w.location = url;
80 w.location = url;
81 });
81 });
82 that.load_sessions();
82 that.load_sessions();
83 });
83 });
84 $('#new-folder').click(function(e) {
84 $('#new-folder').click(function(e) {
85 that.contents.new_untitled(that.notebook_path || '', {type: 'directory'})
85 that.contents.new_untitled(that.notebook_path || '', {type: 'directory'})
86 .then(function(){
86 .then(function(){
87 that.load_list();
87 that.load_list();
88 });
88 });
89 });
89 });
90
90
91 $('.rename-button').click($.proxy(this.rename_selected, this));
91 $('.rename-button').click($.proxy(this.rename_selected, this));
92 $('.shutdown-button').click($.proxy(this.shutdown_selected, this));
92 $('.shutdown-button').click($.proxy(this.shutdown_selected, this));
93 $('.duplicate-button').click($.proxy(this.duplicate_selected, this));
93 $('.duplicate-button').click($.proxy(this.duplicate_selected, this));
94 $('.delete-button').click($.proxy(this.delete_selected, this));
94 $('.delete-button').click($.proxy(this.delete_selected, this));
95 }
95 }
96 };
96 };
97
97
98 NotebookList.prototype.handleFilesUpload = function(event, dropOrForm) {
98 NotebookList.prototype.handleFilesUpload = function(event, dropOrForm) {
99 var that = this;
99 var that = this;
100 var files;
100 var files;
101 if(dropOrForm =='drop'){
101 if(dropOrForm =='drop'){
102 files = event.originalEvent.dataTransfer.files;
102 files = event.originalEvent.dataTransfer.files;
103 } else
103 } else
104 {
104 {
105 files = event.originalEvent.target.files;
105 files = event.originalEvent.target.files;
106 }
106 }
107 for (var i = 0; i < files.length; i++) {
107 for (var i = 0; i < files.length; i++) {
108 var f = files[i];
108 var f = files[i];
109 var name_and_ext = utils.splitext(f.name);
109 var name_and_ext = utils.splitext(f.name);
110 var file_ext = name_and_ext[1];
110 var file_ext = name_and_ext[1];
111
111
112 var reader = new FileReader();
112 var reader = new FileReader();
113 if (file_ext === '.ipynb') {
113 if (file_ext === '.ipynb') {
114 reader.readAsText(f);
114 reader.readAsText(f);
115 } else {
115 } else {
116 // read non-notebook files as binary
116 // read non-notebook files as binary
117 reader.readAsArrayBuffer(f);
117 reader.readAsArrayBuffer(f);
118 }
118 }
119 var item = that.new_item(0, true);
119 var item = that.new_item(0, true);
120 item.addClass('new-file');
120 item.addClass('new-file');
121 that.add_name_input(f.name, item, file_ext == '.ipynb' ? 'notebook' : 'file');
121 that.add_name_input(f.name, item, file_ext == '.ipynb' ? 'notebook' : 'file');
122 // Store the list item in the reader so we can use it later
122 // Store the list item in the reader so we can use it later
123 // to know which item it belongs to.
123 // to know which item it belongs to.
124 $(reader).data('item', item);
124 $(reader).data('item', item);
125 reader.onload = function (event) {
125 reader.onload = function (event) {
126 var item = $(event.target).data('item');
126 var item = $(event.target).data('item');
127 that.add_file_data(event.target.result, item);
127 that.add_file_data(event.target.result, item);
128 that.add_upload_button(item);
128 that.add_upload_button(item);
129 };
129 };
130 reader.onerror = function (event) {
130 reader.onerror = function (event) {
131 var item = $(event.target).data('item');
131 var item = $(event.target).data('item');
132 var name = item.data('name');
132 var name = item.data('name');
133 item.remove();
133 item.remove();
134 dialog.modal({
134 dialog.modal({
135 title : 'Failed to read file',
135 title : 'Failed to read file',
136 body : "Failed to read file '" + name + "'",
136 body : "Failed to read file '" + name + "'",
137 buttons : {'OK' : { 'class' : 'btn-primary' }}
137 buttons : {'OK' : { 'class' : 'btn-primary' }}
138 });
138 });
139 };
139 };
140 }
140 }
141 // Replace the file input form wth a clone of itself. This is required to
141 // Replace the file input form wth a clone of itself. This is required to
142 // reset the form. Otherwise, if you upload a file, delete it and try to
142 // reset the form. Otherwise, if you upload a file, delete it and try to
143 // upload it again, the changed event won't fire.
143 // upload it again, the changed event won't fire.
144 var form = $('input.fileinput');
144 var form = $('input.fileinput');
145 form.replaceWith(form.clone(true));
145 form.replaceWith(form.clone(true));
146 return false;
146 return false;
147 };
147 };
148
148
149 NotebookList.prototype.clear_list = function (remove_uploads) {
149 NotebookList.prototype.clear_list = function (remove_uploads) {
150 /**
150 /**
151 * Clears the navigation tree.
151 * Clears the navigation tree.
152 *
152 *
153 * Parameters
153 * Parameters
154 * remove_uploads: bool=False
154 * remove_uploads: bool=False
155 * Should upload prompts also be removed from the tree.
155 * Should upload prompts also be removed from the tree.
156 */
156 */
157 if (remove_uploads) {
157 if (remove_uploads) {
158 this.element.children('.list_item').remove();
158 this.element.children('.list_item').remove();
159 } else {
159 } else {
160 this.element.children('.list_item:not(.new-file)').remove();
160 this.element.children('.list_item:not(.new-file)').remove();
161 }
161 }
162 };
162 };
163
163
164 NotebookList.prototype.load_sessions = function(){
164 NotebookList.prototype.load_sessions = function(){
165 this.session_list.load_sessions();
165 this.session_list.load_sessions();
166 };
166 };
167
167
168
168
169 NotebookList.prototype.sessions_loaded = function(data){
169 NotebookList.prototype.sessions_loaded = function(data){
170 this.sessions = data;
170 this.sessions = data;
171 this.load_list();
171 this.load_list();
172 };
172 };
173
173
174 NotebookList.prototype.load_list = function () {
174 NotebookList.prototype.load_list = function () {
175 var that = this;
175 var that = this;
176 this.contents.list_contents(that.notebook_path).then(
176 this.contents.list_contents(that.notebook_path).then(
177 $.proxy(this.draw_notebook_list, this),
177 $.proxy(this.draw_notebook_list, this),
178 function(error) {
178 function(error) {
179 that.draw_notebook_list({content: []}, "Server error: " + error.message);
179 that.draw_notebook_list({content: []}, "Server error: " + error.message);
180 }
180 }
181 );
181 );
182 };
182 };
183
183
184 /**
184 /**
185 * Draw the list of notebooks
185 * Draw the list of notebooks
186 * @method draw_notebook_list
186 * @method draw_notebook_list
187 * @param {Array} list An array of dictionaries representing files or
187 * @param {Array} list An array of dictionaries representing files or
188 * directories.
188 * directories.
189 * @param {String} error_msg An error message
189 * @param {String} error_msg An error message
190 */
190 */
191
191
192
192
193 var type_order = {'directory':0,'notebook':1,'file':2};
193 var type_order = {'directory':0,'notebook':1,'file':2};
194
194
195 NotebookList.prototype.draw_notebook_list = function (list, error_msg) {
195 NotebookList.prototype.draw_notebook_list = function (list, error_msg) {
196 list.content.sort(function(a, b) {
196 list.content.sort(function(a, b) {
197 if (type_order[a['type']] < type_order[b['type']]) {
197 if (type_order[a['type']] < type_order[b['type']]) {
198 return -1;
198 return -1;
199 }
199 }
200 if (type_order[a['type']] > type_order[b['type']]) {
200 if (type_order[a['type']] > type_order[b['type']]) {
201 return 1;
201 return 1;
202 }
202 }
203 if (a['name'] < b['name']) {
203 if (a['name'] < b['name']) {
204 return -1;
204 return -1;
205 }
205 }
206 if (a['name'] > b['name']) {
206 if (a['name'] > b['name']) {
207 return 1;
207 return 1;
208 }
208 }
209 return 0;
209 return 0;
210 });
210 });
211 var message = error_msg || 'Notebook list empty.';
211 var message = error_msg || 'Notebook list empty.';
212 var item = null;
212 var item = null;
213 var model = null;
213 var model = null;
214 var len = list.content.length;
214 var len = list.content.length;
215 this.clear_list();
215 this.clear_list();
216 var n_uploads = this.element.children('.list_item').length;
216 var n_uploads = this.element.children('.list_item').length;
217 if (len === 0) {
217 if (len === 0) {
218 item = this.new_item(0);
218 item = this.new_item(0);
219 var span12 = item.children().first();
219 var span12 = item.children().first();
220 span12.empty();
220 span12.empty();
221 span12.append($('<div style="margin:auto;text-align:center;color:grey"/>').text(message));
221 span12.append($('<div style="margin:auto;text-align:center;color:grey"/>').text(message));
222 }
222 }
223 var path = this.notebook_path;
223 var path = this.notebook_path;
224 var offset = n_uploads;
224 var offset = n_uploads;
225 if (path !== '') {
225 if (path !== '') {
226 item = this.new_item(offset, false);
226 item = this.new_item(offset, false);
227 model = {
227 model = {
228 type: 'directory',
228 type: 'directory',
229 name: '..',
229 name: '..',
230 path: utils.url_path_split(path)[0],
230 path: utils.url_path_split(path)[0],
231 };
231 };
232 this.add_link(model, item);
232 this.add_link(model, item);
233 offset += 1;
233 offset += 1;
234 }
234 }
235 for (var i=0; i<len; i++) {
235 for (var i=0; i<len; i++) {
236 model = list.content[i];
236 model = list.content[i];
237 item = this.new_item(i+offset, true);
237 item = this.new_item(i+offset, true);
238 this.add_link(model, item);
238 this.add_link(model, item);
239 }
239 }
240 // Trigger an event when we've finished drawing the notebook list.
240 // Trigger an event when we've finished drawing the notebook list.
241 events.trigger('draw_notebook_list.NotebookList');
241 events.trigger('draw_notebook_list.NotebookList');
242 this._selection_changed();
242 this._selection_changed();
243 };
243 };
244
244
245
245
246 /**
246 /**
247 * Creates a new item.
247 * Creates a new item.
248 * @param {integer} index
248 * @param {integer} index
249 * @param {boolean} [selectable] - tristate, undefined: don't draw checkbox,
249 * @param {boolean} [selectable] - tristate, undefined: don't draw checkbox,
250 * false: don't draw checkbox but pad
250 * false: don't draw checkbox but pad
251 * where it should be, true: draw checkbox.
251 * where it should be, true: draw checkbox.
252 * @return {JQuery} row
252 * @return {JQuery} row
253 */
253 */
254 NotebookList.prototype.new_item = function (index, selectable) {
254 NotebookList.prototype.new_item = function (index, selectable) {
255 var row = $('<div/>')
255 var row = $('<div/>')
256 .addClass("list_item")
256 .addClass("list_item")
257 .addClass("row");
257 .addClass("row");
258
258
259 var item = $("<div/>")
259 var item = $("<div/>")
260 .addClass("col-md-12")
260 .addClass("col-md-12")
261 .appendTo(row);
261 .appendTo(row);
262
262
263 var checkbox;
263 var checkbox;
264 if (selectable !== undefined) {
264 if (selectable !== undefined) {
265 checkbox = $('<input/>')
265 checkbox = $('<input/>')
266 .attr('type', 'checkbox')
266 .attr('type', 'checkbox')
267 .appendTo(item);
267 .appendTo(item);
268 }
268 }
269
269
270 $('<i/>')
270 $('<i/>')
271 .addClass('item_icon')
271 .addClass('item_icon')
272 .appendTo(item);
272 .appendTo(item);
273
273
274 var link = $("<a/>")
274 var link = $("<a/>")
275 .addClass("item_link")
275 .addClass("item_link")
276 .appendTo(item);
276 .appendTo(item);
277
277
278 $("<span/>")
278 $("<span/>")
279 .addClass("item_name")
279 .addClass("item_name")
280 .appendTo(link);
280 .appendTo(link);
281
281
282 if (selectable === false) {
282 if (selectable === false) {
283 checkbox.css('visibility', 'hidden');
283 checkbox.css('visibility', 'hidden');
284 } else if (selectable === true) {
284 } else if (selectable === true) {
285 var that = this;
285 var that = this;
286 link.click(function(e) {
286 link.click(function(e) {
287 e.stopPropagation();
287 e.stopPropagation();
288 });
288 });
289 checkbox.click(function(e) {
289 checkbox.click(function(e) {
290 e.stopPropagation();
290 e.stopPropagation();
291 that._selection_changed();
291 that._selection_changed();
292 });
292 });
293 row.click(function(e) {
293 row.click(function(e) {
294 e.stopPropagation();
294 e.stopPropagation();
295 checkbox.prop('checked', !checkbox.prop('checked'));
295 checkbox.prop('checked', !checkbox.prop('checked'));
296 that._selection_changed();
296 that._selection_changed();
297 });
297 });
298 }
298 }
299
299
300 var buttons = $('<div/>')
300 var buttons = $('<div/>')
301 .addClass("item_buttons pull-right")
301 .addClass("item_buttons pull-right")
302 .appendTo(item);
302 .appendTo(item);
303
303
304 $('<i/>')
304 $('<i/>')
305 .addClass('fa fa-power-off running-indicator')
305 .addClass('fa fa-power-off running-indicator')
306 .css('visible', 'hidden')
306 .css('visible', 'hidden')
307 .appendTo(buttons);
307 .appendTo(buttons);
308
308
309 if (index === -1) {
309 if (index === -1) {
310 this.element.append(row);
310 this.element.append(row);
311 } else {
311 } else {
312 this.element.children().eq(index).after(row);
312 this.element.children().eq(index).after(row);
313 }
313 }
314 return row;
314 return row;
315 };
315 };
316
316
317
317
318 NotebookList.icons = {
318 NotebookList.icons = {
319 directory: 'folder_icon',
319 directory: 'folder_icon',
320 notebook: 'notebook_icon',
320 notebook: 'notebook_icon',
321 file: 'file_icon',
321 file: 'file_icon',
322 };
322 };
323
323
324 NotebookList.uri_prefixes = {
324 NotebookList.uri_prefixes = {
325 directory: 'tree',
325 directory: 'tree',
326 notebook: 'notebooks',
326 notebook: 'notebooks',
327 file: 'edit',
327 file: 'edit',
328 };
328 };
329
329
330 NotebookList.prototype._selection_changed = function() {
330 NotebookList.prototype._selection_changed = function() {
331 var selected = [];
331 var selected = [];
332 var has_notebook = false;
332 var has_notebook = false;
333 var has_directory = false;
333 var has_directory = false;
334 $('.list_item :checked').each(function(index, item) {
334 $('.list_item :checked').each(function(index, item) {
335 var parent = $(item).parent().parent();
335 var parent = $(item).parent().parent();
336 selected.push({
336 selected.push({
337 name: parent.data('name'),
337 name: parent.data('name'),
338 path: parent.data('path'),
338 path: parent.data('path'),
339 type: parent.data('type')
339 type: parent.data('type')
340 });
340 });
341
341
342 has_notebook = has_notebook || parent.data('type') == 'notebook';
342 has_notebook = has_notebook || parent.data('type') == 'notebook';
343 has_directory = has_directory || parent.data('type') == 'directory';
343 has_directory = has_directory || parent.data('type') == 'directory';
344 });
344 });
345 this.selected = selected;
345 this.selected = selected;
346
346
347 // Rename is only visible when one item is selected.
347 // Rename is only visible when one item is selected.
348 if (selected.length==1) {
348 if (selected.length==1) {
349 $('.rename-button').css('display', 'inline-block');
349 $('.rename-button').css('display', 'inline-block');
350 } else {
350 } else {
351 $('.rename-button').css('display', 'none');
351 $('.rename-button').css('display', 'none');
352 }
352 }
353
353
354 // Shutdown is only visible when one or more notebooks are visible.
354 // Shutdown is only visible when one or more notebooks are visible.
355 if (has_notebook) {
355 if (has_notebook) {
356 $('.shutdown-button').css('display', 'inline-block');
356 $('.shutdown-button').css('display', 'inline-block');
357 } else {
357 } else {
358 $('.shutdown-button').css('display', 'none');
358 $('.shutdown-button').css('display', 'none');
359 }
359 }
360
360
361 // Duplicate isn't visible if a directory is selected.
361 // Duplicate isn't visible if a directory is selected.
362 if (selected.length > 0 && !has_directory) {
362 if (selected.length > 0 && !has_directory) {
363 $('.duplicate-button').css('display', 'inline-block');
363 $('.duplicate-button').css('display', 'inline-block');
364 } else {
364 } else {
365 $('.duplicate-button').css('display', 'none');
365 $('.duplicate-button').css('display', 'none');
366 }
366 }
367
367
368 // Delete is visible if one or more items are selected.
368 // Delete is visible if one or more items are selected.
369 if (selected.length > 0) {
369 if (selected.length > 0) {
370 $('.delete-button').css('display', 'inline-block');
370 $('.delete-button').css('display', 'inline-block');
371 } else {
371 } else {
372 $('.delete-button').css('display', 'none');
372 $('.delete-button').css('display', 'none');
373 }
373 }
374 };
374 };
375
375
376 NotebookList.prototype.add_link = function (model, item) {
376 NotebookList.prototype.add_link = function (model, item) {
377 var path = model.path,
377 var path = model.path,
378 name = model.name;
378 name = model.name;
379 item.data('name', name);
379 item.data('name', name);
380 item.data('path', path);
380 item.data('path', path);
381 item.data('type', model.type);
381 item.data('type', model.type);
382 item.find(".item_name").text(name);
382 item.find(".item_name").text(name);
383 var icon = NotebookList.icons[model.type];
383 var icon = NotebookList.icons[model.type];
384 var uri_prefix = NotebookList.uri_prefixes[model.type];
384 var uri_prefix = NotebookList.uri_prefixes[model.type];
385 item.find(".item_icon").addClass(icon).addClass('icon-fixed-width');
385 item.find(".item_icon").addClass(icon).addClass('icon-fixed-width');
386 var link = item.find("a.item_link")
386 var link = item.find("a.item_link")
387 .attr('href',
387 .attr('href',
388 utils.url_join_encode(
388 utils.url_join_encode(
389 this.base_url,
389 this.base_url,
390 uri_prefix,
390 uri_prefix,
391 path
391 path
392 )
392 )
393 );
393 );
394
394
395 var running = (model.type == 'notebook' && this.sessions[path] !== undefined);
395 var running = (model.type == 'notebook' && this.sessions[path] !== undefined);
396 item.find(".item_buttons i.running-indicator").css('visibility', running ? '' : 'hidden');
396 item.find(".item_buttons i.running-indicator").css('visibility', running ? '' : 'hidden');
397
397
398 // directory nav doesn't open new tabs
398 // directory nav doesn't open new tabs
399 // files, notebooks do
399 // files, notebooks do
400 if (model.type !== "directory") {
400 if (model.type !== "directory") {
401 link.attr('target','_blank');
401 link.attr('target','_blank');
402 }
402 }
403 };
403 };
404
404
405
405
406 NotebookList.prototype.add_name_input = function (name, item, icon_type) {
406 NotebookList.prototype.add_name_input = function (name, item, icon_type) {
407 item.data('name', name);
407 item.data('name', name);
408 item.find(".item_icon").addClass(NotebookList.icons[icon_type]).addClass('icon-fixed-width');
408 item.find(".item_icon").addClass(NotebookList.icons[icon_type]).addClass('icon-fixed-width');
409 item.find(".item_name").empty().append(
409 item.find(".item_name").empty().append(
410 $('<input/>')
410 $('<input/>')
411 .addClass("filename_input")
411 .addClass("filename_input")
412 .attr('value', name)
412 .attr('value', name)
413 .attr('size', '30')
413 .attr('size', '30')
414 .attr('type', 'text')
414 .attr('type', 'text')
415 .keyup(function(event){
415 .keyup(function(event){
416 if(event.keyCode == 13){item.find('.upload_button').click();}
416 if(event.keyCode == 13){item.find('.upload_button').click();}
417 else if(event.keyCode == 27){item.remove();}
417 else if(event.keyCode == 27){item.remove();}
418 })
418 })
419 );
419 );
420 };
420 };
421
421
422
422
423 NotebookList.prototype.add_file_data = function (data, item) {
423 NotebookList.prototype.add_file_data = function (data, item) {
424 item.data('filedata', data);
424 item.data('filedata', data);
425 };
425 };
426
426
427
427
428 NotebookList.prototype.shutdown_selected = function() {
428 NotebookList.prototype.shutdown_selected = function() {
429 var that = this;
429 var that = this;
430 var settings = {
430 var settings = {
431 processData : false,
431 processData : false,
432 cache : false,
432 cache : false,
433 type : "DELETE",
433 type : "DELETE",
434 dataType : "json",
434 dataType : "json",
435 success : function () {
435 success : function () {
436 that.load_sessions();
436 that.load_sessions();
437 },
437 },
438 error : utils.log_ajax_error,
438 error : utils.log_ajax_error,
439 };
439 };
440
440
441 this.selected.forEach(function(item) {
441 this.selected.forEach(function(item) {
442 if (item.type == 'notebook') {
442 if (item.type == 'notebook') {
443 var session = that.sessions[item.path];
443 var session = that.sessions[item.path];
444 if (session) {
444 if (session) {
445 var url = utils.url_join_encode(
445 var url = utils.url_join_encode(
446 that.base_url,
446 that.base_url,
447 'api/sessions',
447 'api/sessions',
448 session
448 session
449 );
449 );
450 $.ajax(url, settings);
450 $.ajax(url, settings);
451 }
451 }
452 }
452 }
453 });
453 });
454 };
454 };
455
455
456 NotebookList.prototype.rename_selected = function() {
456 NotebookList.prototype.rename_selected = function() {
457 if (this.selected.length != 1) return;
457 if (this.selected.length != 1) return;
458
458
459 var that = this;
459 var that = this;
460 var path = this.selected[0].path;
460 var path = this.selected[0].path;
461 var input = $('<input/>').attr('type','text').attr('size','25').addClass('form-control')
461 var input = $('<input/>').attr('type','text').attr('size','25').addClass('form-control')
462 .val(path);
462 .val(path);
463 var dialog_body = $('<div/>').append(
463 var dialog_body = $('<div/>').append(
464 $("<p/>").addClass("rename-message")
464 $("<p/>").addClass("rename-message")
465 .text('Enter a new directory name:')
465 .text('Enter a new directory name:')
466 ).append(
466 ).append(
467 $("<br/>")
467 $("<br/>")
468 ).append(input);
468 ).append(input);
469 var d = dialog.modal({
469 var d = dialog.modal({
470 title : "Rename directory",
470 title : "Rename directory",
471 body : dialog_body,
471 body : dialog_body,
472 buttons : {
472 buttons : {
473 OK : {
473 OK : {
474 class: "btn-primary",
474 class: "btn-primary",
475 click: function() {
475 click: function() {
476 that.contents.rename(path, input.val()).then(function() {
476 that.contents.rename(path, input.val()).then(function() {
477 that.load_list();
477 that.load_list();
478 }).catch(function(e) {
478 }).catch(function(e) {
479 dialog.modal({
479 dialog.modal({
480 title : "Error",
480 title : "Error",
481 body : $('<div/>')
481 body : $('<div/>')
482 .text("An error occurred while renaming \"" + path + "\" to \"" + input.val() + "\".")
482 .text("An error occurred while renaming \"" + path + "\" to \"" + input.val() + "\".")
483 .append($('<div/>').addClass('alert alert-danger').text(String(e))),
483 .append($('<div/>').addClass('alert alert-danger').text(String(e))),
484 buttons : {
484 buttons : {
485 OK : {}
485 OK : {}
486 }
486 }
487 });
487 });
488 });
488 });
489 }
489 }
490 },
490 },
491 Cancel : {}
491 Cancel : {}
492 },
492 },
493 open : function () {
493 open : function () {
494 // Upon ENTER, click the OK button.
494 // Upon ENTER, click the OK button.
495 input.keydown(function (event) {
495 input.keydown(function (event) {
496 if (event.which === keyboard.keycodes.enter) {
496 if (event.which === keyboard.keycodes.enter) {
497 d.find('.btn-primary').first().click();
497 d.find('.btn-primary').first().click();
498 return false;
498 return false;
499 }
499 }
500 });
500 });
501 input.focus().select();
501 input.focus().select();
502 }
502 }
503 });
503 });
504 };
504 };
505
505
506 NotebookList.prototype.delete_selected = function() {
506 NotebookList.prototype.delete_selected = function() {
507 var message;
507 var message;
508 if (this.selected.length == 1) {
508 if (this.selected.length == 1) {
509 message = 'Are you sure you want to permanently delete: ' + this.selected[0].name + '?';
509 message = 'Are you sure you want to permanently delete: ' + this.selected[0].name + '?';
510 } else {
510 } else {
511 message = 'Are you sure you want to permanently delete the ' + this.selected.length + ' files selected?';
511 message = 'Are you sure you want to permanently delete the ' + this.selected.length + ' files selected?';
512 }
512 }
513 var that = this;
513 var that = this;
514 dialog.modal({
514 dialog.modal({
515 title : "Delete",
515 title : "Delete",
516 body : message,
516 body : message,
517 buttons : {
517 buttons : {
518 Delete : {
518 Delete : {
519 class: "btn-danger",
519 class: "btn-danger",
520 click: function() {
520 click: function() {
521 // Shutdown any/all selected notebooks before deleting
522 // the files.
523 that.shutdown_selected();
524
525 // Delete selected.
521 that.selected.forEach(function(item) {
526 that.selected.forEach(function(item) {
522 that.contents.delete(item.path).then(function() {
527 that.contents.delete(item.path).then(function() {
523 that.notebook_deleted(item.path);
528 that.notebook_deleted(item.path);
524 }).catch(function(e) {
529 }).catch(function(e) {
525 dialog.modal({
530 dialog.modal({
526 title : "Error",
531 title : "Error",
527 body : $('<div/>')
532 body : $('<div/>')
528 .text("An error occurred while deleting \"" + item.path + "\".")
533 .text("An error occurred while deleting \"" + item.path + "\".")
529 .append($('<div/>').addClass('alert alert-danger').text(String(e))),
534 .append($('<div/>').addClass('alert alert-danger').text(String(e))),
530 buttons : {
535 buttons : {
531 OK : {}
536 OK : {}
532 }
537 }
533 });
538 });
534 });
539 });
535 });
540 });
536 }
541 }
537 },
542 },
538 Cancel : {}
543 Cancel : {}
539 }
544 }
540 });
545 });
541 };
546 };
542
547
543 NotebookList.prototype.duplicate_selected = function() {
548 NotebookList.prototype.duplicate_selected = function() {
544 var message;
549 var message;
545 if (this.selected.length == 1) {
550 if (this.selected.length == 1) {
546 message = 'Are you sure you want to duplicate: ' + this.selected[0].name + '?';
551 message = 'Are you sure you want to duplicate: ' + this.selected[0].name + '?';
547 } else {
552 } else {
548 message = 'Are you sure you want to duplicate the ' + this.selected.length + ' files selected?';
553 message = 'Are you sure you want to duplicate the ' + this.selected.length + ' files selected?';
549 }
554 }
550 var that = this;
555 var that = this;
551 dialog.modal({
556 dialog.modal({
552 title : "Delete",
557 title : "Delete",
553 body : message,
558 body : message,
554 buttons : {
559 buttons : {
555 Duplicate : {
560 Duplicate : {
556 class: "btn-primary",
561 class: "btn-primary",
557 click: function() {
562 click: function() {
558 that.selected.forEach(function(item) {
563 that.selected.forEach(function(item) {
559 that.contents.copy(item.path, that.notebook_path).then(function () {
564 that.contents.copy(item.path, that.notebook_path).then(function () {
560 that.load_list();
565 that.load_list();
561 }).catch(function(e) {
566 }).catch(function(e) {
562 dialog.modal({
567 dialog.modal({
563 title : "Error",
568 title : "Error",
564 body : $('<div/>')
569 body : $('<div/>')
565 .text("An error occurred while copying \"" + item.path + "\".")
570 .text("An error occurred while copying \"" + item.path + "\".")
566 .append($('<div/>').addClass('alert alert-danger').text(String(e))),
571 .append($('<div/>').addClass('alert alert-danger').text(String(e))),
567 buttons : {
572 buttons : {
568 OK : {}
573 OK : {}
569 }
574 }
570 });
575 });
571 });
576 });
572 });
577 });
573 }
578 }
574 },
579 },
575 Cancel : {}
580 Cancel : {}
576 }
581 }
577 });
582 });
578 };
583 };
579
584
580 NotebookList.prototype.notebook_deleted = function(path) {
585 NotebookList.prototype.notebook_deleted = function(path) {
581 /**
586 /**
582 * Remove the deleted notebook.
587 * Remove the deleted notebook.
583 */
588 */
584 $( ":data(path)" ).each(function() {
589 $( ":data(path)" ).each(function() {
585 var element = $(this);
590 var element = $(this);
586 if (element.data("path") == path) {
591 if (element.data("path") == path) {
587 element.remove();
592 element.remove();
588 events.trigger('notebook_deleted.NotebookList');
593 events.trigger('notebook_deleted.NotebookList');
589 this._selection_changed();
594 this._selection_changed();
590 }
595 }
591 });
596 });
592 };
597 };
593
598
594
599
595 NotebookList.prototype.add_upload_button = function (item) {
600 NotebookList.prototype.add_upload_button = function (item) {
596 var that = this;
601 var that = this;
597 var upload_button = $('<button/>').text("Upload")
602 var upload_button = $('<button/>').text("Upload")
598 .addClass('btn btn-primary btn-xs upload_button')
603 .addClass('btn btn-primary btn-xs upload_button')
599 .click(function (e) {
604 .click(function (e) {
600 var filename = item.find('.item_name > input').val();
605 var filename = item.find('.item_name > input').val();
601 var path = utils.url_path_join(that.notebook_path, filename);
606 var path = utils.url_path_join(that.notebook_path, filename);
602 var filedata = item.data('filedata');
607 var filedata = item.data('filedata');
603 var format = 'text';
608 var format = 'text';
604 if (filename.length === 0 || filename[0] === '.') {
609 if (filename.length === 0 || filename[0] === '.') {
605 dialog.modal({
610 dialog.modal({
606 title : 'Invalid file name',
611 title : 'Invalid file name',
607 body : "File names must be at least one character and not start with a dot",
612 body : "File names must be at least one character and not start with a dot",
608 buttons : {'OK' : { 'class' : 'btn-primary' }}
613 buttons : {'OK' : { 'class' : 'btn-primary' }}
609 });
614 });
610 return false;
615 return false;
611 }
616 }
612 if (filedata instanceof ArrayBuffer) {
617 if (filedata instanceof ArrayBuffer) {
613 // base64-encode binary file data
618 // base64-encode binary file data
614 var bytes = '';
619 var bytes = '';
615 var buf = new Uint8Array(filedata);
620 var buf = new Uint8Array(filedata);
616 var nbytes = buf.byteLength;
621 var nbytes = buf.byteLength;
617 for (var i=0; i<nbytes; i++) {
622 for (var i=0; i<nbytes; i++) {
618 bytes += String.fromCharCode(buf[i]);
623 bytes += String.fromCharCode(buf[i]);
619 }
624 }
620 filedata = btoa(bytes);
625 filedata = btoa(bytes);
621 format = 'base64';
626 format = 'base64';
622 }
627 }
623 var model = {};
628 var model = {};
624
629
625 var name_and_ext = utils.splitext(filename);
630 var name_and_ext = utils.splitext(filename);
626 var file_ext = name_and_ext[1];
631 var file_ext = name_and_ext[1];
627 var content_type;
632 var content_type;
628 if (file_ext === '.ipynb') {
633 if (file_ext === '.ipynb') {
629 model.type = 'notebook';
634 model.type = 'notebook';
630 model.format = 'json';
635 model.format = 'json';
631 try {
636 try {
632 model.content = JSON.parse(filedata);
637 model.content = JSON.parse(filedata);
633 } catch (e) {
638 } catch (e) {
634 dialog.modal({
639 dialog.modal({
635 title : 'Cannot upload invalid Notebook',
640 title : 'Cannot upload invalid Notebook',
636 body : "The error was: " + e,
641 body : "The error was: " + e,
637 buttons : {'OK' : {
642 buttons : {'OK' : {
638 'class' : 'btn-primary',
643 'class' : 'btn-primary',
639 click: function () {
644 click: function () {
640 item.remove();
645 item.remove();
641 }
646 }
642 }}
647 }}
643 });
648 });
644 return false;
649 return false;
645 }
650 }
646 content_type = 'application/json';
651 content_type = 'application/json';
647 } else {
652 } else {
648 model.type = 'file';
653 model.type = 'file';
649 model.format = format;
654 model.format = format;
650 model.content = filedata;
655 model.content = filedata;
651 content_type = 'application/octet-stream';
656 content_type = 'application/octet-stream';
652 }
657 }
653 filedata = item.data('filedata');
658 filedata = item.data('filedata');
654
659
655 var on_success = function () {
660 var on_success = function () {
656 item.removeClass('new-file');
661 item.removeClass('new-file');
657 that.add_link(model, item);
662 that.add_link(model, item);
658 that.add_delete_button(item);
663 that.add_delete_button(item);
659 that.session_list.load_sessions();
664 that.session_list.load_sessions();
660 };
665 };
661
666
662 var exists = false;
667 var exists = false;
663 $.each(that.element.find('.list_item:not(.new-file)'), function(k,v){
668 $.each(that.element.find('.list_item:not(.new-file)'), function(k,v){
664 if ($(v).data('name') === filename) { exists = true; return false; }
669 if ($(v).data('name') === filename) { exists = true; return false; }
665 });
670 });
666
671
667 if (exists) {
672 if (exists) {
668 dialog.modal({
673 dialog.modal({
669 title : "Replace file",
674 title : "Replace file",
670 body : 'There is already a file named ' + filename + ', do you want to replace it?',
675 body : 'There is already a file named ' + filename + ', do you want to replace it?',
671 buttons : {
676 buttons : {
672 Overwrite : {
677 Overwrite : {
673 class: "btn-danger",
678 class: "btn-danger",
674 click: function () {
679 click: function () {
675 that.contents.save(path, model).then(on_success);
680 that.contents.save(path, model).then(on_success);
676 }
681 }
677 },
682 },
678 Cancel : {
683 Cancel : {
679 click: function() { item.remove(); }
684 click: function() { item.remove(); }
680 }
685 }
681 }
686 }
682 });
687 });
683 } else {
688 } else {
684 that.contents.save(path, model).then(on_success);
689 that.contents.save(path, model).then(on_success);
685 }
690 }
686
691
687 return false;
692 return false;
688 });
693 });
689 var cancel_button = $('<button/>').text("Cancel")
694 var cancel_button = $('<button/>').text("Cancel")
690 .addClass("btn btn-default btn-xs")
695 .addClass("btn btn-default btn-xs")
691 .click(function (e) {
696 .click(function (e) {
692 item.remove();
697 item.remove();
693 return false;
698 return false;
694 });
699 });
695 item.find(".item_buttons").empty()
700 item.find(".item_buttons").empty()
696 .append(upload_button)
701 .append(upload_button)
697 .append(cancel_button);
702 .append(cancel_button);
698 };
703 };
699
704
700
705
701 // Backwards compatability.
706 // Backwards compatability.
702 IPython.NotebookList = NotebookList;
707 IPython.NotebookList = NotebookList;
703
708
704 return {'NotebookList': NotebookList};
709 return {'NotebookList': NotebookList};
705 });
710 });
General Comments 0
You need to be logged in to leave comments. Login now