##// END OF EJS Templates
fix notebook deletion....
Matthias BUSSONNIER -
Show More
@@ -1,332 +1,333
1 //----------------------------------------------------------------------------
1 //----------------------------------------------------------------------------
2 // Copyright (C) 2008-2011 The IPython Development Team
2 // Copyright (C) 2008-2011 The IPython Development Team
3 //
3 //
4 // Distributed under the terms of the BSD License. The full license is in
4 // Distributed under the terms of the BSD License. The full license is in
5 // the file COPYING, distributed as part of this software.
5 // the file COPYING, distributed as part of this software.
6 //----------------------------------------------------------------------------
6 //----------------------------------------------------------------------------
7
7
8 //============================================================================
8 //============================================================================
9 // NotebookList
9 // NotebookList
10 //============================================================================
10 //============================================================================
11
11
12 var IPython = (function (IPython) {
12 var IPython = (function (IPython) {
13
13
14 var NotebookList = function (selector) {
14 var NotebookList = function (selector) {
15 this.selector = selector;
15 this.selector = selector;
16 if (this.selector !== undefined) {
16 if (this.selector !== undefined) {
17 this.element = $(selector);
17 this.element = $(selector);
18 this.style();
18 this.style();
19 this.bind_events();
19 this.bind_events();
20 }
20 }
21 };
21 };
22
22
23 NotebookList.prototype.baseProjectUrl = function () {
23 NotebookList.prototype.baseProjectUrl = function () {
24 return $('body').data('baseProjectUrl')
24 return $('body').data('baseProjectUrl')
25 };
25 };
26
26
27 NotebookList.prototype.style = function () {
27 NotebookList.prototype.style = function () {
28 $('#notebook_toolbar').addClass('list_toolbar');
28 $('#notebook_toolbar').addClass('list_toolbar');
29 $('#drag_info').addClass('toolbar_info');
29 $('#drag_info').addClass('toolbar_info');
30 $('#notebook_buttons').addClass('toolbar_buttons');
30 $('#notebook_buttons').addClass('toolbar_buttons');
31 $('div#project_name').addClass('list_header ui-widget ui-widget-header');
31 $('div#project_name').addClass('list_header ui-widget ui-widget-header');
32 $('#refresh_notebook_list').button({
32 $('#refresh_notebook_list').button({
33 icons : {primary: 'ui-icon-arrowrefresh-1-s'},
33 icons : {primary: 'ui-icon-arrowrefresh-1-s'},
34 text : false
34 text : false
35 });
35 });
36 };
36 };
37
37
38
38
39 NotebookList.prototype.bind_events = function () {
39 NotebookList.prototype.bind_events = function () {
40 if (IPython.read_only){
40 if (IPython.read_only){
41 return;
41 return;
42 }
42 }
43 var that = this;
43 var that = this;
44 $('#refresh_notebook_list').click(function () {
44 $('#refresh_notebook_list').click(function () {
45 that.load_list();
45 that.load_list();
46 });
46 });
47 this.element.bind('dragover', function () {
47 this.element.bind('dragover', function () {
48 return false;
48 return false;
49 });
49 });
50 this.element.bind('drop', function(event){
50 this.element.bind('drop', function(event){
51 that.handelFilesUpload(event,'drop');
51 that.handelFilesUpload(event,'drop');
52 return false;
52 return false;
53 });
53 });
54 };
54 };
55
55
56 NotebookList.prototype.handelFilesUpload = function(event, dropOrForm) {
56 NotebookList.prototype.handelFilesUpload = function(event, dropOrForm) {
57 var that = this;
57 var that = this;
58 var files;
58 var files;
59 if(dropOrForm =='drop'){
59 if(dropOrForm =='drop'){
60 files = event.originalEvent.dataTransfer.files;
60 files = event.originalEvent.dataTransfer.files;
61 } else
61 } else
62 {
62 {
63 files = event.originalEvent.target.files
63 files = event.originalEvent.target.files
64 }
64 }
65 for (var i = 0, f; f = files[i]; i++) {
65 for (var i = 0, f; f = files[i]; i++) {
66 var reader = new FileReader();
66 var reader = new FileReader();
67 reader.readAsText(f);
67 reader.readAsText(f);
68 var fname = f.name.split('.');
68 var fname = f.name.split('.');
69 var nbname = fname.slice(0,-1).join('.');
69 var nbname = fname.slice(0,-1).join('.');
70 var nbformat = fname.slice(-1)[0];
70 var nbformat = fname.slice(-1)[0];
71 if (nbformat === 'ipynb') {nbformat = 'json';};
71 if (nbformat === 'ipynb') {nbformat = 'json';};
72 if (nbformat === 'py' || nbformat === 'json') {
72 if (nbformat === 'py' || nbformat === 'json') {
73 var item = that.new_notebook_item(0);
73 var item = that.new_notebook_item(0);
74 that.add_name_input(nbname, item);
74 that.add_name_input(nbname, item);
75 item.data('nbformat', nbformat);
75 item.data('nbformat', nbformat);
76 // Store the notebook item in the reader so we can use it later
76 // Store the notebook item in the reader so we can use it later
77 // to know which item it belongs to.
77 // to know which item it belongs to.
78 $(reader).data('item', item);
78 $(reader).data('item', item);
79 reader.onload = function (event) {
79 reader.onload = function (event) {
80 var nbitem = $(event.target).data('item');
80 var nbitem = $(event.target).data('item');
81 that.add_notebook_data(event.target.result, nbitem);
81 that.add_notebook_data(event.target.result, nbitem);
82 that.add_upload_button(nbitem);
82 that.add_upload_button(nbitem);
83 };
83 };
84 };
84 };
85 }
85 }
86 return false;
86 return false;
87 };
87 };
88
88
89 NotebookList.prototype.clear_list = function () {
89 NotebookList.prototype.clear_list = function () {
90 this.element.children('.list_item').remove();
90 this.element.children('.list_item').remove();
91 };
91 };
92
92
93
93
94 NotebookList.prototype.load_list = function () {
94 NotebookList.prototype.load_list = function () {
95 var that = this;
95 var that = this;
96 var settings = {
96 var settings = {
97 processData : false,
97 processData : false,
98 cache : false,
98 cache : false,
99 type : "GET",
99 type : "GET",
100 dataType : "json",
100 dataType : "json",
101 success : $.proxy(this.list_loaded, this),
101 success : $.proxy(this.list_loaded, this),
102 error : $.proxy( function(){
102 error : $.proxy( function(){
103 that.list_loaded([], null, null, {msg:"Error connecting to server."});
103 that.list_loaded([], null, null, {msg:"Error connecting to server."});
104 },this)
104 },this)
105 };
105 };
106
106
107 var url = this.baseProjectUrl() + 'notebooks';
107 var url = this.baseProjectUrl() + 'notebooks';
108 $.ajax(url, settings);
108 $.ajax(url, settings);
109 };
109 };
110
110
111
111
112 NotebookList.prototype.list_loaded = function (data, status, xhr, param) {
112 NotebookList.prototype.list_loaded = function (data, status, xhr, param) {
113 var message = 'Notebook list empty.';
113 var message = 'Notebook list empty.';
114 if (param !== undefined && param.msg) {
114 if (param !== undefined && param.msg) {
115 var message = param.msg;
115 var message = param.msg;
116 }
116 }
117 var len = data.length;
117 var len = data.length;
118 this.clear_list();
118 this.clear_list();
119
119
120 if(len == 0)
120 if(len == 0)
121 {
121 {
122 $(this.new_notebook_item(0))
122 $(this.new_notebook_item(0))
123 .append(
123 .append(
124 $('<div style="margin:auto;text-align:center;color:grey"/>')
124 $('<div style="margin:auto;text-align:center;color:grey"/>')
125 .text(message)
125 .text(message)
126 )
126 )
127 }
127 }
128
128
129 for (var i=0; i<len; i++) {
129 for (var i=0; i<len; i++) {
130 var notebook_id = data[i].notebook_id;
130 var notebook_id = data[i].notebook_id;
131 var nbname = data[i].name;
131 var nbname = data[i].name;
132 var kernel = data[i].kernel_id;
132 var kernel = data[i].kernel_id;
133 var item = this.new_notebook_item(i);
133 var item = this.new_notebook_item(i);
134 this.add_link(notebook_id, nbname, item);
134 this.add_link(notebook_id, nbname, item);
135 if (!IPython.read_only){
135 if (!IPython.read_only){
136 // hide delete buttons when readonly
136 // hide delete buttons when readonly
137 if(kernel == null){
137 if(kernel == null){
138 this.add_delete_button(item);
138 this.add_delete_button(item);
139 } else {
139 } else {
140 this.add_shutdown_button(item,kernel);
140 this.add_shutdown_button(item,kernel);
141 }
141 }
142 }
142 }
143 };
143 };
144 };
144 };
145
145
146
146
147 NotebookList.prototype.new_notebook_item = function (index) {
147 NotebookList.prototype.new_notebook_item = function (index) {
148 var item = $('<div/>');
148 var item = $('<div/>');
149 item.addClass('list_item ui-widget ui-widget-content ui-helper-clearfix');
149 item.addClass('list_item ui-widget ui-widget-content ui-helper-clearfix');
150 item.css('border-top-style','none');
150 item.css('border-top-style','none');
151 var item_name = $('<span/>').addClass('item_name');
151 var item_name = $('<span/>').addClass('item_name');
152
152
153 item.append(item_name);
153 item.append(item_name);
154 if (index === -1) {
154 if (index === -1) {
155 this.element.append(item);
155 this.element.append(item);
156 } else {
156 } else {
157 this.element.children().eq(index).after(item);
157 this.element.children().eq(index).after(item);
158 }
158 }
159 return item;
159 return item;
160 };
160 };
161
161
162
162
163 NotebookList.prototype.add_link = function (notebook_id, nbname, item) {
163 NotebookList.prototype.add_link = function (notebook_id, nbname, item) {
164 item.data('nbname', nbname);
164 item.data('nbname', nbname);
165 item.data('notebook_id', notebook_id);
165 item.data('notebook_id', notebook_id);
166 var new_item_name = $('<span/>').addClass('item_name');
166 var new_item_name = $('<span/>').addClass('item_name');
167 new_item_name.append(
167 new_item_name.append(
168 $('<a/>').
168 $('<a/>').
169 attr('href', this.baseProjectUrl()+notebook_id).
169 attr('href', this.baseProjectUrl()+notebook_id).
170 attr('target','_blank').
170 attr('target','_blank').
171 text(nbname)
171 text(nbname)
172 );
172 );
173 var e = item.find('.item_name');
173 var e = item.find('.item_name');
174 if (e.length === 0) {
174 if (e.length === 0) {
175 item.append(new_item_name);
175 item.append(new_item_name);
176 } else {
176 } else {
177 e.replaceWith(new_item_name);
177 e.replaceWith(new_item_name);
178 };
178 };
179 };
179 };
180
180
181
181
182 NotebookList.prototype.add_name_input = function (nbname, item) {
182 NotebookList.prototype.add_name_input = function (nbname, item) {
183 item.data('nbname', nbname);
183 item.data('nbname', nbname);
184 var new_item_name = $('<span/>').addClass('item_name');
184 var new_item_name = $('<span/>').addClass('item_name');
185 new_item_name.append(
185 new_item_name.append(
186 $('<input/>').addClass('ui-widget ui-widget-content').
186 $('<input/>').addClass('ui-widget ui-widget-content').
187 attr('value', nbname).
187 attr('value', nbname).
188 attr('size', '30').
188 attr('size', '30').
189 attr('type', 'text')
189 attr('type', 'text')
190 );
190 );
191 var e = item.find('.item_name');
191 var e = item.find('.item_name');
192 if (e.length === 0) {
192 if (e.length === 0) {
193 item.append(new_item_name);
193 item.append(new_item_name);
194 } else {
194 } else {
195 e.replaceWith(new_item_name);
195 e.replaceWith(new_item_name);
196 };
196 };
197 };
197 };
198
198
199
199
200 NotebookList.prototype.add_notebook_data = function (data, item) {
200 NotebookList.prototype.add_notebook_data = function (data, item) {
201 item.data('nbdata',data);
201 item.data('nbdata',data);
202 };
202 };
203
203
204
204
205 NotebookList.prototype.add_shutdown_button = function (item,kernel) {
205 NotebookList.prototype.add_shutdown_button = function (item,kernel) {
206 var new_buttons = $('<span/>').addClass('item_buttons');
206 var new_buttons = $('<span/>').addClass('item_buttons');
207 var that = this;
207 var that = this;
208 var shutdown_button = $('<button>Shutdown</button>').button().
208 var shutdown_button = $('<button>Shutdown</button>').button().
209 click(function (e) {
209 click(function (e) {
210 var settings = {
210 var settings = {
211 processData : false,
211 processData : false,
212 cache : false,
212 cache : false,
213 type : "DELETE",
213 type : "DELETE",
214 dataType : "json",
214 dataType : "json",
215 success : function (data, status, xhr) {
215 success : function (data, status, xhr) {
216 that.load_list();
216 that.load_list();
217 }
217 }
218 };
218 };
219 var url = that.baseProjectUrl() + 'kernels/'+kernel;
219 var url = that.baseProjectUrl() + 'kernels/'+kernel;
220 $.ajax(url, settings);
220 $.ajax(url, settings);
221 });
221 });
222 new_buttons.append(shutdown_button);
222 new_buttons.append(shutdown_button);
223 var e = item.find('.item_buttons');
223 var e = item.find('.item_buttons');
224 if (e.length === 0) {
224 if (e.length === 0) {
225 item.append(new_buttons);
225 item.append(new_buttons);
226 } else {
226 } else {
227 e.replaceWith(new_buttons);
227 e.replaceWith(new_buttons);
228 };
228 };
229 };
229 };
230
230
231 NotebookList.prototype.add_delete_button = function (item) {
231 NotebookList.prototype.add_delete_button = function (item) {
232 var new_buttons = $('<span/>').addClass('item_buttons');
232 var new_buttons = $('<span/>').addClass('item_buttons');
233 var notebooklist = this;
233 var delete_button = $('<button>Delete</button>').button().
234 var delete_button = $('<button>Delete</button>').button().
234 click(function (e) {
235 click(function (e) {
235 // $(this) is the button that was clicked.
236 // $(this) is the button that was clicked.
236 var that = $(this);
237 var that = $(this);
237 // We use the nbname and notebook_id from the parent notebook_item element's
238 // We use the nbname and notebook_id from the parent notebook_item element's
238 // data because the outer scopes values change as we iterate through the loop.
239 // data because the outer scopes values change as we iterate through the loop.
239 var parent_item = that.parents('div.list_item');
240 var parent_item = that.parents('div.list_item');
240 var nbname = parent_item.data('nbname');
241 var nbname = parent_item.data('nbname');
241 var notebook_id = parent_item.data('notebook_id');
242 var notebook_id = parent_item.data('notebook_id');
242 var dialog = $('<div/>');
243 var dialog = $('<div/>');
243 dialog.html('Are you sure you want to permanently delete the notebook: ' + nbname + '?');
244 dialog.html('Are you sure you want to permanently delete the notebook: ' + nbname + '?');
244 parent_item.append(dialog);
245 parent_item.append(dialog);
245 dialog.dialog({
246 dialog.dialog({
246 resizable: false,
247 resizable: false,
247 modal: true,
248 modal: true,
248 title: "Delete notebook",
249 title: "Delete notebook",
249 buttons : {
250 buttons : {
250 "Delete": function () {
251 "Delete": function () {
251 var settings = {
252 var settings = {
252 processData : false,
253 processData : false,
253 cache : false,
254 cache : false,
254 type : "DELETE",
255 type : "DELETE",
255 dataType : "json",
256 dataType : "json",
256 success : function (data, status, xhr) {
257 success : function (data, status, xhr) {
257 parent_item.remove();
258 parent_item.remove();
258 }
259 }
259 };
260 };
260 var url = that.baseProjectUrl() + 'notebooks/' + notebook_id;
261 var url = notebooklist.baseProjectUrl() + 'notebooks/' + notebook_id;
261 $.ajax(url, settings);
262 $.ajax(url, settings);
262 $(this).dialog('close');
263 $(this).dialog('close');
263 },
264 },
264 "Cancel": function () {
265 "Cancel": function () {
265 $(this).dialog('close');
266 $(this).dialog('close');
266 }
267 }
267 }
268 }
268 });
269 });
269 });
270 });
270 new_buttons.append(delete_button);
271 new_buttons.append(delete_button);
271 var e = item.find('.item_buttons');
272 var e = item.find('.item_buttons');
272 if (e.length === 0) {
273 if (e.length === 0) {
273 item.append(new_buttons);
274 item.append(new_buttons);
274 } else {
275 } else {
275 e.replaceWith(new_buttons);
276 e.replaceWith(new_buttons);
276 };
277 };
277 };
278 };
278
279
279
280
280 NotebookList.prototype.add_upload_button = function (item) {
281 NotebookList.prototype.add_upload_button = function (item) {
281 var that = this;
282 var that = this;
282 var new_buttons = $('<span/>').addClass('item_buttons');
283 var new_buttons = $('<span/>').addClass('item_buttons');
283 var upload_button = $('<button>Upload</button>').button().
284 var upload_button = $('<button>Upload</button>').button().
284 addClass('upload-button').
285 addClass('upload-button').
285 click(function (e) {
286 click(function (e) {
286 var nbname = item.find('.item_name > input').attr('value');
287 var nbname = item.find('.item_name > input').attr('value');
287 var nbformat = item.data('nbformat');
288 var nbformat = item.data('nbformat');
288 var nbdata = item.data('nbdata');
289 var nbdata = item.data('nbdata');
289 var content_type = 'text/plain';
290 var content_type = 'text/plain';
290 if (nbformat === 'json') {
291 if (nbformat === 'json') {
291 content_type = 'application/json';
292 content_type = 'application/json';
292 } else if (nbformat === 'py') {
293 } else if (nbformat === 'py') {
293 content_type = 'application/x-python';
294 content_type = 'application/x-python';
294 };
295 };
295 var settings = {
296 var settings = {
296 processData : false,
297 processData : false,
297 cache : false,
298 cache : false,
298 type : 'POST',
299 type : 'POST',
299 dataType : 'json',
300 dataType : 'json',
300 data : nbdata,
301 data : nbdata,
301 headers : {'Content-Type': content_type},
302 headers : {'Content-Type': content_type},
302 success : function (data, status, xhr) {
303 success : function (data, status, xhr) {
303 that.add_link(data, nbname, item);
304 that.add_link(data, nbname, item);
304 that.add_delete_button(item);
305 that.add_delete_button(item);
305 }
306 }
306 };
307 };
307
308
308 var qs = $.param({name:nbname, format:nbformat});
309 var qs = $.param({name:nbname, format:nbformat});
309 var url = that.baseProjectUrl() + 'notebooks?' + qs;
310 var url = that.baseProjectUrl() + 'notebooks?' + qs;
310 $.ajax(url, settings);
311 $.ajax(url, settings);
311 });
312 });
312 var cancel_button = $('<button>Cancel</button>').button().
313 var cancel_button = $('<button>Cancel</button>').button().
313 click(function (e) {
314 click(function (e) {
314 item.remove();
315 item.remove();
315 });
316 });
316 upload_button.addClass('upload_button');
317 upload_button.addClass('upload_button');
317 new_buttons.append(upload_button).append(cancel_button);
318 new_buttons.append(upload_button).append(cancel_button);
318 var e = item.find('.item_buttons');
319 var e = item.find('.item_buttons');
319 if (e.length === 0) {
320 if (e.length === 0) {
320 item.append(new_buttons);
321 item.append(new_buttons);
321 } else {
322 } else {
322 e.replaceWith(new_buttons);
323 e.replaceWith(new_buttons);
323 };
324 };
324 };
325 };
325
326
326
327
327 IPython.NotebookList = NotebookList;
328 IPython.NotebookList = NotebookList;
328
329
329 return IPython;
330 return IPython;
330
331
331 }(IPython));
332 }(IPython));
332
333
General Comments 0
You need to be logged in to leave comments. Login now