debian-mirror-gitlab/app/assets/javascripts/protected_branches/protected_branch_dropdown.js

84 lines
2.2 KiB
JavaScript
Raw Normal View History

2017-08-17 22:00:37 +05:30
/* eslint-disable comma-dangle, no-unused-vars */
2016-09-13 17:45:13 +05:30
class ProtectedBranchDropdown {
constructor(options) {
this.onSelect = options.onSelect;
this.$dropdown = options.$dropdown;
this.$dropdownContainer = this.$dropdown.parent();
this.$dropdownFooter = this.$dropdownContainer.find('.dropdown-footer');
2017-08-17 22:00:37 +05:30
this.$protectedBranch = this.$dropdownContainer.find('.js-create-new-protected-branch');
2016-09-13 17:45:13 +05:30
this.buildDropdown();
this.bindEvents();
// Hide footer
this.$dropdownFooter.addClass('hidden');
}
buildDropdown() {
this.$dropdown.glDropdown({
data: this.getProtectedBranches.bind(this),
filterable: true,
remote: false,
search: {
fields: ['title']
},
selectable: true,
toggleLabel(selected) {
return (selected && 'id' in selected) ? selected.title : 'Protected Branch';
},
fieldName: 'protected_branch[name]',
text(protectedBranch) {
return _.escape(protectedBranch.title);
},
id(protectedBranch) {
return _.escape(protectedBranch.id);
},
onFilter: this.toggleCreateNewButton.bind(this),
2017-08-17 22:00:37 +05:30
clicked: (options) => {
const { $el, e } = options;
2016-09-13 17:45:13 +05:30
e.preventDefault();
this.onSelect();
}
});
}
bindEvents() {
this.$protectedBranch.on('click', this.onClickCreateWildcard.bind(this));
}
2017-08-17 22:00:37 +05:30
onClickCreateWildcard(e) {
e.preventDefault();
2016-09-29 09:46:39 +05:30
// Refresh the dropdown's data, which ends up calling `getProtectedBranches`
2016-09-13 17:45:13 +05:30
this.$dropdown.data('glDropdown').remote.execute();
2017-08-17 22:00:37 +05:30
this.$dropdown.data('glDropdown').selectRowAtIndex();
2016-09-13 17:45:13 +05:30
}
getProtectedBranches(term, callback) {
if (this.selectedBranch) {
callback(gon.open_branches.concat(this.selectedBranch));
} else {
callback(gon.open_branches);
}
}
toggleCreateNewButton(branchName) {
this.selectedBranch = {
title: branchName,
id: branchName,
text: branchName
};
if (branchName) {
this.$dropdownContainer
2017-08-17 22:00:37 +05:30
.find('.js-create-new-protected-branch code')
2016-09-13 17:45:13 +05:30
.text(branchName);
}
this.$dropdownFooter.toggleClass('hidden', !branchName);
}
}
2017-08-17 22:00:37 +05:30
window.ProtectedBranchDropdown = ProtectedBranchDropdown;