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