Files
grafana/public/app/features/templating/adhoc_variable.ts
Torkel Ödegaard 4aa227dd84 [Tech]: Start migrating to Jest for tests (#9610)
* tech: investigating karma + jest mix

* tech: migrating tests to jest

* tech: moved anoter test file to jest

* test: migrated two more test files to jest

* test: updated readme and made test fail to verify that it causes CI build failure

* tech: added code coverage for jest tests

* tech: testing codecov coverage

* tech: migrated more tests

* tech: migrated template srv to typescript and the tests to jest

* tech: minor build fix

* tech: build fixes

* build: another attempt at fixing go test with coverage
2017-10-22 07:03:26 +02:00

83 lines
1.6 KiB
TypeScript

import _ from 'lodash';
import {Variable, assignModelProperties, variableTypes} from './variable';
export class AdhocVariable implements Variable {
filters: any[];
defaults = {
type: 'adhoc',
name: '',
label: '',
hide: 0,
datasource: null,
filters: [],
};
/** @ngInject **/
constructor(private model) {
assignModelProperties(this, model, this.defaults);
}
setValue(option) {
return Promise.resolve();
}
getSaveModel() {
assignModelProperties(this.model, this, this.defaults);
return this.model;
}
updateOptions() {
return Promise.resolve();
}
dependsOn(variable) {
return false;
}
setValueFromUrl(urlValue) {
if (!_.isArray(urlValue)) {
urlValue = [urlValue];
}
this.filters = urlValue.map(item => {
var values = item.split('|').map(value => {
return this.unescapeDelimiter(value);
});
return {
key: values[0],
operator: values[1],
value: values[2],
};
});
return Promise.resolve();
}
getValueForUrl() {
return this.filters.map(filter => {
return [filter.key, filter.operator, filter.value].map(value => {
return this.escapeDelimiter(value);
}).join('|');
});
}
escapeDelimiter(value) {
return value.replace(/\|/g, '__gfp__');
}
unescapeDelimiter(value) {
return value.replace(/__gfp__/g, '|');
}
setFilters(filters: any[]) {
this.filters = filters;
}
}
variableTypes['adhoc'] = {
name: 'Ad hoc filters',
ctor: AdhocVariable,
description: 'Add key/value filters on the fly',
};