/
app.js
159 lines (133 loc) · 4.22 KB
/
app.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
import { WEBGL } from 'three/examples/jsm/WebGL.js';
import { Viewer } from './viewer.js';
import { SimpleDropzone } from 'simple-dropzone';
import { ValidationController } from './validation-controller.js';
import { queryString } from 'query-string';
if (!(window.File && window.FileReader && window.FileList && window.Blob)) {
console.error('The File APIs are not fully supported in this browser.');
} else if (!WEBGL.isWebGLAvailable()) {
console.error('WebGL is not supported in this browser.');
}
class App {
/**
* @param {Element} el
* @param {Location} location
*/
constructor (el, location) {
const hash = location.hash ? queryString.parse(location.hash) : {};
this.options = {
kiosk: Boolean(hash.kiosk),
model: hash.model || '',
preset: hash.preset || '',
cameraPosition: hash.cameraPosition
? hash.cameraPosition.split(',').map(Number)
: null
};
this.el = el;
this.viewer = null;
this.viewerEl = null;
this.spinnerEl = el.querySelector('.spinner');
this.dropEl = el.querySelector('.dropzone');
this.inputEl = el.querySelector('#file-input');
this.validationCtrl = new ValidationController(el);
this.createDropzone();
this.hideSpinner();
const options = this.options;
if (options.kiosk) {
const headerEl = document.querySelector('header');
headerEl.style.display = 'none';
}
if (options.model) {
this.view(options.model, '', new Map());
}
}
/**
* Sets up the drag-and-drop controller.
*/
createDropzone () {
const dropCtrl = new SimpleDropzone(this.dropEl, this.inputEl);
dropCtrl.on('drop', ({files}) => this.load(files));
dropCtrl.on('dropstart', () => this.showSpinner());
dropCtrl.on('droperror', () => this.hideSpinner());
}
/**
* Sets up the view manager.
* @return {Viewer}
*/
createViewer () {
this.viewerEl = document.createElement('div');
this.viewerEl.classList.add('viewer');
this.dropEl.innerHTML = '';
this.dropEl.appendChild(this.viewerEl);
this.viewer = new Viewer(this.viewerEl, this.options);
return this.viewer;
}
/**
* Loads a fileset provided by user action.
* @param {Map<string, File>} fileMap
*/
load (fileMap) {
let rootFile;
let rootPath;
Array.from(fileMap).forEach(([path, file]) => {
if (file.name.match(/\.(gltf|glb)$/)) {
rootFile = file;
rootPath = path.replace(file.name, '');
}
});
if (!rootFile) {
this.onError('No .gltf or .glb asset found.');
}
this.view(rootFile, rootPath, fileMap);
}
/**
* Passes a model to the viewer, given file and resources.
* @param {File|string} rootFile
* @param {string} rootPath
* @param {Map<string, File>} fileMap
*/
view (rootFile, rootPath, fileMap) {
if (this.viewer) this.viewer.clear();
const viewer = this.viewer || this.createViewer();
const fileURL = typeof rootFile === 'string'
? rootFile
: URL.createObjectURL(rootFile);
const cleanup = () => {
this.hideSpinner();
if (typeof rootFile === 'object') URL.revokeObjectURL(fileURL);
};
viewer
.load(fileURL, rootPath, fileMap)
.catch((e) => this.onError(e))
.then((gltf) => {
if (!this.options.kiosk) {
this.validationCtrl.validate(fileURL, rootPath, fileMap, gltf);
}
cleanup();
});
}
/**
* @param {Error} error
*/
onError (error) {
let message = (error||{}).message || error.toString();
if (message.match(/ProgressEvent/)) {
message = 'Unable to retrieve this file. Check JS console and browser network tab.';
} else if (message.match(/Unexpected token/)) {
message = `Unable to parse file content. Verify that this file is valid. Error: "${message}"`;
} else if (error && error.target && error.target instanceof Image) {
message = 'Missing texture: ' + error.target.src.split('/').pop();
}
window.alert(message);
console.error(error);
}
showSpinner () {
this.spinnerEl.style.display = '';
}
hideSpinner () {
this.spinnerEl.style.display = 'none';
}
}
document.addEventListener('DOMContentLoaded', () => {
const app = new App(document.body, location);
});