gitlab-org--gitlab-foss/app/assets/javascripts/protected_tags/protected_tag_dropdown.js

89 lines
2.2 KiB
JavaScript
Raw Normal View History

import _ from 'underscore';
2017-04-06 08:47:19 +00:00
export default class ProtectedTagDropdown {
2017-04-06 09:17:50 +00:00
/**
* @param {Object} options containing
* `$dropdown` target element
* `onSelect` event callback
* $dropdown must be an element created using `dropdown_tag()` rails helper
*/
2017-03-21 15:51:56 +00:00
constructor(options) {
this.onSelect = options.onSelect;
this.$dropdown = options.$dropdown;
this.$dropdownContainer = this.$dropdown.parent();
this.$dropdownFooter = this.$dropdownContainer.find('.dropdown-footer');
this.$protectedTag = this.$dropdownContainer.find('.js-create-new-protected-tag');
2017-03-21 15:51:56 +00:00
this.buildDropdown();
this.bindEvents();
// Hide footer
2017-04-06 09:17:50 +00:00
this.toggleFooter(true);
2017-03-21 15:51:56 +00:00
}
buildDropdown() {
this.$dropdown.glDropdown({
data: this.getProtectedTags.bind(this),
filterable: true,
remote: false,
search: {
2017-04-06 08:47:19 +00:00
fields: ['title'],
2017-03-21 15:51:56 +00:00
},
selectable: true,
toggleLabel(selected) {
return (selected && 'id' in selected) ? selected.title : 'Protected Tag';
},
fieldName: 'protected_tag[name]',
text(protectedTag) {
return _.escape(protectedTag.title);
},
id(protectedTag) {
return _.escape(protectedTag.id);
},
onFilter: this.toggleCreateNewButton.bind(this),
2017-05-04 20:55:36 +00:00
clicked: (options) => {
options.e.preventDefault();
2017-03-21 15:51:56 +00:00
this.onSelect();
2017-04-06 08:47:19 +00:00
},
2017-03-21 15:51:56 +00:00
});
}
bindEvents() {
this.$protectedTag.on('click', this.onClickCreateWildcard.bind(this));
}
2017-04-06 13:39:48 +00:00
onClickCreateWildcard(e) {
2017-03-21 15:51:56 +00:00
this.$dropdown.data('glDropdown').remote.execute();
this.$dropdown.data('glDropdown').selectRowAtIndex();
2017-04-06 13:39:48 +00:00
e.preventDefault();
2017-03-21 15:51:56 +00:00
}
getProtectedTags(term, callback) {
if (this.selectedTag) {
callback(gon.open_tags.concat(this.selectedTag));
2017-03-21 15:51:56 +00:00
} else {
callback(gon.open_tags);
2017-03-21 15:51:56 +00:00
}
}
toggleCreateNewButton(tagName) {
if (tagName) {
2017-04-06 09:17:50 +00:00
this.selectedTag = {
title: tagName,
id: tagName,
text: tagName,
};
2017-03-21 15:51:56 +00:00
this.$dropdownContainer
.find('.js-create-new-protected-tag code')
2017-03-21 15:51:56 +00:00
.text(tagName);
}
2017-04-06 09:17:50 +00:00
this.toggleFooter(!tagName);
}
toggleFooter(toggleState) {
this.$dropdownFooter.toggleClass('hidden', toggleState);
2017-03-21 15:51:56 +00:00
}
}