debian-mirror-gitlab/app/assets/javascripts/filtered_search/filtered_search_token_keys.js

97 lines
2.4 KiB
JavaScript
Raw Normal View History

2019-07-31 22:56:46 +05:30
import { __ } from '~/locale';
2018-12-05 23:21:45 +05:30
export default class FilteredSearchTokenKeys {
constructor(tokenKeys = [], alternativeTokenKeys = [], conditions = []) {
this.tokenKeys = tokenKeys;
this.alternativeTokenKeys = alternativeTokenKeys;
this.conditions = conditions;
2018-03-17 18:26:18 +05:30
2018-12-05 23:21:45 +05:30
this.tokenKeysWithAlternative = this.tokenKeys.concat(this.alternativeTokenKeys);
}
2017-08-17 22:00:37 +05:30
2018-12-05 23:21:45 +05:30
get() {
return this.tokenKeys;
}
getKeys() {
return this.tokenKeys.map(i => i.key);
}
getAlternatives() {
return this.alternativeTokenKeys;
2017-08-17 22:00:37 +05:30
}
2018-12-05 23:21:45 +05:30
getConditions() {
return this.conditions;
2017-09-10 17:25:29 +05:30
}
2018-12-05 23:21:45 +05:30
shouldUppercaseTokenName(tokenKey) {
const token = this.searchByKey(tokenKey.toLowerCase());
return token && token.uppercaseTokenName;
2017-08-17 22:00:37 +05:30
}
2018-12-05 23:21:45 +05:30
shouldCapitalizeTokenValue(tokenKey) {
const token = this.searchByKey(tokenKey.toLowerCase());
return token && token.capitalizeTokenValue;
2017-08-17 22:00:37 +05:30
}
2018-12-05 23:21:45 +05:30
searchByKey(key) {
return this.tokenKeys.find(tokenKey => tokenKey.key === key) || null;
2017-08-17 22:00:37 +05:30
}
2018-12-05 23:21:45 +05:30
searchBySymbol(symbol) {
return this.tokenKeys.find(tokenKey => tokenKey.symbol === symbol) || null;
2017-08-17 22:00:37 +05:30
}
2018-12-05 23:21:45 +05:30
searchByKeyParam(keyParam) {
2018-12-13 13:39:08 +05:30
return (
this.tokenKeysWithAlternative.find(tokenKey => {
let tokenKeyParam = tokenKey.key;
2017-08-17 22:00:37 +05:30
2018-12-13 13:39:08 +05:30
// Replace hyphen with underscore to compare keyParam with tokenKeyParam
// e.g. 'my-reaction' => 'my_reaction'
tokenKeyParam = tokenKeyParam.replace('-', '_');
2018-03-17 18:26:18 +05:30
2018-12-13 13:39:08 +05:30
if (tokenKey.param) {
tokenKeyParam += `_${tokenKey.param}`;
}
2017-08-17 22:00:37 +05:30
2018-12-13 13:39:08 +05:30
return keyParam === tokenKeyParam;
}) || null
);
2017-08-17 22:00:37 +05:30
}
2018-12-05 23:21:45 +05:30
searchByConditionUrl(url) {
return this.conditions.find(condition => condition.url === url) || null;
2017-08-17 22:00:37 +05:30
}
2020-03-13 15:44:24 +05:30
searchByConditionKeyValue(key, operator, value) {
2018-12-13 13:39:08 +05:30
return (
2019-03-02 22:35:43 +05:30
this.conditions.find(
condition =>
2020-03-13 15:44:24 +05:30
condition.tokenKey === key &&
condition.operator === operator &&
condition.value.toLowerCase() === value.toLowerCase(),
2019-03-02 22:35:43 +05:30
) || null
2018-12-13 13:39:08 +05:30
);
2017-08-17 22:00:37 +05:30
}
2018-12-05 23:21:45 +05:30
2019-07-07 11:18:12 +05:30
addExtraTokensForIssues() {
const confidentialToken = {
2020-03-13 15:44:24 +05:30
formattedKey: __('Confidential'),
2019-07-07 11:18:12 +05:30
key: 'confidential',
2018-12-05 23:21:45 +05:30
type: 'string',
param: '',
symbol: '',
2019-07-07 11:18:12 +05:30
icon: 'eye-slash',
2019-07-31 22:56:46 +05:30
tag: __('Yes or No'),
2018-12-05 23:21:45 +05:30
lowercaseValueOnSubmit: true,
2019-07-07 11:18:12 +05:30
uppercaseTokenName: false,
2018-12-05 23:21:45 +05:30
capitalizeTokenValue: true,
};
2019-07-07 11:18:12 +05:30
this.tokenKeys.push(confidentialToken);
this.tokenKeysWithAlternative.push(confidentialToken);
2018-12-05 23:21:45 +05:30
}
2017-08-17 22:00:37 +05:30
}