forked from ziageek/smartvcard
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAttachment.vue
executable file
·155 lines (152 loc) · 4.31 KB
/
Attachment.vue
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
<template>
<div class="flex mt-6">
<transition name="list">
<Cropper
v-if="showCropper"
:src="tempURL"
@closeCropper="closeCropper"
:content="content"
:mime="mime"
:resizeImage="resizeImage"
/>
</transition>
<div class="flex flex-wrap items-center">
<img
class="w-12 h-12 rounded object-contain"
v-if="imageAttached"
:src="content[type].url"
:title="`${type == 'logo' ? 'Brand logo' : 'Card holder\'s photo'}`"
/>
<button
v-if="!imageAttached"
class="p-3 rounded bg-gray-700 cursor-pointer hover:bg-gray-600 focus:bg-gray-600 transition-colors duration-200 focus:outline-none"
@click="attachFile(null, type, false)"
:class="
dragOver ? 'bg-gray-900 outline-white' : 'bg-gray-700 border-none'
"
:aria-label="label"
@drop.prevent="attachFile($event, type, true)"
@dragleave.prevent.self="dragOver = false"
@dragover.prevent.self="dragOver = true"
>
<input
:ref="`import${type}`"
type="file"
:accept="`.png,.jpg,.jpeg,.gif,.webp${type == 'logo' ? ',.svg' : ''}`"
v-show="false"
@change="fileLoaded($event, type, false)"
@click="$event.target.files = null"
/>
<div
class="w-6 h-6 pointer-events-none"
v-html="require(`~/assets/icons/add.svg?include`)"
></div>
</button>
<p v-if="!imageAttached" class="ml-3 leading-none">
{{ label
}}<span class="text-sm text-gray-400"><br />{{ description }}</span>
</p>
<button
v-else
class="p-1 m-2 flex-shrink-0 focus:outline-none rounded hover:bg-gray-700 focus:bg-gray-700 transition-colors duration-200"
@click="content[type].url = null"
:aria-label="`Remove ${type}`"
:title="`Remove ${type}`"
>
<div
class="w-6 h-6"
v-html="require(`~/assets/icons/x.svg?include`)"
></div>
</button>
</div>
</div>
</template>
<script>
export default {
props: [
'content',
'type',
'label',
'description',
'resizeImage',
'showAlert'
],
data() {
return {
dragOver: false,
showCropper: false,
tempURL: null,
mime: null
}
},
computed: {
imageAttached() {
return this.content[this.type].url ? true : false
}
},
methods: {
closeCropper() {
this.showCropper = false
},
attachFile(e, type, dropped) {
dropped
? (this.fileLoaded(e, type, true), (this.dragOver = false))
: this.$refs[`import${type}`].click()
},
fileLoaded(e, type, dropped) {
if (
(dropped && e.dataTransfer.files.length) ||
(!dropped && e.target.files.length)
) {
let file = dropped ? e.dataTransfer.files[0] : e.target.files[0]
let mime = file.type
if (
type == 'logo' &&
file.type.match(/image\/(svg\+xml|png|jpeg|gif|webp)/)
) {
this.imageLoaded(file, type, mime)
} else if (file.type.match(/image\/(png|jpeg|gif|webp)/)) {
this.imageLoaded(file, type, mime)
} else {
if (type == 'logo') {
this.showAlert(
'Unsupported file format.\nOnly jpeg, png, webp, gif and svg file can be attached.'
)
} else {
this.showAlert(
'Unsupported file format.\nOnly jpeg, png, webp and gif file can be attached.'
)
}
}
}
},
imageLoaded(file, type, mime) {
let reader = new FileReader()
reader.onload = f => {
let dataURI = f.target.result
let ext = dataURI
.split(',')[0]
.split(':')[1]
.split('/')[1]
.match(/^\w+/g)[0]
if (type == 'logo' || type == 'icon' || mime.match(/gif|webp/)) {
this.content[type] = {
url: dataURI,
blob: file,
ext,
mime,
resized: file
}
if (!mime.match(/svg|gif|webp/)) this.resizeImage(type, mime)
} else {
this.content.photo.ext = ext
this.mime = mime
this.tempURL = dataURI
this.showCropper = true
}
}
reader.readAsDataURL(file)
}
}
}
</script>