gitlab-org--gitlab-foss/app/assets/javascripts/pipeline_schedules/components/timezone_dropdown.js

67 lines
1.5 KiB
JavaScript
Raw Normal View History

/* eslint-disable class-methods-use-this */
2017-05-12 21:15:27 -04:00
const defaultTimezone = 'UTC';
export default class TimezoneDropdown {
constructor() {
this.$dropdown = $('.js-timezone-dropdown');
this.$dropdownToggle = this.$dropdown.find('.dropdown-toggle-text');
this.$input = $('#schedule_cron_timezone');
this.timezoneData = this.$dropdown.data('data');
2017-05-12 21:15:27 -04:00
this.initDefaultTimezone();
this.initDropdown();
}
initDropdown() {
this.$dropdown.glDropdown({
data: this.timezoneData,
filterable: true,
selectable: true,
toggleLabel: item => item.name,
search: {
fields: ['name'],
},
clicked: cfg => this.updateInputValue(cfg),
text: item => this.formatTimezone(item),
});
this.setDropdownToggle();
}
formatUtcOffset(offset) {
let prefix = '';
if (offset > 0) {
prefix = '+';
} else if (offset < 0) {
prefix = '-';
}
return `${prefix} ${Math.abs(offset / 3600)}`;
}
formatTimezone(item) {
return `[UTC ${this.formatUtcOffset(item.offset)}] ${item.name}`;
}
2017-05-12 21:15:27 -04:00
initDefaultTimezone() {
const initialValue = this.$input.val();
if (!initialValue) {
this.$input.val(defaultTimezone);
}
}
2017-05-12 21:15:27 -04:00
setDropdownToggle() {
const initialValue = this.$input.val();
this.$dropdownToggle.text(initialValue);
}
updateInputValue({ selectedObj, e }) {
e.preventDefault();
this.$input.val(selectedObj.identifier);
gl.pipelineScheduleFieldErrors.updateFormValidityState();
}
}