Commit 214cf9c2 authored by Filipa Lacerda's avatar Filipa Lacerda

Merge branch 'ce-backport-generalize-protected-branch-tags-to-create-item-dropdown' into 'master'

Generalize protected branch/tag dropdowns -- CE backport

See merge request gitlab-org/gitlab-ce!16420
parents b35b57a1 b63686ce
import _ from 'underscore'; import _ from 'underscore';
export default class ProtectedTagDropdown { export default class CreateItemDropdown {
/** /**
* @param {Object} options containing * @param {Object} options containing
* `$dropdown` target element * `$dropdown` target element
...@@ -8,11 +8,14 @@ export default class ProtectedTagDropdown { ...@@ -8,11 +8,14 @@ export default class ProtectedTagDropdown {
* $dropdown must be an element created using `dropdown_tag()` rails helper * $dropdown must be an element created using `dropdown_tag()` rails helper
*/ */
constructor(options) { constructor(options) {
this.onSelect = options.onSelect; this.defaultToggleLabel = options.defaultToggleLabel;
this.fieldName = options.fieldName;
this.onSelect = options.onSelect || (() => {});
this.getDataOption = options.getData;
this.$dropdown = options.$dropdown; this.$dropdown = options.$dropdown;
this.$dropdownContainer = this.$dropdown.parent(); this.$dropdownContainer = this.$dropdown.parent();
this.$dropdownFooter = this.$dropdownContainer.find('.dropdown-footer'); this.$dropdownFooter = this.$dropdownContainer.find('.dropdown-footer');
this.$protectedTag = this.$dropdownContainer.find('.js-create-new-protected-tag'); this.$createButton = this.$dropdownContainer.find('.js-dropdown-create-new-item');
this.buildDropdown(); this.buildDropdown();
this.bindEvents(); this.bindEvents();
...@@ -23,7 +26,7 @@ export default class ProtectedTagDropdown { ...@@ -23,7 +26,7 @@ export default class ProtectedTagDropdown {
buildDropdown() { buildDropdown() {
this.$dropdown.glDropdown({ this.$dropdown.glDropdown({
data: this.getProtectedTags.bind(this), data: this.getData.bind(this),
filterable: true, filterable: true,
remote: false, remote: false,
search: { search: {
...@@ -31,14 +34,14 @@ export default class ProtectedTagDropdown { ...@@ -31,14 +34,14 @@ export default class ProtectedTagDropdown {
}, },
selectable: true, selectable: true,
toggleLabel(selected) { toggleLabel(selected) {
return (selected && 'id' in selected) ? selected.title : 'Protected Tag'; return (selected && 'id' in selected) ? selected.title : this.defaultToggleLabel;
}, },
fieldName: 'protected_tag[name]', fieldName: this.fieldName,
text(protectedTag) { text(item) {
return _.escape(protectedTag.title); return _.escape(item.title);
}, },
id(protectedTag) { id(item) {
return _.escape(protectedTag.id); return _.escape(item.id);
}, },
onFilter: this.toggleCreateNewButton.bind(this), onFilter: this.toggleCreateNewButton.bind(this),
clicked: (options) => { clicked: (options) => {
...@@ -49,37 +52,37 @@ export default class ProtectedTagDropdown { ...@@ -49,37 +52,37 @@ export default class ProtectedTagDropdown {
} }
bindEvents() { bindEvents() {
this.$protectedTag.on('click', this.onClickCreateWildcard.bind(this)); this.$createButton.on('click', this.onClickCreateWildcard.bind(this));
} }
onClickCreateWildcard(e) { onClickCreateWildcard(e) {
e.preventDefault();
// Refresh the dropdown's data, which ends up calling `getData`
this.$dropdown.data('glDropdown').remote.execute(); this.$dropdown.data('glDropdown').remote.execute();
this.$dropdown.data('glDropdown').selectRowAtIndex(); this.$dropdown.data('glDropdown').selectRowAtIndex();
e.preventDefault();
} }
getProtectedTags(term, callback) { getData(term, callback) {
if (this.selectedTag) { this.getDataOption(term, (data = []) => {
callback(gon.open_tags.concat(this.selectedTag)); callback(data.concat(this.selectedItem || []));
} else { });
callback(gon.open_tags);
}
} }
toggleCreateNewButton(tagName) { toggleCreateNewButton(item) {
if (tagName) { if (item) {
this.selectedTag = { this.selectedItem = {
title: tagName, title: item,
id: tagName, id: item,
text: tagName, text: item,
}; };
this.$dropdownContainer this.$dropdownContainer
.find('.js-create-new-protected-tag code') .find('.js-dropdown-create-new-item code')
.text(tagName); .text(item);
} }
this.toggleFooter(!tagName); this.toggleFooter(!item);
} }
toggleFooter(toggleState) { toggleFooter(toggleState) {
......
import _ from 'underscore'; import _ from 'underscore';
import ProtectedBranchAccessDropdown from './protected_branch_access_dropdown'; import ProtectedBranchAccessDropdown from './protected_branch_access_dropdown';
import ProtectedBranchDropdown from './protected_branch_dropdown'; import CreateItemDropdown from '../create_item_dropdown';
import AccessorUtilities from '../lib/utils/accessor'; import AccessorUtilities from '../lib/utils/accessor';
const PB_LOCAL_STORAGE_KEY = 'protected-branches-defaults'; const PB_LOCAL_STORAGE_KEY = 'protected-branches-defaults';
...@@ -35,10 +35,12 @@ export default class ProtectedBranchCreate { ...@@ -35,10 +35,12 @@ export default class ProtectedBranchCreate {
onSelect: this.onSelectCallback, onSelect: this.onSelectCallback,
}); });
// Protected branch dropdown this.createItemDropdown = new CreateItemDropdown({
this.protectedBranchDropdown = new ProtectedBranchDropdown({
$dropdown: $protectedBranchDropdown, $dropdown: $protectedBranchDropdown,
defaultToggleLabel: 'Protected Branch',
fieldName: 'protected_branch[name]',
onSelect: this.onSelectCallback, onSelect: this.onSelectCallback,
getData: ProtectedBranchCreate.getProtectedBranches,
}); });
this.loadPreviousSelection($allowedToMergeDropdown.data('glDropdown'), $allowedToPushDropdown.data('glDropdown')); this.loadPreviousSelection($allowedToMergeDropdown.data('glDropdown'), $allowedToPushDropdown.data('glDropdown'));
...@@ -60,6 +62,10 @@ export default class ProtectedBranchCreate { ...@@ -60,6 +62,10 @@ export default class ProtectedBranchCreate {
this.$form.find('input[type="submit"]').attr('disabled', completedForm); this.$form.find('input[type="submit"]').attr('disabled', completedForm);
} }
static getProtectedBranches(term, callback) {
callback(gon.open_branches);
}
loadPreviousSelection(mergeDropdown, pushDropdown) { loadPreviousSelection(mergeDropdown, pushDropdown) {
let mergeIndex = 0; let mergeIndex = 0;
let pushIndex = 0; let pushIndex = 0;
......
import _ from 'underscore';
export default class ProtectedBranchDropdown {
/**
* @param {Object} options containing
* `$dropdown` target element
* `onSelect` event callback
* $dropdown must be an element created using `dropdown_branch()` rails helper
*/
constructor(options) {
this.onSelect = options.onSelect;
this.$dropdown = options.$dropdown;
this.$dropdownContainer = this.$dropdown.parent();
this.$dropdownFooter = this.$dropdownContainer.find('.dropdown-footer');
this.$protectedBranch = this.$dropdownContainer.find('.js-create-new-protected-branch');
this.buildDropdown();
this.bindEvents();
// Hide footer
this.toggleFooter(true);
}
buildDropdown() {
this.$dropdown.glDropdown({
data: this.getProtectedBranches.bind(this),
filterable: true,
remote: false,
search: {
fields: ['title'],
},
selectable: true,
toggleLabel(selected) {
return (selected && 'id' in selected) ? selected.title : 'Protected Branch';
},
fieldName: 'protected_branch[name]',
text(protectedBranch) {
return _.escape(protectedBranch.title);
},
id(protectedBranch) {
return _.escape(protectedBranch.id);
},
onFilter: this.toggleCreateNewButton.bind(this),
clicked: (options) => {
options.e.preventDefault();
this.onSelect();
},
});
}
bindEvents() {
this.$protectedBranch.on('click', this.onClickCreateWildcard.bind(this));
}
onClickCreateWildcard(e) {
e.preventDefault();
// Refresh the dropdown's data, which ends up calling `getProtectedBranches`
this.$dropdown.data('glDropdown').remote.execute();
this.$dropdown.data('glDropdown').selectRowAtIndex();
}
getProtectedBranches(term, callback) {
if (this.selectedBranch) {
callback(gon.open_branches.concat(this.selectedBranch));
} else {
callback(gon.open_branches);
}
}
toggleCreateNewButton(branchName) {
if (branchName) {
this.selectedBranch = {
title: branchName,
id: branchName,
text: branchName,
};
this.$dropdownContainer
.find('.js-create-new-protected-branch code')
.text(branchName);
}
this.toggleFooter(!branchName);
}
toggleFooter(toggleState) {
this.$dropdownFooter.toggleClass('hidden', toggleState);
}
}
import ProtectedTagAccessDropdown from './protected_tag_access_dropdown'; import ProtectedTagAccessDropdown from './protected_tag_access_dropdown';
import ProtectedTagDropdown from './protected_tag_dropdown'; import CreateItemDropdown from '../create_item_dropdown';
export default class ProtectedTagCreate { export default class ProtectedTagCreate {
constructor() { constructor() {
...@@ -24,9 +24,12 @@ export default class ProtectedTagCreate { ...@@ -24,9 +24,12 @@ export default class ProtectedTagCreate {
$allowedToCreateDropdown.data('glDropdown').selectRowAtIndex(0); $allowedToCreateDropdown.data('glDropdown').selectRowAtIndex(0);
// Protected tag dropdown // Protected tag dropdown
this.protectedTagDropdown = new ProtectedTagDropdown({ this.createItemDropdown = new CreateItemDropdown({
$dropdown: this.$form.find('.js-protected-tag-select'), $dropdown: this.$form.find('.js-protected-tag-select'),
defaultToggleLabel: 'Protected Tag',
fieldName: 'protected_tag[name]',
onSelect: this.onSelectCallback, onSelect: this.onSelectCallback,
getData: ProtectedTagCreate.getProtectedTags,
}); });
} }
...@@ -38,4 +41,8 @@ export default class ProtectedTagCreate { ...@@ -38,4 +41,8 @@ export default class ProtectedTagCreate {
this.$form.find('input[type="submit"]').attr('disabled', !($tagInput.val() && $allowedToCreateInput.length)); this.$form.find('input[type="submit"]').attr('disabled', !($tagInput.val() && $allowedToCreateInput.length));
} }
static getProtectedTags(term, callback) {
callback(gon.open_tags);
}
} }
...@@ -666,6 +666,16 @@ ...@@ -666,6 +666,16 @@
} }
} }
.dropdown-create-new-item-button {
@include dropdown-link;
width: 100%;
background-color: transparent;
border: 0;
text-align: left;
text-overflow: ellipsis;
}
.dropdown-loading { .dropdown-loading {
position: absolute; position: absolute;
top: 0; top: 0;
......
...@@ -895,17 +895,6 @@ pre.light-well { ...@@ -895,17 +895,6 @@ pre.light-well {
} }
} }
.create-new-protected-branch-button,
.create-new-protected-tag-button {
@include dropdown-link;
width: 100%;
background-color: transparent;
border: 0;
text-align: left;
text-overflow: ellipsis;
}
.protected-branches-list, .protected-branches-list,
.protected-tags-list { .protected-tags-list {
margin-bottom: 30px; margin-bottom: 30px;
......
...@@ -10,6 +10,6 @@ ...@@ -10,6 +10,6 @@
%ul.dropdown-footer-list %ul.dropdown-footer-list
%li %li
%button{ class: "create-new-protected-branch-button js-create-new-protected-branch", title: "New Protected Branch" } %button{ class: "dropdown-create-new-item-button js-dropdown-create-new-item", title: "New Protected Branch" }
Create wildcard Create wildcard
%code %code
...@@ -10,6 +10,6 @@ ...@@ -10,6 +10,6 @@
%ul.dropdown-footer-list %ul.dropdown-footer-list
%li %li
%button{ class: "create-new-protected-tag-button js-create-new-protected-tag", title: "New Protected Tag" } %button{ class: "dropdown-create-new-item-button js-dropdown-create-new-item", title: "New Protected Tag" }
Create wildcard Create wildcard
%code %code
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