gitlab-org--gitlab-foss/app/assets/javascripts/close_reopen_report_toggle.js

93 lines
2.3 KiB
JavaScript
Raw Normal View History

2017-06-08 16:15:34 +00:00
import DropLab from './droplab/drop_lab';
2017-06-07 10:18:35 +00:00
import ISetter from './droplab/plugins/input_setter';
// Todo: Remove this when fixing issue in input_setter plugin
const InputSetter = { ...ISetter };
2017-06-07 10:18:35 +00:00
class CloseReopenReportToggle {
constructor(opts = {}) {
this.dropdownTrigger = opts.dropdownTrigger;
this.dropdownList = opts.dropdownList;
this.button = opts.button;
2017-06-07 19:34:19 +00:00
}
2017-06-07 10:18:35 +00:00
2017-06-07 19:34:19 +00:00
initDroplab() {
2017-06-07 10:18:35 +00:00
this.reopenItem = this.dropdownList.querySelector('.reopen-item');
this.closeItem = this.dropdownList.querySelector('.close-item');
2017-06-08 16:15:34 +00:00
this.droplab = new DropLab();
2017-06-07 10:18:35 +00:00
const config = this.setConfig();
this.droplab.init(this.dropdownTrigger, this.dropdownList, [InputSetter], config);
}
updateButton(isClosed) {
2017-06-07 13:17:03 +00:00
this.toggleButtonType(isClosed);
2017-06-07 10:18:35 +00:00
this.button.blur();
}
2017-06-07 13:17:03 +00:00
toggleButtonType(isClosed) {
const [showItem, hideItem] = this.getButtonTypes(isClosed);
2017-06-07 10:18:35 +00:00
2017-06-07 13:17:03 +00:00
showItem.classList.remove('hidden');
showItem.classList.add('droplab-item-selected');
2017-06-07 10:18:35 +00:00
2017-06-07 13:17:03 +00:00
hideItem.classList.add('hidden');
hideItem.classList.remove('droplab-item-selected');
2017-06-07 10:18:35 +00:00
2017-06-07 13:17:03 +00:00
showItem.click();
}
2017-06-07 10:18:35 +00:00
2017-06-07 13:17:03 +00:00
getButtonTypes(isClosed) {
return isClosed ? [this.reopenItem, this.closeItem] : [this.closeItem, this.reopenItem];
2017-06-07 10:18:35 +00:00
}
setDisable(shouldDisable = true) {
2017-06-07 10:18:35 +00:00
if (shouldDisable) {
this.button.setAttribute('disabled', 'true');
this.dropdownTrigger.setAttribute('disabled', 'true');
} else {
this.button.removeAttribute('disabled');
this.dropdownTrigger.removeAttribute('disabled');
}
}
setConfig() {
const config = {
InputSetter: [
{
input: this.button,
valueAttribute: 'data-text',
inputAttribute: 'data-value',
},
{
input: this.button,
valueAttribute: 'data-text',
inputAttribute: 'title',
},
{
input: this.button,
valueAttribute: 'data-button-class',
inputAttribute: 'class',
},
{
input: this.dropdownTrigger,
valueAttribute: 'data-toggle-class',
inputAttribute: 'class',
},
{
input: this.button,
valueAttribute: 'data-url',
inputAttribute: 'data-endpoint',
2017-06-07 10:18:35 +00:00
},
],
};
return config;
}
}
export default CloseReopenReportToggle;