Show More
@@ -1,462 +1,481 b'' | |||||
1 | // Copyright (c) IPython Development Team. |
|
1 | // Copyright (c) IPython Development Team. | |
2 | // Distributed under the terms of the Modified BSD License. |
|
2 | // Distributed under the terms of the Modified BSD License. | |
3 |
|
3 | |||
4 | define([ |
|
4 | define([ | |
5 | 'base/js/namespace', |
|
5 | 'base/js/namespace', | |
6 | 'jquery', |
|
6 | 'jquery', | |
7 | 'base/js/utils', |
|
7 | 'base/js/utils', | |
8 | 'base/js/dialog', |
|
8 | 'base/js/dialog', | |
9 | ], function(IPython, $, utils, dialog) { |
|
9 | ], function(IPython, $, utils, dialog) { | |
10 | "use strict"; |
|
10 | "use strict"; | |
11 |
|
11 | |||
12 | var NotebookList = function (selector, options) { |
|
12 | var NotebookList = function (selector, options) { | |
13 | // Constructor |
|
13 | // Constructor | |
14 | // |
|
14 | // | |
15 | // Parameters: |
|
15 | // Parameters: | |
16 | // selector: string |
|
16 | // selector: string | |
17 | // options: dictionary |
|
17 | // options: dictionary | |
18 | // Dictionary of keyword arguments. |
|
18 | // Dictionary of keyword arguments. | |
19 | // session_list: SessionList instance |
|
19 | // session_list: SessionList instance | |
20 | // element_name: string |
|
20 | // element_name: string | |
21 | // base_url: string |
|
21 | // base_url: string | |
22 | // notebook_path: string |
|
22 | // notebook_path: string | |
23 | var that = this; |
|
23 | var that = this; | |
24 | this.session_list = options.session_list; |
|
24 | this.session_list = options.session_list; | |
25 | // allow code re-use by just changing element_name in kernellist.js |
|
25 | // allow code re-use by just changing element_name in kernellist.js | |
26 | this.element_name = options.element_name || 'notebook'; |
|
26 | this.element_name = options.element_name || 'notebook'; | |
27 | this.selector = selector; |
|
27 | this.selector = selector; | |
28 | if (this.selector !== undefined) { |
|
28 | if (this.selector !== undefined) { | |
29 | this.element = $(selector); |
|
29 | this.element = $(selector); | |
30 | this.style(); |
|
30 | this.style(); | |
31 | this.bind_events(); |
|
31 | this.bind_events(); | |
32 | } |
|
32 | } | |
33 | this.notebooks_list = []; |
|
33 | this.notebooks_list = []; | |
34 | this.sessions = {}; |
|
34 | this.sessions = {}; | |
35 | this.base_url = options.base_url || utils.get_body_data("baseUrl"); |
|
35 | this.base_url = options.base_url || utils.get_body_data("baseUrl"); | |
36 | this.notebook_path = options.notebook_path || utils.get_body_data("notebookPath"); |
|
36 | this.notebook_path = options.notebook_path || utils.get_body_data("notebookPath"); | |
37 | if (this.session_list && this.session_list.events) { |
|
37 | if (this.session_list && this.session_list.events) { | |
38 | this.session_list.events.on('sessions_loaded.Dashboard', |
|
38 | this.session_list.events.on('sessions_loaded.Dashboard', | |
39 | function(e, d) { that.sessions_loaded(d); }); |
|
39 | function(e, d) { that.sessions_loaded(d); }); | |
40 | } |
|
40 | } | |
41 | }; |
|
41 | }; | |
42 |
|
42 | |||
43 | NotebookList.prototype.style = function () { |
|
43 | NotebookList.prototype.style = function () { | |
44 | var prefix = '#' + this.element_name; |
|
44 | var prefix = '#' + this.element_name; | |
45 | $(prefix + '_toolbar').addClass('list_toolbar'); |
|
45 | $(prefix + '_toolbar').addClass('list_toolbar'); | |
46 | $(prefix + '_list_info').addClass('toolbar_info'); |
|
46 | $(prefix + '_list_info').addClass('toolbar_info'); | |
47 | $(prefix + '_buttons').addClass('toolbar_buttons'); |
|
47 | $(prefix + '_buttons').addClass('toolbar_buttons'); | |
48 | $(prefix + '_list_header').addClass('list_header'); |
|
48 | $(prefix + '_list_header').addClass('list_header'); | |
49 | this.element.addClass("list_container"); |
|
49 | this.element.addClass("list_container"); | |
50 | }; |
|
50 | }; | |
51 |
|
51 | |||
52 |
|
52 | |||
53 | NotebookList.prototype.bind_events = function () { |
|
53 | NotebookList.prototype.bind_events = function () { | |
54 | var that = this; |
|
54 | var that = this; | |
55 | $('#refresh_' + this.element_name + '_list').click(function () { |
|
55 | $('#refresh_' + this.element_name + '_list').click(function () { | |
56 | that.load_sessions(); |
|
56 | that.load_sessions(); | |
57 | }); |
|
57 | }); | |
58 | this.element.bind('dragover', function () { |
|
58 | this.element.bind('dragover', function () { | |
59 | return false; |
|
59 | return false; | |
60 | }); |
|
60 | }); | |
61 | this.element.bind('drop', function(event){ |
|
61 | this.element.bind('drop', function(event){ | |
62 | that.handleFilesUpload(event,'drop'); |
|
62 | that.handleFilesUpload(event,'drop'); | |
63 | return false; |
|
63 | return false; | |
64 | }); |
|
64 | }); | |
65 | }; |
|
65 | }; | |
66 |
|
66 | |||
67 | NotebookList.prototype.handleFilesUpload = function(event, dropOrForm) { |
|
67 | NotebookList.prototype.handleFilesUpload = function(event, dropOrForm) { | |
68 | var that = this; |
|
68 | var that = this; | |
69 | var files; |
|
69 | var files; | |
70 | if(dropOrForm =='drop'){ |
|
70 | if(dropOrForm =='drop'){ | |
71 | files = event.originalEvent.dataTransfer.files; |
|
71 | files = event.originalEvent.dataTransfer.files; | |
72 | } else |
|
72 | } else | |
73 | { |
|
73 | { | |
74 | files = event.originalEvent.target.files; |
|
74 | files = event.originalEvent.target.files; | |
75 | } |
|
75 | } | |
76 | for (var i = 0; i < files.length; i++) { |
|
76 | for (var i = 0; i < files.length; i++) { | |
77 | var f = files[i]; |
|
77 | var f = files[i]; | |
78 | var reader = new FileReader(); |
|
|||
79 | reader.readAsText(f); |
|
|||
80 | var name_and_ext = utils.splitext(f.name); |
|
78 | var name_and_ext = utils.splitext(f.name); | |
81 | var file_ext = name_and_ext[1]; |
|
79 | var file_ext = name_and_ext[1]; | |
|
80 | ||||
|
81 | var reader = new FileReader(); | |||
82 | if (file_ext === '.ipynb') { |
|
82 | if (file_ext === '.ipynb') { | |
83 | var item = that.new_item(0); |
|
83 | reader.readAsText(f); | |
84 | item.addClass('new-file'); |
|
|||
85 | that.add_name_input(f.name, item); |
|
|||
86 | // Store the notebook item in the reader so we can use it later |
|
|||
87 | // to know which item it belongs to. |
|
|||
88 | $(reader).data('item', item); |
|
|||
89 | reader.onload = function (event) { |
|
|||
90 | var nbitem = $(event.target).data('item'); |
|
|||
91 | that.add_notebook_data(event.target.result, nbitem); |
|
|||
92 | that.add_upload_button(nbitem); |
|
|||
93 | }; |
|
|||
94 | } else { |
|
84 | } else { | |
95 | var dialog_body = 'Uploaded notebooks must be .ipynb files'; |
|
85 | // read non-notebook files as binary | |
96 | dialog.modal({ |
|
86 | reader.readAsArrayBuffer(f); | |
97 | title : 'Invalid file type', |
|
|||
98 | body : dialog_body, |
|
|||
99 | buttons : {'OK' : {'class' : 'btn-primary'}} |
|
|||
100 | }); |
|
|||
101 | } |
|
87 | } | |
|
88 | var item = that.new_item(0); | |||
|
89 | item.addClass('new-file'); | |||
|
90 | that.add_name_input(f.name, item); | |||
|
91 | // Store the list item in the reader so we can use it later | |||
|
92 | // to know which item it belongs to. | |||
|
93 | $(reader).data('item', item); | |||
|
94 | reader.onload = function (event) { | |||
|
95 | var item = $(event.target).data('item'); | |||
|
96 | that.add_file_data(event.target.result, item); | |||
|
97 | that.add_upload_button(item); | |||
|
98 | }; | |||
102 | } |
|
99 | } | |
103 | // Replace the file input form wth a clone of itself. This is required to |
|
100 | // Replace the file input form wth a clone of itself. This is required to | |
104 | // reset the form. Otherwise, if you upload a file, delete it and try to |
|
101 | // reset the form. Otherwise, if you upload a file, delete it and try to | |
105 | // upload it again, the changed event won't fire. |
|
102 | // upload it again, the changed event won't fire. | |
106 | var form = $('input.fileinput'); |
|
103 | var form = $('input.fileinput'); | |
107 | form.replaceWith(form.clone(true)); |
|
104 | form.replaceWith(form.clone(true)); | |
108 | return false; |
|
105 | return false; | |
109 | }; |
|
106 | }; | |
110 |
|
107 | |||
111 | NotebookList.prototype.clear_list = function (remove_uploads) { |
|
108 | NotebookList.prototype.clear_list = function (remove_uploads) { | |
112 | // Clears the navigation tree. |
|
109 | // Clears the navigation tree. | |
113 | // |
|
110 | // | |
114 | // Parameters |
|
111 | // Parameters | |
115 | // remove_uploads: bool=False |
|
112 | // remove_uploads: bool=False | |
116 | // Should upload prompts also be removed from the tree. |
|
113 | // Should upload prompts also be removed from the tree. | |
117 | if (remove_uploads) { |
|
114 | if (remove_uploads) { | |
118 | this.element.children('.list_item').remove(); |
|
115 | this.element.children('.list_item').remove(); | |
119 | } else { |
|
116 | } else { | |
120 | this.element.children('.list_item:not(.new-file)').remove(); |
|
117 | this.element.children('.list_item:not(.new-file)').remove(); | |
121 | } |
|
118 | } | |
122 | }; |
|
119 | }; | |
123 |
|
120 | |||
124 | NotebookList.prototype.load_sessions = function(){ |
|
121 | NotebookList.prototype.load_sessions = function(){ | |
125 | this.session_list.load_sessions(); |
|
122 | this.session_list.load_sessions(); | |
126 | }; |
|
123 | }; | |
127 |
|
124 | |||
128 |
|
125 | |||
129 | NotebookList.prototype.sessions_loaded = function(data){ |
|
126 | NotebookList.prototype.sessions_loaded = function(data){ | |
130 | this.sessions = data; |
|
127 | this.sessions = data; | |
131 | this.load_list(); |
|
128 | this.load_list(); | |
132 | }; |
|
129 | }; | |
133 |
|
130 | |||
134 | NotebookList.prototype.load_list = function () { |
|
131 | NotebookList.prototype.load_list = function () { | |
135 | var that = this; |
|
132 | var that = this; | |
136 | var settings = { |
|
133 | var settings = { | |
137 | processData : false, |
|
134 | processData : false, | |
138 | cache : false, |
|
135 | cache : false, | |
139 | type : "GET", |
|
136 | type : "GET", | |
140 | dataType : "json", |
|
137 | dataType : "json", | |
141 | success : $.proxy(this.list_loaded, this), |
|
138 | success : $.proxy(this.list_loaded, this), | |
142 | error : $.proxy( function(xhr, status, error){ |
|
139 | error : $.proxy( function(xhr, status, error){ | |
143 | utils.log_ajax_error(xhr, status, error); |
|
140 | utils.log_ajax_error(xhr, status, error); | |
144 | that.list_loaded([], null, null, {msg:"Error connecting to server."}); |
|
141 | that.list_loaded([], null, null, {msg:"Error connecting to server."}); | |
145 | },this) |
|
142 | },this) | |
146 | }; |
|
143 | }; | |
147 |
|
144 | |||
148 | var url = utils.url_join_encode( |
|
145 | var url = utils.url_join_encode( | |
149 | this.base_url, |
|
146 | this.base_url, | |
150 | 'api', |
|
147 | 'api', | |
151 | 'contents', |
|
148 | 'contents', | |
152 | this.notebook_path |
|
149 | this.notebook_path | |
153 | ); |
|
150 | ); | |
154 | $.ajax(url, settings); |
|
151 | $.ajax(url, settings); | |
155 | }; |
|
152 | }; | |
156 |
|
153 | |||
157 |
|
154 | |||
158 | NotebookList.prototype.list_loaded = function (data, status, xhr, param) { |
|
155 | NotebookList.prototype.list_loaded = function (data, status, xhr, param) { | |
159 | var message = 'Notebook list empty.'; |
|
156 | var message = 'Notebook list empty.'; | |
160 | if (param !== undefined && param.msg) { |
|
157 | if (param !== undefined && param.msg) { | |
161 | message = param.msg; |
|
158 | message = param.msg; | |
162 | } |
|
159 | } | |
163 | var item = null; |
|
160 | var item = null; | |
164 | var model = null; |
|
161 | var model = null; | |
165 | var list = data.content; |
|
162 | var list = data.content; | |
166 | var len = list.length; |
|
163 | var len = list.length; | |
167 | this.clear_list(); |
|
164 | this.clear_list(); | |
168 | if (len === 0) { |
|
165 | if (len === 0) { | |
169 | item = this.new_item(0); |
|
166 | item = this.new_item(0); | |
170 | var span12 = item.children().first(); |
|
167 | var span12 = item.children().first(); | |
171 | span12.empty(); |
|
168 | span12.empty(); | |
172 | span12.append($('<div style="margin:auto;text-align:center;color:grey"/>').text(message)); |
|
169 | span12.append($('<div style="margin:auto;text-align:center;color:grey"/>').text(message)); | |
173 | } |
|
170 | } | |
174 | var path = this.notebook_path; |
|
171 | var path = this.notebook_path; | |
175 | var offset = 0; |
|
172 | var offset = 0; | |
176 | if (path !== '') { |
|
173 | if (path !== '') { | |
177 | item = this.new_item(0); |
|
174 | item = this.new_item(0); | |
178 | model = { |
|
175 | model = { | |
179 | type: 'directory', |
|
176 | type: 'directory', | |
180 | name: '..', |
|
177 | name: '..', | |
181 | path: path, |
|
178 | path: path, | |
182 | }; |
|
179 | }; | |
183 | this.add_link(model, item); |
|
180 | this.add_link(model, item); | |
184 | offset = 1; |
|
181 | offset = 1; | |
185 | } |
|
182 | } | |
186 | for (var i=0; i<len; i++) { |
|
183 | for (var i=0; i<len; i++) { | |
187 | model = list[i]; |
|
184 | model = list[i]; | |
188 | item = this.new_item(i+offset); |
|
185 | item = this.new_item(i+offset); | |
189 | this.add_link(model, item); |
|
186 | this.add_link(model, item); | |
190 | } |
|
187 | } | |
191 | }; |
|
188 | }; | |
192 |
|
189 | |||
193 |
|
190 | |||
194 | NotebookList.prototype.new_item = function (index) { |
|
191 | NotebookList.prototype.new_item = function (index) { | |
195 | var item = $('<div/>').addClass("list_item").addClass("row"); |
|
192 | var item = $('<div/>').addClass("list_item").addClass("row"); | |
196 | // item.addClass('list_item ui-widget ui-widget-content ui-helper-clearfix'); |
|
193 | // item.addClass('list_item ui-widget ui-widget-content ui-helper-clearfix'); | |
197 | // item.css('border-top-style','none'); |
|
194 | // item.css('border-top-style','none'); | |
198 | item.append($("<div/>").addClass("col-md-12").append( |
|
195 | item.append($("<div/>").addClass("col-md-12").append( | |
199 | $('<i/>').addClass('item_icon') |
|
196 | $('<i/>').addClass('item_icon') | |
200 | ).append( |
|
197 | ).append( | |
201 | $("<a/>").addClass("item_link").append( |
|
198 | $("<a/>").addClass("item_link").append( | |
202 | $("<span/>").addClass("item_name") |
|
199 | $("<span/>").addClass("item_name") | |
203 | ) |
|
200 | ) | |
204 | ).append( |
|
201 | ).append( | |
205 | $('<div/>').addClass("item_buttons btn-group pull-right") |
|
202 | $('<div/>').addClass("item_buttons btn-group pull-right") | |
206 | )); |
|
203 | )); | |
207 |
|
204 | |||
208 | if (index === -1) { |
|
205 | if (index === -1) { | |
209 | this.element.append(item); |
|
206 | this.element.append(item); | |
210 | } else { |
|
207 | } else { | |
211 | this.element.children().eq(index).after(item); |
|
208 | this.element.children().eq(index).after(item); | |
212 | } |
|
209 | } | |
213 | return item; |
|
210 | return item; | |
214 | }; |
|
211 | }; | |
215 |
|
212 | |||
216 |
|
213 | |||
217 | NotebookList.icons = { |
|
214 | NotebookList.icons = { | |
218 | directory: 'folder_icon', |
|
215 | directory: 'folder_icon', | |
219 | notebook: 'notebook_icon', |
|
216 | notebook: 'notebook_icon', | |
220 | file: 'file_icon', |
|
217 | file: 'file_icon', | |
221 | }; |
|
218 | }; | |
222 |
|
219 | |||
223 | NotebookList.uri_prefixes = { |
|
220 | NotebookList.uri_prefixes = { | |
224 | directory: 'tree', |
|
221 | directory: 'tree', | |
225 | notebook: 'notebooks', |
|
222 | notebook: 'notebooks', | |
226 | file: 'files', |
|
223 | file: 'files', | |
227 | }; |
|
224 | }; | |
228 |
|
225 | |||
229 |
|
226 | |||
230 | NotebookList.prototype.add_link = function (model, item) { |
|
227 | NotebookList.prototype.add_link = function (model, item) { | |
231 | var path = model.path, |
|
228 | var path = model.path, | |
232 | name = model.name; |
|
229 | name = model.name; | |
233 | item.data('name', name); |
|
230 | item.data('name', name); | |
234 | item.data('path', path); |
|
231 | item.data('path', path); | |
235 | item.find(".item_name").text(name); |
|
232 | item.find(".item_name").text(name); | |
236 | var icon = NotebookList.icons[model.type]; |
|
233 | var icon = NotebookList.icons[model.type]; | |
237 | var uri_prefix = NotebookList.uri_prefixes[model.type]; |
|
234 | var uri_prefix = NotebookList.uri_prefixes[model.type]; | |
238 | item.find(".item_icon").addClass(icon).addClass('icon-fixed-width'); |
|
235 | item.find(".item_icon").addClass(icon).addClass('icon-fixed-width'); | |
239 | var link = item.find("a.item_link") |
|
236 | var link = item.find("a.item_link") | |
240 | .attr('href', |
|
237 | .attr('href', | |
241 | utils.url_join_encode( |
|
238 | utils.url_join_encode( | |
242 | this.base_url, |
|
239 | this.base_url, | |
243 | uri_prefix, |
|
240 | uri_prefix, | |
244 | path, |
|
241 | path, | |
245 | name |
|
242 | name | |
246 | ) |
|
243 | ) | |
247 | ); |
|
244 | ); | |
248 | // directory nav doesn't open new tabs |
|
245 | // directory nav doesn't open new tabs | |
249 | // files, notebooks do |
|
246 | // files, notebooks do | |
250 | if (model.type !== "directory") { |
|
247 | if (model.type !== "directory") { | |
251 | link.attr('target','_blank'); |
|
248 | link.attr('target','_blank'); | |
252 | } |
|
249 | } | |
253 | var path_name = utils.url_path_join(path, name); |
|
250 | var path_name = utils.url_path_join(path, name); | |
254 | if (model.type == 'file') { |
|
251 | if (model.type == 'file') { | |
255 | this.add_delete_button(item); |
|
252 | this.add_delete_button(item); | |
256 | } else if (model.type == 'notebook') { |
|
253 | } else if (model.type == 'notebook') { | |
257 | if(this.sessions[path_name] === undefined){ |
|
254 | if(this.sessions[path_name] === undefined){ | |
258 | this.add_delete_button(item); |
|
255 | this.add_delete_button(item); | |
259 | } else { |
|
256 | } else { | |
260 | this.add_shutdown_button(item, this.sessions[path_name]); |
|
257 | this.add_shutdown_button(item, this.sessions[path_name]); | |
261 | } |
|
258 | } | |
262 | } |
|
259 | } | |
263 | }; |
|
260 | }; | |
264 |
|
261 | |||
265 |
|
262 | |||
266 | NotebookList.prototype.add_name_input = function (name, item) { |
|
263 | NotebookList.prototype.add_name_input = function (name, item) { | |
267 | item.data('name', name); |
|
264 | item.data('name', name); | |
268 | item.find(".item_icon").addClass('notebook_icon').addClass('icon-fixed-width'); |
|
265 | item.find(".item_icon").addClass('notebook_icon').addClass('icon-fixed-width'); | |
269 | item.find(".item_name").empty().append( |
|
266 | item.find(".item_name").empty().append( | |
270 | $('<input/>') |
|
267 | $('<input/>') | |
271 |
.addClass(" |
|
268 | .addClass("filename_input") | |
272 |
.attr('value', |
|
269 | .attr('value', name) | |
273 | .attr('size', '30') |
|
270 | .attr('size', '30') | |
274 | .attr('type', 'text') |
|
271 | .attr('type', 'text') | |
275 | ); |
|
272 | ); | |
276 | }; |
|
273 | }; | |
277 |
|
274 | |||
278 |
|
275 | |||
279 |
NotebookList.prototype.add_ |
|
276 | NotebookList.prototype.add_file_data = function (data, item) { | |
280 |
item.data(' |
|
277 | item.data('filedata', data); | |
281 | }; |
|
278 | }; | |
282 |
|
279 | |||
283 |
|
280 | |||
284 | NotebookList.prototype.add_shutdown_button = function (item, session) { |
|
281 | NotebookList.prototype.add_shutdown_button = function (item, session) { | |
285 | var that = this; |
|
282 | var that = this; | |
286 | var shutdown_button = $("<button/>").text("Shutdown").addClass("btn btn-xs btn-danger"). |
|
283 | var shutdown_button = $("<button/>").text("Shutdown").addClass("btn btn-xs btn-danger"). | |
287 | click(function (e) { |
|
284 | click(function (e) { | |
288 | var settings = { |
|
285 | var settings = { | |
289 | processData : false, |
|
286 | processData : false, | |
290 | cache : false, |
|
287 | cache : false, | |
291 | type : "DELETE", |
|
288 | type : "DELETE", | |
292 | dataType : "json", |
|
289 | dataType : "json", | |
293 | success : function () { |
|
290 | success : function () { | |
294 | that.load_sessions(); |
|
291 | that.load_sessions(); | |
295 | }, |
|
292 | }, | |
296 | error : utils.log_ajax_error, |
|
293 | error : utils.log_ajax_error, | |
297 | }; |
|
294 | }; | |
298 | var url = utils.url_join_encode( |
|
295 | var url = utils.url_join_encode( | |
299 | that.base_url, |
|
296 | that.base_url, | |
300 | 'api/sessions', |
|
297 | 'api/sessions', | |
301 | session |
|
298 | session | |
302 | ); |
|
299 | ); | |
303 | $.ajax(url, settings); |
|
300 | $.ajax(url, settings); | |
304 | return false; |
|
301 | return false; | |
305 | }); |
|
302 | }); | |
306 | // var new_buttons = item.find('a'); // shutdown_button; |
|
303 | // var new_buttons = item.find('a'); // shutdown_button; | |
307 | item.find(".item_buttons").text("").append(shutdown_button); |
|
304 | item.find(".item_buttons").text("").append(shutdown_button); | |
308 | }; |
|
305 | }; | |
309 |
|
306 | |||
310 | NotebookList.prototype.add_delete_button = function (item) { |
|
307 | NotebookList.prototype.add_delete_button = function (item) { | |
311 | var new_buttons = $('<span/>').addClass("btn-group pull-right"); |
|
308 | var new_buttons = $('<span/>').addClass("btn-group pull-right"); | |
312 | var notebooklist = this; |
|
309 | var notebooklist = this; | |
313 | var delete_button = $("<button/>").text("Delete").addClass("btn btn-default btn-xs"). |
|
310 | var delete_button = $("<button/>").text("Delete").addClass("btn btn-default btn-xs"). | |
314 | click(function (e) { |
|
311 | click(function (e) { | |
315 | // $(this) is the button that was clicked. |
|
312 | // $(this) is the button that was clicked. | |
316 | var that = $(this); |
|
313 | var that = $(this); | |
317 |
// We use the |
|
314 | // We use the filename from the parent list_item element's | |
318 | // data because the outer scopes values change as we iterate through the loop. |
|
315 | // data because the outer scope's values change as we iterate through the loop. | |
319 | var parent_item = that.parents('div.list_item'); |
|
316 | var parent_item = that.parents('div.list_item'); | |
320 | var name = parent_item.data('name'); |
|
317 | var name = parent_item.data('name'); | |
321 | var message = 'Are you sure you want to permanently delete the file: ' + name + '?'; |
|
318 | var message = 'Are you sure you want to permanently delete the file: ' + name + '?'; | |
322 | dialog.modal({ |
|
319 | dialog.modal({ | |
323 | title : "Delete file", |
|
320 | title : "Delete file", | |
324 | body : message, |
|
321 | body : message, | |
325 | buttons : { |
|
322 | buttons : { | |
326 | Delete : { |
|
323 | Delete : { | |
327 | class: "btn-danger", |
|
324 | class: "btn-danger", | |
328 | click: function() { |
|
325 | click: function() { | |
329 | var settings = { |
|
326 | var settings = { | |
330 | processData : false, |
|
327 | processData : false, | |
331 | cache : false, |
|
328 | cache : false, | |
332 | type : "DELETE", |
|
329 | type : "DELETE", | |
333 | dataType : "json", |
|
330 | dataType : "json", | |
334 | success : function (data, status, xhr) { |
|
331 | success : function (data, status, xhr) { | |
335 | parent_item.remove(); |
|
332 | parent_item.remove(); | |
336 | }, |
|
333 | }, | |
337 | error : utils.log_ajax_error, |
|
334 | error : utils.log_ajax_error, | |
338 | }; |
|
335 | }; | |
339 | var url = utils.url_join_encode( |
|
336 | var url = utils.url_join_encode( | |
340 | notebooklist.base_url, |
|
337 | notebooklist.base_url, | |
341 | 'api/contents', |
|
338 | 'api/contents', | |
342 | notebooklist.notebook_path, |
|
339 | notebooklist.notebook_path, | |
343 | name |
|
340 | name | |
344 | ); |
|
341 | ); | |
345 | $.ajax(url, settings); |
|
342 | $.ajax(url, settings); | |
346 | } |
|
343 | } | |
347 | }, |
|
344 | }, | |
348 | Cancel : {} |
|
345 | Cancel : {} | |
349 | } |
|
346 | } | |
350 | }); |
|
347 | }); | |
351 | return false; |
|
348 | return false; | |
352 | }); |
|
349 | }); | |
353 | item.find(".item_buttons").text("").append(delete_button); |
|
350 | item.find(".item_buttons").text("").append(delete_button); | |
354 | }; |
|
351 | }; | |
355 |
|
352 | |||
356 |
|
353 | |||
357 | NotebookList.prototype.add_upload_button = function (item) { |
|
354 | NotebookList.prototype.add_upload_button = function (item, type) { | |
358 | var that = this; |
|
355 | var that = this; | |
359 | var upload_button = $('<button/>').text("Upload") |
|
356 | var upload_button = $('<button/>').text("Upload") | |
360 | .addClass('btn btn-primary btn-xs upload_button') |
|
357 | .addClass('btn btn-primary btn-xs upload_button') | |
361 | .click(function (e) { |
|
358 | .click(function (e) { | |
362 | var nbname = item.find('.item_name > input').val(); |
|
|||
363 | if (nbname.slice(nbname.length-6, nbname.length) != ".ipynb") { |
|
|||
364 | nbname = nbname + ".ipynb"; |
|
|||
365 | } |
|
|||
366 | var path = that.notebook_path; |
|
359 | var path = that.notebook_path; | |
367 |
var |
|
360 | var filename = item.find('.item_name > input').val(); | |
368 | var content_type = 'application/json'; |
|
361 | var filedata = item.data('filedata'); | |
|
362 | var format = 'text'; | |||
|
363 | if (filedata instanceof ArrayBuffer) { | |||
|
364 | // base64-encode binary file data | |||
|
365 | var bytes = ''; | |||
|
366 | var buf = new Uint8Array(filedata); | |||
|
367 | var nbytes = buf.byteLength; | |||
|
368 | for (var i=0; i<nbytes; i++) { | |||
|
369 | bytes += String.fromCharCode(buf[i]); | |||
|
370 | } | |||
|
371 | filedata = btoa(bytes); | |||
|
372 | format = 'base64'; | |||
|
373 | } | |||
369 | var model = { |
|
374 | var model = { | |
370 | path: path, |
|
375 | path: path, | |
371 |
name: |
|
376 | name: filename | |
372 | content : JSON.parse(nbdata), |
|
|||
373 | type : 'notebook' |
|
|||
374 | }; |
|
377 | }; | |
|
378 | ||||
|
379 | var name_and_ext = utils.splitext(filename); | |||
|
380 | var file_ext = name_and_ext[1]; | |||
|
381 | var content_type; | |||
|
382 | if (file_ext === '.ipynb') { | |||
|
383 | model.type = 'notebook'; | |||
|
384 | model.format = 'json'; | |||
|
385 | model.content = JSON.parse(filedata); | |||
|
386 | content_type = 'application/json'; | |||
|
387 | } else { | |||
|
388 | model.type = 'file'; | |||
|
389 | model.format = format; | |||
|
390 | model.content = filedata; | |||
|
391 | content_type = 'application/octet-stream'; | |||
|
392 | } | |||
|
393 | var filedata = item.data('filedata'); | |||
|
394 | ||||
375 | var settings = { |
|
395 | var settings = { | |
376 | processData : false, |
|
396 | processData : false, | |
377 | cache : false, |
|
397 | cache : false, | |
378 | type : 'PUT', |
|
398 | type : 'PUT', | |
379 | dataType : 'json', |
|
|||
380 | data : JSON.stringify(model), |
|
399 | data : JSON.stringify(model), | |
381 | headers : {'Content-Type': content_type}, |
|
400 | headers : {'Content-Type': content_type}, | |
382 | success : function (data, status, xhr) { |
|
401 | success : function (data, status, xhr) { | |
|
402 | item.removeClass('new-file'); | |||
383 | that.add_link(model, item); |
|
403 | that.add_link(model, item); | |
384 | that.add_delete_button(item); |
|
404 | that.add_delete_button(item); | |
385 | }, |
|
405 | }, | |
386 | error : utils.log_ajax_error, |
|
406 | error : utils.log_ajax_error, | |
387 | }; |
|
407 | }; | |
388 |
|
408 | |||
389 | var url = utils.url_join_encode( |
|
409 | var url = utils.url_join_encode( | |
390 | that.base_url, |
|
410 | that.base_url, | |
391 | 'api/contents', |
|
411 | 'api/contents', | |
392 | that.notebook_path, |
|
412 | that.notebook_path, | |
393 |
|
|
413 | filename | |
394 | ); |
|
414 | ); | |
395 | $.ajax(url, settings); |
|
415 | $.ajax(url, settings); | |
396 | return false; |
|
416 | return false; | |
397 | }); |
|
417 | }); | |
398 | var cancel_button = $('<button/>').text("Cancel") |
|
418 | var cancel_button = $('<button/>').text("Cancel") | |
399 | .addClass("btn btn-default btn-xs") |
|
419 | .addClass("btn btn-default btn-xs") | |
400 | .click(function (e) { |
|
420 | .click(function (e) { | |
401 | console.log('cancel click'); |
|
|||
402 | item.remove(); |
|
421 | item.remove(); | |
403 | return false; |
|
422 | return false; | |
404 | }); |
|
423 | }); | |
405 | item.find(".item_buttons").empty() |
|
424 | item.find(".item_buttons").empty() | |
406 | .append(upload_button) |
|
425 | .append(upload_button) | |
407 | .append(cancel_button); |
|
426 | .append(cancel_button); | |
408 | }; |
|
427 | }; | |
409 |
|
428 | |||
410 |
|
429 | |||
411 | NotebookList.prototype.new_notebook = function(){ |
|
430 | NotebookList.prototype.new_notebook = function(){ | |
412 | var path = this.notebook_path; |
|
431 | var path = this.notebook_path; | |
413 | var base_url = this.base_url; |
|
432 | var base_url = this.base_url; | |
414 | var settings = { |
|
433 | var settings = { | |
415 | processData : false, |
|
434 | processData : false, | |
416 | cache : false, |
|
435 | cache : false, | |
417 | type : "POST", |
|
436 | type : "POST", | |
418 | dataType : "json", |
|
437 | dataType : "json", | |
419 | async : false, |
|
438 | async : false, | |
420 | success : function (data, status, xhr) { |
|
439 | success : function (data, status, xhr) { | |
421 | var notebook_name = data.name; |
|
440 | var notebook_name = data.name; | |
422 | window.open( |
|
441 | window.open( | |
423 | utils.url_join_encode( |
|
442 | utils.url_join_encode( | |
424 | base_url, |
|
443 | base_url, | |
425 | 'notebooks', |
|
444 | 'notebooks', | |
426 | path, |
|
445 | path, | |
427 | notebook_name), |
|
446 | notebook_name), | |
428 | '_blank' |
|
447 | '_blank' | |
429 | ); |
|
448 | ); | |
430 | }, |
|
449 | }, | |
431 | error : $.proxy(this.new_notebook_failed, this), |
|
450 | error : $.proxy(this.new_notebook_failed, this), | |
432 | }; |
|
451 | }; | |
433 | var url = utils.url_join_encode( |
|
452 | var url = utils.url_join_encode( | |
434 | base_url, |
|
453 | base_url, | |
435 | 'api/contents', |
|
454 | 'api/contents', | |
436 | path |
|
455 | path | |
437 | ); |
|
456 | ); | |
438 | $.ajax(url, settings); |
|
457 | $.ajax(url, settings); | |
439 | }; |
|
458 | }; | |
440 |
|
459 | |||
441 |
|
460 | |||
442 | NotebookList.prototype.new_notebook_failed = function (xhr, status, error) { |
|
461 | NotebookList.prototype.new_notebook_failed = function (xhr, status, error) { | |
443 | utils.log_ajax_error(xhr, status, error); |
|
462 | utils.log_ajax_error(xhr, status, error); | |
444 | var msg; |
|
463 | var msg; | |
445 | if (xhr.responseJSON && xhr.responseJSON.message) { |
|
464 | if (xhr.responseJSON && xhr.responseJSON.message) { | |
446 | msg = xhr.responseJSON.message; |
|
465 | msg = xhr.responseJSON.message; | |
447 | } else { |
|
466 | } else { | |
448 | msg = xhr.statusText; |
|
467 | msg = xhr.statusText; | |
449 | } |
|
468 | } | |
450 | dialog.modal({ |
|
469 | dialog.modal({ | |
451 | title : 'Creating Notebook Failed', |
|
470 | title : 'Creating Notebook Failed', | |
452 | body : "The error was: " + msg, |
|
471 | body : "The error was: " + msg, | |
453 | buttons : {'OK' : {'class' : 'btn-primary'}} |
|
472 | buttons : {'OK' : {'class' : 'btn-primary'}} | |
454 | }); |
|
473 | }); | |
455 | }; |
|
474 | }; | |
456 |
|
475 | |||
457 |
|
476 | |||
458 | // Backwards compatability. |
|
477 | // Backwards compatability. | |
459 | IPython.NotebookList = NotebookList; |
|
478 | IPython.NotebookList = NotebookList; | |
460 |
|
479 | |||
461 | return {'NotebookList': NotebookList}; |
|
480 | return {'NotebookList': NotebookList}; | |
462 | }); |
|
481 | }); |
General Comments 0
You need to be logged in to leave comments.
Login now