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