Commit 67c343e7 authored by Alfredo Sumaran's avatar Alfredo Sumaran

Mark active items on dropdown

[ci skip]
parent ff05f4bd
...@@ -14,6 +14,24 @@ ...@@ -14,6 +14,24 @@
return fieldName; return fieldName;
} }
getActiveIds() {
let selected = [];
// Todo: Find a better way to get the wrap element of each dropdown
let $wrap = this.$dropdown.parents().eq(1); // Please, don't judge me
$wrap.find('input[name^="protected_branch[merge_access_levels_attributes]"]')
.map((i, el) => {
const $el = $(el);
selected.push({
id: parseInt($el.val()),
type: $el.data('type')
});
});
return selected;
}
} }
global.gl.allowedToMergeDropdown = allowedToMergeDropdown; global.gl.allowedToMergeDropdown = allowedToMergeDropdown;
......
...@@ -14,6 +14,24 @@ ...@@ -14,6 +14,24 @@
return fieldName; return fieldName;
} }
getActiveIds() {
let selected = [];
// Todo: Find a better way to get the wrap element of each dropdown
let $wrap = this.$dropdown.parents().eq(1); // Please, don't judge me
$wrap.find('input[name^="protected_branch[push_access_levels_attributes]"]')
.map((i, el) => {
const $el = $(el);
selected.push({
id: parseInt($el.val()),
type: $el.data('type')
});
});
return selected;
}
} }
global.gl.allowedToPushDropdown = allowedToPushDropdown; global.gl.allowedToPushDropdown = allowedToPushDropdown;
......
...@@ -20,7 +20,8 @@ ...@@ -20,7 +20,8 @@
renderRow: this.renderRow.bind(this), renderRow: this.renderRow.bind(this),
toggleLabel: this.toggleLabel.bind(this), toggleLabel: this.toggleLabel.bind(this),
fieldName: this.fieldName.bind(this), fieldName: this.fieldName.bind(this),
setActiveIds() { hidden() {
// Here because last selected item is not considered after first close
this.activeIds = self.getActiveIds(); this.activeIds = self.getActiveIds();
}, },
clicked(item, $el, e) { clicked(item, $el, e) {
...@@ -36,6 +37,10 @@ ...@@ -36,6 +37,10 @@
throw new Error('No fieldName method defined'); throw new Error('No fieldName method defined');
} }
getActiveIds() {
throw new Error('No getActiveIds method defined');
}
toggleLabel(selectedItem, el) { toggleLabel(selectedItem, el) {
let currentItems = this.$dropdown.siblings('.dropdown-menu').find('.is-active'); let currentItems = this.$dropdown.siblings('.dropdown-menu').find('.is-active');
let types = _.groupBy(currentItems, (item) => { return item.dataset.type; }); let types = _.groupBy(currentItems, (item) => { return item.dataset.type; });
...@@ -100,27 +105,23 @@ ...@@ -100,27 +105,23 @@
} }
renderRow(item, instance) { renderRow(item, instance) {
const isActive = _.findWhere(instance.activeIds, {id: item.id, type: item.type}) ? 'is-active' : '';
if (item.type === 'user') { if (item.type === 'user') {
return this.userRowHtml(item); return this.userRowHtml(item, isActive);
} else if (item.type === 'role') { } else if (item.type === 'role') {
return this.roleRowHtml(item); return this.roleRowHtml(item, isActive);
} }
} }
userRowHtml(user) { userRowHtml(user, isActive) {
const avatarHtml = `<img src='${user.avatar_url}' class='avatar avatar-inline' width='30'>`; const avatarHtml = `<img src='${user.avatar_url}' class='avatar avatar-inline' width='30'>`;
const nameHtml = `<strong class='dropdown-menu-user-full-name'>${user.name}</strong>`; const nameHtml = `<strong class='dropdown-menu-user-full-name'>${user.name}</strong>`;
const usernameHtml = `<span class='dropdown-menu-user-username'>${user.username}</span>`; const usernameHtml = `<span class='dropdown-menu-user-username'>${user.username}</span>`;
return `<li><a href='#' class='${isActive ? 'is-active' : ''}' data-type='${user.type}'>${avatarHtml} ${nameHtml} ${usernameHtml}</a></li>`;
return `<li><a href='#' data-type='${user.type}'>${avatarHtml} ${nameHtml} ${usernameHtml}</a></li>`;
} }
roleRowHtml(role) { roleRowHtml(role, isActive) {
return `<li><a href='#' data-type='${role.type}'>${role.text}</a></li>`; return `<li><a href='#' class='${isActive ? 'is-active' : ''}' data-type='${role.type}'>${role.text}</a></li>`;
}
getActiveIds() {
console.log('getActiveIds');
} }
} }
......
...@@ -40,9 +40,8 @@ ...@@ -40,9 +40,8 @@
const $branchInput = this.$wrap.find('input[name="protected_branch[name]"]'); const $branchInput = this.$wrap.find('input[name="protected_branch[name]"]');
const $allowedToMergeInputs = this.$wrap.find('input[name^="protected_branch[merge_access_levels_attributes]"]'); const $allowedToMergeInputs = this.$wrap.find('input[name^="protected_branch[merge_access_levels_attributes]"]');
const $allowedToPushInputs = this.$wrap.find('input[name^="protected_branch[push_access_levels_attributes]"]'); const $allowedToPushInputs = this.$wrap.find('input[name^="protected_branch[push_access_levels_attributes]"]');
console.log('x');
if ($branchInput.length && $allowedToMergeInputs.length && $allowedToPushInputs.length){ if ($branchInput.length && $allowedToMergeInputs.length && $allowedToPushInputs.length){
console.log('onSelect');
this.$form.find('input[type="submit"]').removeAttr('disabled'); this.$form.find('input[type="submit"]').removeAttr('disabled');
} }
} }
......
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