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