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