-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
131 lines (131 loc) · 4.54 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
let app = new Vue({
el: '#app',
data: {
selected: 'resistance',
kind: 'deck',
assets: {},
item: 'back',
ctx: null,
style: null
},
created: function() {
this.loadStyle().then((link) => {
this.style = link
})
this.loadData()
},
methods: {
savePng: function() {
console.log(this.ctx)
saveSvgAsPng(this.ctx.node, this.fileName).then((result) => {
console.log('downloaded');
});
},
async loadData() {
let file = `/data/${this.selected}.json`
if (this.assets[this.selected]) return this.assets[this.selected]
try {
const response = await fetch(file)
const data = await response.json()
this.$set(this.assets, this.selected, data)
console.log('assets loaded:', file)
return data
} catch (err) {
this.$set(this.assets, this.selected, null)
console.log('error loading data:', file)
return err
}
},
async generateAsset() {
const config = this.config
await this.loadStyle()
await this.loadData()
let result = document.querySelector('#result')
if (result) result.innerHTML = ''
for (const key in config) {
if (config[key] === null) return
}
this.ctx = drawAsset('#result', config)
console.log('assets generated')
},
loadStyle() {
return new Promise((resolve, reject) => {
let path = `/style/${this.selected}.css`
let style = this.style || document.createElement('link')
style.type = 'text/css'
style.rel = 'stylesheet'
style.addEventListener('load', (ev) => {
console.log('stylesheet changed: ', path)
resolve(style)
})
style.addEventListener('error', (ev) => {
reject(ev)
})
style.href = path
document.head.appendChild(style)
})
}
},
computed: {
notSingle() {
return !['token', 'other'].includes(this.kind)
},
factionSpecific() {
return !['terrain', 'objectives'].includes(this.selected)
},
assetList() {
if (!this.assets[this.selected]) return []
const assetObject = this.assets[this.selected]
if (this.factionSpecific) {
if (this.kind == 'deck') {
let list = Array.from(assetObject.characters)
list.push(assetObject.fog)
return list
} else if (this.kind == 'vehicle') return assetObject.vehicles
else if (this.kind == 'token') {
return assetObject.vehicles.concat(assetObject.characters.filter(c => !c.rank || c.rank == 0))
}
} else {
const kind = ['front', 'back'].includes(this.kind) ? this.kind : 'front'
return assetObject[kind]
}
return []
},
config() {
let list = []
if (this.item == 'all') list = this.assetList
else if (this.item != 'back') {
if (this.assetList[this.item]) {
let elem = this.assetList[this.item]
elem.index = this.item
list = [elem]
}
else this.item = 0
} else if (this.kind == 'token') {
this.item = 0
}
const config = {
kind: this.kind,
selected: this.selected,
back: this.item == 'back',
list: list
}
return config
},
fileName() {
let title = [this.selected, this.kind]
switch(this.kind) {
case 'deck':
case 'vehicle':
switch(this.item) {
case 'back': title.push('back'); break
case 'all': break
default: title.push(this.assetList[this.item].name)
}
break
case 'token': title.push(this.assetList[this.item].name)
}
return title.join(' ').replaceAll(' ', '_') + '.png'
}
}
})