##// END OF EJS Templates
Merge pull request #2159 from Carreau/dashbord_refresh_on_error...
Bussonnier Matthias -
r7942:aa922b15 merge
parent child Browse files
Show More
@@ -1,319 +1,325 b''
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 var that = this;
91 92 var settings = {
92 93 processData : false,
93 94 cache : false,
94 95 type : "GET",
95 96 dataType : "json",
96 success : $.proxy(this.list_loaded, this)
97 success : $.proxy(this.list_loaded, this),
98 error : $.proxy( function(){
99 that.list_loaded([], null, null, {msg:"Error connecting to server."});
100 },this)
97 101 };
102
98 103 var url = $('body').data('baseProjectUrl') + 'notebooks';
99 104 $.ajax(url, settings);
100 105 };
101 106
102 107
103 NotebookList.prototype.list_loaded = function (data, status, xhr) {
108 NotebookList.prototype.list_loaded = function (data, status, xhr, param) {
109 var message = param.msg || 'Notebook list empty.';
104 110 var len = data.length;
105 111 this.clear_list();
106 112
107 113 if(len == 0)
108 114 {
109 115 $(this.new_notebook_item(0))
110 116 .append(
111 117 $('<div style="margin:auto;text-align:center;color:grey"/>')
112 .text('Notebook list empty.')
118 .text(message)
113 119 )
114 120 }
115 121
116 122 for (var i=0; i<len; i++) {
117 123 var notebook_id = data[i].notebook_id;
118 124 var nbname = data[i].name;
119 125 var kernel = data[i].kernel_id;
120 126 var item = this.new_notebook_item(i);
121 127 this.add_link(notebook_id, nbname, item);
122 128 if (!IPython.read_only){
123 129 // hide delete buttons when readonly
124 130 if(kernel == null){
125 131 this.add_delete_button(item);
126 132 } else {
127 133 this.add_shutdown_button(item,kernel);
128 134 }
129 135 }
130 136 };
131 137 };
132 138
133 139
134 140 NotebookList.prototype.new_notebook_item = function (index) {
135 141 var item = $('<div/>');
136 142 item.addClass('list_item ui-widget ui-widget-content ui-helper-clearfix');
137 143 item.css('border-top-style','none');
138 144 var item_name = $('<span/>').addClass('item_name');
139 145
140 146 item.append(item_name);
141 147 if (index === -1) {
142 148 this.element.append(item);
143 149 } else {
144 150 this.element.children().eq(index).after(item);
145 151 }
146 152 return item;
147 153 };
148 154
149 155
150 156 NotebookList.prototype.add_link = function (notebook_id, nbname, item) {
151 157 item.data('nbname', nbname);
152 158 item.data('notebook_id', notebook_id);
153 159 var new_item_name = $('<span/>').addClass('item_name');
154 160 new_item_name.append(
155 161 $('<a/>').
156 162 attr('href', $('body').data('baseProjectUrl')+notebook_id).
157 163 attr('target','_blank').
158 164 text(nbname)
159 165 );
160 166 var e = item.find('.item_name');
161 167 if (e.length === 0) {
162 168 item.append(new_item_name);
163 169 } else {
164 170 e.replaceWith(new_item_name);
165 171 };
166 172 };
167 173
168 174
169 175 NotebookList.prototype.add_name_input = function (nbname, item) {
170 176 item.data('nbname', nbname);
171 177 var new_item_name = $('<span/>').addClass('item_name');
172 178 new_item_name.append(
173 179 $('<input/>').addClass('ui-widget ui-widget-content').
174 180 attr('value', nbname).
175 181 attr('size', '30').
176 182 attr('type', 'text')
177 183 );
178 184 var e = item.find('.item_name');
179 185 if (e.length === 0) {
180 186 item.append(new_item_name);
181 187 } else {
182 188 e.replaceWith(new_item_name);
183 189 };
184 190 };
185 191
186 192
187 193 NotebookList.prototype.add_notebook_data = function (data, item) {
188 194 item.data('nbdata',data);
189 195 };
190 196
191 197
192 198 NotebookList.prototype.add_shutdown_button = function (item,kernel) {
193 199 var new_buttons = $('<span/>').addClass('item_buttons');
194 200 var that = this;
195 201 var shutdown_button = $('<button>Shutdown</button>').button().
196 202 click(function (e) {
197 203 var settings = {
198 204 processData : false,
199 205 cache : false,
200 206 type : "DELETE",
201 207 dataType : "json",
202 208 success : function (data, status, xhr) {
203 209 that.load_list();
204 210 }
205 211 };
206 212 var url = $('body').data('baseProjectUrl') + 'kernels/'+kernel;
207 213 $.ajax(url, settings);
208 214 });
209 215 new_buttons.append(shutdown_button);
210 216 var e = item.find('.item_buttons');
211 217 if (e.length === 0) {
212 218 item.append(new_buttons);
213 219 } else {
214 220 e.replaceWith(new_buttons);
215 221 };
216 222 };
217 223
218 224 NotebookList.prototype.add_delete_button = function (item) {
219 225 var new_buttons = $('<span/>').addClass('item_buttons');
220 226 var delete_button = $('<button>Delete</button>').button().
221 227 click(function (e) {
222 228 // $(this) is the button that was clicked.
223 229 var that = $(this);
224 230 // We use the nbname and notebook_id from the parent notebook_item element's
225 231 // data because the outer scopes values change as we iterate through the loop.
226 232 var parent_item = that.parents('div.list_item');
227 233 var nbname = parent_item.data('nbname');
228 234 var notebook_id = parent_item.data('notebook_id');
229 235 var dialog = $('<div/>');
230 236 dialog.html('Are you sure you want to permanently delete the notebook: ' + nbname + '?');
231 237 parent_item.append(dialog);
232 238 dialog.dialog({
233 239 resizable: false,
234 240 modal: true,
235 241 title: "Delete notebook",
236 242 buttons : {
237 243 "Delete": function () {
238 244 var settings = {
239 245 processData : false,
240 246 cache : false,
241 247 type : "DELETE",
242 248 dataType : "json",
243 249 success : function (data, status, xhr) {
244 250 parent_item.remove();
245 251 }
246 252 };
247 253 var url = $('body').data('baseProjectUrl') + 'notebooks/' + notebook_id;
248 254 $.ajax(url, settings);
249 255 $(this).dialog('close');
250 256 },
251 257 "Cancel": function () {
252 258 $(this).dialog('close');
253 259 }
254 260 }
255 261 });
256 262 });
257 263 new_buttons.append(delete_button);
258 264 var e = item.find('.item_buttons');
259 265 if (e.length === 0) {
260 266 item.append(new_buttons);
261 267 } else {
262 268 e.replaceWith(new_buttons);
263 269 };
264 270 };
265 271
266 272
267 273 NotebookList.prototype.add_upload_button = function (item) {
268 274 var that = this;
269 275 var new_buttons = $('<span/>').addClass('item_buttons');
270 276 var upload_button = $('<button>Upload</button>').button().
271 277 addClass('upload-button').
272 278 click(function (e) {
273 279 var nbname = item.find('.item_name > input').attr('value');
274 280 var nbformat = item.data('nbformat');
275 281 var nbdata = item.data('nbdata');
276 282 var content_type = 'text/plain';
277 283 if (nbformat === 'json') {
278 284 content_type = 'application/json';
279 285 } else if (nbformat === 'py') {
280 286 content_type = 'application/x-python';
281 287 };
282 288 var settings = {
283 289 processData : false,
284 290 cache : false,
285 291 type : 'POST',
286 292 dataType : 'json',
287 293 data : nbdata,
288 294 headers : {'Content-Type': content_type},
289 295 success : function (data, status, xhr) {
290 296 that.add_link(data, nbname, item);
291 297 that.add_delete_button(item);
292 298 }
293 299 };
294 300
295 301 var qs = $.param({name:nbname, format:nbformat});
296 302 var url = $('body').data('baseProjectUrl') + 'notebooks?' + qs;
297 303 $.ajax(url, settings);
298 304 });
299 305 var cancel_button = $('<button>Cancel</button>').button().
300 306 click(function (e) {
301 307 item.remove();
302 308 });
303 309 upload_button.addClass('upload_button');
304 310 new_buttons.append(upload_button).append(cancel_button);
305 311 var e = item.find('.item_buttons');
306 312 if (e.length === 0) {
307 313 item.append(new_buttons);
308 314 } else {
309 315 e.replaceWith(new_buttons);
310 316 };
311 317 };
312 318
313 319
314 320 IPython.NotebookList = NotebookList;
315 321
316 322 return IPython;
317 323
318 324 }(IPython));
319 325
General Comments 0
You need to be logged in to leave comments. Login now