2018-03-17 18:26:18 +05:30
|
|
|
/* eslint-disable func-names, space-before-function-paren, no-var, comma-dangle, object-shorthand, no-else-return, prefer-template, quotes, prefer-arrow-callback, max-len */
|
2018-05-09 12:01:36 +05:30
|
|
|
|
|
|
|
import $ from 'jquery';
|
2017-09-10 17:25:29 +05:30
|
|
|
import Api from './api';
|
2018-03-17 18:26:18 +05:30
|
|
|
import { mergeUrlParams } from './lib/utils/url_utility';
|
2017-08-17 22:00:37 +05:30
|
|
|
|
2018-03-17 18:26:18 +05:30
|
|
|
export default class NamespaceSelect {
|
|
|
|
constructor(opts) {
|
|
|
|
const isFilter = opts.dropdown.dataset.isFilter === 'true';
|
|
|
|
const fieldName = opts.dropdown.dataset.fieldName || 'namespace_id';
|
2016-09-13 17:45:13 +05:30
|
|
|
|
2018-03-17 18:26:18 +05:30
|
|
|
$(opts.dropdown).glDropdown({
|
|
|
|
filterable: true,
|
|
|
|
selectable: true,
|
|
|
|
filterRemote: true,
|
|
|
|
search: {
|
|
|
|
fields: ['path']
|
|
|
|
},
|
|
|
|
fieldName: fieldName,
|
|
|
|
toggleLabel: function(selected) {
|
|
|
|
if (selected.id == null) {
|
|
|
|
return selected.text;
|
|
|
|
} else {
|
|
|
|
return selected.kind + ": " + selected.full_path;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
data: function(term, dataCallback) {
|
|
|
|
return Api.namespaces(term, function(namespaces) {
|
|
|
|
if (isFilter) {
|
|
|
|
const anyNamespace = {
|
|
|
|
text: 'Any namespace',
|
|
|
|
id: null
|
|
|
|
};
|
|
|
|
namespaces.unshift(anyNamespace);
|
|
|
|
namespaces.splice(1, 0, 'divider');
|
|
|
|
}
|
|
|
|
return dataCallback(namespaces);
|
2016-09-13 17:45:13 +05:30
|
|
|
});
|
2018-03-17 18:26:18 +05:30
|
|
|
},
|
|
|
|
text: function(namespace) {
|
|
|
|
if (namespace.id == null) {
|
|
|
|
return namespace.text;
|
|
|
|
} else {
|
|
|
|
return namespace.kind + ": " + namespace.full_path;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
renderRow: this.renderRow,
|
|
|
|
clicked(options) {
|
|
|
|
if (!isFilter) {
|
|
|
|
const { e } = options;
|
|
|
|
e.preventDefault();
|
|
|
|
}
|
|
|
|
},
|
|
|
|
url(namespace) {
|
|
|
|
return mergeUrlParams({ [fieldName]: namespace.id }, window.location.href);
|
|
|
|
},
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|