2020-07-17 14:09:20 -04:00
|
|
|
<script>
|
2020-10-15 17:09:12 -04:00
|
|
|
import { GlDropdown, GlDropdownItem, GlSearchBoxByType } from '@gitlab/ui';
|
2021-02-14 13:09:20 -05:00
|
|
|
import { secondsToHours } from '~/lib/utils/datetime_utility';
|
2020-07-17 14:09:20 -04:00
|
|
|
import { __ } from '~/locale';
|
|
|
|
import autofocusonshow from '~/vue_shared/directives/autofocusonshow';
|
|
|
|
|
|
|
|
export default {
|
2020-07-23 14:10:06 -04:00
|
|
|
name: 'TimezoneDropdown',
|
2020-07-17 14:09:20 -04:00
|
|
|
components: {
|
2020-09-14 08:09:34 -04:00
|
|
|
GlDropdown,
|
2020-10-15 17:09:12 -04:00
|
|
|
GlDropdownItem,
|
2020-07-17 14:09:20 -04:00
|
|
|
GlSearchBoxByType,
|
|
|
|
},
|
|
|
|
directives: {
|
|
|
|
autofocusonshow,
|
|
|
|
},
|
|
|
|
props: {
|
|
|
|
value: {
|
|
|
|
type: String,
|
2020-07-23 14:10:06 -04:00
|
|
|
required: true,
|
2020-07-17 14:09:20 -04:00
|
|
|
default: '',
|
|
|
|
},
|
|
|
|
timezoneData: {
|
|
|
|
type: Array,
|
|
|
|
required: true,
|
|
|
|
default: () => [],
|
|
|
|
},
|
|
|
|
},
|
|
|
|
data() {
|
|
|
|
return {
|
2020-07-23 14:10:06 -04:00
|
|
|
searchTerm: '',
|
2020-07-17 14:09:20 -04:00
|
|
|
};
|
|
|
|
},
|
|
|
|
tranlations: {
|
|
|
|
noResultsText: __('No matching results'),
|
|
|
|
},
|
|
|
|
computed: {
|
|
|
|
timezones() {
|
2020-12-23 16:10:24 -05:00
|
|
|
return this.timezoneData.map((timezone) => ({
|
2020-07-17 14:09:20 -04:00
|
|
|
formattedTimezone: this.formatTimezone(timezone),
|
|
|
|
identifier: timezone.identifier,
|
|
|
|
}));
|
|
|
|
},
|
|
|
|
filteredResults() {
|
|
|
|
const lowerCasedSearchTerm = this.searchTerm.toLowerCase();
|
2020-12-23 16:10:24 -05:00
|
|
|
return this.timezones.filter((timezone) =>
|
2020-07-17 14:09:20 -04:00
|
|
|
timezone.formattedTimezone.toLowerCase().includes(lowerCasedSearchTerm),
|
|
|
|
);
|
|
|
|
},
|
2020-07-23 14:10:06 -04:00
|
|
|
selectedTimezoneLabel() {
|
2020-07-17 14:09:20 -04:00
|
|
|
return this.value || __('Select timezone');
|
|
|
|
},
|
|
|
|
},
|
|
|
|
methods: {
|
2020-07-23 14:10:06 -04:00
|
|
|
selectTimezone(selectedTimezone) {
|
|
|
|
this.$emit('input', selectedTimezone);
|
2020-07-17 14:09:20 -04:00
|
|
|
this.searchTerm = '';
|
|
|
|
},
|
|
|
|
isSelected(timezone) {
|
|
|
|
return this.value === timezone.formattedTimezone;
|
|
|
|
},
|
|
|
|
formatTimezone(item) {
|
2020-11-01 22:08:43 -05:00
|
|
|
return `[UTC ${secondsToHours(item.offset)}] ${item.name}`;
|
2020-07-17 14:09:20 -04:00
|
|
|
},
|
|
|
|
},
|
|
|
|
};
|
|
|
|
</script>
|
|
|
|
<template>
|
2020-10-15 17:09:12 -04:00
|
|
|
<gl-dropdown :text="selectedTimezoneLabel" block lazy menu-class="gl-w-full!">
|
2020-10-15 14:08:43 -04:00
|
|
|
<gl-search-box-by-type v-model.trim="searchTerm" v-autofocusonshow autofocus />
|
2020-10-15 17:09:12 -04:00
|
|
|
<gl-dropdown-item
|
2020-07-17 14:09:20 -04:00
|
|
|
v-for="timezone in filteredResults"
|
|
|
|
:key="timezone.formattedTimezone"
|
2020-10-15 17:09:12 -04:00
|
|
|
:is-checked="isSelected(timezone)"
|
|
|
|
:is-check-item="true"
|
2020-07-17 14:09:20 -04:00
|
|
|
@click="selectTimezone(timezone)"
|
|
|
|
>
|
|
|
|
{{ timezone.formattedTimezone }}
|
2020-10-15 17:09:12 -04:00
|
|
|
</gl-dropdown-item>
|
|
|
|
<gl-dropdown-item
|
|
|
|
v-if="!filteredResults.length"
|
|
|
|
class="gl-pointer-events-none"
|
|
|
|
data-testid="noMatchingResults"
|
|
|
|
>
|
2020-07-17 14:09:20 -04:00
|
|
|
{{ $options.tranlations.noResultsText }}
|
2020-10-15 17:09:12 -04:00
|
|
|
</gl-dropdown-item>
|
2020-09-14 08:09:34 -04:00
|
|
|
</gl-dropdown>
|
2020-07-17 14:09:20 -04:00
|
|
|
</template>
|