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

130 lines
4.1 KiB
JavaScript
Raw Normal View History

2018-05-09 12:01:36 +05:30
import $ from 'jquery';
2020-10-24 23:57:45 +05:30
import AccessDropdown from '~/projects/settings/access_dropdown';
import axios from '~/lib/utils/axios_utils';
import AccessorUtilities from '~/lib/utils/accessor';
import { deprecatedCreateFlash as Flash } from '~/flash';
import CreateItemDropdown from '~/create_item_dropdown';
import { ACCESS_LEVELS, LEVEL_TYPES } from './constants';
2019-07-31 22:56:46 +05:30
import { __ } from '~/locale';
2018-03-17 18:26:18 +05:30
2017-09-10 17:25:29 +05:30
export default class ProtectedBranchCreate {
2020-10-24 23:57:45 +05:30
constructor(options) {
this.hasLicense = options.hasLicense;
2017-09-10 17:25:29 +05:30
this.$form = $('.js-new-protected-branch');
2018-03-17 18:26:18 +05:30
this.isLocalStorageAvailable = AccessorUtilities.isLocalStorageAccessSafe();
this.currentProjectUserDefaults = {};
2017-09-10 17:25:29 +05:30
this.buildDropdowns();
2020-10-24 23:57:45 +05:30
this.$codeOwnerToggle = this.$form.find('.js-code-owner-toggle');
this.bindEvents();
}
bindEvents() {
if (this.hasLicense) {
this.$codeOwnerToggle.on('click', this.onCodeOwnerToggleClick.bind(this));
}
this.$form.on('submit', this.onFormSubmit.bind(this));
}
onCodeOwnerToggleClick() {
this.$codeOwnerToggle.toggleClass('is-checked');
2017-09-10 17:25:29 +05:30
}
buildDropdowns() {
const $allowedToMergeDropdown = this.$form.find('.js-allowed-to-merge');
const $allowedToPushDropdown = this.$form.find('.js-allowed-to-push');
// Cache callback
this.onSelectCallback = this.onSelect.bind(this);
// Allowed to Merge dropdown
2020-10-24 23:57:45 +05:30
this[`${ACCESS_LEVELS.MERGE}_dropdown`] = new AccessDropdown({
2017-09-10 17:25:29 +05:30
$dropdown: $allowedToMergeDropdown,
2020-10-24 23:57:45 +05:30
accessLevelsData: gon.merge_access_levels,
2017-09-10 17:25:29 +05:30
onSelect: this.onSelectCallback,
2020-10-24 23:57:45 +05:30
accessLevel: ACCESS_LEVELS.MERGE,
hasLicense: this.hasLicense,
2017-09-10 17:25:29 +05:30
});
// Allowed to Push dropdown
2020-10-24 23:57:45 +05:30
this[`${ACCESS_LEVELS.PUSH}_dropdown`] = new AccessDropdown({
2017-09-10 17:25:29 +05:30
$dropdown: $allowedToPushDropdown,
2020-10-24 23:57:45 +05:30
accessLevelsData: gon.push_access_levels,
2017-09-10 17:25:29 +05:30
onSelect: this.onSelectCallback,
2020-10-24 23:57:45 +05:30
accessLevel: ACCESS_LEVELS.PUSH,
hasLicense: this.hasLicense,
2017-09-10 17:25:29 +05:30
});
2018-03-17 18:26:18 +05:30
this.createItemDropdown = new CreateItemDropdown({
2020-10-24 23:57:45 +05:30
$dropdown: this.$form.find('.js-protected-branch-select'),
2019-07-31 22:56:46 +05:30
defaultToggleLabel: __('Protected Branch'),
2018-03-17 18:26:18 +05:30
fieldName: 'protected_branch[name]',
2017-09-10 17:25:29 +05:30
onSelect: this.onSelectCallback,
2018-03-17 18:26:18 +05:30
getData: ProtectedBranchCreate.getProtectedBranches,
2017-09-10 17:25:29 +05:30
});
}
2020-10-24 23:57:45 +05:30
// Enable submit button after selecting an option
2017-09-10 17:25:29 +05:30
onSelect() {
2020-10-24 23:57:45 +05:30
const $allowedToMerge = this[`${ACCESS_LEVELS.MERGE}_dropdown`].getSelectedItems();
const $allowedToPush = this[`${ACCESS_LEVELS.PUSH}_dropdown`].getSelectedItems();
const toggle = !(
this.$form.find('input[name="protected_branch[name]"]').val() &&
$allowedToMerge.length &&
$allowedToPush.length
2018-03-17 18:26:18 +05:30
);
2020-10-24 23:57:45 +05:30
this.$form.find('input[type="submit"]').attr('disabled', toggle);
2018-03-17 18:26:18 +05:30
}
static getProtectedBranches(term, callback) {
callback(gon.open_branches);
}
2020-10-24 23:57:45 +05:30
getFormData() {
const formData = {
authenticity_token: this.$form.find('input[name="authenticity_token"]').val(),
protected_branch: {
name: this.$form.find('input[name="protected_branch[name]"]').val(),
code_owner_approval_required: this.$codeOwnerToggle.hasClass('is-checked'),
},
};
Object.keys(ACCESS_LEVELS).forEach(level => {
const accessLevel = ACCESS_LEVELS[level];
const selectedItems = this[`${accessLevel}_dropdown`].getSelectedItems();
const levelAttributes = [];
selectedItems.forEach(item => {
if (item.type === LEVEL_TYPES.USER) {
levelAttributes.push({
user_id: item.user_id,
});
} else if (item.type === LEVEL_TYPES.ROLE) {
levelAttributes.push({
access_level: item.access_level,
});
} else if (item.type === LEVEL_TYPES.GROUP) {
levelAttributes.push({
group_id: item.group_id,
});
}
});
formData.protected_branch[`${accessLevel}_attributes`] = levelAttributes;
});
return formData;
}
onFormSubmit(e) {
e.preventDefault();
axios[this.$form.attr('method')](this.$form.attr('action'), this.getFormData())
.then(() => {
window.location.reload();
})
.catch(() => Flash(__('Failed to protect the branch')));
}
2017-09-10 17:25:29 +05:30
}