-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathMailto.vue
127 lines (117 loc) · 2.88 KB
/
Mailto.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
<template>
<a
href="#"
@click="mailtoHandler"
>
<!-- eslint-disable-next-line vue/no-v-html -->
<slot><span v-html="encoded" /></slot>
</a>
</template>
<script>
/**
* validate an email address.
*
* @param {string} string
* @return {boolean}
*/
function isEmail(string) {
// Email address matcher.
const matcher = /^[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?$/
if (string.length > 320) return false
return matcher.test(string)
}
function formatMail(mail, asArray = false, pretty = true) {
if (typeof mail === 'string') {
if (mail.endsWith(',')) {
mail = mail.substring(0, mail.length - 1)
}
if (mail.startsWith(',')) {
mail = mail.substring(1)
}
mail = mail.split(',')
}
mail = mail.map((email) => {
return email.trim()
})
if (asArray) { return mail }
return pretty ? mail.join(', ') : mail.join(',')
}
function validateMail(value) {
let isValid = true
value = formatMail(value, true, false)
value.forEach((email) => {
if (!isEmail(email)) { isValid = false }
})
return isValid
}
function queryParameters(subject, body, cc, bcc) {
const params = []
cc = cc !== undefined ? `CC=${formatMail(cc)}` : null
bcc = bcc !== undefined ? `BCC=${formatMail(bcc)}` : null
subject = subject !== undefined ? `subject=${encodeURIComponent(subject)}` : null
body = body !== undefined ? `body=${encodeURIComponent(body)}` : null
if (cc) { params.push(cc) }
if (bcc) { params.push(bcc) }
if (subject) { params.push(subject) }
if (body) { params.push(body) }
if (params.length > 0) {
return `?${params.join('&')}`
}
else {
return ''
}
}
export default {
props: {
mail: {
type: [String, Array],
required: true,
validator: value => validateMail(value),
},
cc: {
type: [String, Array],
default: undefined,
validator: value => validateMail(value),
},
bcc: {
type: [String, Array],
default: undefined,
validator: value => validateMail(value),
},
subject: {
type: String,
default: undefined,
},
body: {
type: String,
default: undefined,
},
target: {
type: [String],
default: undefined,
},
},
computed: {
encoded() {
const mail = formatMail(this.mail)
const buf = []
for (let i = mail.length - 1; i >= 0; i--) {
buf.unshift(['&#', mail.charCodeAt(i), ';'].join(''))
}
return buf.join('')
},
},
methods: {
mailtoHandler(e) {
e.preventDefault()
const href = ['mailto:', formatMail(this.mail), queryParameters(this.subject, this.body, this.cc, this.bcc)]
if (this.target === '_blank') {
window.open(href.join(''), '_blank')
}
else {
window.location.href = href.join('')
}
},
},
}
</script>