Onboarding drag and drop box in VNF packages and NS packages section
[osm/LW-UI.git] / static / src / projecthandler / onboard_package.js
1 var dropZone = document.getElementById('drop-zone');
2 dropZone.ondrop = function (e) {
3 e.preventDefault();
4 this.className = 'upload-drop-zone';
5 create(e.dataTransfer.files, true);
6 };
7
8 dropZone.ondragover = function () {
9 this.className = 'upload-drop-zone drop';
10 return false;
11 };
12
13 dropZone.ondragleave = function () {
14 this.className = 'upload-drop-zone';
15 return false;
16 };
17
18
19 function create(fs, dropzone) {
20 var id = $('.nav-tabs .active').attr('id');
21 if (dropzone) id = 'file_li';
22 var type, text;
23 var data = new FormData();
24 switch (id) {
25
26 case 'file_li':
27 type = 'file';
28
29 var files = dropzone ? fs : document.getElementById('js-upload-files').files;
30 if (!files || !files.length) {
31 files = document.getElementById('drop-zone').files;
32 if (!files || !files.length) {
33 alert("Select a file");
34 return
35 }
36 }
37 console.log(files[0])
38 var patt1 = /\.([0-9a-z]+)(?:[\?#]|$)/i;
39 console.log(files[0].name.match(patt1));
40 var extension = files[0].name.substr(files[0].name.lastIndexOf('.') + 1);
41 console.log(extension);
42 if (!(extension == 'gz' )) {
43 alert("The file must be .tar.gz");
44 return
45 }
46
47 data.append('file', files[0]);
48 break;
49 }
50 data.append('csrfmiddlewaretoken', csrf_token);
51 data.append('type', type);
52 data.append('text', text);
53 data.append('id', '{{descriptor_id}}');
54 console.log(text);
55 $.ajax({
56 url: "new",
57 type: 'POST',
58 data: data,
59 cache: false,
60 contentType: false,
61 processData: false,
62 success: function (result) {
63 console.log(result);
64
65 window.location.href = descr_list_url
66
67 },
68 error: function (result) {
69 showAlert(result);
70 }
71 });
72 }