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

59 lines
1.6 KiB
JavaScript
Raw Normal View History

2018-05-09 12:01:36 +05:30
import $ from 'jquery';
2019-12-21 20:55:43 +05:30
import '~/gl_dropdown';
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';
2019-02-15 15:39:39 +05:30
import { parseBoolean } from '~/lib/utils/common_utils';
2019-09-04 21:01:54 +05:30
import { __ } from './locale';
2017-08-17 22:00:37 +05:30
2018-03-17 18:26:18 +05:30
export default class NamespaceSelect {
constructor(opts) {
2019-02-15 15:39:39 +05:30
const isFilter = parseBoolean(opts.dropdown.dataset.isFilter);
2018-03-17 18:26:18 +05:30
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: {
2018-12-13 13:39:08 +05:30
fields: ['path'],
2018-03-17 18:26:18 +05:30
},
2019-12-04 20:38:33 +05:30
fieldName,
toggleLabel(selected) {
2018-03-17 18:26:18 +05:30
if (selected.id == null) {
return selected.text;
}
2020-05-24 23:13:21 +05:30
return `${selected.kind}: ${selected.full_path}`;
2018-03-17 18:26:18 +05:30
},
2019-12-04 20:38:33 +05:30
data(term, dataCallback) {
2019-12-21 20:55:43 +05:30
return Api.namespaces(term, namespaces => {
2018-03-17 18:26:18 +05:30
if (isFilter) {
const anyNamespace = {
2019-09-04 21:01:54 +05:30
text: __('Any namespace'),
2018-12-13 13:39:08 +05:30
id: null,
2018-03-17 18:26:18 +05:30
};
namespaces.unshift(anyNamespace);
2019-12-04 20:38:33 +05:30
namespaces.splice(1, 0, { type: 'divider' });
2018-03-17 18:26:18 +05:30
}
return dataCallback(namespaces);
2016-09-13 17:45:13 +05:30
});
2018-03-17 18:26:18 +05:30
},
2019-12-04 20:38:33 +05:30
text(namespace) {
2018-03-17 18:26:18 +05:30
if (namespace.id == null) {
return namespace.text;
}
2020-05-24 23:13:21 +05:30
return `${namespace.kind}: ${namespace.full_path}`;
2018-03-17 18:26:18 +05:30
},
renderRow: this.renderRow,
clicked(options) {
if (!isFilter) {
const { e } = options;
e.preventDefault();
}
},
url(namespace) {
return mergeUrlParams({ [fieldName]: namespace.id }, window.location.href);
},
});
}
}