2017-09-10 17:25:29 +05:30
|
|
|
/* eslint-disable no-new */
|
2017-08-17 22:00:37 +05:30
|
|
|
/* global Flash */
|
|
|
|
|
2017-09-10 17:25:29 +05:30
|
|
|
import ProtectedBranchAccessDropdown from './protected_branch_access_dropdown';
|
2016-09-13 17:45:13 +05:30
|
|
|
|
2017-09-10 17:25:29 +05:30
|
|
|
export default class ProtectedBranchEdit {
|
|
|
|
constructor(options) {
|
|
|
|
this.$wrap = options.$wrap;
|
|
|
|
this.$allowedToMergeDropdown = this.$wrap.find('.js-allowed-to-merge');
|
|
|
|
this.$allowedToPushDropdown = this.$wrap.find('.js-allowed-to-push');
|
|
|
|
this.onSelectCallback = this.onSelect.bind(this);
|
2016-09-13 17:45:13 +05:30
|
|
|
|
2017-09-10 17:25:29 +05:30
|
|
|
this.buildDropdowns();
|
|
|
|
}
|
2016-09-13 17:45:13 +05:30
|
|
|
|
2017-09-10 17:25:29 +05:30
|
|
|
buildDropdowns() {
|
|
|
|
// Allowed to merge dropdown
|
|
|
|
this.protectedBranchAccessDropdown = new ProtectedBranchAccessDropdown({
|
|
|
|
$dropdown: this.$allowedToMergeDropdown,
|
|
|
|
data: gon.merge_access_levels,
|
|
|
|
onSelect: this.onSelectCallback,
|
|
|
|
});
|
2016-09-13 17:45:13 +05:30
|
|
|
|
2017-09-10 17:25:29 +05:30
|
|
|
// Allowed to push dropdown
|
|
|
|
this.protectedBranchAccessDropdown = new ProtectedBranchAccessDropdown({
|
|
|
|
$dropdown: this.$allowedToPushDropdown,
|
|
|
|
data: gon.push_access_levels,
|
|
|
|
onSelect: this.onSelectCallback,
|
|
|
|
});
|
|
|
|
}
|
2016-09-13 17:45:13 +05:30
|
|
|
|
2017-09-10 17:25:29 +05:30
|
|
|
onSelect() {
|
|
|
|
const $allowedToMergeInput = this.$wrap.find(`input[name="${this.$allowedToMergeDropdown.data('fieldName')}"]`);
|
|
|
|
const $allowedToPushInput = this.$wrap.find(`input[name="${this.$allowedToPushDropdown.data('fieldName')}"]`);
|
2016-09-13 17:45:13 +05:30
|
|
|
|
2017-09-10 17:25:29 +05:30
|
|
|
// Do not update if one dropdown has not selected any option
|
|
|
|
if (!($allowedToMergeInput.length && $allowedToPushInput.length)) return;
|
2017-08-17 22:00:37 +05:30
|
|
|
|
2017-09-10 17:25:29 +05:30
|
|
|
this.$allowedToMergeDropdown.disable();
|
|
|
|
this.$allowedToPushDropdown.disable();
|
2016-09-13 17:45:13 +05:30
|
|
|
|
2017-09-10 17:25:29 +05:30
|
|
|
$.ajax({
|
|
|
|
type: 'POST',
|
|
|
|
url: this.$wrap.data('url'),
|
|
|
|
dataType: 'json',
|
|
|
|
data: {
|
|
|
|
_method: 'PATCH',
|
|
|
|
protected_branch: {
|
|
|
|
merge_access_levels_attributes: [{
|
|
|
|
id: this.$allowedToMergeDropdown.data('access-level-id'),
|
|
|
|
access_level: $allowedToMergeInput.val(),
|
|
|
|
}],
|
|
|
|
push_access_levels_attributes: [{
|
|
|
|
id: this.$allowedToPushDropdown.data('access-level-id'),
|
|
|
|
access_level: $allowedToPushInput.val(),
|
|
|
|
}],
|
2016-09-13 17:45:13 +05:30
|
|
|
},
|
2017-09-10 17:25:29 +05:30
|
|
|
},
|
|
|
|
error() {
|
|
|
|
new Flash('Failed to update branch!', null, $('.js-protected-branches-list'));
|
|
|
|
},
|
|
|
|
}).always(() => {
|
|
|
|
this.$allowedToMergeDropdown.enable();
|
|
|
|
this.$allowedToPushDropdown.enable();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|