2017-05-07 18:35:56 -04:00
|
|
|
/* eslint-disable class-methods-use-this */
|
|
|
|
|
2017-05-12 21:15:27 -04:00
|
|
|
const defaultTimezone = 'UTC';
|
|
|
|
|
2017-05-07 18:35:56 -04:00
|
|
|
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();
|
2017-05-07 18:35:56 -04:00
|
|
|
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-07 18:35:56 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-12 21:15:27 -04:00
|
|
|
setDropdownToggle() {
|
|
|
|
const initialValue = this.$input.val();
|
|
|
|
|
|
|
|
this.$dropdownToggle.text(initialValue);
|
|
|
|
}
|
|
|
|
|
2017-05-07 18:35:56 -04:00
|
|
|
updateInputValue({ selectedObj, e }) {
|
|
|
|
e.preventDefault();
|
|
|
|
this.$input.val(selectedObj.identifier);
|
|
|
|
gl.pipelineScheduleFieldErrors.updateFormValidityState();
|
|
|
|
}
|
|
|
|
}
|