Rift.IO OSM R1 Initial Submission
[osm/UI.git] / skyquake / plugins / composer / src / src / libraries / CatalogPackageManagerUploadDropZone.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
28 /**
29 * This class is responsible for wiring the DropZone.js to our React actions.
30 */
31
32 const ACTIONS = {
33 onboard: 'onboard',
34 update: 'update'
35 };
36
37 function getCatalogPackageManagerServerOrigin() {
38 return Utils.getSearchParams(window.location).upload_server + ':4567';
39 }
40
41 function initializeDropZone(element = '#dropzone', button = false, action = ACTIONS.onboard) {
42 DropZone.autoDiscover = false;
43 return new DropZone(element, {
44 paramName: 'descriptor',
45 url() {
46 if (action === ACTIONS.update) {
47 return getCatalogPackageManagerServerOrigin() + '/api/update';
48 }
49 return getCatalogPackageManagerServerOrigin() + '/api/upload';
50 },
51 maxFilesize: 10000000000,
52 clickable: button,
53 acceptedFiles: 'application/octet-stream,.gz,.tar.gz,.tar,.qcow,.qcow2,.iso,application/yaml,.yaml,application/json,.json,application/zip,.zip,application/x-rar-compressed,.rar,application/x-7z-compressed,.7z,application/x-bzip,.bz,application/x-bzip2,.bz2,application/x-gtar,.gtar',
54 autoProcessQueue: true,
55 previewTemplate: '',
56 sending(file, xhr, formData) {
57 // NOTE ie11 does not get this form data
58 formData.append('id', file.id);
59 },
60 error(file, errorMessage) {
61 const response = {
62 state: file,
63 data: {
64 status: 'upload-error',
65 message: errorMessage
66 }
67 };
68 CatalogPackageManagerActions.uploadCatalogPackageError(response);
69 },
70 success(file) {
71 const data = JSON.parse(file.xhr.responseText);
72 data.status = 'upload-success';
73 const response = {
74 state: file,
75 data: data
76 };
77 CatalogPackageManagerActions.uploadCatalogPackageStatusUpdated(response);
78 },
79 addedfile(file) {
80 file.id = file.id || guid();
81 file.riftAction = action;
82 CatalogPackageManagerActions.uploadCatalogPackage(file);
83 },
84 thumbnail(file, dataUrl) {
85 const response = {
86 state: file,
87 data: {
88 status: 'upload-thumbnail',
89 dataUrl: dataUrl
90 }
91 };
92 CatalogPackageManagerActions.uploadCatalogPackageStatusUpdated(response);
93 },
94 uploadprogress(file, progress, bytesSent) {
95 const response = {
96 state: file,
97 data: {
98 status: 'upload-progress',
99 progress: progress,
100 bytesSent: bytesSent
101 }
102 };
103 CatalogPackageManagerActions.uploadCatalogPackageStatusUpdated(response);
104 }
105 });
106 }
107
108 export default class CatalogPackageManagerUploadDropZone {
109
110 constructor(element, button, action) {
111 this.dropZone = initializeDropZone(element, button, action);
112 }
113
114 static get ACTIONS() {
115 return ACTIONS;
116 }
117
118 on(eventName, eventCallback) {
119 this.dropZone.on(eventName, eventCallback);
120 }
121
122 }