-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
105 lines (91 loc) · 3.09 KB
/
app.js
File metadata and controls
105 lines (91 loc) · 3.09 KB
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
// Element references
var inputModel = document.querySelector('#input-model')
var outputModel = document.querySelector('#output-model')
var previewLoadingOverlay = document.querySelector('#preview-loading-overlay')
var furnitureId = document.querySelector('#furniture-id')
var modifierId = document.querySelector('#modifier-id')
var inputSnippetAframe = document.querySelector('#input-snippet-aframe')
var outputSnippetAframe = document.querySelector('#output-snippet-aframe')
//helpers
function hideElement (elem) {
elem.style.display = 'none'
}
function updateData3dView (entity, key) {
entity.setAttribute('io3d-data3d', 'key:'+ key)
}
function updateFurnitureView (entity, id) {
entity.setAttribute('io3d-furniture', 'id:'+ id)
}
function getKeyFromId (id) {
return io3d.furniture.getInfo(id).then(function (result) {
var url = result.data3dUrl
// Fixme get KEY from URL // 3dio.storage
return url.replace('https://storage.3d.io/', '')
})
}
function debounce(wait, immediate, func) {
var timeout;
return function() {
var context = this, args = arguments;
var later = function() {
timeout = null;
if (!immediate) func.apply(context, args);
};
var callNow = immediate && !timeout;
clearTimeout(timeout);
timeout = setTimeout(later, wait);
if (callNow) func.apply(context, args);
};
}
function furnitureIdChanged() {
furnitureId.style.backgroundColor = null
if(furnitureId.value.length !== 0) inputSnippetAframe.innerHTML = inputSnippetAframe.innerHTML.replace(/id:([^"'<]+)/gmi, 'id:'+furnitureId.value)
sendModifyRequest().catch(function onFailure (err) {
furnitureId.style.backgroundColor = "#FF9800"
console.log(err)
})
}
function sendModifyRequest() {
// Furniture ID empty
if (furnitureId.value.length === 0) {
return Promise.reject('No furniture ID provided')
}
// Verify furniture ID
return io3d.furniture.getInfo(furnitureId.value).then(function (info){
// Update input view
updateFurnitureView(inputModel, furnitureId.value)
// Modify 3d model and update output view
getKeyFromId(furnitureId.value)
.then(function (key) {
return io3d.fish.modify(key, getModifyOptions())
}).then(function onApiResponse (result) {
updateData3dView(outputModel, result)
outputSnippetAframe.innerHTML = outputSnippetAframe.innerHTML.replace(/key:([^"'<]+)/gmi, 'key:/'+result)
}).catch(function onFailure(err) {
console.log('Modify failed: ', err)
})
})
}
function getModifyOptions() {
var options = {}
if (modifierId.value) {
options.modifiers = [modifierId.value]
}
return options
}
// listeners
furnitureId.addEventListener('input', debounce(1000, false, furnitureIdChanged))
furnitureId.addEventListener('click', furnitureId.select)
modifierId.addEventListener('input', function () {
sendModifyRequest().catch(function (err) {
console.log('Modify failed: ', err)
})
})
// main
function main () {
//hideElement(previewLoadingOverlay)
io3d.utils.auth.getSession().then(function (result) {
if (!result.isAuthenticated) return io3d.utils.ui.login()
})
}
main()