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