debian-mirror-gitlab/app/assets/javascripts/namespace_select.js

86 lines
2.6 KiB
JavaScript
Raw Normal View History

2017-08-17 22:00:37 +05:30
/* eslint-disable func-names, space-before-function-paren, no-var, prefer-rest-params, wrap-iife, one-var, vars-on-top, one-var-declaration-per-line, comma-dangle, object-shorthand, no-else-return, prefer-template, quotes, prefer-arrow-callback, no-param-reassign, no-cond-assign, max-len */
2017-09-10 17:25:29 +05:30
import Api from './api';
2017-08-17 22:00:37 +05:30
2016-09-13 17:45:13 +05:30
(function() {
2017-08-17 22:00:37 +05:30
window.NamespaceSelect = (function() {
2016-09-13 17:45:13 +05:30
function NamespaceSelect(opts) {
2017-09-10 17:25:29 +05:30
this.onSelectItem = this.onSelectItem.bind(this);
2016-09-13 17:45:13 +05:30
var fieldName, showAny;
this.dropdown = opts.dropdown;
showAny = true;
fieldName = 'namespace_id';
if (this.dropdown.attr('data-field-name')) {
fieldName = this.dropdown.data('fieldName');
}
if (this.dropdown.attr('data-show-any')) {
showAny = this.dropdown.data('showAny');
}
this.dropdown.glDropdown({
filterable: true,
selectable: true,
filterRemote: true,
search: {
fields: ['path']
},
fieldName: fieldName,
toggleLabel: function(selected) {
if (selected.id == null) {
return selected.text;
} else {
2017-08-17 22:00:37 +05:30
return selected.kind + ": " + selected.full_path;
2016-09-13 17:45:13 +05:30
}
},
data: function(term, dataCallback) {
return Api.namespaces(term, function(namespaces) {
var anyNamespace;
if (showAny) {
anyNamespace = {
text: 'Any namespace',
id: null
};
namespaces.unshift(anyNamespace);
namespaces.splice(1, 0, 'divider');
}
return dataCallback(namespaces);
});
},
text: function(namespace) {
if (namespace.id == null) {
return namespace.text;
} else {
2017-08-17 22:00:37 +05:30
return namespace.kind + ": " + namespace.full_path;
2016-09-13 17:45:13 +05:30
}
},
renderRow: this.renderRow,
clicked: this.onSelectItem
});
}
2017-08-17 22:00:37 +05:30
NamespaceSelect.prototype.onSelectItem = function(options) {
const { e } = options;
2016-09-13 17:45:13 +05:30
return e.preventDefault();
};
return NamespaceSelect;
})();
2017-08-17 22:00:37 +05:30
window.NamespaceSelects = (function() {
2016-09-13 17:45:13 +05:30
function NamespaceSelects(opts) {
var ref;
if (opts == null) {
opts = {};
}
this.$dropdowns = (ref = opts.$dropdowns) != null ? ref : $('.js-namespace-select');
this.$dropdowns.each(function(i, dropdown) {
var $dropdown;
$dropdown = $(dropdown);
2017-08-17 22:00:37 +05:30
return new window.NamespaceSelect({
2016-09-13 17:45:13 +05:30
dropdown: $dropdown
});
});
}
return NamespaceSelects;
})();
2017-08-17 22:00:37 +05:30
}).call(window);