2017-01-11 22:35:41 -05:00
|
|
|
/* eslint-disable no-new, arrow-parens, no-param-reassign, comma-dangle, max-len */
|
2016-12-13 22:01:05 -05:00
|
|
|
/* global Flash */
|
|
|
|
|
2016-08-05 02:57:53 -04:00
|
|
|
(global => {
|
2017-01-11 19:48:34 -05:00
|
|
|
global.gl = global.gl || {};
|
2016-08-03 14:02:42 -04:00
|
|
|
|
2016-08-05 02:57:53 -04:00
|
|
|
gl.ProtectedBranchEdit = class {
|
|
|
|
constructor(options) {
|
|
|
|
this.$wrap = options.$wrap;
|
|
|
|
this.$allowedToMergeDropdown = this.$wrap.find('.js-allowed-to-merge');
|
|
|
|
this.$allowedToPushDropdown = this.$wrap.find('.js-allowed-to-push');
|
2016-08-03 14:02:42 -04:00
|
|
|
|
2016-08-05 02:57:53 -04:00
|
|
|
this.buildDropdowns();
|
|
|
|
}
|
|
|
|
|
|
|
|
buildDropdowns() {
|
|
|
|
// Allowed to merge dropdown
|
|
|
|
new gl.ProtectedBranchAccessDropdown({
|
|
|
|
$dropdown: this.$allowedToMergeDropdown,
|
|
|
|
data: gon.merge_access_levels,
|
|
|
|
onSelect: this.onSelect.bind(this)
|
|
|
|
});
|
2016-08-03 14:02:42 -04:00
|
|
|
|
2016-08-05 02:57:53 -04:00
|
|
|
// Allowed to push dropdown
|
|
|
|
new gl.ProtectedBranchAccessDropdown({
|
|
|
|
$dropdown: this.$allowedToPushDropdown,
|
|
|
|
data: gon.push_access_levels,
|
|
|
|
onSelect: this.onSelect.bind(this)
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
onSelect() {
|
2016-08-05 18:09:21 -04:00
|
|
|
const $allowedToMergeInput = this.$wrap.find(`input[name="${this.$allowedToMergeDropdown.data('fieldName')}"]`);
|
|
|
|
const $allowedToPushInput = this.$wrap.find(`input[name="${this.$allowedToPushDropdown.data('fieldName')}"]`);
|
2016-08-05 02:57:53 -04:00
|
|
|
|
2016-08-21 01:19:22 -04:00
|
|
|
// Do not update if one dropdown has not selected any option
|
2016-12-13 22:01:05 -05:00
|
|
|
if (!($allowedToMergeInput.length && $allowedToPushInput.length)) return;
|
2016-08-21 01:19:22 -04:00
|
|
|
|
2017-01-03 11:31:43 -05:00
|
|
|
this.$allowedToMergeDropdown.disable();
|
|
|
|
this.$allowedToPushDropdown.disable();
|
|
|
|
|
2016-08-05 02:57:53 -04:00
|
|
|
$.ajax({
|
|
|
|
type: 'POST',
|
|
|
|
url: this.$wrap.data('url'),
|
|
|
|
dataType: 'json',
|
|
|
|
data: {
|
|
|
|
_method: 'PATCH',
|
|
|
|
protected_branch: {
|
2016-08-16 01:09:13 -04:00
|
|
|
merge_access_levels_attributes: [{
|
|
|
|
id: this.$allowedToMergeDropdown.data('access-level-id'),
|
2016-08-05 02:57:53 -04:00
|
|
|
access_level: $allowedToMergeInput.val()
|
2016-08-16 01:09:13 -04:00
|
|
|
}],
|
|
|
|
push_access_levels_attributes: [{
|
|
|
|
id: this.$allowedToPushDropdown.data('access-level-id'),
|
2016-08-05 02:57:53 -04:00
|
|
|
access_level: $allowedToPushInput.val()
|
2016-08-16 01:09:13 -04:00
|
|
|
}]
|
2016-08-03 14:02:42 -04:00
|
|
|
}
|
2016-08-05 02:57:53 -04:00
|
|
|
},
|
|
|
|
error() {
|
|
|
|
$.scrollTo(0);
|
|
|
|
new Flash('Failed to update branch!');
|
2016-08-03 14:02:42 -04:00
|
|
|
}
|
2017-01-03 11:31:43 -05:00
|
|
|
}).always(() => {
|
|
|
|
this.$allowedToMergeDropdown.enable();
|
|
|
|
this.$allowedToPushDropdown.enable();
|
2016-08-05 02:57:53 -04:00
|
|
|
});
|
|
|
|
}
|
2017-01-10 17:54:56 -05:00
|
|
|
};
|
2016-08-05 02:57:53 -04:00
|
|
|
})(window);
|