2019-09-24 08:06:20 -04:00
|
|
|
/* eslint-disable no-else-return */
|
2018-03-09 15:18:59 -05:00
|
|
|
|
|
|
|
import $ from 'jquery';
|
2019-09-19 11:06:08 -04:00
|
|
|
import '~/gl_dropdown';
|
2017-05-19 17:22:46 -04:00
|
|
|
import Api from './api';
|
2017-12-07 07:30:53 -05:00
|
|
|
import { mergeUrlParams } from './lib/utils/url_utility';
|
2018-11-21 10:20:32 -05:00
|
|
|
import { parseBoolean } from '~/lib/utils/common_utils';
|
2019-05-06 10:19:34 -04:00
|
|
|
import { __ } from './locale';
|
2016-12-14 00:26:26 -05:00
|
|
|
|
2017-10-26 07:03:44 -04:00
|
|
|
export default class NamespaceSelect {
|
|
|
|
constructor(opts) {
|
2018-11-21 10:20:32 -05:00
|
|
|
const isFilter = parseBoolean(opts.dropdown.dataset.isFilter);
|
2017-10-26 08:53:57 -04:00
|
|
|
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: {
|
2018-10-10 03:13:34 -04:00
|
|
|
fields: ['path'],
|
2017-10-26 07:03:44 -04:00
|
|
|
},
|
2019-09-18 10:02:45 -04:00
|
|
|
fieldName,
|
|
|
|
toggleLabel(selected) {
|
2017-10-26 07:03:44 -04:00
|
|
|
if (selected.id == null) {
|
|
|
|
return selected.text;
|
|
|
|
} else {
|
2019-09-24 08:06:20 -04:00
|
|
|
return `${selected.kind}: ${selected.full_path}`;
|
2017-10-26 07:03:44 -04:00
|
|
|
}
|
|
|
|
},
|
2019-09-18 10:02:45 -04:00
|
|
|
data(term, dataCallback) {
|
2019-09-24 05:06:04 -04:00
|
|
|
return Api.namespaces(term, namespaces => {
|
2017-10-26 07:17:02 -04:00
|
|
|
if (isFilter) {
|
2017-10-26 08:53:57 -04:00
|
|
|
const anyNamespace = {
|
2019-05-06 10:19:34 -04:00
|
|
|
text: __('Any namespace'),
|
2018-10-10 03:13:34 -04:00
|
|
|
id: null,
|
2017-10-26 07:03:44 -04:00
|
|
|
};
|
|
|
|
namespaces.unshift(anyNamespace);
|
2019-09-13 09:26:31 -04:00
|
|
|
namespaces.splice(1, 0, { type: 'divider' });
|
2017-10-26 07:03:44 -04:00
|
|
|
}
|
|
|
|
return dataCallback(namespaces);
|
|
|
|
});
|
|
|
|
},
|
2019-09-18 10:02:45 -04:00
|
|
|
text(namespace) {
|
2017-10-26 07:03:44 -04:00
|
|
|
if (namespace.id == null) {
|
|
|
|
return namespace.text;
|
|
|
|
} else {
|
2019-09-24 08:06:20 -04:00
|
|
|
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) {
|
2017-10-26 08:53:57 -04:00
|
|
|
if (!isFilter) {
|
|
|
|
const { e } = options;
|
|
|
|
e.preventDefault();
|
|
|
|
}
|
|
|
|
},
|
|
|
|
url(namespace) {
|
2017-12-07 07:30:53 -05:00
|
|
|
return mergeUrlParams({ [fieldName]: namespace.id }, window.location.href);
|
2017-10-26 07:04:42 -04:00
|
|
|
},
|
2017-10-26 07:03:44 -04:00
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|