[aggConfig] unify field option handling

This commit is contained in:
spalger 2016-10-18 12:45:24 -07:00
parent e9363d21d0
commit e49ba04d06
3 changed files with 45 additions and 36 deletions

View file

@ -59,6 +59,7 @@ uiModules
// create child scope, used in the editors
$aggParamEditorsScope = $scope.$new();
$aggParamEditorsScope.indexedFields = $scope.agg.getFieldOptions();
const agg = $scope.agg;
if (!agg) return;
@ -81,10 +82,6 @@ uiModules
// build collection of agg params html
type.params.forEach(function (param, i) {
let aggParam;
// if field param exists, compute allowed fields
if (param.name === 'field') {
$aggParamEditorsScope.indexedFields = getIndexedFields(param);
}
if ($aggParamEditorsScope.indexedFields) {
const hasIndexedFields = $aggParamEditorsScope.indexedFields.length > 0;
@ -136,30 +133,6 @@ uiModules
.append(param.editor)
.get(0);
}
function getIndexedFields(param) {
let fields = _.filter($scope.agg.vis.indexPattern.fields.raw, 'aggregatable');
const fieldTypes = param.filterFieldTypes;
if (fieldTypes) {
fields = $filter('fieldType')(fields, fieldTypes);
fields = $filter('orderBy')(fields, ['type', 'name']);
}
return new IndexedArray({
/**
* @type {Array}
*/
index: ['name'],
/**
* [group description]
* @type {Array}
*/
initialSet: fields
});
}
}
};
});

View file

@ -2,8 +2,10 @@ import { SavedObjectNotFound } from 'ui/errors';
import _ from 'lodash';
import editorHtml from 'ui/agg_types/controls/field.html';
import AggTypesParamTypesBaseProvider from 'ui/agg_types/param_types/base';
export default function FieldAggParamFactory(Private) {
import 'ui/filters/field_type';
import IndexedArray from 'ui/indexed_array';
export default function FieldAggParamFactory(Private, $filter) {
let BaseAggParam = Private(AggTypesParamTypesBaseProvider);
_.class(FieldAggParam).inherits(BaseAggParam);
@ -25,6 +27,28 @@ export default function FieldAggParamFactory(Private) {
return field.name;
};
/**
* Get the options for this field from the indexPattern
*/
FieldAggParam.prototype.getFieldOptions = function (aggConfig) {
const indexPattern = aggConfig.getIndexPattern();
let fields = indexPattern.fields.raw;
fields = fields.filter(f => f.aggregatable);
if (this.filterFieldTypes) {
fields = $filter('fieldType')(fields, this.filterFieldTypes);
}
fields = $filter('orderBy')(fields, ['type', 'name']);
return new IndexedArray({
index: ['name'],
group: ['type'],
initialSet: fields
});
};
/**
* Called to read values from a database record into the
* aggConfig object
@ -33,7 +57,7 @@ export default function FieldAggParamFactory(Private) {
* @return {field}
*/
FieldAggParam.prototype.deserialize = function (fieldName, aggConfig) {
let field = aggConfig.vis.indexPattern.fields.byName[fieldName];
const field = aggConfig.getIndexPattern().fields.byName[fieldName];
if (!field) {
throw new SavedObjectNotFound('index-pattern-field', fieldName);
@ -56,7 +80,7 @@ export default function FieldAggParamFactory(Private) {
let field = aggConfig.getField();
if (!field) {
throw new Error(`"${aggConfig.makeLabel()}" requires a field`);
throw new TypeError('"field" is a required parameter');
}
if (field.scripted) {

View file

@ -148,13 +148,11 @@ export default function AggConfigFactory(Private, fieldTypeFilter) {
* @return {object} the new params object
*/
AggConfig.prototype.resetParams = function () {
let fieldParam = this.type && this.type.params.byName.field;
let field;
const fieldOptions = this.getFieldOptions();
if (fieldParam) {
let prevField = this.params.field;
let fieldOpts = fieldTypeFilter(this.vis.indexPattern.fields, fieldParam.filterFieldTypes);
field = _.contains(fieldOpts, prevField) ? prevField : null;
if (fieldOptions) {
field = fieldOptions.byName[this.fieldName()] || null;
}
return this.fillDefaults({ row: this.params.row, field: field });
@ -286,6 +284,20 @@ export default function AggConfigFactory(Private, fieldTypeFilter) {
return pre += this.type.makeLabel(this);
};
AggConfig.prototype.getIndexPattern = function () {
return this.vis.indexPattern;
};
AggConfig.prototype.getFieldOptions = function () {
const fieldParamType = this.type && this.type.params.byName.field;
if (!fieldParamType || !fieldParamType.getFieldOptions) {
return null;
}
return fieldParamType.getFieldOptions(this);
};
AggConfig.prototype.fieldFormatter = function (contentType, defaultFormat) {
let format = this.type && this.type.getFormat(this);
if (format) return format.getConverterFor(contentType);