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