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