glib-web 0.5.77 → 0.5.78
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +0 -0
- package/action.js +167 -167
- package/actions/analytics/logEvent.js +0 -0
- package/actions/cables/push.js +0 -0
- package/actions/dialogs/notification.js +0 -0
- package/actions/http/patch.js +0 -0
- package/actions/panels/scrollTo.js +18 -18
- package/actions/panels/scrollToBottom.js +0 -0
- package/actions/timeouts/set.js +20 -20
- package/actions/windows/close.js +0 -0
- package/actions/windows/closeAll.js +0 -0
- package/actions/windows/closeWithReload.js +0 -0
- package/actions/windows/refreshState.js +5 -5
- package/actions/windows/reload.js +0 -0
- package/actions/ws/push.js +0 -0
- package/app.vue +180 -180
- package/components/_button.vue +101 -101
- package/components/_dropdownMenu.vue +76 -76
- package/components/_icon.vue +50 -50
- package/components/avatar.vue +16 -16
- package/components/banners/alert.vue +0 -0
- package/components/banners/select.vue +82 -82
- package/components/component.vue +222 -222
- package/components/fields/_patternText.vue +61 -61
- package/components/fields/_select.vue +86 -86
- package/components/fields/autocomplete.vue +73 -73
- package/components/fields/check.vue +104 -104
- package/components/fields/creditCard.vue +0 -0
- package/components/fields/dynamicGroup.vue +0 -0
- package/components/fields/hidden.vue +0 -0
- package/components/fields/location.vue +0 -0
- package/components/fields/newRichText.vue +0 -0
- package/components/fields/phone/countries.js +315 -315
- package/components/fields/phone/field.vue +348 -348
- package/components/fields/phone/sprite.css +1071 -1071
- package/components/fields/radio.vue +64 -64
- package/components/fields/radioGroup.vue +0 -0
- package/components/fields/rating.vue +0 -0
- package/components/fields/richText.vue +0 -0
- package/components/fields/select.vue +17 -17
- package/components/fields/stripe/stripeFields.vue +93 -93
- package/components/fields/stripe/stripeIndividualFields.vue +207 -207
- package/components/fields/stripeExternalAccount.vue +135 -135
- package/components/fields/text.vue +0 -0
- package/components/fields/textarea.vue +0 -0
- package/components/fields/timeZone.vue +22 -22
- package/components/fields/timer.vue +83 -83
- package/components/h1.vue +28 -28
- package/components/h2.vue +20 -20
- package/components/h3.vue +22 -22
- package/components/h4.vue +20 -20
- package/components/h5.vue +20 -20
- package/components/h6.vue +20 -20
- package/components/icon.vue +25 -25
- package/components/label.vue +62 -62
- package/components/map.vue +0 -0
- package/components/mixins/events.js +178 -178
- package/components/mixins/generic.js +0 -0
- package/components/mixins/list/autoload.js +0 -0
- package/components/mixins/styles.js +221 -221
- package/components/mixins/table/autoload.js +0 -0
- package/components/mixins/text.js +20 -20
- package/components/mixins/ws/actionCable.js +48 -48
- package/components/mixins/ws/phoenixSocket.js +0 -0
- package/components/panels/carousel.vue +55 -55
- package/components/panels/flow.vue +81 -81
- package/components/panels/form.vue +126 -126
- package/components/panels/list.vue +0 -0
- package/components/panels/scroll.vue +0 -0
- package/components/panels/table.vue +0 -0
- package/components/panels/ul.vue +34 -34
- package/components/panels/vertical.vue +0 -0
- package/components/panels/web.vue +11 -11
- package/components/spacer.vue +0 -0
- package/components/switch.vue +42 -42
- package/extensions/string.js +21 -21
- package/index.js +195 -195
- package/nav/content.vue +0 -0
- package/nav/dialog.vue +127 -127
- package/nav/snackbar.vue +0 -0
- package/package.json +42 -42
- package/static/plugins/alignment/alignment.js +0 -0
- package/static/plugins/alignment/alignment.min.js +0 -0
- package/static/plugins/beyondgrammar/beyondgrammar.js +0 -0
- package/static/plugins/beyondgrammar/beyondgrammar.min.js +0 -0
- package/static/plugins/blockcode/blockcode.js +0 -0
- package/static/plugins/blockcode/blockcode.min.js +0 -0
- package/static/plugins/clips/clips.js +0 -0
- package/static/plugins/clips/clips.min.js +0 -0
- package/static/plugins/counter/counter.js +0 -0
- package/static/plugins/counter/counter.min.js +0 -0
- package/static/plugins/definedlinks/definedlinks.js +0 -0
- package/static/plugins/definedlinks/definedlinks.min.js +0 -0
- package/static/plugins/handle/handle.js +0 -0
- package/static/plugins/handle/handle.min.js +0 -0
- package/static/plugins/icons/icons.js +0 -0
- package/static/plugins/icons/icons.min.js +0 -0
- package/static/plugins/imageposition/imageposition.js +0 -0
- package/static/plugins/imageposition/imageposition.min.js +0 -0
- package/static/plugins/inlineformat/inlineformat.js +0 -0
- package/static/plugins/inlineformat/inlineformat.min.js +0 -0
- package/static/plugins/removeformat/removeformat.js +0 -0
- package/static/plugins/removeformat/removeformat.min.js +0 -0
- package/static/plugins/selector/selector.js +0 -0
- package/static/plugins/selector/selector.min.js +0 -0
- package/static/plugins/specialchars/specialchars.js +0 -0
- package/static/plugins/specialchars/specialchars.min.js +0 -0
- package/static/plugins/textdirection/textdirection.js +0 -0
- package/static/plugins/textdirection/textdirection.min.js +0 -0
- package/static/plugins/textexpander/textexpander.js +0 -0
- package/static/plugins/textexpander/textexpander.min.js +0 -0
- package/static/plugins/underline/underline.js +0 -0
- package/static/plugins/underline/underline.min.js +0 -0
- package/static/redactorx.css +0 -0
- package/static/redactorx.js +0 -0
- package/static/redactorx.min.css +0 -0
- package/static/redactorx.min.js +0 -0
- package/static/redactorx.usm.min.js +0 -0
- package/templates/_menu.vue +38 -38
- package/templates/comment.vue +202 -202
- package/templates/featured.vue +32 -32
- package/templates/thumbnail.vue +138 -138
- package/templates/unsupported.vue +12 -12
- package/utils/app.js +0 -0
- package/utils/hash.js +0 -0
- package/utils/helper.js +0 -0
- package/utils/history.js +70 -70
- package/utils/http.js +209 -209
- package/utils/launch.js +135 -135
- package/utils/private/ws.js +0 -0
|
@@ -1,348 +1,348 @@
|
|
|
1
|
-
<template>
|
|
2
|
-
<div :style="$styles()" :class="classes()">
|
|
3
|
-
<div class="country-code">
|
|
4
|
-
<v-select
|
|
5
|
-
v-model="countryCode"
|
|
6
|
-
:items="sortedCountries"
|
|
7
|
-
:disabled="spec.readOnly"
|
|
8
|
-
:outlined="$classes().includes('outlined')"
|
|
9
|
-
:rounded="$classes().includes('rounded')"
|
|
10
|
-
:dense="$classes().includes('dense')"
|
|
11
|
-
item-text="name"
|
|
12
|
-
item-value="iso2"
|
|
13
|
-
return-object
|
|
14
|
-
@change="onChangeCountryCode"
|
|
15
|
-
>
|
|
16
|
-
<template v-slot:selection>
|
|
17
|
-
<div :class="activeCountry.iso2.toLowerCase()" class="country_flag" />
|
|
18
|
-
</template>
|
|
19
|
-
<template v-slot:item="data">
|
|
20
|
-
<span :class="data.item.iso2.toLowerCase()" class="country_flag" />
|
|
21
|
-
<span>{{ data.item.name }} {{ `+${data.item.dialCode}` }}</span>
|
|
22
|
-
</template>
|
|
23
|
-
</v-select>
|
|
24
|
-
</div>
|
|
25
|
-
<v-text-field
|
|
26
|
-
:id="inputId"
|
|
27
|
-
ref="input"
|
|
28
|
-
v-model="phone"
|
|
29
|
-
class="tel"
|
|
30
|
-
:label="spec.label"
|
|
31
|
-
:name="fieldName"
|
|
32
|
-
:value="spec.value"
|
|
33
|
-
:hint="spec.hint"
|
|
34
|
-
:placeholder="spec.placeholder"
|
|
35
|
-
:maxlength="spec.maxLength || 25"
|
|
36
|
-
:disabled="spec.readOnly"
|
|
37
|
-
type="tel"
|
|
38
|
-
:rules="rules"
|
|
39
|
-
:prefix="spec.leftText"
|
|
40
|
-
:suffix="spec.rightText"
|
|
41
|
-
:outlined="$classes().includes('outlined')"
|
|
42
|
-
:dense="$classes().includes('dense')"
|
|
43
|
-
:rounded="$classes().includes('rounded')"
|
|
44
|
-
:autofocus="spec.autoFocus || false"
|
|
45
|
-
validate-on-blur
|
|
46
|
-
>
|
|
47
|
-
</v-text-field>
|
|
48
|
-
<input
|
|
49
|
-
v-if="spec.readOnly"
|
|
50
|
-
type="hidden"
|
|
51
|
-
:name="fieldName"
|
|
52
|
-
:value="phone"
|
|
53
|
-
/>
|
|
54
|
-
</div>
|
|
55
|
-
</template>
|
|
56
|
-
|
|
57
|
-
<script>
|
|
58
|
-
import PhoneNumber from "awesome-phonenumber";
|
|
59
|
-
import allCountries from "./countries";
|
|
60
|
-
|
|
61
|
-
function getCountryByIp() {
|
|
62
|
-
return fetch("https://ip2c.org/s")
|
|
63
|
-
.then(response => response.text())
|
|
64
|
-
.then(response => {
|
|
65
|
-
const result = (response || "").toString();
|
|
66
|
-
if (!result || result[0] !== "1") {
|
|
67
|
-
throw new Error("unable to fetch the country");
|
|
68
|
-
}
|
|
69
|
-
return result.substr(2, 2);
|
|
70
|
-
});
|
|
71
|
-
}
|
|
72
|
-
|
|
73
|
-
export default {
|
|
74
|
-
props: {
|
|
75
|
-
spec: {
|
|
76
|
-
type: Object,
|
|
77
|
-
required: true
|
|
78
|
-
},
|
|
79
|
-
rules: {
|
|
80
|
-
type: Array,
|
|
81
|
-
default: () => [
|
|
82
|
-
v =>
|
|
83
|
-
!v ||
|
|
84
|
-
/^\+[1-9]([0-9\(\)\-\ ]*)$/.test(v) ||
|
|
85
|
-
"Must be a valid international phone number with prefix (e.g. +1 416 555 0134)"
|
|
86
|
-
]
|
|
87
|
-
},
|
|
88
|
-
disableFetchingCountry: {
|
|
89
|
-
type: Boolean,
|
|
90
|
-
default: () => false
|
|
91
|
-
},
|
|
92
|
-
mode: {
|
|
93
|
-
type: String,
|
|
94
|
-
default: () => ""
|
|
95
|
-
},
|
|
96
|
-
allCountries: {
|
|
97
|
-
type: Array,
|
|
98
|
-
default: () => allCountries
|
|
99
|
-
},
|
|
100
|
-
preferredCountries: {
|
|
101
|
-
type: Array,
|
|
102
|
-
default: () => []
|
|
103
|
-
},
|
|
104
|
-
inputId: {
|
|
105
|
-
type: String,
|
|
106
|
-
default: () => ""
|
|
107
|
-
}
|
|
108
|
-
},
|
|
109
|
-
data() {
|
|
110
|
-
return {
|
|
111
|
-
phone: "",
|
|
112
|
-
activeCountry: { iso2: "" },
|
|
113
|
-
selectedIndex: null,
|
|
114
|
-
countryCode: null
|
|
115
|
-
};
|
|
116
|
-
},
|
|
117
|
-
computed: {
|
|
118
|
-
parsedMode() {
|
|
119
|
-
if (this.mode) {
|
|
120
|
-
if (!["international", "national"].includes(this.mode)) {
|
|
121
|
-
console.error('Invalid value of prop "mode"');
|
|
122
|
-
} else {
|
|
123
|
-
return this.mode;
|
|
124
|
-
}
|
|
125
|
-
}
|
|
126
|
-
if (!this.phone || this.phone[0] !== "+") {
|
|
127
|
-
return "national";
|
|
128
|
-
}
|
|
129
|
-
return "international";
|
|
130
|
-
},
|
|
131
|
-
sortedCountries() {
|
|
132
|
-
// Sort by preferred countries
|
|
133
|
-
const preferredCountries = this.getCountries(
|
|
134
|
-
this.preferredCountries
|
|
135
|
-
).map(country => ({ ...country, preferred: true }));
|
|
136
|
-
return [...preferredCountries, ...this.allCountries];
|
|
137
|
-
},
|
|
138
|
-
phoneObject() {
|
|
139
|
-
const result = PhoneNumber(
|
|
140
|
-
this.phone || "",
|
|
141
|
-
this.activeCountry.iso2
|
|
142
|
-
).toJSON();
|
|
143
|
-
Object.assign(result, {
|
|
144
|
-
isValid: result.valid,
|
|
145
|
-
country: this.activeCountry
|
|
146
|
-
});
|
|
147
|
-
if (!this.phone) {
|
|
148
|
-
return {
|
|
149
|
-
...result,
|
|
150
|
-
number: {
|
|
151
|
-
input: ""
|
|
152
|
-
}
|
|
153
|
-
};
|
|
154
|
-
}
|
|
155
|
-
return result;
|
|
156
|
-
},
|
|
157
|
-
phoneText() {
|
|
158
|
-
let key = "input";
|
|
159
|
-
if (this.phoneObject.valid) {
|
|
160
|
-
key = this.parsedMode;
|
|
161
|
-
}
|
|
162
|
-
return this.phoneObject.number[key] || "";
|
|
163
|
-
}
|
|
164
|
-
},
|
|
165
|
-
watch: {
|
|
166
|
-
phone(val) {
|
|
167
|
-
if (val) {
|
|
168
|
-
if (val[0] === "+") {
|
|
169
|
-
const code = PhoneNumber(val).getRegionCode();
|
|
170
|
-
if (code) {
|
|
171
|
-
this.activeCountry = this.findCountry(code) || this.activeCountry;
|
|
172
|
-
}
|
|
173
|
-
}
|
|
174
|
-
}
|
|
175
|
-
}
|
|
176
|
-
},
|
|
177
|
-
mounted() {
|
|
178
|
-
this.initializeCountry()
|
|
179
|
-
.then(() => {
|
|
180
|
-
if (!this.phone && this.activeCountry.dialCode) {
|
|
181
|
-
this.phone = `+${this.activeCountry.dialCode}`;
|
|
182
|
-
}
|
|
183
|
-
this.countryCode = this.activeCountry;
|
|
184
|
-
})
|
|
185
|
-
.catch(console.error);
|
|
186
|
-
},
|
|
187
|
-
created() {
|
|
188
|
-
if (this.spec.value) {
|
|
189
|
-
this.phone = this.spec.value.trim();
|
|
190
|
-
}
|
|
191
|
-
},
|
|
192
|
-
methods: {
|
|
193
|
-
classes() {
|
|
194
|
-
return this.$classes().concat(["g-text-field--hintless", "fields-phone"]);
|
|
195
|
-
},
|
|
196
|
-
initializeCountry() {
|
|
197
|
-
return new Promise(resolve => {
|
|
198
|
-
// 1. If the phone included prefix (+12), try to get the country and set it
|
|
199
|
-
if (this.phone && this.phone[0] === "+") {
|
|
200
|
-
const activeCountry = PhoneNumber(this.phone).getRegionCode();
|
|
201
|
-
if (activeCountry) {
|
|
202
|
-
this.choose(activeCountry);
|
|
203
|
-
resolve();
|
|
204
|
-
return;
|
|
205
|
-
}
|
|
206
|
-
}
|
|
207
|
-
// 2. Use default country if passed from parent
|
|
208
|
-
if (this.spec.defaultCountry) {
|
|
209
|
-
const defaultCountry = this.findCountry(this.spec.defaultCountry);
|
|
210
|
-
if (defaultCountry) {
|
|
211
|
-
this.choose(defaultCountry);
|
|
212
|
-
resolve();
|
|
213
|
-
return;
|
|
214
|
-
}
|
|
215
|
-
}
|
|
216
|
-
const fallbackCountry =
|
|
217
|
-
this.findCountry(this.preferredCountries[0]) || this.allCountries[0];
|
|
218
|
-
// 3. Check if fetching country based on user's IP is allowed, set it as the default country
|
|
219
|
-
if (!this.disableFetchingCountry) {
|
|
220
|
-
getCountryByIp()
|
|
221
|
-
.then(res => {
|
|
222
|
-
if (this.phone === "") {
|
|
223
|
-
this.activeCountry =
|
|
224
|
-
this.findCountry(res) || this.activeCountry;
|
|
225
|
-
}
|
|
226
|
-
})
|
|
227
|
-
.catch(error => {
|
|
228
|
-
console.warn(error);
|
|
229
|
-
// 4. Use the first country from preferred list (if available) or all countries list
|
|
230
|
-
this.choose(fallbackCountry);
|
|
231
|
-
})
|
|
232
|
-
.finally(() => {
|
|
233
|
-
resolve();
|
|
234
|
-
});
|
|
235
|
-
} else {
|
|
236
|
-
// 4. Use the first country from preferred list (if available) or all countries list
|
|
237
|
-
this.choose(fallbackCountry);
|
|
238
|
-
resolve();
|
|
239
|
-
}
|
|
240
|
-
});
|
|
241
|
-
},
|
|
242
|
-
// Get ISO2 code from a list of countries
|
|
243
|
-
getCountries(list = []) {
|
|
244
|
-
return list
|
|
245
|
-
.map(countryCode => this.findCountry(countryCode))
|
|
246
|
-
.filter(Boolean);
|
|
247
|
-
},
|
|
248
|
-
findCountry(iso = "") {
|
|
249
|
-
return this.allCountries.find(
|
|
250
|
-
country => country.iso2 === iso.toUpperCase()
|
|
251
|
-
);
|
|
252
|
-
},
|
|
253
|
-
getItemClass(index, iso2) {
|
|
254
|
-
const highlighted = this.selectedIndex === index;
|
|
255
|
-
const lastPreferred = index === this.preferredCountries.length - 1;
|
|
256
|
-
const preferred = this.preferredCountries.some(
|
|
257
|
-
c => c.toUpperCase() === iso2
|
|
258
|
-
);
|
|
259
|
-
return {
|
|
260
|
-
highlighted,
|
|
261
|
-
"last-preferred": lastPreferred,
|
|
262
|
-
preferred
|
|
263
|
-
};
|
|
264
|
-
},
|
|
265
|
-
choose(country) {
|
|
266
|
-
this.activeCountry = country || this.activeCountry || {};
|
|
267
|
-
if (
|
|
268
|
-
this.phone &&
|
|
269
|
-
this.phone[0] === "+" &&
|
|
270
|
-
this.activeCountry.iso2 &&
|
|
271
|
-
this.phoneObject.number.significant
|
|
272
|
-
) {
|
|
273
|
-
// If country is selected & the field has a valid international phone number
|
|
274
|
-
this.phone = PhoneNumber(
|
|
275
|
-
this.phoneObject.number.significant,
|
|
276
|
-
this.activeCountry.iso2
|
|
277
|
-
).getNumber("international");
|
|
278
|
-
} else if (
|
|
279
|
-
country &&
|
|
280
|
-
this.phone &&
|
|
281
|
-
(!this.phoneObject.number.significant || this.phone[0] !== "+")
|
|
282
|
-
) {
|
|
283
|
-
// If a new country is selected & the field has NO valid international phone number
|
|
284
|
-
this.phone = `+${country.dialCode}`;
|
|
285
|
-
} else if (country && !this.phone) {
|
|
286
|
-
// If a new country is selected & the field has NO value
|
|
287
|
-
this.phone = `+${country.dialCode}`;
|
|
288
|
-
}
|
|
289
|
-
},
|
|
290
|
-
reset() {
|
|
291
|
-
this.selectedIndex = this.sortedCountries
|
|
292
|
-
.map(c => c.iso2)
|
|
293
|
-
.indexOf(this.activeCountry.iso2);
|
|
294
|
-
},
|
|
295
|
-
onChangeCountryCode() {
|
|
296
|
-
this.choose(this.countryCode, true);
|
|
297
|
-
}
|
|
298
|
-
}
|
|
299
|
-
};
|
|
300
|
-
</script>
|
|
301
|
-
|
|
302
|
-
<style src="./sprite.css"></style>
|
|
303
|
-
<style lang="scss">
|
|
304
|
-
.g-text-field--hintless {
|
|
305
|
-
.v-text-field {
|
|
306
|
-
.v-text-field__details {
|
|
307
|
-
min-height: 0;
|
|
308
|
-
margin-bottom: 0;
|
|
309
|
-
.v-messages__message {
|
|
310
|
-
min-height: 0;
|
|
311
|
-
line-height: 14px;
|
|
312
|
-
}
|
|
313
|
-
}
|
|
314
|
-
}
|
|
315
|
-
}
|
|
316
|
-
.country_flag {
|
|
317
|
-
margin-right: 8px;
|
|
318
|
-
}
|
|
319
|
-
.fields-phone {
|
|
320
|
-
display: flex;
|
|
321
|
-
align-items: center;
|
|
322
|
-
.country-code {
|
|
323
|
-
width: 75px;
|
|
324
|
-
}
|
|
325
|
-
li.last-preferred {
|
|
326
|
-
border-bottom: 1px solid #cacaca;
|
|
327
|
-
}
|
|
328
|
-
.v-text-field {
|
|
329
|
-
.v-select__selections {
|
|
330
|
-
position: relative;
|
|
331
|
-
.country_flag {
|
|
332
|
-
position: absolute;
|
|
333
|
-
margin-left: 18px;
|
|
334
|
-
}
|
|
335
|
-
}
|
|
336
|
-
&--outlined {
|
|
337
|
-
.v-select__selections {
|
|
338
|
-
.country_flag {
|
|
339
|
-
margin-left: auto;
|
|
340
|
-
}
|
|
341
|
-
}
|
|
342
|
-
}
|
|
343
|
-
}
|
|
344
|
-
}
|
|
345
|
-
.fields-phone.outlined .country-code {
|
|
346
|
-
margin-right: 6px;
|
|
347
|
-
}
|
|
348
|
-
</style>
|
|
1
|
+
<template>
|
|
2
|
+
<div :style="$styles()" :class="classes()">
|
|
3
|
+
<div class="country-code">
|
|
4
|
+
<v-select
|
|
5
|
+
v-model="countryCode"
|
|
6
|
+
:items="sortedCountries"
|
|
7
|
+
:disabled="spec.readOnly"
|
|
8
|
+
:outlined="$classes().includes('outlined')"
|
|
9
|
+
:rounded="$classes().includes('rounded')"
|
|
10
|
+
:dense="$classes().includes('dense')"
|
|
11
|
+
item-text="name"
|
|
12
|
+
item-value="iso2"
|
|
13
|
+
return-object
|
|
14
|
+
@change="onChangeCountryCode"
|
|
15
|
+
>
|
|
16
|
+
<template v-slot:selection>
|
|
17
|
+
<div :class="activeCountry.iso2.toLowerCase()" class="country_flag" />
|
|
18
|
+
</template>
|
|
19
|
+
<template v-slot:item="data">
|
|
20
|
+
<span :class="data.item.iso2.toLowerCase()" class="country_flag" />
|
|
21
|
+
<span>{{ data.item.name }} {{ `+${data.item.dialCode}` }}</span>
|
|
22
|
+
</template>
|
|
23
|
+
</v-select>
|
|
24
|
+
</div>
|
|
25
|
+
<v-text-field
|
|
26
|
+
:id="inputId"
|
|
27
|
+
ref="input"
|
|
28
|
+
v-model="phone"
|
|
29
|
+
class="tel"
|
|
30
|
+
:label="spec.label"
|
|
31
|
+
:name="fieldName"
|
|
32
|
+
:value="spec.value"
|
|
33
|
+
:hint="spec.hint"
|
|
34
|
+
:placeholder="spec.placeholder"
|
|
35
|
+
:maxlength="spec.maxLength || 25"
|
|
36
|
+
:disabled="spec.readOnly"
|
|
37
|
+
type="tel"
|
|
38
|
+
:rules="rules"
|
|
39
|
+
:prefix="spec.leftText"
|
|
40
|
+
:suffix="spec.rightText"
|
|
41
|
+
:outlined="$classes().includes('outlined')"
|
|
42
|
+
:dense="$classes().includes('dense')"
|
|
43
|
+
:rounded="$classes().includes('rounded')"
|
|
44
|
+
:autofocus="spec.autoFocus || false"
|
|
45
|
+
validate-on-blur
|
|
46
|
+
>
|
|
47
|
+
</v-text-field>
|
|
48
|
+
<input
|
|
49
|
+
v-if="spec.readOnly"
|
|
50
|
+
type="hidden"
|
|
51
|
+
:name="fieldName"
|
|
52
|
+
:value="phone"
|
|
53
|
+
/>
|
|
54
|
+
</div>
|
|
55
|
+
</template>
|
|
56
|
+
|
|
57
|
+
<script>
|
|
58
|
+
import PhoneNumber from "awesome-phonenumber";
|
|
59
|
+
import allCountries from "./countries";
|
|
60
|
+
|
|
61
|
+
function getCountryByIp() {
|
|
62
|
+
return fetch("https://ip2c.org/s")
|
|
63
|
+
.then(response => response.text())
|
|
64
|
+
.then(response => {
|
|
65
|
+
const result = (response || "").toString();
|
|
66
|
+
if (!result || result[0] !== "1") {
|
|
67
|
+
throw new Error("unable to fetch the country");
|
|
68
|
+
}
|
|
69
|
+
return result.substr(2, 2);
|
|
70
|
+
});
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
export default {
|
|
74
|
+
props: {
|
|
75
|
+
spec: {
|
|
76
|
+
type: Object,
|
|
77
|
+
required: true
|
|
78
|
+
},
|
|
79
|
+
rules: {
|
|
80
|
+
type: Array,
|
|
81
|
+
default: () => [
|
|
82
|
+
v =>
|
|
83
|
+
!v ||
|
|
84
|
+
/^\+[1-9]([0-9\(\)\-\ ]*)$/.test(v) ||
|
|
85
|
+
"Must be a valid international phone number with prefix (e.g. +1 416 555 0134)"
|
|
86
|
+
]
|
|
87
|
+
},
|
|
88
|
+
disableFetchingCountry: {
|
|
89
|
+
type: Boolean,
|
|
90
|
+
default: () => false
|
|
91
|
+
},
|
|
92
|
+
mode: {
|
|
93
|
+
type: String,
|
|
94
|
+
default: () => ""
|
|
95
|
+
},
|
|
96
|
+
allCountries: {
|
|
97
|
+
type: Array,
|
|
98
|
+
default: () => allCountries
|
|
99
|
+
},
|
|
100
|
+
preferredCountries: {
|
|
101
|
+
type: Array,
|
|
102
|
+
default: () => []
|
|
103
|
+
},
|
|
104
|
+
inputId: {
|
|
105
|
+
type: String,
|
|
106
|
+
default: () => ""
|
|
107
|
+
}
|
|
108
|
+
},
|
|
109
|
+
data() {
|
|
110
|
+
return {
|
|
111
|
+
phone: "",
|
|
112
|
+
activeCountry: { iso2: "" },
|
|
113
|
+
selectedIndex: null,
|
|
114
|
+
countryCode: null
|
|
115
|
+
};
|
|
116
|
+
},
|
|
117
|
+
computed: {
|
|
118
|
+
parsedMode() {
|
|
119
|
+
if (this.mode) {
|
|
120
|
+
if (!["international", "national"].includes(this.mode)) {
|
|
121
|
+
console.error('Invalid value of prop "mode"');
|
|
122
|
+
} else {
|
|
123
|
+
return this.mode;
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
if (!this.phone || this.phone[0] !== "+") {
|
|
127
|
+
return "national";
|
|
128
|
+
}
|
|
129
|
+
return "international";
|
|
130
|
+
},
|
|
131
|
+
sortedCountries() {
|
|
132
|
+
// Sort by preferred countries
|
|
133
|
+
const preferredCountries = this.getCountries(
|
|
134
|
+
this.preferredCountries
|
|
135
|
+
).map(country => ({ ...country, preferred: true }));
|
|
136
|
+
return [...preferredCountries, ...this.allCountries];
|
|
137
|
+
},
|
|
138
|
+
phoneObject() {
|
|
139
|
+
const result = PhoneNumber(
|
|
140
|
+
this.phone || "",
|
|
141
|
+
this.activeCountry.iso2
|
|
142
|
+
).toJSON();
|
|
143
|
+
Object.assign(result, {
|
|
144
|
+
isValid: result.valid,
|
|
145
|
+
country: this.activeCountry
|
|
146
|
+
});
|
|
147
|
+
if (!this.phone) {
|
|
148
|
+
return {
|
|
149
|
+
...result,
|
|
150
|
+
number: {
|
|
151
|
+
input: ""
|
|
152
|
+
}
|
|
153
|
+
};
|
|
154
|
+
}
|
|
155
|
+
return result;
|
|
156
|
+
},
|
|
157
|
+
phoneText() {
|
|
158
|
+
let key = "input";
|
|
159
|
+
if (this.phoneObject.valid) {
|
|
160
|
+
key = this.parsedMode;
|
|
161
|
+
}
|
|
162
|
+
return this.phoneObject.number[key] || "";
|
|
163
|
+
}
|
|
164
|
+
},
|
|
165
|
+
watch: {
|
|
166
|
+
phone(val) {
|
|
167
|
+
if (val) {
|
|
168
|
+
if (val[0] === "+") {
|
|
169
|
+
const code = PhoneNumber(val).getRegionCode();
|
|
170
|
+
if (code) {
|
|
171
|
+
this.activeCountry = this.findCountry(code) || this.activeCountry;
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
},
|
|
177
|
+
mounted() {
|
|
178
|
+
this.initializeCountry()
|
|
179
|
+
.then(() => {
|
|
180
|
+
if (!this.phone && this.activeCountry.dialCode) {
|
|
181
|
+
this.phone = `+${this.activeCountry.dialCode}`;
|
|
182
|
+
}
|
|
183
|
+
this.countryCode = this.activeCountry;
|
|
184
|
+
})
|
|
185
|
+
.catch(console.error);
|
|
186
|
+
},
|
|
187
|
+
created() {
|
|
188
|
+
if (this.spec.value) {
|
|
189
|
+
this.phone = this.spec.value.trim();
|
|
190
|
+
}
|
|
191
|
+
},
|
|
192
|
+
methods: {
|
|
193
|
+
classes() {
|
|
194
|
+
return this.$classes().concat(["g-text-field--hintless", "fields-phone"]);
|
|
195
|
+
},
|
|
196
|
+
initializeCountry() {
|
|
197
|
+
return new Promise(resolve => {
|
|
198
|
+
// 1. If the phone included prefix (+12), try to get the country and set it
|
|
199
|
+
if (this.phone && this.phone[0] === "+") {
|
|
200
|
+
const activeCountry = PhoneNumber(this.phone).getRegionCode();
|
|
201
|
+
if (activeCountry) {
|
|
202
|
+
this.choose(activeCountry);
|
|
203
|
+
resolve();
|
|
204
|
+
return;
|
|
205
|
+
}
|
|
206
|
+
}
|
|
207
|
+
// 2. Use default country if passed from parent
|
|
208
|
+
if (this.spec.defaultCountry) {
|
|
209
|
+
const defaultCountry = this.findCountry(this.spec.defaultCountry);
|
|
210
|
+
if (defaultCountry) {
|
|
211
|
+
this.choose(defaultCountry);
|
|
212
|
+
resolve();
|
|
213
|
+
return;
|
|
214
|
+
}
|
|
215
|
+
}
|
|
216
|
+
const fallbackCountry =
|
|
217
|
+
this.findCountry(this.preferredCountries[0]) || this.allCountries[0];
|
|
218
|
+
// 3. Check if fetching country based on user's IP is allowed, set it as the default country
|
|
219
|
+
if (!this.disableFetchingCountry) {
|
|
220
|
+
getCountryByIp()
|
|
221
|
+
.then(res => {
|
|
222
|
+
if (this.phone === "") {
|
|
223
|
+
this.activeCountry =
|
|
224
|
+
this.findCountry(res) || this.activeCountry;
|
|
225
|
+
}
|
|
226
|
+
})
|
|
227
|
+
.catch(error => {
|
|
228
|
+
console.warn(error);
|
|
229
|
+
// 4. Use the first country from preferred list (if available) or all countries list
|
|
230
|
+
this.choose(fallbackCountry);
|
|
231
|
+
})
|
|
232
|
+
.finally(() => {
|
|
233
|
+
resolve();
|
|
234
|
+
});
|
|
235
|
+
} else {
|
|
236
|
+
// 4. Use the first country from preferred list (if available) or all countries list
|
|
237
|
+
this.choose(fallbackCountry);
|
|
238
|
+
resolve();
|
|
239
|
+
}
|
|
240
|
+
});
|
|
241
|
+
},
|
|
242
|
+
// Get ISO2 code from a list of countries
|
|
243
|
+
getCountries(list = []) {
|
|
244
|
+
return list
|
|
245
|
+
.map(countryCode => this.findCountry(countryCode))
|
|
246
|
+
.filter(Boolean);
|
|
247
|
+
},
|
|
248
|
+
findCountry(iso = "") {
|
|
249
|
+
return this.allCountries.find(
|
|
250
|
+
country => country.iso2 === iso.toUpperCase()
|
|
251
|
+
);
|
|
252
|
+
},
|
|
253
|
+
getItemClass(index, iso2) {
|
|
254
|
+
const highlighted = this.selectedIndex === index;
|
|
255
|
+
const lastPreferred = index === this.preferredCountries.length - 1;
|
|
256
|
+
const preferred = this.preferredCountries.some(
|
|
257
|
+
c => c.toUpperCase() === iso2
|
|
258
|
+
);
|
|
259
|
+
return {
|
|
260
|
+
highlighted,
|
|
261
|
+
"last-preferred": lastPreferred,
|
|
262
|
+
preferred
|
|
263
|
+
};
|
|
264
|
+
},
|
|
265
|
+
choose(country) {
|
|
266
|
+
this.activeCountry = country || this.activeCountry || {};
|
|
267
|
+
if (
|
|
268
|
+
this.phone &&
|
|
269
|
+
this.phone[0] === "+" &&
|
|
270
|
+
this.activeCountry.iso2 &&
|
|
271
|
+
this.phoneObject.number.significant
|
|
272
|
+
) {
|
|
273
|
+
// If country is selected & the field has a valid international phone number
|
|
274
|
+
this.phone = PhoneNumber(
|
|
275
|
+
this.phoneObject.number.significant,
|
|
276
|
+
this.activeCountry.iso2
|
|
277
|
+
).getNumber("international");
|
|
278
|
+
} else if (
|
|
279
|
+
country &&
|
|
280
|
+
this.phone &&
|
|
281
|
+
(!this.phoneObject.number.significant || this.phone[0] !== "+")
|
|
282
|
+
) {
|
|
283
|
+
// If a new country is selected & the field has NO valid international phone number
|
|
284
|
+
this.phone = `+${country.dialCode}`;
|
|
285
|
+
} else if (country && !this.phone) {
|
|
286
|
+
// If a new country is selected & the field has NO value
|
|
287
|
+
this.phone = `+${country.dialCode}`;
|
|
288
|
+
}
|
|
289
|
+
},
|
|
290
|
+
reset() {
|
|
291
|
+
this.selectedIndex = this.sortedCountries
|
|
292
|
+
.map(c => c.iso2)
|
|
293
|
+
.indexOf(this.activeCountry.iso2);
|
|
294
|
+
},
|
|
295
|
+
onChangeCountryCode() {
|
|
296
|
+
this.choose(this.countryCode, true);
|
|
297
|
+
}
|
|
298
|
+
}
|
|
299
|
+
};
|
|
300
|
+
</script>
|
|
301
|
+
|
|
302
|
+
<style src="./sprite.css"></style>
|
|
303
|
+
<style lang="scss">
|
|
304
|
+
.g-text-field--hintless {
|
|
305
|
+
.v-text-field {
|
|
306
|
+
.v-text-field__details {
|
|
307
|
+
min-height: 0;
|
|
308
|
+
margin-bottom: 0;
|
|
309
|
+
.v-messages__message {
|
|
310
|
+
min-height: 0;
|
|
311
|
+
line-height: 14px;
|
|
312
|
+
}
|
|
313
|
+
}
|
|
314
|
+
}
|
|
315
|
+
}
|
|
316
|
+
.country_flag {
|
|
317
|
+
margin-right: 8px;
|
|
318
|
+
}
|
|
319
|
+
.fields-phone {
|
|
320
|
+
display: flex;
|
|
321
|
+
align-items: center;
|
|
322
|
+
.country-code {
|
|
323
|
+
width: 75px;
|
|
324
|
+
}
|
|
325
|
+
li.last-preferred {
|
|
326
|
+
border-bottom: 1px solid #cacaca;
|
|
327
|
+
}
|
|
328
|
+
.v-text-field {
|
|
329
|
+
.v-select__selections {
|
|
330
|
+
position: relative;
|
|
331
|
+
.country_flag {
|
|
332
|
+
position: absolute;
|
|
333
|
+
margin-left: 18px;
|
|
334
|
+
}
|
|
335
|
+
}
|
|
336
|
+
&--outlined {
|
|
337
|
+
.v-select__selections {
|
|
338
|
+
.country_flag {
|
|
339
|
+
margin-left: auto;
|
|
340
|
+
}
|
|
341
|
+
}
|
|
342
|
+
}
|
|
343
|
+
}
|
|
344
|
+
}
|
|
345
|
+
.fields-phone.outlined .country-code {
|
|
346
|
+
margin-right: 6px;
|
|
347
|
+
}
|
|
348
|
+
</style>
|