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