gitlab-org--gitlab-foss/app/assets/javascripts/namespace_select.js

63 lines
1.7 KiB
JavaScript
Raw Normal View History

/* eslint-disable no-else-return */
import $ from 'jquery';
import '~/gl_dropdown';
import Api from './api';
import { mergeUrlParams } from './lib/utils/url_utility';
import { parseBoolean } from '~/lib/utils/common_utils';
import { __ } from './locale';
2017-10-26 07:03:44 -04:00
export default class NamespaceSelect {
constructor(opts) {
const isFilter = parseBoolean(opts.dropdown.dataset.isFilter);
const fieldName = opts.dropdown.dataset.fieldName || 'namespace_id';
$(opts.dropdown).glDropdown({
2017-10-26 07:03:44 -04:00
filterable: true,
selectable: true,
filterRemote: true,
search: {
fields: ['path'],
2017-10-26 07:03:44 -04:00
},
fieldName,
toggleLabel(selected) {
2017-10-26 07:03:44 -04:00
if (selected.id == null) {
return selected.text;
} else {
return `${selected.kind}: ${selected.full_path}`;
2017-10-26 07:03:44 -04:00
}
},
data(term, dataCallback) {
return Api.namespaces(term, namespaces => {
if (isFilter) {
const anyNamespace = {
text: __('Any namespace'),
id: null,
2017-10-26 07:03:44 -04:00
};
namespaces.unshift(anyNamespace);
namespaces.splice(1, 0, { type: 'divider' });
2017-10-26 07:03:44 -04:00
}
return dataCallback(namespaces);
});
},
text(namespace) {
2017-10-26 07:03:44 -04:00
if (namespace.id == null) {
return namespace.text;
} else {
return `${namespace.kind}: ${namespace.full_path}`;
2017-10-26 07:03:44 -04:00
}
},
renderRow: this.renderRow,
2017-10-26 07:04:42 -04:00
clicked(options) {
if (!isFilter) {
const { e } = options;
e.preventDefault();
}
},
url(namespace) {
return mergeUrlParams({ [fieldName]: namespace.id }, window.location.href);
2017-10-26 07:04:42 -04:00
},
2017-10-26 07:03:44 -04:00
});
}
}