2017-12-20 05:33:33 -06:00
|
|
|
import _ from 'lodash';
|
2019-10-31 04:59:30 -05:00
|
|
|
import {
|
|
|
|
AdHocVariableFilter,
|
|
|
|
AdHocVariableModel,
|
|
|
|
assignModelProperties,
|
|
|
|
VariableActions,
|
|
|
|
VariableHide,
|
|
|
|
VariableType,
|
|
|
|
variableTypes,
|
|
|
|
} from './variable';
|
2016-09-19 11:41:42 -05:00
|
|
|
|
2019-10-31 04:59:30 -05:00
|
|
|
export class AdhocVariable implements AdHocVariableModel, VariableActions {
|
|
|
|
type: VariableType;
|
|
|
|
name: string;
|
|
|
|
label: string;
|
|
|
|
hide: VariableHide;
|
2018-07-11 12:06:36 -05:00
|
|
|
skipUrlSync: boolean;
|
2019-10-31 04:59:30 -05:00
|
|
|
filters: AdHocVariableFilter[];
|
|
|
|
datasource: string;
|
2016-09-19 11:41:42 -05:00
|
|
|
|
2019-10-31 04:59:30 -05:00
|
|
|
defaults: AdHocVariableModel = {
|
2017-12-20 05:33:33 -06:00
|
|
|
type: 'adhoc',
|
|
|
|
name: '',
|
|
|
|
label: '',
|
2019-10-31 04:59:30 -05:00
|
|
|
hide: VariableHide.dontHide,
|
|
|
|
skipUrlSync: false,
|
2016-09-19 11:41:42 -05:00
|
|
|
datasource: null,
|
2017-12-20 05:33:33 -06:00
|
|
|
filters: [],
|
2016-09-19 11:41:42 -05:00
|
|
|
};
|
|
|
|
|
2018-08-31 09:40:43 -05:00
|
|
|
/** @ngInject */
|
2019-07-16 04:35:42 -05:00
|
|
|
constructor(private model: any) {
|
2016-09-19 11:41:42 -05:00
|
|
|
assignModelProperties(this, model, this.defaults);
|
|
|
|
}
|
|
|
|
|
2019-07-16 04:35:42 -05:00
|
|
|
setValue(option: any) {
|
2016-09-19 11:41:42 -05:00
|
|
|
return Promise.resolve();
|
|
|
|
}
|
|
|
|
|
2016-11-17 04:28:33 -06:00
|
|
|
getSaveModel() {
|
2016-09-19 11:41:42 -05:00
|
|
|
assignModelProperties(this.model, this, this.defaults);
|
|
|
|
return this.model;
|
|
|
|
}
|
|
|
|
|
|
|
|
updateOptions() {
|
|
|
|
return Promise.resolve();
|
|
|
|
}
|
|
|
|
|
2019-07-16 04:35:42 -05:00
|
|
|
dependsOn(variable: any) {
|
2016-09-19 11:41:42 -05:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-07-16 04:35:42 -05:00
|
|
|
setValueFromUrl(urlValue: string[] | string[]) {
|
2016-09-20 04:57:43 -05:00
|
|
|
if (!_.isArray(urlValue)) {
|
|
|
|
urlValue = [urlValue];
|
|
|
|
}
|
|
|
|
|
|
|
|
this.filters = urlValue.map(item => {
|
2018-08-26 14:52:57 -05:00
|
|
|
const values = item.split('|').map(value => {
|
2017-04-07 02:31:36 -05:00
|
|
|
return this.unescapeDelimiter(value);
|
|
|
|
});
|
2016-09-20 04:57:43 -05:00
|
|
|
return {
|
|
|
|
key: values[0],
|
|
|
|
operator: values[1],
|
2017-12-20 05:33:33 -06:00
|
|
|
value: values[2],
|
2019-10-31 04:59:30 -05:00
|
|
|
condition: '',
|
2016-09-20 04:57:43 -05:00
|
|
|
};
|
|
|
|
});
|
|
|
|
|
2016-09-19 11:41:42 -05:00
|
|
|
return Promise.resolve();
|
|
|
|
}
|
2016-09-20 04:57:43 -05:00
|
|
|
|
|
|
|
getValueForUrl() {
|
|
|
|
return this.filters.map(filter => {
|
2017-12-19 09:06:54 -06:00
|
|
|
return [filter.key, filter.operator, filter.value]
|
|
|
|
.map(value => {
|
|
|
|
return this.escapeDelimiter(value);
|
|
|
|
})
|
2017-12-20 05:33:33 -06:00
|
|
|
.join('|');
|
2016-09-20 04:57:43 -05:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2019-07-16 04:35:42 -05:00
|
|
|
escapeDelimiter(value: string) {
|
2017-12-20 05:33:33 -06:00
|
|
|
return value.replace(/\|/g, '__gfp__');
|
2017-04-07 02:31:36 -05:00
|
|
|
}
|
|
|
|
|
2019-07-16 04:35:42 -05:00
|
|
|
unescapeDelimiter(value: string) {
|
2017-12-20 05:33:33 -06:00
|
|
|
return value.replace(/__gfp__/g, '|');
|
2017-04-07 02:31:36 -05:00
|
|
|
}
|
|
|
|
|
2016-09-20 04:57:43 -05:00
|
|
|
setFilters(filters: any[]) {
|
|
|
|
this.filters = filters;
|
|
|
|
}
|
2016-09-19 11:41:42 -05:00
|
|
|
}
|
|
|
|
|
2017-12-20 05:33:33 -06:00
|
|
|
variableTypes['adhoc'] = {
|
|
|
|
name: 'Ad hoc filters',
|
2016-09-19 11:41:42 -05:00
|
|
|
ctor: AdhocVariable,
|
2017-12-20 05:33:33 -06:00
|
|
|
description: 'Add key/value filters on the fly',
|
2016-09-19 11:41:42 -05:00
|
|
|
};
|