Show More
@@ -1,851 +1,853 | |||||
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 | 'base/js/events', |
|
9 | 'base/js/events', | |
10 | 'base/js/keyboard', |
|
10 | 'base/js/keyboard', | |
11 | ], function(IPython, $, utils, dialog, events, keyboard) { |
|
11 | ], function(IPython, $, utils, dialog, events, keyboard) { | |
12 | "use strict"; |
|
12 | "use strict"; | |
13 |
|
13 | |||
14 | var NotebookList = function (selector, options) { |
|
14 | var NotebookList = function (selector, options) { | |
15 | /** |
|
15 | /** | |
16 | * Constructor |
|
16 | * Constructor | |
17 | * |
|
17 | * | |
18 | * Parameters: |
|
18 | * Parameters: | |
19 | * selector: string |
|
19 | * selector: string | |
20 | * options: dictionary |
|
20 | * options: dictionary | |
21 | * Dictionary of keyword arguments. |
|
21 | * Dictionary of keyword arguments. | |
22 | * session_list: SessionList instance |
|
22 | * session_list: SessionList instance | |
23 | * element_name: string |
|
23 | * element_name: string | |
24 | * base_url: string |
|
24 | * base_url: string | |
25 | * notebook_path: string |
|
25 | * notebook_path: string | |
26 | * contents: Contents instance |
|
26 | * contents: Contents instance | |
27 | */ |
|
27 | */ | |
28 | var that = this; |
|
28 | var that = this; | |
29 | this.session_list = options.session_list; |
|
29 | this.session_list = options.session_list; | |
30 | // allow code re-use by just changing element_name in kernellist.js |
|
30 | // allow code re-use by just changing element_name in kernellist.js | |
31 | this.element_name = options.element_name || 'notebook'; |
|
31 | this.element_name = options.element_name || 'notebook'; | |
32 | this.selector = selector; |
|
32 | this.selector = selector; | |
33 | if (this.selector !== undefined) { |
|
33 | if (this.selector !== undefined) { | |
34 | this.element = $(selector); |
|
34 | this.element = $(selector); | |
35 | this.style(); |
|
35 | this.style(); | |
36 | this.bind_events(); |
|
36 | this.bind_events(); | |
37 | } |
|
37 | } | |
38 | this.notebooks_list = []; |
|
38 | this.notebooks_list = []; | |
39 | this.sessions = {}; |
|
39 | this.sessions = {}; | |
40 | this.base_url = options.base_url || utils.get_body_data("baseUrl"); |
|
40 | this.base_url = options.base_url || utils.get_body_data("baseUrl"); | |
41 | this.notebook_path = options.notebook_path || utils.get_body_data("notebookPath"); |
|
41 | this.notebook_path = options.notebook_path || utils.get_body_data("notebookPath"); | |
42 | this.contents = options.contents; |
|
42 | this.contents = options.contents; | |
43 | if (this.session_list && this.session_list.events) { |
|
43 | if (this.session_list && this.session_list.events) { | |
44 | this.session_list.events.on('sessions_loaded.Dashboard', |
|
44 | this.session_list.events.on('sessions_loaded.Dashboard', | |
45 | function(e, d) { that.sessions_loaded(d); }); |
|
45 | function(e, d) { that.sessions_loaded(d); }); | |
46 | } |
|
46 | } | |
47 | this.selected = []; |
|
47 | this.selected = []; | |
48 | }; |
|
48 | }; | |
49 |
|
49 | |||
50 | NotebookList.prototype.style = function () { |
|
50 | NotebookList.prototype.style = function () { | |
51 | var prefix = '#' + this.element_name; |
|
51 | var prefix = '#' + this.element_name; | |
52 | $(prefix + '_toolbar').addClass('list_toolbar'); |
|
52 | $(prefix + '_toolbar').addClass('list_toolbar'); | |
53 | $(prefix + '_list_info').addClass('toolbar_info'); |
|
53 | $(prefix + '_list_info').addClass('toolbar_info'); | |
54 | $(prefix + '_buttons').addClass('toolbar_buttons'); |
|
54 | $(prefix + '_buttons').addClass('toolbar_buttons'); | |
55 | $(prefix + '_list_header').addClass('list_header'); |
|
55 | $(prefix + '_list_header').addClass('list_header'); | |
56 | this.element.addClass("list_container"); |
|
56 | this.element.addClass("list_container"); | |
57 | }; |
|
57 | }; | |
58 |
|
58 | |||
59 | NotebookList.prototype.bind_events = function () { |
|
59 | NotebookList.prototype.bind_events = function () { | |
60 | var that = this; |
|
60 | var that = this; | |
61 | $('#refresh_' + this.element_name + '_list').click(function () { |
|
61 | $('#refresh_' + this.element_name + '_list').click(function () { | |
62 | that.load_sessions(); |
|
62 | that.load_sessions(); | |
63 | }); |
|
63 | }); | |
64 | this.element.bind('dragover', function () { |
|
64 | this.element.bind('dragover', function () { | |
65 | return false; |
|
65 | return false; | |
66 | }); |
|
66 | }); | |
67 | this.element.bind('drop', function(event){ |
|
67 | this.element.bind('drop', function(event){ | |
68 | that.handleFilesUpload(event,'drop'); |
|
68 | that.handleFilesUpload(event,'drop'); | |
69 | return false; |
|
69 | return false; | |
70 | }); |
|
70 | }); | |
71 |
|
71 | |||
72 | // Bind events for singleton controls. |
|
72 | // Bind events for singleton controls. | |
73 | if (!NotebookList._bound_singletons) { |
|
73 | if (!NotebookList._bound_singletons) { | |
74 | NotebookList._bound_singletons = true; |
|
74 | NotebookList._bound_singletons = true; | |
75 | $('#new-file').click(function(e) { |
|
75 | $('#new-file').click(function(e) { | |
76 | var w = window.open(undefined, IPython._target); |
|
76 | var w = window.open(undefined, IPython._target); | |
77 | that.contents.new_untitled(that.notebook_path || '', {type: 'file', ext: '.txt'}).then(function(data) { |
|
77 | that.contents.new_untitled(that.notebook_path || '', {type: 'file', ext: '.txt'}).then(function(data) { | |
78 | var url = utils.url_join_encode( |
|
78 | var url = utils.url_join_encode( | |
79 | that.base_url, 'edit', data.path |
|
79 | that.base_url, 'edit', data.path | |
80 | ); |
|
80 | ); | |
81 | w.location = url; |
|
81 | w.location = url; | |
82 | }).catch(function (e) { |
|
82 | }).catch(function (e) { | |
83 | w.close(); |
|
83 | w.close(); | |
84 | dialog.modal({ |
|
84 | dialog.modal({ | |
85 | title: 'Creating File Failed', |
|
85 | title: 'Creating File Failed', | |
86 | body: $('<div/>') |
|
86 | body: $('<div/>') | |
87 | .text("An error occurred while creating a new file.") |
|
87 | .text("An error occurred while creating a new file.") | |
88 | .append($('<div/>') |
|
88 | .append($('<div/>') | |
89 | .addClass('alert alert-danger') |
|
89 | .addClass('alert alert-danger') | |
90 | .text(e.message || e)), |
|
90 | .text(e.message || e)), | |
91 | buttons: { |
|
91 | buttons: { | |
92 | OK: {'class': 'btn-primary'} |
|
92 | OK: {'class': 'btn-primary'} | |
93 | } |
|
93 | } | |
94 | }); |
|
94 | }); | |
95 | console.warn('Error durring New file creation', e); |
|
95 | console.warn('Error durring New file creation', e); | |
96 | }); |
|
96 | }); | |
97 | that.load_sessions(); |
|
97 | that.load_sessions(); | |
98 | }); |
|
98 | }); | |
99 | $('#new-folder').click(function(e) { |
|
99 | $('#new-folder').click(function(e) { | |
100 | that.contents.new_untitled(that.notebook_path || '', {type: 'directory'}) |
|
100 | that.contents.new_untitled(that.notebook_path || '', {type: 'directory'}) | |
101 | .then(function(){ |
|
101 | .then(function(){ | |
102 | that.load_list(); |
|
102 | that.load_list(); | |
103 | }).catch(function (e) { |
|
103 | }).catch(function (e) { | |
104 | dialog.modal({ |
|
104 | dialog.modal({ | |
105 | title: 'Creating Folder Failed', |
|
105 | title: 'Creating Folder Failed', | |
106 | body: $('<div/>') |
|
106 | body: $('<div/>') | |
107 | .text("An error occurred while creating a new folder.") |
|
107 | .text("An error occurred while creating a new folder.") | |
108 | .append($('<div/>') |
|
108 | .append($('<div/>') | |
109 | .addClass('alert alert-danger') |
|
109 | .addClass('alert alert-danger') | |
110 | .text(e.message || e)), |
|
110 | .text(e.message || e)), | |
111 | buttons: { |
|
111 | buttons: { | |
112 | OK: {'class': 'btn-primary'} |
|
112 | OK: {'class': 'btn-primary'} | |
113 | } |
|
113 | } | |
114 | }); |
|
114 | }); | |
115 | console.warn('Error durring New directory creation', e); |
|
115 | console.warn('Error durring New directory creation', e); | |
116 | }); |
|
116 | }); | |
117 | that.load_sessions(); |
|
117 | that.load_sessions(); | |
118 | }); |
|
118 | }); | |
119 |
|
119 | |||
120 | // Bind events for action buttons. |
|
120 | // Bind events for action buttons. | |
121 | $('.rename-button').click($.proxy(this.rename_selected, this)); |
|
121 | $('.rename-button').click($.proxy(this.rename_selected, this)); | |
122 | $('.shutdown-button').click($.proxy(this.shutdown_selected, this)); |
|
122 | $('.shutdown-button').click($.proxy(this.shutdown_selected, this)); | |
123 | $('.duplicate-button').click($.proxy(this.duplicate_selected, this)); |
|
123 | $('.duplicate-button').click($.proxy(this.duplicate_selected, this)); | |
124 | $('.delete-button').click($.proxy(this.delete_selected, this)); |
|
124 | $('.delete-button').click($.proxy(this.delete_selected, this)); | |
125 |
|
125 | |||
126 | // Bind events for selection menu buttons. |
|
126 | // Bind events for selection menu buttons. | |
127 | $('#selector-menu').click(function(event){that.select($(event.target).attr('id'))}); |
|
127 | $('#selector-menu').click(function(event){that.select($(event.target).attr('id'))}); | |
128 | $('#select-all').change(function(){that.select($(this).is(':checked') ? 'select-all' : 'select-none')}); |
|
128 | $('#select-all').change(function(){that.select($(this).is(':checked') ? 'select-all' : 'select-none')}); | |
129 | $('#button-select-all').click(function(e) { |
|
129 | $('#button-select-all').click(function(e) { | |
130 | // toggle checkbox if the click doesn't come from the checkbox already |
|
130 | // toggle checkbox if the click doesn't come from the checkbox already | |
131 | if (!$(e.target).is('input[type=checkbox]')) { |
|
131 | if (!$(e.target).is('input[type=checkbox]')) { | |
132 | var checkbox = $('#select-all'); |
|
132 | var checkbox = $('#select-all'); | |
133 | checkbox.prop('checked', !checkbox.prop('checked')); |
|
133 | checkbox.prop('checked', !checkbox.prop('checked')); | |
134 | that.select(checkbox.prop('checked') ? 'select-all' : 'select-none'); |
|
134 | that.select(checkbox.prop('checked') ? 'select-all' : 'select-none'); | |
135 | } |
|
135 | } | |
136 | }); |
|
136 | }); | |
137 | } |
|
137 | } | |
138 | }; |
|
138 | }; | |
139 |
|
139 | |||
140 | NotebookList.prototype.handleFilesUpload = function(event, dropOrForm) { |
|
140 | NotebookList.prototype.handleFilesUpload = function(event, dropOrForm) { | |
141 | var that = this; |
|
141 | var that = this; | |
142 | var files; |
|
142 | var files; | |
143 | if(dropOrForm =='drop'){ |
|
143 | if(dropOrForm =='drop'){ | |
144 | files = event.originalEvent.dataTransfer.files; |
|
144 | files = event.originalEvent.dataTransfer.files; | |
145 | } else |
|
145 | } else | |
146 | { |
|
146 | { | |
147 | files = event.originalEvent.target.files; |
|
147 | files = event.originalEvent.target.files; | |
148 | } |
|
148 | } | |
149 | for (var i = 0; i < files.length; i++) { |
|
149 | for (var i = 0; i < files.length; i++) { | |
150 | var f = files[i]; |
|
150 | var f = files[i]; | |
151 | var name_and_ext = utils.splitext(f.name); |
|
151 | var name_and_ext = utils.splitext(f.name); | |
152 | var file_ext = name_and_ext[1]; |
|
152 | var file_ext = name_and_ext[1]; | |
153 |
|
153 | |||
154 | var reader = new FileReader(); |
|
154 | var reader = new FileReader(); | |
155 | if (file_ext === '.ipynb') { |
|
155 | if (file_ext === '.ipynb') { | |
156 | reader.readAsText(f); |
|
156 | reader.readAsText(f); | |
157 | } else { |
|
157 | } else { | |
158 | // read non-notebook files as binary |
|
158 | // read non-notebook files as binary | |
159 | reader.readAsArrayBuffer(f); |
|
159 | reader.readAsArrayBuffer(f); | |
160 | } |
|
160 | } | |
161 | var item = that.new_item(0, true); |
|
161 | var item = that.new_item(0, true); | |
162 | item.addClass('new-file'); |
|
162 | item.addClass('new-file'); | |
163 | that.add_name_input(f.name, item, file_ext == '.ipynb' ? 'notebook' : 'file'); |
|
163 | that.add_name_input(f.name, item, file_ext == '.ipynb' ? 'notebook' : 'file'); | |
164 | // Store the list item in the reader so we can use it later |
|
164 | // Store the list item in the reader so we can use it later | |
165 | // to know which item it belongs to. |
|
165 | // to know which item it belongs to. | |
166 | $(reader).data('item', item); |
|
166 | $(reader).data('item', item); | |
167 | reader.onload = function (event) { |
|
167 | reader.onload = function (event) { | |
168 | var item = $(event.target).data('item'); |
|
168 | var item = $(event.target).data('item'); | |
169 | that.add_file_data(event.target.result, item); |
|
169 | that.add_file_data(event.target.result, item); | |
170 | that.add_upload_button(item); |
|
170 | that.add_upload_button(item); | |
171 | }; |
|
171 | }; | |
172 | reader.onerror = function (event) { |
|
172 | reader.onerror = function (event) { | |
173 | var item = $(event.target).data('item'); |
|
173 | var item = $(event.target).data('item'); | |
174 | var name = item.data('name'); |
|
174 | var name = item.data('name'); | |
175 | item.remove(); |
|
175 | item.remove(); | |
176 | dialog.modal({ |
|
176 | dialog.modal({ | |
177 | title : 'Failed to read file', |
|
177 | title : 'Failed to read file', | |
178 | body : "Failed to read file '" + name + "'", |
|
178 | body : "Failed to read file '" + name + "'", | |
179 | buttons : {'OK' : { 'class' : 'btn-primary' }} |
|
179 | buttons : {'OK' : { 'class' : 'btn-primary' }} | |
180 | }); |
|
180 | }); | |
181 | }; |
|
181 | }; | |
182 | } |
|
182 | } | |
183 | // Replace the file input form wth a clone of itself. This is required to |
|
183 | // Replace the file input form wth a clone of itself. This is required to | |
184 | // reset the form. Otherwise, if you upload a file, delete it and try to |
|
184 | // reset the form. Otherwise, if you upload a file, delete it and try to | |
185 | // upload it again, the changed event won't fire. |
|
185 | // upload it again, the changed event won't fire. | |
186 | var form = $('input.fileinput'); |
|
186 | var form = $('input.fileinput'); | |
187 | form.replaceWith(form.clone(true)); |
|
187 | form.replaceWith(form.clone(true)); | |
188 | return false; |
|
188 | return false; | |
189 | }; |
|
189 | }; | |
190 |
|
190 | |||
191 | NotebookList.prototype.clear_list = function (remove_uploads) { |
|
191 | NotebookList.prototype.clear_list = function (remove_uploads) { | |
192 | /** |
|
192 | /** | |
193 | * Clears the navigation tree. |
|
193 | * Clears the navigation tree. | |
194 | * |
|
194 | * | |
195 | * Parameters |
|
195 | * Parameters | |
196 | * remove_uploads: bool=False |
|
196 | * remove_uploads: bool=False | |
197 | * Should upload prompts also be removed from the tree. |
|
197 | * Should upload prompts also be removed from the tree. | |
198 | */ |
|
198 | */ | |
199 | if (remove_uploads) { |
|
199 | if (remove_uploads) { | |
200 | this.element.children('.list_item').remove(); |
|
200 | this.element.children('.list_item').remove(); | |
201 | } else { |
|
201 | } else { | |
202 | this.element.children('.list_item:not(.new-file)').remove(); |
|
202 | this.element.children('.list_item:not(.new-file)').remove(); | |
203 | } |
|
203 | } | |
204 | }; |
|
204 | }; | |
205 |
|
205 | |||
206 | NotebookList.prototype.load_sessions = function(){ |
|
206 | NotebookList.prototype.load_sessions = function(){ | |
207 | this.session_list.load_sessions(); |
|
207 | this.session_list.load_sessions(); | |
208 | }; |
|
208 | }; | |
209 |
|
209 | |||
210 |
|
210 | |||
211 | NotebookList.prototype.sessions_loaded = function(data){ |
|
211 | NotebookList.prototype.sessions_loaded = function(data){ | |
212 | this.sessions = data; |
|
212 | this.sessions = data; | |
213 | this.load_list(); |
|
213 | this.load_list(); | |
214 | }; |
|
214 | }; | |
215 |
|
215 | |||
216 | NotebookList.prototype.load_list = function () { |
|
216 | NotebookList.prototype.load_list = function () { | |
217 | var that = this; |
|
217 | var that = this; | |
218 | this.contents.list_contents(that.notebook_path).then( |
|
218 | this.contents.list_contents(that.notebook_path).then( | |
219 | $.proxy(this.draw_notebook_list, this), |
|
219 | $.proxy(this.draw_notebook_list, this), | |
220 | function(error) { |
|
220 | function(error) { | |
221 | that.draw_notebook_list({content: []}, "Server error: " + error.message); |
|
221 | that.draw_notebook_list({content: []}, "Server error: " + error.message); | |
222 | } |
|
222 | } | |
223 | ); |
|
223 | ); | |
224 | }; |
|
224 | }; | |
225 |
|
225 | |||
226 | /** |
|
226 | /** | |
227 | * Draw the list of notebooks |
|
227 | * Draw the list of notebooks | |
228 | * @method draw_notebook_list |
|
228 | * @method draw_notebook_list | |
229 | * @param {Array} list An array of dictionaries representing files or |
|
229 | * @param {Array} list An array of dictionaries representing files or | |
230 | * directories. |
|
230 | * directories. | |
231 | * @param {String} error_msg An error message |
|
231 | * @param {String} error_msg An error message | |
232 | */ |
|
232 | */ | |
233 |
|
233 | |||
234 |
|
234 | |||
235 | var type_order = {'directory':0,'notebook':1,'file':2}; |
|
235 | var type_order = {'directory':0,'notebook':1,'file':2}; | |
236 |
|
236 | |||
237 | NotebookList.prototype.draw_notebook_list = function (list, error_msg) { |
|
237 | NotebookList.prototype.draw_notebook_list = function (list, error_msg) { | |
238 | // Remember what was selected before the refresh. |
|
238 | // Remember what was selected before the refresh. | |
239 | var selected_before = this.selected; |
|
239 | var selected_before = this.selected; | |
240 |
|
240 | |||
241 | list.content.sort(function(a, b) { |
|
241 | list.content.sort(function(a, b) { | |
242 | if (type_order[a['type']] < type_order[b['type']]) { |
|
242 | if (type_order[a['type']] < type_order[b['type']]) { | |
243 | return -1; |
|
243 | return -1; | |
244 | } |
|
244 | } | |
245 | if (type_order[a['type']] > type_order[b['type']]) { |
|
245 | if (type_order[a['type']] > type_order[b['type']]) { | |
246 | return 1; |
|
246 | return 1; | |
247 | } |
|
247 | } | |
248 | if (a['name'] < b['name']) { |
|
248 | if (a['name'] < b['name']) { | |
249 | return -1; |
|
249 | return -1; | |
250 | } |
|
250 | } | |
251 | if (a['name'] > b['name']) { |
|
251 | if (a['name'] > b['name']) { | |
252 | return 1; |
|
252 | return 1; | |
253 | } |
|
253 | } | |
254 | return 0; |
|
254 | return 0; | |
255 | }); |
|
255 | }); | |
256 | var message = error_msg || 'Notebook list empty.'; |
|
256 | var message = error_msg || 'Notebook list empty.'; | |
257 | var item = null; |
|
257 | var item = null; | |
258 | var model = null; |
|
258 | var model = null; | |
259 | var len = list.content.length; |
|
259 | var len = list.content.length; | |
260 | this.clear_list(); |
|
260 | this.clear_list(); | |
261 | var n_uploads = this.element.children('.list_item').length; |
|
261 | var n_uploads = this.element.children('.list_item').length; | |
262 | if (len === 0) { |
|
262 | if (len === 0) { | |
263 | item = this.new_item(0); |
|
263 | item = this.new_item(0); | |
264 | var span12 = item.children().first(); |
|
264 | var span12 = item.children().first(); | |
265 | span12.empty(); |
|
265 | span12.empty(); | |
266 | span12.append($('<div style="margin:auto;text-align:center;color:grey"/>').text(message)); |
|
266 | span12.append($('<div style="margin:auto;text-align:center;color:grey"/>').text(message)); | |
267 | } |
|
267 | } | |
268 | var path = this.notebook_path; |
|
268 | var path = this.notebook_path; | |
269 | var offset = n_uploads; |
|
269 | var offset = n_uploads; | |
270 | if (path !== '') { |
|
270 | if (path !== '') { | |
271 | item = this.new_item(offset, false); |
|
271 | item = this.new_item(offset, false); | |
272 | model = { |
|
272 | model = { | |
273 | type: 'directory', |
|
273 | type: 'directory', | |
274 | name: '..', |
|
274 | name: '..', | |
275 | path: utils.url_path_split(path)[0], |
|
275 | path: utils.url_path_split(path)[0], | |
276 | }; |
|
276 | }; | |
277 | this.add_link(model, item); |
|
277 | this.add_link(model, item); | |
278 | offset += 1; |
|
278 | offset += 1; | |
279 | } |
|
279 | } | |
280 | for (var i=0; i<len; i++) { |
|
280 | for (var i=0; i<len; i++) { | |
281 | model = list.content[i]; |
|
281 | model = list.content[i]; | |
282 | item = this.new_item(i+offset, true); |
|
282 | item = this.new_item(i+offset, true); | |
283 | this.add_link(model, item); |
|
283 | this.add_link(model, item); | |
284 | } |
|
284 | } | |
285 | // Trigger an event when we've finished drawing the notebook list. |
|
285 | // Trigger an event when we've finished drawing the notebook list. | |
286 | events.trigger('draw_notebook_list.NotebookList'); |
|
286 | events.trigger('draw_notebook_list.NotebookList'); | |
287 |
|
287 | |||
288 | // Reselect the items that were selected before. Notify listeners |
|
288 | // Reselect the items that were selected before. Notify listeners | |
289 | // that the selected items may have changed. O(n^2) operation. |
|
289 | // that the selected items may have changed. O(n^2) operation. | |
290 | selected_before.forEach(function(item) { |
|
290 | selected_before.forEach(function(item) { | |
291 | var list_items = $('.list_item'); |
|
291 | var list_items = $('.list_item'); | |
292 | for (var i=0; i<list_items.length; i++) { |
|
292 | for (var i=0; i<list_items.length; i++) { | |
293 | var $list_item = $(list_items[i]); |
|
293 | var $list_item = $(list_items[i]); | |
294 | if ($list_item.data('path') == item.path) { |
|
294 | if ($list_item.data('path') == item.path) { | |
295 | $list_item.find('input[type=checkbox]').prop('checked', true); |
|
295 | $list_item.find('input[type=checkbox]').prop('checked', true); | |
296 | break; |
|
296 | break; | |
297 | } |
|
297 | } | |
298 | } |
|
298 | } | |
299 | }); |
|
299 | }); | |
300 | this._selection_changed(); |
|
300 | this._selection_changed(); | |
301 | }; |
|
301 | }; | |
302 |
|
302 | |||
303 |
|
303 | |||
304 | /** |
|
304 | /** | |
305 | * Creates a new item. |
|
305 | * Creates a new item. | |
306 | * @param {integer} index |
|
306 | * @param {integer} index | |
307 | * @param {boolean} [selectable] - tristate, undefined: don't draw checkbox, |
|
307 | * @param {boolean} [selectable] - tristate, undefined: don't draw checkbox, | |
308 | * false: don't draw checkbox but pad |
|
308 | * false: don't draw checkbox but pad | |
309 | * where it should be, true: draw checkbox. |
|
309 | * where it should be, true: draw checkbox. | |
310 | * @return {JQuery} row |
|
310 | * @return {JQuery} row | |
311 | */ |
|
311 | */ | |
312 | NotebookList.prototype.new_item = function (index, selectable) { |
|
312 | NotebookList.prototype.new_item = function (index, selectable) { | |
313 | var row = $('<div/>') |
|
313 | var row = $('<div/>') | |
314 | .addClass("list_item") |
|
314 | .addClass("list_item") | |
315 | .addClass("row"); |
|
315 | .addClass("row"); | |
316 |
|
316 | |||
317 | var item = $("<div/>") |
|
317 | var item = $("<div/>") | |
318 | .addClass("col-md-12") |
|
318 | .addClass("col-md-12") | |
319 | .appendTo(row); |
|
319 | .appendTo(row); | |
320 |
|
320 | |||
321 | var checkbox; |
|
321 | var checkbox; | |
322 | if (selectable !== undefined) { |
|
322 | if (selectable !== undefined) { | |
323 | checkbox = $('<input/>') |
|
323 | checkbox = $('<input/>') | |
324 | .attr('type', 'checkbox') |
|
324 | .attr('type', 'checkbox') | |
325 | .attr('title', 'Click here to rename, delete, etc.') |
|
325 | .attr('title', 'Click here to rename, delete, etc.') | |
326 | .appendTo(item); |
|
326 | .appendTo(item); | |
327 | } |
|
327 | } | |
328 |
|
328 | |||
329 | $('<i/>') |
|
329 | $('<i/>') | |
330 | .addClass('item_icon') |
|
330 | .addClass('item_icon') | |
331 | .appendTo(item); |
|
331 | .appendTo(item); | |
332 |
|
332 | |||
333 | var link = $("<a/>") |
|
333 | var link = $("<a/>") | |
334 | .addClass("item_link") |
|
334 | .addClass("item_link") | |
335 | .appendTo(item); |
|
335 | .appendTo(item); | |
336 |
|
336 | |||
337 | $("<span/>") |
|
337 | $("<span/>") | |
338 | .addClass("item_name") |
|
338 | .addClass("item_name") | |
339 | .appendTo(link); |
|
339 | .appendTo(link); | |
340 |
|
340 | |||
341 | if (selectable === false) { |
|
341 | if (selectable === false) { | |
342 | checkbox.css('visibility', 'hidden'); |
|
342 | checkbox.css('visibility', 'hidden'); | |
343 | } else if (selectable === true) { |
|
343 | } else if (selectable === true) { | |
344 | var that = this; |
|
344 | var that = this; | |
345 | row.click(function(e) { |
|
345 | row.click(function(e) { | |
346 | // toggle checkbox only if the click doesn't come from the checkbox or the link |
|
346 | // toggle checkbox only if the click doesn't come from the checkbox or the link | |
347 | if (!$(e.target).is('span[class=item_name]') && !$(e.target).is('input[type=checkbox]')) { |
|
347 | if (!$(e.target).is('span[class=item_name]') && !$(e.target).is('input[type=checkbox]')) { | |
348 | checkbox.prop('checked', !checkbox.prop('checked')); |
|
348 | checkbox.prop('checked', !checkbox.prop('checked')); | |
349 | } |
|
349 | } | |
350 | that._selection_changed(); |
|
350 | that._selection_changed(); | |
351 | }); |
|
351 | }); | |
352 | } |
|
352 | } | |
353 |
|
353 | |||
354 | var buttons = $('<div/>') |
|
354 | var buttons = $('<div/>') | |
355 | .addClass("item_buttons pull-right") |
|
355 | .addClass("item_buttons pull-right") | |
356 | .appendTo(item); |
|
356 | .appendTo(item); | |
357 |
|
357 | |||
358 | $('<div/>') |
|
358 | $('<div/>') | |
359 | .addClass('running-indicator') |
|
359 | .addClass('running-indicator') | |
360 | .text('Running') |
|
360 | .text('Running') | |
361 | .css('visibility', 'hidden') |
|
361 | .css('visibility', 'hidden') | |
362 | .appendTo(buttons); |
|
362 | .appendTo(buttons); | |
363 |
|
363 | |||
364 | if (index === -1) { |
|
364 | if (index === -1) { | |
365 | this.element.append(row); |
|
365 | this.element.append(row); | |
366 | } else { |
|
366 | } else { | |
367 | this.element.children().eq(index).after(row); |
|
367 | this.element.children().eq(index).after(row); | |
368 | } |
|
368 | } | |
369 | return row; |
|
369 | return row; | |
370 | }; |
|
370 | }; | |
371 |
|
371 | |||
372 |
|
372 | |||
373 | NotebookList.icons = { |
|
373 | NotebookList.icons = { | |
374 | directory: 'folder_icon', |
|
374 | directory: 'folder_icon', | |
375 | notebook: 'notebook_icon', |
|
375 | notebook: 'notebook_icon', | |
376 | file: 'file_icon', |
|
376 | file: 'file_icon', | |
377 | }; |
|
377 | }; | |
378 |
|
378 | |||
379 | NotebookList.uri_prefixes = { |
|
379 | NotebookList.uri_prefixes = { | |
380 | directory: 'tree', |
|
380 | directory: 'tree', | |
381 | notebook: 'notebooks', |
|
381 | notebook: 'notebooks', | |
382 | file: 'edit', |
|
382 | file: 'edit', | |
383 | }; |
|
383 | }; | |
384 |
|
384 | |||
385 | /** |
|
385 | /** | |
386 | * Select all items in the tree of specified type. |
|
386 | * Select all items in the tree of specified type. | |
387 | * selection_type : string among "select-all", "select-folders", "select-notebooks", "select-running-notebooks", "select-files" |
|
387 | * selection_type : string among "select-all", "select-folders", "select-notebooks", "select-running-notebooks", "select-files" | |
388 | * any other string (like "select-none") deselects all items |
|
388 | * any other string (like "select-none") deselects all items | |
389 | */ |
|
389 | */ | |
390 | NotebookList.prototype.select = function(selection_type) { |
|
390 | NotebookList.prototype.select = function(selection_type) { | |
391 | var that = this; |
|
391 | var that = this; | |
392 | $('.list_item').each(function(index, item) { |
|
392 | $('.list_item').each(function(index, item) { | |
393 | var item_type = $(item).data('type'); |
|
393 | var item_type = $(item).data('type'); | |
394 | var state = false; |
|
394 | var state = false; | |
395 | state = state || (selection_type === "select-all"); |
|
395 | state = state || (selection_type === "select-all"); | |
396 | state = state || (selection_type === "select-folders" && item_type === 'directory'); |
|
396 | state = state || (selection_type === "select-folders" && item_type === 'directory'); | |
397 | state = state || (selection_type === "select-notebooks" && item_type === 'notebook'); |
|
397 | state = state || (selection_type === "select-notebooks" && item_type === 'notebook'); | |
398 | state = state || (selection_type === "select-running-notebooks" && item_type === 'notebook' && that.sessions[$(item).data('path')] !== undefined); |
|
398 | state = state || (selection_type === "select-running-notebooks" && item_type === 'notebook' && that.sessions[$(item).data('path')] !== undefined); | |
399 | state = state || (selection_type === "select-files" && item_type === 'file'); |
|
399 | state = state || (selection_type === "select-files" && item_type === 'file'); | |
400 | $(item).find('input[type=checkbox]').prop('checked', state); |
|
400 | $(item).find('input[type=checkbox]').prop('checked', state); | |
401 | }); |
|
401 | }); | |
402 | this._selection_changed(); |
|
402 | this._selection_changed(); | |
403 | }; |
|
403 | }; | |
404 |
|
404 | |||
405 |
|
405 | |||
406 | /** |
|
406 | /** | |
407 | * Handles when any row selector checkbox is toggled. |
|
407 | * Handles when any row selector checkbox is toggled. | |
408 | */ |
|
408 | */ | |
409 | NotebookList.prototype._selection_changed = function() { |
|
409 | NotebookList.prototype._selection_changed = function() { | |
410 | // Use a JQuery selector to find each row with a checked checkbox. If |
|
410 | // Use a JQuery selector to find each row with a checked checkbox. If | |
411 | // we decide to add more checkboxes in the future, this code will need |
|
411 | // we decide to add more checkboxes in the future, this code will need | |
412 | // to be changed to distinguish which checkbox is the row selector. |
|
412 | // to be changed to distinguish which checkbox is the row selector. | |
413 | var selected = []; |
|
413 | var selected = []; | |
414 | var has_running_notebook = false; |
|
414 | var has_running_notebook = false; | |
415 | var has_directory = false; |
|
415 | var has_directory = false; | |
416 | var has_file = false; |
|
416 | var has_file = false; | |
417 | var that = this; |
|
417 | var that = this; | |
418 | var checked = 0; |
|
418 | var checked = 0; | |
419 | $('.list_item :checked').each(function(index, item) { |
|
419 | $('.list_item :checked').each(function(index, item) { | |
420 | var parent = $(item).parent().parent(); |
|
420 | var parent = $(item).parent().parent(); | |
421 |
|
421 | |||
422 | // If the item doesn't have an upload button, isn't the |
|
422 | // If the item doesn't have an upload button, isn't the | |
423 | // breadcrumbs and isn't the parent folder '..', then it can be selected. |
|
423 | // breadcrumbs and isn't the parent folder '..', then it can be selected. | |
424 | // Breadcrumbs path == ''. |
|
424 | // Breadcrumbs path == ''. | |
425 | if (parent.find('.upload_button').length === 0 && parent.data('path') !== '' && parent.data('path') !== utils.url_path_split(that.notebook_path)[0]) { |
|
425 | if (parent.find('.upload_button').length === 0 && parent.data('path') !== '' && parent.data('path') !== utils.url_path_split(that.notebook_path)[0]) { | |
426 | checked++; |
|
426 | checked++; | |
427 | selected.push({ |
|
427 | selected.push({ | |
428 | name: parent.data('name'), |
|
428 | name: parent.data('name'), | |
429 | path: parent.data('path'), |
|
429 | path: parent.data('path'), | |
430 | type: parent.data('type') |
|
430 | type: parent.data('type') | |
431 | }); |
|
431 | }); | |
432 |
|
432 | |||
433 | // Set flags according to what is selected. Flags are later |
|
433 | // Set flags according to what is selected. Flags are later | |
434 | // used to decide which action buttons are visible. |
|
434 | // used to decide which action buttons are visible. | |
435 | has_running_notebook = has_running_notebook || |
|
435 | has_running_notebook = has_running_notebook || | |
436 | (parent.data('type') == 'notebook' && that.sessions[parent.data('path')] !== undefined); |
|
436 | (parent.data('type') == 'notebook' && that.sessions[parent.data('path')] !== undefined); | |
437 | has_file = has_file || parent.data('type') == 'file'; |
|
437 | has_file = has_file || parent.data('type') == 'file'; | |
438 | has_directory = has_directory || parent.data('type') == 'directory'; |
|
438 | has_directory = has_directory || parent.data('type') == 'directory'; | |
439 | } |
|
439 | } | |
440 | }); |
|
440 | }); | |
441 | this.selected = selected; |
|
441 | this.selected = selected; | |
442 |
|
442 | |||
443 |
// Rename is only visible when one item is selected |
|
443 | // Rename is only visible when one item is selected, and it is not a running notebook | |
444 | if (selected.length==1) { |
|
444 | if (selected.length==1 && !has_running_notebook) { | |
445 | $('.rename-button').css('display', 'inline-block'); |
|
445 | $('.rename-button').css('display', 'inline-block'); | |
446 | } else { |
|
446 | } else { | |
447 | $('.rename-button').css('display', 'none'); |
|
447 | $('.rename-button').css('display', 'none'); | |
448 | } |
|
448 | } | |
449 |
|
449 | |||
450 | // Shutdown is only visible when one or more notebooks running notebooks |
|
450 | // Shutdown is only visible when one or more notebooks running notebooks | |
451 | // are selected and no non-notebook items are selected. |
|
451 | // are selected and no non-notebook items are selected. | |
452 | if (has_running_notebook && !(has_file || has_directory)) { |
|
452 | if (has_running_notebook && !(has_file || has_directory)) { | |
453 | $('.shutdown-button').css('display', 'inline-block'); |
|
453 | $('.shutdown-button').css('display', 'inline-block'); | |
454 | } else { |
|
454 | } else { | |
455 | $('.shutdown-button').css('display', 'none'); |
|
455 | $('.shutdown-button').css('display', 'none'); | |
456 | } |
|
456 | } | |
457 |
|
457 | |||
458 | // Duplicate isn't visible when a directory is selected. |
|
458 | // Duplicate isn't visible when a directory is selected. | |
459 | if (selected.length > 0 && !has_directory) { |
|
459 | if (selected.length > 0 && !has_directory) { | |
460 | $('.duplicate-button').css('display', 'inline-block'); |
|
460 | $('.duplicate-button').css('display', 'inline-block'); | |
461 | } else { |
|
461 | } else { | |
462 | $('.duplicate-button').css('display', 'none'); |
|
462 | $('.duplicate-button').css('display', 'none'); | |
463 | } |
|
463 | } | |
464 |
|
464 | |||
465 | // Delete is visible if one or more items are selected. |
|
465 | // Delete is visible if one or more items are selected. | |
466 | if (selected.length > 0) { |
|
466 | if (selected.length > 0) { | |
467 | $('.delete-button').css('display', 'inline-block'); |
|
467 | $('.delete-button').css('display', 'inline-block'); | |
468 | } else { |
|
468 | } else { | |
469 | $('.delete-button').css('display', 'none'); |
|
469 | $('.delete-button').css('display', 'none'); | |
470 | } |
|
470 | } | |
471 |
|
471 | |||
472 | // If all of the items are selected, show the selector as checked. If |
|
472 | // If all of the items are selected, show the selector as checked. If | |
473 | // some of the items are selected, show it as checked. Otherwise, |
|
473 | // some of the items are selected, show it as checked. Otherwise, | |
474 | // uncheck it. |
|
474 | // uncheck it. | |
475 | var total = 0; |
|
475 | var total = 0; | |
476 | $('.list_item input[type=checkbox]').each(function(index, item) { |
|
476 | $('.list_item input[type=checkbox]').each(function(index, item) { | |
477 | var parent = $(item).parent().parent(); |
|
477 | var parent = $(item).parent().parent(); | |
478 | // If the item doesn't have an upload button and it's not the |
|
478 | // If the item doesn't have an upload button and it's not the | |
479 | // breadcrumbs, it can be selected. Breadcrumbs path == ''. |
|
479 | // breadcrumbs, it can be selected. Breadcrumbs path == ''. | |
480 | if (parent.find('.upload_button').length === 0 && parent.data('path') !== '' && parent.data('path') !== utils.url_path_split(that.notebook_path)[0]) { |
|
480 | if (parent.find('.upload_button').length === 0 && parent.data('path') !== '' && parent.data('path') !== utils.url_path_split(that.notebook_path)[0]) { | |
481 | total++; |
|
481 | total++; | |
482 | } |
|
482 | } | |
483 | }); |
|
483 | }); | |
484 | if (checked === 0) { |
|
484 | if (checked === 0) { | |
485 | $('#tree-selector input[type=checkbox]')[0].indeterminate = false; |
|
485 | $('#tree-selector input[type=checkbox]')[0].indeterminate = false; | |
486 | $('#tree-selector input[type=checkbox]').prop('checked', false); |
|
486 | $('#tree-selector input[type=checkbox]').prop('checked', false); | |
487 | } else if (checked === total) { |
|
487 | } else if (checked === total) { | |
488 | $('#tree-selector input[type=checkbox]')[0].indeterminate = false; |
|
488 | $('#tree-selector input[type=checkbox]')[0].indeterminate = false; | |
489 | $('#tree-selector input[type=checkbox]').prop('checked', true); |
|
489 | $('#tree-selector input[type=checkbox]').prop('checked', true); | |
490 | } else { |
|
490 | } else { | |
491 | $('#tree-selector input[type=checkbox]').prop('checked', false); |
|
491 | $('#tree-selector input[type=checkbox]').prop('checked', false); | |
492 | $('#tree-selector input[type=checkbox]')[0].indeterminate = true; |
|
492 | $('#tree-selector input[type=checkbox]')[0].indeterminate = true; | |
493 | } |
|
493 | } | |
494 | // Update total counter |
|
494 | // Update total counter | |
495 | $('#counter-select-all').html(checked===0 ? ' ' : checked); |
|
495 | $('#counter-select-all').html(checked===0 ? ' ' : checked); | |
496 | }; |
|
496 | }; | |
497 |
|
497 | |||
498 | NotebookList.prototype.add_link = function (model, item) { |
|
498 | NotebookList.prototype.add_link = function (model, item) { | |
499 | var path = model.path, |
|
499 | var path = model.path, | |
500 | name = model.name; |
|
500 | name = model.name; | |
501 | var running = (model.type == 'notebook' && this.sessions[path] !== undefined); |
|
501 | var running = (model.type == 'notebook' && this.sessions[path] !== undefined); | |
502 |
|
502 | |||
503 | item.data('name', name); |
|
503 | item.data('name', name); | |
504 | item.data('path', path); |
|
504 | item.data('path', path); | |
505 | item.data('type', model.type); |
|
505 | item.data('type', model.type); | |
506 | item.find(".item_name").text(name); |
|
506 | item.find(".item_name").text(name); | |
507 | var icon = NotebookList.icons[model.type]; |
|
507 | var icon = NotebookList.icons[model.type]; | |
508 | if (running) { |
|
508 | if (running) { | |
509 | icon = 'running_' + icon; |
|
509 | icon = 'running_' + icon; | |
510 | } |
|
510 | } | |
511 | var uri_prefix = NotebookList.uri_prefixes[model.type]; |
|
511 | var uri_prefix = NotebookList.uri_prefixes[model.type]; | |
512 | item.find(".item_icon").addClass(icon).addClass('icon-fixed-width'); |
|
512 | item.find(".item_icon").addClass(icon).addClass('icon-fixed-width'); | |
513 | var link = item.find("a.item_link") |
|
513 | var link = item.find("a.item_link") | |
514 | .attr('href', |
|
514 | .attr('href', | |
515 | utils.url_join_encode( |
|
515 | utils.url_join_encode( | |
516 | this.base_url, |
|
516 | this.base_url, | |
517 | uri_prefix, |
|
517 | uri_prefix, | |
518 | path |
|
518 | path | |
519 | ) |
|
519 | ) | |
520 | ); |
|
520 | ); | |
521 |
|
521 | |||
522 | item.find(".item_buttons .running-indicator").css('visibility', running ? '' : 'hidden'); |
|
522 | item.find(".item_buttons .running-indicator").css('visibility', running ? '' : 'hidden'); | |
523 |
|
523 | |||
524 | // directory nav doesn't open new tabs |
|
524 | // directory nav doesn't open new tabs | |
525 | // files, notebooks do |
|
525 | // files, notebooks do | |
526 | if (model.type !== "directory") { |
|
526 | if (model.type !== "directory") { | |
527 | link.attr('target',IPython._target); |
|
527 | link.attr('target',IPython._target); | |
528 | } |
|
528 | } | |
529 | }; |
|
529 | }; | |
530 |
|
530 | |||
531 |
|
531 | |||
532 | NotebookList.prototype.add_name_input = function (name, item, icon_type) { |
|
532 | NotebookList.prototype.add_name_input = function (name, item, icon_type) { | |
533 | item.data('name', name); |
|
533 | item.data('name', name); | |
534 | item.find(".item_icon").addClass(NotebookList.icons[icon_type]).addClass('icon-fixed-width'); |
|
534 | item.find(".item_icon").addClass(NotebookList.icons[icon_type]).addClass('icon-fixed-width'); | |
535 | item.find(".item_name").empty().append( |
|
535 | item.find(".item_name").empty().append( | |
536 | $('<input/>') |
|
536 | $('<input/>') | |
537 | .addClass("filename_input") |
|
537 | .addClass("filename_input") | |
538 | .attr('value', name) |
|
538 | .attr('value', name) | |
539 | .attr('size', '30') |
|
539 | .attr('size', '30') | |
540 | .attr('type', 'text') |
|
540 | .attr('type', 'text') | |
541 | .keyup(function(event){ |
|
541 | .keyup(function(event){ | |
542 | if(event.keyCode == 13){item.find('.upload_button').click();} |
|
542 | if(event.keyCode == 13){item.find('.upload_button').click();} | |
543 | else if(event.keyCode == 27){item.remove();} |
|
543 | else if(event.keyCode == 27){item.remove();} | |
544 | }) |
|
544 | }) | |
545 | ); |
|
545 | ); | |
546 | }; |
|
546 | }; | |
547 |
|
547 | |||
548 |
|
548 | |||
549 | NotebookList.prototype.add_file_data = function (data, item) { |
|
549 | NotebookList.prototype.add_file_data = function (data, item) { | |
550 | item.data('filedata', data); |
|
550 | item.data('filedata', data); | |
551 | }; |
|
551 | }; | |
552 |
|
552 | |||
553 |
|
553 | |||
554 | NotebookList.prototype.shutdown_selected = function() { |
|
554 | NotebookList.prototype.shutdown_selected = function() { | |
555 | var that = this; |
|
555 | var that = this; | |
556 | this.selected.forEach(function(item) { |
|
556 | this.selected.forEach(function(item) { | |
557 | if (item.type == 'notebook') { |
|
557 | if (item.type == 'notebook') { | |
558 | that.shutdown_notebook(item.path); |
|
558 | that.shutdown_notebook(item.path); | |
559 | } |
|
559 | } | |
560 | }); |
|
560 | }); | |
561 | }; |
|
561 | }; | |
562 |
|
562 | |||
563 | NotebookList.prototype.shutdown_notebook = function(path) { |
|
563 | NotebookList.prototype.shutdown_notebook = function(path) { | |
564 | var that = this; |
|
564 | var that = this; | |
565 | var settings = { |
|
565 | var settings = { | |
566 | processData : false, |
|
566 | processData : false, | |
567 | cache : false, |
|
567 | cache : false, | |
568 | type : "DELETE", |
|
568 | type : "DELETE", | |
569 | dataType : "json", |
|
569 | dataType : "json", | |
570 | success : function () { |
|
570 | success : function () { | |
571 | that.load_sessions(); |
|
571 | that.load_sessions(); | |
572 | }, |
|
572 | }, | |
573 | error : utils.log_ajax_error, |
|
573 | error : utils.log_ajax_error, | |
574 | }; |
|
574 | }; | |
575 |
|
575 | |||
576 | var session = this.sessions[path]; |
|
576 | var session = this.sessions[path]; | |
577 | if (session) { |
|
577 | if (session) { | |
578 | var url = utils.url_join_encode( |
|
578 | var url = utils.url_join_encode( | |
579 | this.base_url, |
|
579 | this.base_url, | |
580 | 'api/sessions', |
|
580 | 'api/sessions', | |
581 | session |
|
581 | session | |
582 | ); |
|
582 | ); | |
583 | $.ajax(url, settings); |
|
583 | $.ajax(url, settings); | |
584 | } |
|
584 | } | |
585 | }; |
|
585 | }; | |
586 |
|
586 | |||
587 | NotebookList.prototype.rename_selected = function() { |
|
587 | NotebookList.prototype.rename_selected = function() { | |
588 | if (this.selected.length != 1) return; |
|
588 | if (this.selected.length != 1) return; | |
589 |
|
589 | |||
590 | var that = this; |
|
590 | var that = this; | |
591 | var path = this.selected[0].path; |
|
591 | var item_path = this.selected[0].path; | |
|
592 | var item_name = this.selected[0].name; | |||
|
593 | var item_type = this.selected[0].type; | |||
592 | var input = $('<input/>').attr('type','text').attr('size','25').addClass('form-control') |
|
594 | var input = $('<input/>').attr('type','text').attr('size','25').addClass('form-control') | |
593 |
.val( |
|
595 | .val(item_name); | |
594 | var dialog_body = $('<div/>').append( |
|
596 | var dialog_body = $('<div/>').append( | |
595 | $("<p/>").addClass("rename-message") |
|
597 | $("<p/>").addClass("rename-message") | |
596 |
.text('Enter a new |
|
598 | .text('Enter a new '+ item_type + ' name:') | |
597 | ).append( |
|
599 | ).append( | |
598 | $("<br/>") |
|
600 | $("<br/>") | |
599 | ).append(input); |
|
601 | ).append(input); | |
600 | var d = dialog.modal({ |
|
602 | var d = dialog.modal({ | |
601 |
title : "Rename |
|
603 | title : "Rename "+ item_type, | |
602 | body : dialog_body, |
|
604 | body : dialog_body, | |
603 | buttons : { |
|
605 | buttons : { | |
604 | OK : { |
|
606 | OK : { | |
605 | class: "btn-primary", |
|
607 | class: "btn-primary", | |
606 | click: function() { |
|
608 | click: function() { | |
607 | that.contents.rename(path, input.val()).then(function() { |
|
609 | that.contents.rename(item_path, utils.url_path_join(that.notebook_path, input.val())).then(function() { | |
608 | that.load_list(); |
|
610 | that.load_list(); | |
609 | }).catch(function(e) { |
|
611 | }).catch(function(e) { | |
610 | dialog.modal({ |
|
612 | dialog.modal({ | |
611 | title: "Rename Failed", |
|
613 | title: "Rename Failed", | |
612 | body: $('<div/>') |
|
614 | body: $('<div/>') | |
613 |
.text("An error occurred while renaming \"" + |
|
615 | .text("An error occurred while renaming \"" + item_name + "\" to \"" + input.val() + "\".") | |
614 | .append($('<div/>') |
|
616 | .append($('<div/>') | |
615 | .addClass('alert alert-danger') |
|
617 | .addClass('alert alert-danger') | |
616 | .text(e.message || e)), |
|
618 | .text(e.message || e)), | |
617 | buttons: { |
|
619 | buttons: { | |
618 | OK: {'class': 'btn-primary'} |
|
620 | OK: {'class': 'btn-primary'} | |
619 | } |
|
621 | } | |
620 | }); |
|
622 | }); | |
621 | console.warn('Error durring renaming :', e); |
|
623 | console.warn('Error durring renaming :', e); | |
622 | }); |
|
624 | }); | |
623 | } |
|
625 | } | |
624 | }, |
|
626 | }, | |
625 | Cancel : {} |
|
627 | Cancel : {} | |
626 | }, |
|
628 | }, | |
627 | open : function () { |
|
629 | open : function () { | |
628 | // Upon ENTER, click the OK button. |
|
630 | // Upon ENTER, click the OK button. | |
629 | input.keydown(function (event) { |
|
631 | input.keydown(function (event) { | |
630 | if (event.which === keyboard.keycodes.enter) { |
|
632 | if (event.which === keyboard.keycodes.enter) { | |
631 | d.find('.btn-primary').first().click(); |
|
633 | d.find('.btn-primary').first().click(); | |
632 | return false; |
|
634 | return false; | |
633 | } |
|
635 | } | |
634 | }); |
|
636 | }); | |
635 | input.focus().select(); |
|
637 | input.focus().select(); | |
636 | } |
|
638 | } | |
637 | }); |
|
639 | }); | |
638 | }; |
|
640 | }; | |
639 |
|
641 | |||
640 | NotebookList.prototype.delete_selected = function() { |
|
642 | NotebookList.prototype.delete_selected = function() { | |
641 | var message; |
|
643 | var message; | |
642 | if (this.selected.length == 1) { |
|
644 | if (this.selected.length == 1) { | |
643 | message = 'Are you sure you want to permanently delete: ' + this.selected[0].name + '?'; |
|
645 | message = 'Are you sure you want to permanently delete: ' + this.selected[0].name + '?'; | |
644 | } else { |
|
646 | } else { | |
645 | message = 'Are you sure you want to permanently delete the ' + this.selected.length + ' files/folders selected?'; |
|
647 | message = 'Are you sure you want to permanently delete the ' + this.selected.length + ' files/folders selected?'; | |
646 | } |
|
648 | } | |
647 | var that = this; |
|
649 | var that = this; | |
648 | dialog.modal({ |
|
650 | dialog.modal({ | |
649 | title : "Delete", |
|
651 | title : "Delete", | |
650 | body : message, |
|
652 | body : message, | |
651 | buttons : { |
|
653 | buttons : { | |
652 | Delete : { |
|
654 | Delete : { | |
653 | class: "btn-danger", |
|
655 | class: "btn-danger", | |
654 | click: function() { |
|
656 | click: function() { | |
655 | // Shutdown any/all selected notebooks before deleting |
|
657 | // Shutdown any/all selected notebooks before deleting | |
656 | // the files. |
|
658 | // the files. | |
657 | that.shutdown_selected(); |
|
659 | that.shutdown_selected(); | |
658 |
|
660 | |||
659 | // Delete selected. |
|
661 | // Delete selected. | |
660 | that.selected.forEach(function(item) { |
|
662 | that.selected.forEach(function(item) { | |
661 | that.contents.delete(item.path).then(function() { |
|
663 | that.contents.delete(item.path).then(function() { | |
662 | that.notebook_deleted(item.path); |
|
664 | that.notebook_deleted(item.path); | |
663 | }).catch(function(e) { |
|
665 | }).catch(function(e) { | |
664 | dialog.modal({ |
|
666 | dialog.modal({ | |
665 | title: "Delete Failed", |
|
667 | title: "Delete Failed", | |
666 | body: $('<div/>') |
|
668 | body: $('<div/>') | |
667 | .text("An error occurred while deleting \"" + item.path + "\".") |
|
669 | .text("An error occurred while deleting \"" + item.path + "\".") | |
668 | .append($('<div/>') |
|
670 | .append($('<div/>') | |
669 | .addClass('alert alert-danger') |
|
671 | .addClass('alert alert-danger') | |
670 | .text(e.message || e)), |
|
672 | .text(e.message || e)), | |
671 | buttons: { |
|
673 | buttons: { | |
672 | OK: {'class': 'btn-primary'} |
|
674 | OK: {'class': 'btn-primary'} | |
673 | } |
|
675 | } | |
674 | }); |
|
676 | }); | |
675 | console.warn('Error durring content deletion:', e); |
|
677 | console.warn('Error durring content deletion:', e); | |
676 | }); |
|
678 | }); | |
677 | }); |
|
679 | }); | |
678 | } |
|
680 | } | |
679 | }, |
|
681 | }, | |
680 | Cancel : {} |
|
682 | Cancel : {} | |
681 | } |
|
683 | } | |
682 | }); |
|
684 | }); | |
683 | }; |
|
685 | }; | |
684 |
|
686 | |||
685 | NotebookList.prototype.duplicate_selected = function() { |
|
687 | NotebookList.prototype.duplicate_selected = function() { | |
686 | var message; |
|
688 | var message; | |
687 | if (this.selected.length == 1) { |
|
689 | if (this.selected.length == 1) { | |
688 | message = 'Are you sure you want to duplicate: ' + this.selected[0].name + '?'; |
|
690 | message = 'Are you sure you want to duplicate: ' + this.selected[0].name + '?'; | |
689 | } else { |
|
691 | } else { | |
690 | message = 'Are you sure you want to duplicate the ' + this.selected.length + ' files selected?'; |
|
692 | message = 'Are you sure you want to duplicate the ' + this.selected.length + ' files selected?'; | |
691 | } |
|
693 | } | |
692 | var that = this; |
|
694 | var that = this; | |
693 | dialog.modal({ |
|
695 | dialog.modal({ | |
694 | title : "Duplicate", |
|
696 | title : "Duplicate", | |
695 | body : message, |
|
697 | body : message, | |
696 | buttons : { |
|
698 | buttons : { | |
697 | Duplicate : { |
|
699 | Duplicate : { | |
698 | class: "btn-primary", |
|
700 | class: "btn-primary", | |
699 | click: function() { |
|
701 | click: function() { | |
700 | that.selected.forEach(function(item) { |
|
702 | that.selected.forEach(function(item) { | |
701 | that.contents.copy(item.path, that.notebook_path).then(function () { |
|
703 | that.contents.copy(item.path, that.notebook_path).then(function () { | |
702 | that.load_list(); |
|
704 | that.load_list(); | |
703 | }).catch(function(e) { |
|
705 | }).catch(function(e) { | |
704 | dialog.modal({ |
|
706 | dialog.modal({ | |
705 | title: "Duplicate Failed", |
|
707 | title: "Duplicate Failed", | |
706 | body: $('<div/>') |
|
708 | body: $('<div/>') | |
707 | .text("An error occurred while duplicating \"" + item.path + "\".") |
|
709 | .text("An error occurred while duplicating \"" + item.path + "\".") | |
708 | .append($('<div/>') |
|
710 | .append($('<div/>') | |
709 | .addClass('alert alert-danger') |
|
711 | .addClass('alert alert-danger') | |
710 | .text(e.message || e)), |
|
712 | .text(e.message || e)), | |
711 | buttons: { |
|
713 | buttons: { | |
712 | OK: {'class': 'btn-primary'} |
|
714 | OK: {'class': 'btn-primary'} | |
713 | } |
|
715 | } | |
714 | }); |
|
716 | }); | |
715 | console.warn('Error durring content duplication', e); |
|
717 | console.warn('Error durring content duplication', e); | |
716 | }); |
|
718 | }); | |
717 | }); |
|
719 | }); | |
718 | } |
|
720 | } | |
719 | }, |
|
721 | }, | |
720 | Cancel : {} |
|
722 | Cancel : {} | |
721 | } |
|
723 | } | |
722 | }); |
|
724 | }); | |
723 | }; |
|
725 | }; | |
724 |
|
726 | |||
725 | NotebookList.prototype.notebook_deleted = function(path) { |
|
727 | NotebookList.prototype.notebook_deleted = function(path) { | |
726 | /** |
|
728 | /** | |
727 | * Remove the deleted notebook. |
|
729 | * Remove the deleted notebook. | |
728 | */ |
|
730 | */ | |
729 | var that = this; |
|
731 | var that = this; | |
730 | $( ":data(path)" ).each(function() { |
|
732 | $( ":data(path)" ).each(function() { | |
731 | var element = $(this); |
|
733 | var element = $(this); | |
732 | if (element.data("path") === path) { |
|
734 | if (element.data("path") === path) { | |
733 | element.remove(); |
|
735 | element.remove(); | |
734 | events.trigger('notebook_deleted.NotebookList'); |
|
736 | events.trigger('notebook_deleted.NotebookList'); | |
735 | that._selection_changed(); |
|
737 | that._selection_changed(); | |
736 | } |
|
738 | } | |
737 | }); |
|
739 | }); | |
738 | }; |
|
740 | }; | |
739 |
|
741 | |||
740 |
|
742 | |||
741 | NotebookList.prototype.add_upload_button = function (item) { |
|
743 | NotebookList.prototype.add_upload_button = function (item) { | |
742 | var that = this; |
|
744 | var that = this; | |
743 | var upload_button = $('<button/>').text("Upload") |
|
745 | var upload_button = $('<button/>').text("Upload") | |
744 | .addClass('btn btn-primary btn-xs upload_button') |
|
746 | .addClass('btn btn-primary btn-xs upload_button') | |
745 | .click(function (e) { |
|
747 | .click(function (e) { | |
746 | var filename = item.find('.item_name > input').val(); |
|
748 | var filename = item.find('.item_name > input').val(); | |
747 | var path = utils.url_path_join(that.notebook_path, filename); |
|
749 | var path = utils.url_path_join(that.notebook_path, filename); | |
748 | var filedata = item.data('filedata'); |
|
750 | var filedata = item.data('filedata'); | |
749 | var format = 'text'; |
|
751 | var format = 'text'; | |
750 | if (filename.length === 0 || filename[0] === '.') { |
|
752 | if (filename.length === 0 || filename[0] === '.') { | |
751 | dialog.modal({ |
|
753 | dialog.modal({ | |
752 | title : 'Invalid file name', |
|
754 | title : 'Invalid file name', | |
753 | body : "File names must be at least one character and not start with a dot", |
|
755 | body : "File names must be at least one character and not start with a dot", | |
754 | buttons : {'OK' : { 'class' : 'btn-primary' }} |
|
756 | buttons : {'OK' : { 'class' : 'btn-primary' }} | |
755 | }); |
|
757 | }); | |
756 | return false; |
|
758 | return false; | |
757 | } |
|
759 | } | |
758 | if (filedata instanceof ArrayBuffer) { |
|
760 | if (filedata instanceof ArrayBuffer) { | |
759 | // base64-encode binary file data |
|
761 | // base64-encode binary file data | |
760 | var bytes = ''; |
|
762 | var bytes = ''; | |
761 | var buf = new Uint8Array(filedata); |
|
763 | var buf = new Uint8Array(filedata); | |
762 | var nbytes = buf.byteLength; |
|
764 | var nbytes = buf.byteLength; | |
763 | for (var i=0; i<nbytes; i++) { |
|
765 | for (var i=0; i<nbytes; i++) { | |
764 | bytes += String.fromCharCode(buf[i]); |
|
766 | bytes += String.fromCharCode(buf[i]); | |
765 | } |
|
767 | } | |
766 | filedata = btoa(bytes); |
|
768 | filedata = btoa(bytes); | |
767 | format = 'base64'; |
|
769 | format = 'base64'; | |
768 | } |
|
770 | } | |
769 | var model = {}; |
|
771 | var model = {}; | |
770 |
|
772 | |||
771 | var name_and_ext = utils.splitext(filename); |
|
773 | var name_and_ext = utils.splitext(filename); | |
772 | var file_ext = name_and_ext[1]; |
|
774 | var file_ext = name_and_ext[1]; | |
773 | var content_type; |
|
775 | var content_type; | |
774 | if (file_ext === '.ipynb') { |
|
776 | if (file_ext === '.ipynb') { | |
775 | model.type = 'notebook'; |
|
777 | model.type = 'notebook'; | |
776 | model.format = 'json'; |
|
778 | model.format = 'json'; | |
777 | try { |
|
779 | try { | |
778 | model.content = JSON.parse(filedata); |
|
780 | model.content = JSON.parse(filedata); | |
779 | } catch (e) { |
|
781 | } catch (e) { | |
780 | dialog.modal({ |
|
782 | dialog.modal({ | |
781 | title : 'Cannot upload invalid Notebook', |
|
783 | title : 'Cannot upload invalid Notebook', | |
782 | body : "The error was: " + e, |
|
784 | body : "The error was: " + e, | |
783 | buttons : {'OK' : { |
|
785 | buttons : {'OK' : { | |
784 | 'class' : 'btn-primary', |
|
786 | 'class' : 'btn-primary', | |
785 | click: function () { |
|
787 | click: function () { | |
786 | item.remove(); |
|
788 | item.remove(); | |
787 | } |
|
789 | } | |
788 | }} |
|
790 | }} | |
789 | }); |
|
791 | }); | |
790 | console.warn('Error durring notebook uploading', e); |
|
792 | console.warn('Error durring notebook uploading', e); | |
791 | return false; |
|
793 | return false; | |
792 | } |
|
794 | } | |
793 | content_type = 'application/json'; |
|
795 | content_type = 'application/json'; | |
794 | } else { |
|
796 | } else { | |
795 | model.type = 'file'; |
|
797 | model.type = 'file'; | |
796 | model.format = format; |
|
798 | model.format = format; | |
797 | model.content = filedata; |
|
799 | model.content = filedata; | |
798 | content_type = 'application/octet-stream'; |
|
800 | content_type = 'application/octet-stream'; | |
799 | } |
|
801 | } | |
800 | filedata = item.data('filedata'); |
|
802 | filedata = item.data('filedata'); | |
801 |
|
803 | |||
802 | var on_success = function () { |
|
804 | var on_success = function () { | |
803 | item.removeClass('new-file'); |
|
805 | item.removeClass('new-file'); | |
804 | that.add_link(model, item); |
|
806 | that.add_link(model, item); | |
805 | that.session_list.load_sessions(); |
|
807 | that.session_list.load_sessions(); | |
806 | }; |
|
808 | }; | |
807 |
|
809 | |||
808 | var exists = false; |
|
810 | var exists = false; | |
809 | $.each(that.element.find('.list_item:not(.new-file)'), function(k,v){ |
|
811 | $.each(that.element.find('.list_item:not(.new-file)'), function(k,v){ | |
810 | if ($(v).data('name') === filename) { exists = true; return false; } |
|
812 | if ($(v).data('name') === filename) { exists = true; return false; } | |
811 | }); |
|
813 | }); | |
812 |
|
814 | |||
813 | if (exists) { |
|
815 | if (exists) { | |
814 | dialog.modal({ |
|
816 | dialog.modal({ | |
815 | title : "Replace file", |
|
817 | title : "Replace file", | |
816 | body : 'There is already a file named ' + filename + ', do you want to replace it?', |
|
818 | body : 'There is already a file named ' + filename + ', do you want to replace it?', | |
817 | buttons : { |
|
819 | buttons : { | |
818 | Overwrite : { |
|
820 | Overwrite : { | |
819 | class: "btn-danger", |
|
821 | class: "btn-danger", | |
820 | click: function () { |
|
822 | click: function () { | |
821 | that.contents.save(path, model).then(on_success); |
|
823 | that.contents.save(path, model).then(on_success); | |
822 | } |
|
824 | } | |
823 | }, |
|
825 | }, | |
824 | Cancel : { |
|
826 | Cancel : { | |
825 | click: function() { item.remove(); } |
|
827 | click: function() { item.remove(); } | |
826 | } |
|
828 | } | |
827 | } |
|
829 | } | |
828 | }); |
|
830 | }); | |
829 | } else { |
|
831 | } else { | |
830 | that.contents.save(path, model).then(on_success); |
|
832 | that.contents.save(path, model).then(on_success); | |
831 | } |
|
833 | } | |
832 |
|
834 | |||
833 | return false; |
|
835 | return false; | |
834 | }); |
|
836 | }); | |
835 | var cancel_button = $('<button/>').text("Cancel") |
|
837 | var cancel_button = $('<button/>').text("Cancel") | |
836 | .addClass("btn btn-default btn-xs") |
|
838 | .addClass("btn btn-default btn-xs") | |
837 | .click(function (e) { |
|
839 | .click(function (e) { | |
838 | item.remove(); |
|
840 | item.remove(); | |
839 | return false; |
|
841 | return false; | |
840 | }); |
|
842 | }); | |
841 | item.find(".item_buttons").empty() |
|
843 | item.find(".item_buttons").empty() | |
842 | .append(upload_button) |
|
844 | .append(upload_button) | |
843 | .append(cancel_button); |
|
845 | .append(cancel_button); | |
844 | }; |
|
846 | }; | |
845 |
|
847 | |||
846 |
|
848 | |||
847 | // Backwards compatability. |
|
849 | // Backwards compatability. | |
848 | IPython.NotebookList = NotebookList; |
|
850 | IPython.NotebookList = NotebookList; | |
849 |
|
851 | |||
850 | return {'NotebookList': NotebookList}; |
|
852 | return {'NotebookList': NotebookList}; | |
851 | }); |
|
853 | }); |
General Comments 0
You need to be logged in to leave comments.
Login now