Package Mananger
[osm/UI.git] / skyquake / plugins / composer / src / src / libraries / FileManagerUploadDropZone.js
1
2 /*
3 *
4 * Copyright 2016 RIFT.IO Inc
5 *
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 *
18 */
19 /**
20 * Created by onvelocity on 10/27/15.
21 */
22
23 import guid from '../libraries/guid'
24 import DropZone from 'dropzone'
25 import Utils from '../libraries/utils'
26 import CatalogPackageManagerActions from '../actions/CatalogPackageManagerActions';
27 import FileManagerActions from '../components/filemanager/FileManagerActions.js';
28
29 /**
30 * This class is responsible for wiring the DropZone.js to our React actions.
31 */
32
33 const ACTIONS = {
34 onboard: 'onboard',
35 update: 'update'
36 };
37
38 function getCatalogPackageManagerServerOrigin() {
39 // return Utils.getSearchParams(window.location).upload_server + ':4567';
40 return window.location.origin;
41 }
42
43 function initializeDropZone(element = '#dropzone', button = false, action = ACTIONS.onboard, type, id, path) {
44 let Auth = 'Basic ' + window.sessionStorage.getItem("auth");
45 let dev_download_server = Utils.getSearchParams(window.location).dev_download_server;
46 DropZone.autoDiscover = false;
47 return new DropZone(element, {
48 paramName: 'package',
49 url() {
50 if (action === ACTIONS.update) {
51 return getCatalogPackageManagerServerOrigin() + '/api/update';
52 }
53 return getCatalogPackageManagerServerOrigin() + '/composer/api/file-manager?api_server=' + Utils.getSearchParams(window.location).api_server + '&package_type=' + type + '&package_id=' + id + '&package_path=' + path + ( dev_download_server ? '&dev_download_server=' + dev_download_server : '');
54 },
55 headers: {
56 'Authorization': Auth
57 },
58 maxFilesize: 10000000000,
59 clickable: button,
60 autoProcessQueue: true,
61 previewTemplate: '',
62 sending(file, xhr, formData) {
63 // NOTE ie11 does not get this form data
64 formData.append('id', file.id);
65 FileManagerActions.addFileSuccess({
66 fileName: file.name,
67 path: path
68 })
69 },
70 error(file, errorMessage) {
71 const response = {
72 state: file,
73 data: {
74 status: 'upload-error',
75 message: errorMessage
76 }
77 };
78 CatalogPackageManagerActions.uploadCatalogPackageError(response);
79 },
80 success(file) {
81 const data = JSON.parse(file.xhr.responseText);
82 data.status = 'upload-success';
83 const response = {
84 state: file,
85 data: data
86 };
87 //CatalogPackageManagerActions.uploadCatalogPackageStatusUpdated(response);
88 },
89 addedfile(file) {
90 file.id = file.id || guid();
91 file.riftAction = action;
92 //CatalogPackageManagerActions.uploadCatalogPackage(file);
93 },
94 thumbnail(file, dataUrl) {
95 const response = {
96 state: file,
97 data: {
98 status: 'upload-thumbnail',
99 dataUrl: dataUrl
100 }
101 };
102 //CatalogPackageManagerActions.uploadCatalogPackageStatusUpdated(response);
103 },
104 uploadprogress(file, progress, bytesSent) {
105 // FileManagerActions.addFileSuccess({
106 // path: path,
107 // fileName: file.name
108 // });
109 const response = {
110 state: file,
111 data: {
112 status: 'upload-progress',
113 progress: progress,
114 bytesSent: bytesSent
115 }
116 };
117 //CatalogPackageManagerActions.uploadCatalogPackageStatusUpdated(response);
118 }
119 });
120 }
121
122 export default class CatalogPackageManagerUploadDropZone {
123
124 constructor(element, button, action, type, id, path) {
125 this.dropZone = initializeDropZone(element, button, action, type, id, path);
126 }
127
128 static get ACTIONS() {
129 return ACTIONS;
130 }
131
132 on(eventName, eventCallback) {
133 this.dropZone.on(eventName, eventCallback);
134 }
135
136 }