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