Commit fb8dffb3 authored by Alfredo Sumaran's avatar Alfredo Sumaran

Delete access level when unselecting it from dropdown

parent 65de9b33
...@@ -3,9 +3,10 @@ ...@@ -3,9 +3,10 @@
gl.ProtectedBranchAccessDropdown = class { gl.ProtectedBranchAccessDropdown = class {
constructor(options) { constructor(options) {
const { $dropdown, onSelect, onHide } = options; const { $dropdown, onSelect, onHide, accessLevel } = options;
const self = this; const self = this;
this.accessLevel = accessLevel;
this.$dropdown = $dropdown; this.$dropdown = $dropdown;
this.$wrap = this.$dropdown.parents().eq(1); // TODO: Find a better way to get the wrap element this.$wrap = this.$dropdown.parents().eq(1); // TODO: Find a better way to get the wrap element
this.usersPath = '/autocomplete/users.json'; this.usersPath = '/autocomplete/users.json';
...@@ -16,7 +17,6 @@ ...@@ -16,7 +17,6 @@
selectable: true, selectable: true,
filterable: true, filterable: true,
filterRemote: true, filterRemote: true,
inputId: $dropdown.data('input-id'),
data: this.getData.bind(this), data: this.getData.bind(this),
multiSelect: $dropdown.hasClass('js-multiselect'), multiSelect: $dropdown.hasClass('js-multiselect'),
renderRow: this.renderRow.bind(this), renderRow: this.renderRow.bind(this),
...@@ -39,7 +39,7 @@ ...@@ -39,7 +39,7 @@
self.inputCount++; self.inputCount++;
if (onSelect) { if (onSelect) {
onSelect(item, $el); onSelect(item, $el, self);
} }
} }
}); });
......
...@@ -27,13 +27,14 @@ ...@@ -27,13 +27,14 @@
// Save initial state with existing dropdowns // Save initial state with existing dropdowns
this.state = {}; this.state = {};
for (let ACCESS_LEVEL in ACCESS_LEVELS) { for (let ACCESS_LEVEL in ACCESS_LEVELS) {
this.state[`${ACCESS_LEVELS[ACCESS_LEVEL]}_attributes`] = this.getAccessLevelData(ACCESS_LEVEL); this.state[`${ACCESS_LEVELS[ACCESS_LEVEL]}_attributes`] = this.getAccessLevelDataFromInputs(ACCESS_LEVEL);
} }
} }
buildDropdowns() { buildDropdowns() {
// Allowed to merge dropdown // Allowed to merge dropdown
new gl.allowedToMergeDropdown({ new gl.allowedToMergeDropdown({
accessLevel: ACCESS_LEVELS.MERGE,
$dropdown: this.$allowedToMergeDropdown, $dropdown: this.$allowedToMergeDropdown,
onSelect: this.onSelectOption.bind(this), onSelect: this.onSelectOption.bind(this),
onHide: this.onDropdownHide.bind(this), onHide: this.onDropdownHide.bind(this),
...@@ -41,14 +42,28 @@ ...@@ -41,14 +42,28 @@
// Allowed to push dropdown // Allowed to push dropdown
new gl.allowedToPushDropdown({ new gl.allowedToPushDropdown({
accessLevel: ACCESS_LEVELS.PUSH,
$dropdown: this.$allowedToPushDropdown, $dropdown: this.$allowedToPushDropdown,
onSelect: this.onSelectOption.bind(this), onSelect: this.onSelectOption.bind(this),
onHide: this.onDropdownHide.bind(this) onHide: this.onDropdownHide.bind(this)
}); });
} }
onSelectOption(item, $el) { onSelectOption(item, $el, dropdownInstance) {
this.hasChanges = true; this.hasChanges = true;
let itemToDestroy;
let accessLevelState = this.state[`${dropdownInstance.accessLevel}_attributes`];
// If we are unselecting an option
if (!$el.is('.is-active')) {
if (item.type === LEVEL_TYPES.USER) {
itemToDestroy = _.findWhere(accessLevelState, { user_id: item.id });
} else if (item.type === LEVEL_TYPES.ROLE) {
itemToDestroy = _.findWhere(accessLevelState, { access_level: item.id });
}
itemToDestroy['_destroy'] = 1;
}
} }
onDropdownHide() { onDropdownHide() {
...@@ -116,7 +131,7 @@ ...@@ -116,7 +131,7 @@
// State takes precedence // State takes precedence
let accessLevel = ACCESS_LEVELS[accessLevelKey]; let accessLevel = ACCESS_LEVELS[accessLevelKey];
let accessLevelData = []; let accessLevelData = [];
let dataFromInputs = this.getAccessLevelData(accessLevelKey); let dataFromInputs = this.getAccessLevelDataFromInputs(accessLevelKey);
for (let i = 0; i < dataFromInputs.length; i++) { for (let i = 0; i < dataFromInputs.length; i++) {
let inState; let inState;
...@@ -146,10 +161,17 @@ ...@@ -146,10 +161,17 @@
} }
} }
// Items to be deleted
this.state[`${accessLevel}_attributes`].forEach((item) => {
if (item._destroy) {
accessLevelData.push(item);
}
});
return accessLevelData; return accessLevelData;
} }
getAccessLevelData(accessLevelKey) { getAccessLevelDataFromInputs(accessLevelKey) {
let accessLevels = []; let accessLevels = [];
let accessLevel = ACCESS_LEVELS[accessLevelKey]; let accessLevel = ACCESS_LEVELS[accessLevelKey];
this.$wraps[accessLevel] this.$wraps[accessLevel]
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment