##// END OF EJS Templates
use fixed-width icons, closes #5555
Paul Ivanov -
Show More
@@ -1,412 +1,412 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, options, element_name) {
17 var NotebookList = function (selector, options, element_name) {
18 var that = this
18 var that = this
19 // allow code re-use by just changing element_name in kernellist.js
19 // allow code re-use by just changing element_name in kernellist.js
20 this.element_name = element_name || 'notebook';
20 this.element_name = element_name || 'notebook';
21 this.selector = selector;
21 this.selector = selector;
22 if (this.selector !== undefined) {
22 if (this.selector !== undefined) {
23 this.element = $(selector);
23 this.element = $(selector);
24 this.style();
24 this.style();
25 this.bind_events();
25 this.bind_events();
26 }
26 }
27 this.notebooks_list = [];
27 this.notebooks_list = [];
28 this.sessions = {};
28 this.sessions = {};
29 this.base_url = options.base_url || utils.get_body_data("baseUrl");
29 this.base_url = options.base_url || utils.get_body_data("baseUrl");
30 this.notebook_path = options.notebook_path || utils.get_body_data("notebookPath");
30 this.notebook_path = options.notebook_path || utils.get_body_data("notebookPath");
31 $([IPython.events]).on('sessions_loaded.Dashboard',
31 $([IPython.events]).on('sessions_loaded.Dashboard',
32 function(e, d) { that.sessions_loaded(d); });
32 function(e, d) { that.sessions_loaded(d); });
33 };
33 };
34
34
35 NotebookList.prototype.style = function () {
35 NotebookList.prototype.style = function () {
36 var prefix = '#' + this.element_name
36 var prefix = '#' + this.element_name
37 $(prefix + '_toolbar').addClass('list_toolbar');
37 $(prefix + '_toolbar').addClass('list_toolbar');
38 $(prefix + '_list_info').addClass('toolbar_info');
38 $(prefix + '_list_info').addClass('toolbar_info');
39 $(prefix + '_buttons').addClass('toolbar_buttons');
39 $(prefix + '_buttons').addClass('toolbar_buttons');
40 $(prefix + '_list_header').addClass('list_header');
40 $(prefix + '_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_' + this.element_name + '_list').click(function () {
47 $('#refresh_' + this.element_name + '_list').click(function () {
48 that.load_sessions();
48 that.load_sessions();
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.handleFilesUpload(event,'drop');
54 that.handleFilesUpload(event,'drop');
55 return false;
55 return false;
56 });
56 });
57 };
57 };
58
58
59 NotebookList.prototype.handleFilesUpload = function(event, dropOrForm) {
59 NotebookList.prototype.handleFilesUpload = 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 name_and_ext = utils.splitext(f.name);
72 var name_and_ext = utils.splitext(f.name);
73 var file_ext = name_and_ext[1];
73 var file_ext = name_and_ext[1];
74 if (file_ext === '.ipynb') {
74 if (file_ext === '.ipynb') {
75 var item = that.new_notebook_item(0);
75 var item = that.new_notebook_item(0);
76 that.add_name_input(f.name, item);
76 that.add_name_input(f.name, item);
77 // Store the notebook item in the reader so we can use it later
77 // Store the notebook item in the reader so we can use it later
78 // to know which item it belongs to.
78 // to know which item it belongs to.
79 $(reader).data('item', item);
79 $(reader).data('item', item);
80 reader.onload = function (event) {
80 reader.onload = function (event) {
81 var nbitem = $(event.target).data('item');
81 var nbitem = $(event.target).data('item');
82 that.add_notebook_data(event.target.result, nbitem);
82 that.add_notebook_data(event.target.result, nbitem);
83 that.add_upload_button(nbitem);
83 that.add_upload_button(nbitem);
84 };
84 };
85 } else {
85 } else {
86 var dialog = 'Uploaded notebooks must be .ipynb files';
86 var dialog = 'Uploaded notebooks must be .ipynb files';
87 IPython.dialog.modal({
87 IPython.dialog.modal({
88 title : 'Invalid file type',
88 title : 'Invalid file type',
89 body : dialog,
89 body : dialog,
90 buttons : {'OK' : {'class' : 'btn-primary'}}
90 buttons : {'OK' : {'class' : 'btn-primary'}}
91 });
91 });
92 }
92 }
93 }
93 }
94 // Replace the file input form wth a clone of itself. This is required to
94 // Replace the file input form wth a clone of itself. This is required to
95 // reset the form. Otherwise, if you upload a file, delete it and try to
95 // reset the form. Otherwise, if you upload a file, delete it and try to
96 // upload it again, the changed event won't fire.
96 // upload it again, the changed event won't fire.
97 var form = $('input.fileinput');
97 var form = $('input.fileinput');
98 form.replaceWith(form.clone(true));
98 form.replaceWith(form.clone(true));
99 return false;
99 return false;
100 };
100 };
101
101
102 NotebookList.prototype.clear_list = function () {
102 NotebookList.prototype.clear_list = function () {
103 this.element.children('.list_item').remove();
103 this.element.children('.list_item').remove();
104 };
104 };
105
105
106 NotebookList.prototype.load_sessions = function(){
106 NotebookList.prototype.load_sessions = function(){
107 IPython.session_list.load_sessions();
107 IPython.session_list.load_sessions();
108 };
108 };
109
109
110
110
111 NotebookList.prototype.sessions_loaded = function(data){
111 NotebookList.prototype.sessions_loaded = function(data){
112 this.sessions = data;
112 this.sessions = data;
113 this.load_list();
113 this.load_list();
114 };
114 };
115
115
116 NotebookList.prototype.load_list = function () {
116 NotebookList.prototype.load_list = function () {
117 var that = this;
117 var that = this;
118 var settings = {
118 var settings = {
119 processData : false,
119 processData : false,
120 cache : false,
120 cache : false,
121 type : "GET",
121 type : "GET",
122 dataType : "json",
122 dataType : "json",
123 success : $.proxy(this.list_loaded, this),
123 success : $.proxy(this.list_loaded, this),
124 error : $.proxy( function(){
124 error : $.proxy( function(){
125 that.list_loaded([], null, null, {msg:"Error connecting to server."});
125 that.list_loaded([], null, null, {msg:"Error connecting to server."});
126 },this)
126 },this)
127 };
127 };
128
128
129 var url = utils.url_join_encode(
129 var url = utils.url_join_encode(
130 this.base_url,
130 this.base_url,
131 'api',
131 'api',
132 'notebooks',
132 'notebooks',
133 this.notebook_path
133 this.notebook_path
134 );
134 );
135 $.ajax(url, settings);
135 $.ajax(url, settings);
136 };
136 };
137
137
138
138
139 NotebookList.prototype.list_loaded = function (data, status, xhr, param) {
139 NotebookList.prototype.list_loaded = function (data, status, xhr, param) {
140 var message = 'Notebook list empty.';
140 var message = 'Notebook list empty.';
141 if (param !== undefined && param.msg) {
141 if (param !== undefined && param.msg) {
142 message = param.msg;
142 message = param.msg;
143 }
143 }
144 var item = null;
144 var item = null;
145 var len = data.length;
145 var len = data.length;
146 this.clear_list();
146 this.clear_list();
147 if (len === 0) {
147 if (len === 0) {
148 item = this.new_notebook_item(0);
148 item = this.new_notebook_item(0);
149 var span12 = item.children().first();
149 var span12 = item.children().first();
150 span12.empty();
150 span12.empty();
151 span12.append($('<div style="margin:auto;text-align:center;color:grey"/>').text(message));
151 span12.append($('<div style="margin:auto;text-align:center;color:grey"/>').text(message));
152 }
152 }
153 var path = this.notebook_path;
153 var path = this.notebook_path;
154 var offset = 0;
154 var offset = 0;
155 if (path !== '') {
155 if (path !== '') {
156 item = this.new_notebook_item(0);
156 item = this.new_notebook_item(0);
157 this.add_dir(path, '..', item);
157 this.add_dir(path, '..', item);
158 offset = 1;
158 offset = 1;
159 }
159 }
160 for (var i=0; i<len; i++) {
160 for (var i=0; i<len; i++) {
161 if (data[i].type === 'directory') {
161 if (data[i].type === 'directory') {
162 var name = data[i].name;
162 var name = data[i].name;
163 item = this.new_notebook_item(i+offset);
163 item = this.new_notebook_item(i+offset);
164 this.add_dir(path, name, item);
164 this.add_dir(path, name, item);
165 } else {
165 } else {
166 var name = data[i].name;
166 var name = data[i].name;
167 item = this.new_notebook_item(i+offset);
167 item = this.new_notebook_item(i+offset);
168 this.add_link(path, name, item);
168 this.add_link(path, name, item);
169 name = utils.url_path_join(path, name);
169 name = utils.url_path_join(path, name);
170 if(this.sessions[name] === undefined){
170 if(this.sessions[name] === undefined){
171 this.add_delete_button(item);
171 this.add_delete_button(item);
172 } else {
172 } else {
173 this.add_shutdown_button(item,this.sessions[name]);
173 this.add_shutdown_button(item,this.sessions[name]);
174 }
174 }
175 }
175 }
176 }
176 }
177 };
177 };
178
178
179
179
180 NotebookList.prototype.new_notebook_item = function (index) {
180 NotebookList.prototype.new_notebook_item = function (index) {
181 var item = $('<div/>').addClass("list_item").addClass("row-fluid");
181 var item = $('<div/>').addClass("list_item").addClass("row-fluid");
182 // item.addClass('list_item ui-widget ui-widget-content ui-helper-clearfix');
182 // item.addClass('list_item ui-widget ui-widget-content ui-helper-clearfix');
183 // item.css('border-top-style','none');
183 // item.css('border-top-style','none');
184 item.append($("<div/>").addClass("span12").append(
184 item.append($("<div/>").addClass("span12").append(
185 $('<i/>').addClass('item_icon')
185 $('<i/>').addClass('item_icon')
186 ).append(
186 ).append(
187 $("<a/>").addClass("item_link").append(
187 $("<a/>").addClass("item_link").append(
188 $("<span/>").addClass("item_name")
188 $("<span/>").addClass("item_name")
189 )
189 )
190 ).append(
190 ).append(
191 $('<div/>').addClass("item_buttons btn-group pull-right")
191 $('<div/>').addClass("item_buttons btn-group pull-right")
192 ));
192 ));
193
193
194 if (index === -1) {
194 if (index === -1) {
195 this.element.append(item);
195 this.element.append(item);
196 } else {
196 } else {
197 this.element.children().eq(index).after(item);
197 this.element.children().eq(index).after(item);
198 }
198 }
199 return item;
199 return item;
200 };
200 };
201
201
202
202
203 NotebookList.prototype.add_dir = function (path, name, item) {
203 NotebookList.prototype.add_dir = function (path, name, item) {
204 item.data('name', name);
204 item.data('name', name);
205 item.data('path', path);
205 item.data('path', path);
206 item.find(".item_name").text(name);
206 item.find(".item_name").text(name);
207 item.find(".item_icon").addClass('folder_icon');
207 item.find(".item_icon").addClass('folder_icon').addClass('icon-fixed-width');
208 item.find("a.item_link")
208 item.find("a.item_link")
209 .attr('href',
209 .attr('href',
210 utils.url_join_encode(
210 utils.url_join_encode(
211 this.base_url,
211 this.base_url,
212 "tree",
212 "tree",
213 path,
213 path,
214 name
214 name
215 )
215 )
216 );
216 );
217 };
217 };
218
218
219
219
220 NotebookList.prototype.add_link = function (path, nbname, item) {
220 NotebookList.prototype.add_link = function (path, nbname, item) {
221 item.data('nbname', nbname);
221 item.data('nbname', nbname);
222 item.data('path', path);
222 item.data('path', path);
223 item.find(".item_name").text(nbname);
223 item.find(".item_name").text(nbname);
224 item.find(".item_icon").addClass('notebook_icon');
224 item.find(".item_icon").addClass('notebook_icon').addClass('icon-fixed-width');
225 item.find("a.item_link")
225 item.find("a.item_link")
226 .attr('href',
226 .attr('href',
227 utils.url_join_encode(
227 utils.url_join_encode(
228 this.base_url,
228 this.base_url,
229 "notebooks",
229 "notebooks",
230 path,
230 path,
231 nbname
231 nbname
232 )
232 )
233 ).attr('target','_blank');
233 ).attr('target','_blank');
234 };
234 };
235
235
236
236
237 NotebookList.prototype.add_name_input = function (nbname, item) {
237 NotebookList.prototype.add_name_input = function (nbname, item) {
238 item.data('nbname', nbname);
238 item.data('nbname', nbname);
239 item.find(".item_icon").addClass('notebook_icon');
239 item.find(".item_icon").addClass('notebook_icon').addClass('icon-fixed-width');
240 item.find(".item_name").empty().append(
240 item.find(".item_name").empty().append(
241 $('<input/>')
241 $('<input/>')
242 .addClass("nbname_input")
242 .addClass("nbname_input")
243 .attr('value', utils.splitext(nbname)[0])
243 .attr('value', utils.splitext(nbname)[0])
244 .attr('size', '30')
244 .attr('size', '30')
245 .attr('type', 'text')
245 .attr('type', 'text')
246 );
246 );
247 };
247 };
248
248
249
249
250 NotebookList.prototype.add_notebook_data = function (data, item) {
250 NotebookList.prototype.add_notebook_data = function (data, item) {
251 item.data('nbdata', data);
251 item.data('nbdata', data);
252 };
252 };
253
253
254
254
255 NotebookList.prototype.add_shutdown_button = function (item, session) {
255 NotebookList.prototype.add_shutdown_button = function (item, session) {
256 var that = this;
256 var that = this;
257 var shutdown_button = $("<button/>").text("Shutdown").addClass("btn btn-mini btn-danger").
257 var shutdown_button = $("<button/>").text("Shutdown").addClass("btn btn-mini btn-danger").
258 click(function (e) {
258 click(function (e) {
259 var settings = {
259 var settings = {
260 processData : false,
260 processData : false,
261 cache : false,
261 cache : false,
262 type : "DELETE",
262 type : "DELETE",
263 dataType : "json",
263 dataType : "json",
264 success : function () {
264 success : function () {
265 that.load_sessions();
265 that.load_sessions();
266 }
266 }
267 };
267 };
268 var url = utils.url_join_encode(
268 var url = utils.url_join_encode(
269 that.base_url,
269 that.base_url,
270 'api/sessions',
270 'api/sessions',
271 session
271 session
272 );
272 );
273 $.ajax(url, settings);
273 $.ajax(url, settings);
274 return false;
274 return false;
275 });
275 });
276 // var new_buttons = item.find('a'); // shutdown_button;
276 // var new_buttons = item.find('a'); // shutdown_button;
277 item.find(".item_buttons").text("").append(shutdown_button);
277 item.find(".item_buttons").text("").append(shutdown_button);
278 };
278 };
279
279
280 NotebookList.prototype.add_delete_button = function (item) {
280 NotebookList.prototype.add_delete_button = function (item) {
281 var new_buttons = $('<span/>').addClass("btn-group pull-right");
281 var new_buttons = $('<span/>').addClass("btn-group pull-right");
282 var notebooklist = this;
282 var notebooklist = this;
283 var delete_button = $("<button/>").text("Delete").addClass("btn btn-mini").
283 var delete_button = $("<button/>").text("Delete").addClass("btn btn-mini").
284 click(function (e) {
284 click(function (e) {
285 // $(this) is the button that was clicked.
285 // $(this) is the button that was clicked.
286 var that = $(this);
286 var that = $(this);
287 // We use the nbname and notebook_id from the parent notebook_item element's
287 // We use the nbname and notebook_id from the parent notebook_item element's
288 // data because the outer scopes values change as we iterate through the loop.
288 // data because the outer scopes values change as we iterate through the loop.
289 var parent_item = that.parents('div.list_item');
289 var parent_item = that.parents('div.list_item');
290 var nbname = parent_item.data('nbname');
290 var nbname = parent_item.data('nbname');
291 var message = 'Are you sure you want to permanently delete the notebook: ' + nbname + '?';
291 var message = 'Are you sure you want to permanently delete the notebook: ' + nbname + '?';
292 IPython.dialog.modal({
292 IPython.dialog.modal({
293 title : "Delete notebook",
293 title : "Delete notebook",
294 body : message,
294 body : message,
295 buttons : {
295 buttons : {
296 Delete : {
296 Delete : {
297 class: "btn-danger",
297 class: "btn-danger",
298 click: function() {
298 click: function() {
299 var settings = {
299 var settings = {
300 processData : false,
300 processData : false,
301 cache : false,
301 cache : false,
302 type : "DELETE",
302 type : "DELETE",
303 dataType : "json",
303 dataType : "json",
304 success : function (data, status, xhr) {
304 success : function (data, status, xhr) {
305 parent_item.remove();
305 parent_item.remove();
306 }
306 }
307 };
307 };
308 var url = utils.url_join_encode(
308 var url = utils.url_join_encode(
309 notebooklist.base_url,
309 notebooklist.base_url,
310 'api/notebooks',
310 'api/notebooks',
311 notebooklist.notebook_path,
311 notebooklist.notebook_path,
312 nbname
312 nbname
313 );
313 );
314 $.ajax(url, settings);
314 $.ajax(url, settings);
315 }
315 }
316 },
316 },
317 Cancel : {}
317 Cancel : {}
318 }
318 }
319 });
319 });
320 return false;
320 return false;
321 });
321 });
322 item.find(".item_buttons").text("").append(delete_button);
322 item.find(".item_buttons").text("").append(delete_button);
323 };
323 };
324
324
325
325
326 NotebookList.prototype.add_upload_button = function (item) {
326 NotebookList.prototype.add_upload_button = function (item) {
327 var that = this;
327 var that = this;
328 var upload_button = $('<button/>').text("Upload")
328 var upload_button = $('<button/>').text("Upload")
329 .addClass('btn btn-primary btn-mini upload_button')
329 .addClass('btn btn-primary btn-mini upload_button')
330 .click(function (e) {
330 .click(function (e) {
331 var nbname = item.find('.item_name > input').val();
331 var nbname = item.find('.item_name > input').val();
332 if (nbname.slice(nbname.length-6, nbname.length) != ".ipynb") {
332 if (nbname.slice(nbname.length-6, nbname.length) != ".ipynb") {
333 nbname = nbname + ".ipynb";
333 nbname = nbname + ".ipynb";
334 }
334 }
335 var path = that.notebook_path;
335 var path = that.notebook_path;
336 var nbdata = item.data('nbdata');
336 var nbdata = item.data('nbdata');
337 var content_type = 'application/json';
337 var content_type = 'application/json';
338 var model = {
338 var model = {
339 content : JSON.parse(nbdata),
339 content : JSON.parse(nbdata),
340 };
340 };
341 var settings = {
341 var settings = {
342 processData : false,
342 processData : false,
343 cache : false,
343 cache : false,
344 type : 'PUT',
344 type : 'PUT',
345 dataType : 'json',
345 dataType : 'json',
346 data : JSON.stringify(model),
346 data : JSON.stringify(model),
347 headers : {'Content-Type': content_type},
347 headers : {'Content-Type': content_type},
348 success : function (data, status, xhr) {
348 success : function (data, status, xhr) {
349 that.add_link(path, nbname, item);
349 that.add_link(path, nbname, item);
350 that.add_delete_button(item);
350 that.add_delete_button(item);
351 },
351 },
352 error : function (data, status, xhr) {
352 error : function (data, status, xhr) {
353 console.log(data, status);
353 console.log(data, status);
354 }
354 }
355 };
355 };
356
356
357 var url = utils.url_join_encode(
357 var url = utils.url_join_encode(
358 that.base_url,
358 that.base_url,
359 'api/notebooks',
359 'api/notebooks',
360 that.notebook_path,
360 that.notebook_path,
361 nbname
361 nbname
362 );
362 );
363 $.ajax(url, settings);
363 $.ajax(url, settings);
364 return false;
364 return false;
365 });
365 });
366 var cancel_button = $('<button/>').text("Cancel")
366 var cancel_button = $('<button/>').text("Cancel")
367 .addClass("btn btn-mini")
367 .addClass("btn btn-mini")
368 .click(function (e) {
368 .click(function (e) {
369 console.log('cancel click');
369 console.log('cancel click');
370 item.remove();
370 item.remove();
371 return false;
371 return false;
372 });
372 });
373 item.find(".item_buttons").empty()
373 item.find(".item_buttons").empty()
374 .append(upload_button)
374 .append(upload_button)
375 .append(cancel_button);
375 .append(cancel_button);
376 };
376 };
377
377
378
378
379 NotebookList.prototype.new_notebook = function(){
379 NotebookList.prototype.new_notebook = function(){
380 var path = this.notebook_path;
380 var path = this.notebook_path;
381 var base_url = this.base_url;
381 var base_url = this.base_url;
382 var settings = {
382 var settings = {
383 processData : false,
383 processData : false,
384 cache : false,
384 cache : false,
385 type : "POST",
385 type : "POST",
386 dataType : "json",
386 dataType : "json",
387 async : false,
387 async : false,
388 success : function (data, status, xhr) {
388 success : function (data, status, xhr) {
389 var notebook_name = data.name;
389 var notebook_name = data.name;
390 window.open(
390 window.open(
391 utils.url_join_encode(
391 utils.url_join_encode(
392 base_url,
392 base_url,
393 'notebooks',
393 'notebooks',
394 path,
394 path,
395 notebook_name),
395 notebook_name),
396 '_blank'
396 '_blank'
397 );
397 );
398 }
398 }
399 };
399 };
400 var url = utils.url_join_encode(
400 var url = utils.url_join_encode(
401 base_url,
401 base_url,
402 'api/notebooks',
402 'api/notebooks',
403 path
403 path
404 );
404 );
405 $.ajax(url, settings);
405 $.ajax(url, settings);
406 };
406 };
407
407
408 IPython.NotebookList = NotebookList;
408 IPython.NotebookList = NotebookList;
409
409
410 return IPython;
410 return IPython;
411
411
412 }(IPython));
412 }(IPython));
General Comments 0
You need to be logged in to leave comments. Login now