##// END OF EJS Templates
mixup notebook_list
MinRK -
Show More
@@ -1,388 +1,389 b''
1 //----------------------------------------------------------------------------
1 //----------------------------------------------------------------------------
2 // Copyright (C) 2011 The IPython Development Team
2 // Copyright (C) 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 "use strict";
13 "use strict";
14
14
15 var utils = IPython.utils;
15 var utils = IPython.utils;
16
16
17 var NotebookList = function (selector) {
17 var NotebookList = function (selector) {
18 this.selector = selector;
18 this.selector = selector;
19 if (this.selector !== undefined) {
19 if (this.selector !== undefined) {
20 this.element = $(selector);
20 this.element = $(selector);
21 this.style();
21 this.style();
22 this.bind_events();
22 this.bind_events();
23 }
23 }
24 this.notebooks_list = [];
24 this.notebooks_list = [];
25 this.sessions = {};
25 this.sessions = {};
26 };
26 };
27
27
28 NotebookList.prototype.baseProjectUrl = function () {
28 NotebookList.prototype.baseProjectUrl = function () {
29 return $('body').data('baseProjectUrl');
29 return $('body').data('baseProjectUrl');
30 };
30 };
31
31
32 NotebookList.prototype.notebookPath = function() {
32 NotebookList.prototype.notebookPath = function() {
33 return $('body').data('notebookPath');
33 return $('body').data('notebookPath');
34 };
34 };
35
35
36 NotebookList.prototype.style = function () {
36 NotebookList.prototype.style = function () {
37 $('#notebook_toolbar').addClass('list_toolbar');
37 $('#notebook_toolbar').addClass('list_toolbar');
38 $('#drag_info').addClass('toolbar_info');
38 $('#drag_info').addClass('toolbar_info');
39 $('#notebook_buttons').addClass('toolbar_buttons');
39 $('#notebook_buttons').addClass('toolbar_buttons');
40 $('#notebook_list_header').addClass('list_header');
40 $('#notebook_list_header').addClass('list_header');
41 this.element.addClass("list_container");
41 this.element.addClass("list_container");
42 };
42 };
43
43
44
44
45 NotebookList.prototype.bind_events = function () {
45 NotebookList.prototype.bind_events = function () {
46 var that = this;
46 var that = this;
47 $('#refresh_notebook_list').click(function () {
47 $('#refresh_notebook_list').click(function () {
48 that.load_list();
48 that.load_list();
49 });
49 });
50 this.element.bind('dragover', function () {
50 this.element.bind('dragover', function () {
51 return false;
51 return false;
52 });
52 });
53 this.element.bind('drop', function(event){
53 this.element.bind('drop', function(event){
54 that.handelFilesUpload(event,'drop');
54 that.handelFilesUpload(event,'drop');
55 return false;
55 return false;
56 });
56 });
57 };
57 };
58
58
59 NotebookList.prototype.handelFilesUpload = function(event, dropOrForm) {
59 NotebookList.prototype.handelFilesUpload = function(event, dropOrForm) {
60 var that = this;
60 var that = this;
61 var files;
61 var files;
62 if(dropOrForm =='drop'){
62 if(dropOrForm =='drop'){
63 files = event.originalEvent.dataTransfer.files;
63 files = event.originalEvent.dataTransfer.files;
64 } else
64 } else
65 {
65 {
66 files = event.originalEvent.target.files;
66 files = event.originalEvent.target.files;
67 }
67 }
68 for (var i = 0; i < files.length; i++) {
68 for (var i = 0; i < files.length; i++) {
69 var f = files[i];
69 var f = files[i];
70 var reader = new FileReader();
70 var reader = new FileReader();
71 reader.readAsText(f);
71 reader.readAsText(f);
72 var fname = f.name.split('.');
72 var fname = f.name.split('.');
73 var nbname = fname.slice(0,-1).join('.');
73 var nbname = fname.slice(0,-1).join('.');
74 var nbformat = fname.slice(-1)[0];
74 var nbformat = fname.slice(-1)[0];
75 if (nbformat === 'ipynb') {nbformat = 'json';}
75 if (nbformat === 'ipynb') {nbformat = 'json';}
76 if (nbformat === 'py' || nbformat === 'json') {
76 if (nbformat === 'py' || nbformat === 'json') {
77 var item = that.new_notebook_item(0);
77 var item = that.new_notebook_item(0);
78 that.add_name_input(nbname, item);
78 that.add_name_input(nbname, item);
79 item.data('nbformat', nbformat);
79 item.data('nbformat', nbformat);
80 // Store the notebook item in the reader so we can use it later
80 // Store the notebook item in the reader so we can use it later
81 // to know which item it belongs to.
81 // to know which item it belongs to.
82 $(reader).data('item', item);
82 $(reader).data('item', item);
83 reader.onload = function (event) {
83 reader.onload = function (event) {
84 var nbitem = $(event.target).data('item');
84 var nbitem = $(event.target).data('item');
85 that.add_notebook_data(event.target.result, nbitem);
85 that.add_notebook_data(event.target.result, nbitem);
86 that.add_upload_button(nbitem);
86 that.add_upload_button(nbitem);
87 };
87 };
88 }
88 }
89 }
89 }
90 return false;
90 return false;
91 };
91 };
92
92
93 NotebookList.prototype.clear_list = function () {
93 NotebookList.prototype.clear_list = function () {
94 this.element.children('.list_item').remove();
94 this.element.children('.list_item').remove();
95 };
95 };
96
96
97 NotebookList.prototype.load_sessions = function(){
97 NotebookList.prototype.load_sessions = function(){
98 var that = this;
98 var that = this;
99 var settings = {
99 var settings = {
100 processData : false,
100 processData : false,
101 cache : false,
101 cache : false,
102 type : "GET",
102 type : "GET",
103 dataType : "json",
103 dataType : "json",
104 success : $.proxy(that.sessions_loaded, this)
104 success : $.proxy(that.sessions_loaded, this)
105 };
105 };
106 var url = this.baseProjectUrl() + 'api/sessions';
106 var url = this.baseProjectUrl() + 'api/sessions';
107 $.ajax(url,settings);
107 $.ajax(url,settings);
108 };
108 };
109
109
110
110
111 NotebookList.prototype.sessions_loaded = function(data){
111 NotebookList.prototype.sessions_loaded = function(data){
112 this.sessions = {};
112 this.sessions = {};
113 var len = data.length;
113 var len = data.length;
114 if (len > 0) {
114 if (len > 0) {
115 for (var i=0; i<len; i++) {
115 for (var i=0; i<len; i++) {
116 var nb_path;
116 if (!data[i].notebook.path) {
117 if (!data[i].notebook.path) {
117 nb_path = data[i].notebook.name;
118 nb_path = data[i].notebook.name;
118 }
119 }
119 else {
120 else {
120 nb_path = utils.url_path_join(
121 nb_path = utils.url_path_join(
121 data[i].notebook.path,
122 data[i].notebook.path,
122 data[i].notebook.name
123 data[i].notebook.name
123 );
124 );
124 }
125 }
125 this.sessions[nb_path] = data[i].id;
126 this.sessions[nb_path] = data[i].id;
126 }
127 }
127 }
128 }
128 this.load_list();
129 this.load_list();
129 };
130 };
130
131
131 NotebookList.prototype.load_list = function () {
132 NotebookList.prototype.load_list = function () {
132 var that = this;
133 var that = this;
133 var settings = {
134 var settings = {
134 processData : false,
135 processData : false,
135 cache : false,
136 cache : false,
136 type : "GET",
137 type : "GET",
137 dataType : "json",
138 dataType : "json",
138 success : $.proxy(this.list_loaded, this),
139 success : $.proxy(this.list_loaded, this),
139 error : $.proxy( function(){
140 error : $.proxy( function(){
140 that.list_loaded([], null, null, {msg:"Error connecting to server."});
141 that.list_loaded([], null, null, {msg:"Error connecting to server."});
141 },this)
142 },this)
142 };
143 };
143
144
144 var url = utils.url_path_join(
145 var url = utils.url_path_join(
145 this.baseProjectUrl(),
146 this.baseProjectUrl(),
146 'api',
147 'api',
147 'notebooks',
148 'notebooks',
148 this.notebookPath()
149 this.notebookPath()
149 );
150 );
150 $.ajax(url, settings);
151 $.ajax(url, settings);
151 };
152 };
152
153
153
154
154 NotebookList.prototype.list_loaded = function (data, status, xhr, param) {
155 NotebookList.prototype.list_loaded = function (data, status, xhr, param) {
155 var message = 'Notebook list empty.';
156 var message = 'Notebook list empty.';
156 if (param !== undefined && param.msg) {
157 if (param !== undefined && param.msg) {
157 message = param.msg;
158 message = param.msg;
158 }
159 }
159 var len = data.length;
160 var len = data.length;
160 this.clear_list();
161 this.clear_list();
161 if (len === 0) {
162 if (len === 0) {
162 $(this.new_notebook_item(0))
163 $(this.new_notebook_item(0))
163 .append(
164 .append(
164 $('<div style="margin:auto;text-align:center;color:grey"/>')
165 $('<div style="margin:auto;text-align:center;color:grey"/>')
165 .text(message)
166 .text(message)
166 );
167 );
167 }
168 }
168 for (var i=0; i<len; i++) {
169 for (var i=0; i<len; i++) {
169 var name = data[i].name;
170 var name = data[i].name;
170 var path = this.notebookPath();
171 var path = this.notebookPath();
171 var nbname = name.split(".")[0];
172 var nbname = name.split(".")[0];
172 var item = this.new_notebook_item(i);
173 var item = this.new_notebook_item(i);
173 this.add_link(path, nbname, item);
174 this.add_link(path, nbname, item);
174 name = this.notebookPath() + name;
175 name = utils.url_path_join(this.notebookPath(), name);
175 if(this.sessions[name] === undefined){
176 if(this.sessions[name] === undefined){
176 this.add_delete_button(item);
177 this.add_delete_button(item);
177 } else {
178 } else {
178 this.add_shutdown_button(item,this.sessions[name]);
179 this.add_shutdown_button(item,this.sessions[name]);
179 }
180 }
180 }
181 }
181 };
182 };
182
183
183
184
184 NotebookList.prototype.new_notebook_item = function (index) {
185 NotebookList.prototype.new_notebook_item = function (index) {
185 var item = $('<div/>').addClass("list_item").addClass("row-fluid");
186 var item = $('<div/>').addClass("list_item").addClass("row-fluid");
186 // item.addClass('list_item ui-widget ui-widget-content ui-helper-clearfix');
187 // item.addClass('list_item ui-widget ui-widget-content ui-helper-clearfix');
187 // item.css('border-top-style','none');
188 // item.css('border-top-style','none');
188 item.append($("<div/>").addClass("span12").append(
189 item.append($("<div/>").addClass("span12").append(
189 $("<a/>").addClass("item_link").append(
190 $("<a/>").addClass("item_link").append(
190 $("<span/>").addClass("item_name")
191 $("<span/>").addClass("item_name")
191 )
192 )
192 ).append(
193 ).append(
193 $('<div/>').addClass("item_buttons btn-group pull-right")
194 $('<div/>').addClass("item_buttons btn-group pull-right")
194 ));
195 ));
195
196
196 if (index === -1) {
197 if (index === -1) {
197 this.element.append(item);
198 this.element.append(item);
198 } else {
199 } else {
199 this.element.children().eq(index).after(item);
200 this.element.children().eq(index).after(item);
200 }
201 }
201 return item;
202 return item;
202 };
203 };
203
204
204
205
205 NotebookList.prototype.add_link = function (path, nbname, item) {
206 NotebookList.prototype.add_link = function (path, nbname, item) {
206 item.data('nbname', nbname);
207 item.data('nbname', nbname);
207 item.data('path', path);
208 item.data('path', path);
208 item.find(".item_name").text(nbname);
209 item.find(".item_name").text(nbname);
209 item.find("a.item_link")
210 item.find("a.item_link")
210 .attr('href',
211 .attr('href',
211 utils.url_path_join(
212 utils.url_path_join(
212 this.baseProjectUrl(),
213 this.baseProjectUrl(),
213 "notebooks",
214 "notebooks",
214 this.notebookPath(),
215 this.notebookPath(),
215 nbname + ".ipynb"
216 nbname + ".ipynb"
216 )
217 )
217 ).attr('target','_blank');
218 ).attr('target','_blank');
218 };
219 };
219
220
220
221
221 NotebookList.prototype.add_name_input = function (nbname, item) {
222 NotebookList.prototype.add_name_input = function (nbname, item) {
222 item.data('nbname', nbname);
223 item.data('nbname', nbname);
223 item.find(".item_name").empty().append(
224 item.find(".item_name").empty().append(
224 $('<input/>')
225 $('<input/>')
225 .addClass("nbname_input")
226 .addClass("nbname_input")
226 .attr('value', nbname)
227 .attr('value', nbname)
227 .attr('size', '30')
228 .attr('size', '30')
228 .attr('type', 'text')
229 .attr('type', 'text')
229 );
230 );
230 };
231 };
231
232
232
233
233 NotebookList.prototype.add_notebook_data = function (data, item) {
234 NotebookList.prototype.add_notebook_data = function (data, item) {
234 item.data('nbdata',data);
235 item.data('nbdata',data);
235 };
236 };
236
237
237
238
238 NotebookList.prototype.add_shutdown_button = function (item, session) {
239 NotebookList.prototype.add_shutdown_button = function (item, session) {
239 var that = this;
240 var that = this;
240 var shutdown_button = $("<button/>").text("Shutdown").addClass("btn btn-mini").
241 var shutdown_button = $("<button/>").text("Shutdown").addClass("btn btn-mini").
241 click(function (e) {
242 click(function (e) {
242 var settings = {
243 var settings = {
243 processData : false,
244 processData : false,
244 cache : false,
245 cache : false,
245 type : "DELETE",
246 type : "DELETE",
246 dataType : "json",
247 dataType : "json",
247 success : function () {
248 success : function () {
248 that.load_sessions();
249 that.load_sessions();
249 }
250 }
250 };
251 };
251 var url = utils.url_path_join(
252 var url = utils.url_path_join(
252 that.baseProjectUrl(),
253 that.baseProjectUrl(),
253 'api/sessions',
254 'api/sessions',
254 session
255 session
255 );
256 );
256 $.ajax(url, settings);
257 $.ajax(url, settings);
257 return false;
258 return false;
258 });
259 });
259 // var new_buttons = item.find('a'); // shutdown_button;
260 // var new_buttons = item.find('a'); // shutdown_button;
260 item.find(".item_buttons").html("").append(shutdown_button);
261 item.find(".item_buttons").html("").append(shutdown_button);
261 };
262 };
262
263
263 NotebookList.prototype.add_delete_button = function (item) {
264 NotebookList.prototype.add_delete_button = function (item) {
264 var new_buttons = $('<span/>').addClass("btn-group pull-right");
265 var new_buttons = $('<span/>').addClass("btn-group pull-right");
265 var notebooklist = this;
266 var notebooklist = this;
266 var delete_button = $("<button/>").text("Delete").addClass("btn btn-mini").
267 var delete_button = $("<button/>").text("Delete").addClass("btn btn-mini").
267 click(function (e) {
268 click(function (e) {
268 // $(this) is the button that was clicked.
269 // $(this) is the button that was clicked.
269 var that = $(this);
270 var that = $(this);
270 // We use the nbname and notebook_id from the parent notebook_item element's
271 // We use the nbname and notebook_id from the parent notebook_item element's
271 // data because the outer scopes values change as we iterate through the loop.
272 // data because the outer scopes values change as we iterate through the loop.
272 var parent_item = that.parents('div.list_item');
273 var parent_item = that.parents('div.list_item');
273 var nbname = parent_item.data('nbname');
274 var nbname = parent_item.data('nbname');
274 var message = 'Are you sure you want to permanently delete the notebook: ' + nbname + '?';
275 var message = 'Are you sure you want to permanently delete the notebook: ' + nbname + '?';
275 IPython.dialog.modal({
276 IPython.dialog.modal({
276 title : "Delete notebook",
277 title : "Delete notebook",
277 body : message,
278 body : message,
278 buttons : {
279 buttons : {
279 Delete : {
280 Delete : {
280 class: "btn-danger",
281 class: "btn-danger",
281 click: function() {
282 click: function() {
282 var settings = {
283 var settings = {
283 processData : false,
284 processData : false,
284 cache : false,
285 cache : false,
285 type : "DELETE",
286 type : "DELETE",
286 dataType : "json",
287 dataType : "json",
287 success : function (data, status, xhr) {
288 success : function (data, status, xhr) {
288 parent_item.remove();
289 parent_item.remove();
289 }
290 }
290 };
291 };
291 var url = utils.url_path_join(
292 var url = utils.url_path_join(
292 notebooklist.baseProjectUrl(),
293 notebooklist.baseProjectUrl(),
293 'api/notebooks',
294 'api/notebooks',
294 notebooklist.notebookPath(),
295 notebooklist.notebookPath(),
295 nbname + '.ipynb'
296 nbname + '.ipynb'
296 );
297 );
297 $.ajax(url, settings);
298 $.ajax(url, settings);
298 }
299 }
299 },
300 },
300 Cancel : {}
301 Cancel : {}
301 }
302 }
302 });
303 });
303 return false;
304 return false;
304 });
305 });
305 item.find(".item_buttons").html("").append(delete_button);
306 item.find(".item_buttons").html("").append(delete_button);
306 };
307 };
307
308
308
309
309 NotebookList.prototype.add_upload_button = function (item) {
310 NotebookList.prototype.add_upload_button = function (item) {
310 var that = this;
311 var that = this;
311 var upload_button = $('<button/>').text("Upload")
312 var upload_button = $('<button/>').text("Upload")
312 .addClass('btn btn-primary btn-mini upload_button')
313 .addClass('btn btn-primary btn-mini upload_button')
313 .click(function (e) {
314 .click(function (e) {
314 var nbname = item.find('.item_name > input').attr('value');
315 var nbname = item.find('.item_name > input').attr('value');
315 var nbformat = item.data('nbformat');
316 var nbformat = item.data('nbformat');
316 var nbdata = item.data('nbdata');
317 var nbdata = item.data('nbdata');
317 var content_type = 'text/plain';
318 var content_type = 'text/plain';
318 if (nbformat === 'json') {
319 if (nbformat === 'json') {
319 content_type = 'application/json';
320 content_type = 'application/json';
320 } else if (nbformat === 'py') {
321 } else if (nbformat === 'py') {
321 content_type = 'application/x-python';
322 content_type = 'application/x-python';
322 }
323 }
323 var settings = {
324 var settings = {
324 processData : false,
325 processData : false,
325 cache : false,
326 cache : false,
326 type : 'POST',
327 type : 'POST',
327 dataType : 'json',
328 dataType : 'json',
328 data : nbdata,
329 data : nbdata,
329 headers : {'Content-Type': content_type},
330 headers : {'Content-Type': content_type},
330 success : function (data, status, xhr) {
331 success : function (data, status, xhr) {
331 that.add_link(data, nbname, item);
332 that.add_link(data, nbname, item);
332 that.add_delete_button(item);
333 that.add_delete_button(item);
333 }
334 }
334 };
335 };
335
336
336 var qs = $.param({name:nbname, format:nbformat});
337 var qs = $.param({name:nbname, format:nbformat});
337 var url = utils.url_path_join(
338 var url = utils.url_path_join(
338 that.baseProjectUrl(),
339 that.baseProjectUrl(),
339 'notebooks?' + qs
340 'notebooks?' + qs
340 );
341 );
341 $.ajax(url, settings);
342 $.ajax(url, settings);
342 return false;
343 return false;
343 });
344 });
344 var cancel_button = $('<button/>').text("Cancel")
345 var cancel_button = $('<button/>').text("Cancel")
345 .addClass("btn btn-mini")
346 .addClass("btn btn-mini")
346 .click(function (e) {
347 .click(function (e) {
347 console.log('cancel click');
348 console.log('cancel click');
348 item.remove();
349 item.remove();
349 return false;
350 return false;
350 });
351 });
351 item.find(".item_buttons").empty()
352 item.find(".item_buttons").empty()
352 .append(upload_button)
353 .append(upload_button)
353 .append(cancel_button);
354 .append(cancel_button);
354 };
355 };
355
356
356
357
357 NotebookList.prototype.new_notebook = function(){
358 NotebookList.prototype.new_notebook = function(){
358 var path = this.notebookPath();
359 var path = this.notebookPath();
359 var settings = {
360 var settings = {
360 processData : false,
361 processData : false,
361 cache : false,
362 cache : false,
362 type : "POST",
363 type : "POST",
363 dataType : "json",
364 dataType : "json",
364 success:$.proxy(function (data, status, xhr){
365 success:$.proxy(function (data, status, xhr){
365 var notebook_name = data.name;
366 var notebook_name = data.name;
366 window.open(
367 window.open(
367 utils.url_path_join(
368 utils.url_path_join(
368 this.baseProjectUrl(),
369 this.baseProjectUrl(),
369 'notebooks',
370 'notebooks',
370 this.notebookPath(),
371 path,
371 notebook_name),
372 notebook_name),
372 '_blank'
373 '_blank'
373 );
374 );
374 }, this)
375 }, this)
375 };
376 };
376 var url = utils.url_path_join(
377 var url = utils.url_path_join(
377 this.baseProjectUrl(),
378 this.baseProjectUrl(),
378 'api/notebooks',
379 'api/notebooks',
379 path
380 path
380 );
381 );
381 $.ajax(url, settings);
382 $.ajax(url, settings);
382 };
383 };
383
384
384 IPython.NotebookList = NotebookList;
385 IPython.NotebookList = NotebookList;
385
386
386 return IPython;
387 return IPython;
387
388
388 }(IPython));
389 }(IPython));
General Comments 0
You need to be logged in to leave comments. Login now