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

86 lines
2.1 KiB
JavaScript
Raw Normal View History

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) {
return this.tokenKeysWithAlternative.find((tokenKey) => {
2017-08-17 22:00:37 +05:30
let tokenKeyParam = tokenKey.key;
2018-03-17 18:26:18 +05:30
// Replace hyphen with underscore to compare keyParam with tokenKeyParam
// e.g. 'my-reaction' => 'my_reaction'
tokenKeyParam = tokenKeyParam.replace('-', '_');
2017-08-17 22:00:37 +05:30
if (tokenKey.param) {
tokenKeyParam += `_${tokenKey.param}`;
}
return keyParam === tokenKeyParam;
}) || null;
}
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
}
2018-12-05 23:21:45 +05:30
searchByConditionKeyValue(key, value) {
return this.conditions
2017-08-17 22:00:37 +05:30
.find(condition => condition.tokenKey === key && condition.value === value) || null;
}
2018-12-05 23:21:45 +05:30
addExtraTokensForMergeRequests() {
const wipToken = {
key: 'wip',
type: 'string',
param: '',
symbol: '',
icon: 'admin',
tag: 'Yes or No',
lowercaseValueOnSubmit: true,
uppercaseTokenName: true,
capitalizeTokenValue: true,
};
this.tokenKeys.push(wipToken);
this.tokenKeysWithAlternative.push(wipToken);
}
2017-08-17 22:00:37 +05:30
}