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