| lombardofr | 1e32006 | 2018-10-30 22:16:25 +0100 | [diff] [blame] | 1 | /* |
| 2 | Copyright 2018 EveryUP srl |
| 3 | |
| 4 | Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | you may not use this file except in compliance with the License. |
| 6 | You may obtain a copy of the License at |
| 7 | |
| 8 | http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | |
| 10 | Unless required by applicable law or agreed to in writing, software |
| 11 | distributed under the License is distributed on an BASIS, |
| 12 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | See the License for the specific language governing permissions and |
| 14 | limitations under the License. |
| 15 | */ |
| 16 | |
| lombardof | 9b8155e | 2018-06-03 18:04:42 +0200 | [diff] [blame] | 17 | var dropZone = document.getElementById('drop-zone'); |
| 18 | dropZone.ondrop = function (e) { |
| 19 | e.preventDefault(); |
| 20 | this.className = 'upload-drop-zone'; |
| 21 | create(e.dataTransfer.files, true); |
| 22 | }; |
| 23 | |
| 24 | dropZone.ondragover = function () { |
| 25 | this.className = 'upload-drop-zone drop'; |
| 26 | return false; |
| 27 | }; |
| 28 | |
| 29 | dropZone.ondragleave = function () { |
| 30 | this.className = 'upload-drop-zone'; |
| 31 | return false; |
| 32 | }; |
| 33 | |
| 34 | |
| 35 | function create(fs, dropzone) { |
| 36 | var id = $('.nav-tabs .active').attr('id'); |
| 37 | if (dropzone) id = 'file_li'; |
| 38 | var type, text; |
| 39 | var data = new FormData(); |
| 40 | switch (id) { |
| 41 | |
| 42 | case 'file_li': |
| 43 | type = 'file'; |
| 44 | |
| 45 | var files = dropzone ? fs : document.getElementById('js-upload-files').files; |
| 46 | if (!files || !files.length) { |
| 47 | files = document.getElementById('drop-zone').files; |
| 48 | if (!files || !files.length) { |
| 49 | alert("Select a file"); |
| 50 | return |
| 51 | } |
| 52 | } |
| 53 | console.log(files[0]) |
| 54 | var patt1 = /\.([0-9a-z]+)(?:[\?#]|$)/i; |
| 55 | console.log(files[0].name.match(patt1)); |
| 56 | var extension = files[0].name.substr(files[0].name.lastIndexOf('.') + 1); |
| 57 | console.log(extension); |
| 58 | if (!(extension == 'gz' )) { |
| 59 | alert("The file must be .tar.gz"); |
| 60 | return |
| 61 | } |
| 62 | |
| 63 | data.append('file', files[0]); |
| 64 | break; |
| 65 | } |
| 66 | data.append('csrfmiddlewaretoken', csrf_token); |
| 67 | data.append('type', type); |
| 68 | data.append('text', text); |
| 69 | data.append('id', '{{descriptor_id}}'); |
| 70 | console.log(text); |
| lombardofr | fbd4aef | 2018-10-06 14:11:43 +0200 | [diff] [blame] | 71 | var dialog = bootbox.dialog({ |
| 72 | message: '<div class="text-center"><i class="fa fa-spin fa-spinner"></i> Onboarding...</div>', |
| 73 | closeButton: true |
| 74 | }); |
| lombardof | 9b8155e | 2018-06-03 18:04:42 +0200 | [diff] [blame] | 75 | $.ajax({ |
| lombardof | 20d0550 | 2018-06-07 15:50:03 +0200 | [diff] [blame] | 76 | url: new_desc_url, |
| lombardof | 9b8155e | 2018-06-03 18:04:42 +0200 | [diff] [blame] | 77 | type: 'POST', |
| 78 | data: data, |
| 79 | cache: false, |
| 80 | contentType: false, |
| 81 | processData: false, |
| 82 | success: function (result) { |
| lombardofr | fbd4aef | 2018-10-06 14:11:43 +0200 | [diff] [blame] | 83 | dialog.modal('hide'); |
| 84 | refreshTable(); |
| lombardof | 9b8155e | 2018-06-03 18:04:42 +0200 | [diff] [blame] | 85 | }, |
| 86 | error: function (result) { |
| lombardofr | fbd4aef | 2018-10-06 14:11:43 +0200 | [diff] [blame] | 87 | dialog.modal('hide'); |
| lombardof | 9b8155e | 2018-06-03 18:04:42 +0200 | [diff] [blame] | 88 | showAlert(result); |
| 89 | } |
| 90 | }); |
| 91 | } |