Skip to content

Commit e49ba04

Browse files
author
spalger
committed
[aggConfig] unify field option handling
1 parent e9363d2 commit e49ba04

File tree

3 files changed

+45
-36
lines changed

3 files changed

+45
-36
lines changed

src/core_plugins/kibana/public/visualize/editor/agg_params.js

Lines changed: 1 addition & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ uiModules
5959

6060
// create child scope, used in the editors
6161
$aggParamEditorsScope = $scope.$new();
62+
$aggParamEditorsScope.indexedFields = $scope.agg.getFieldOptions();
6263

6364
const agg = $scope.agg;
6465
if (!agg) return;
@@ -81,10 +82,6 @@ uiModules
8182
// build collection of agg params html
8283
type.params.forEach(function (param, i) {
8384
let aggParam;
84-
// if field param exists, compute allowed fields
85-
if (param.name === 'field') {
86-
$aggParamEditorsScope.indexedFields = getIndexedFields(param);
87-
}
8885

8986
if ($aggParamEditorsScope.indexedFields) {
9087
const hasIndexedFields = $aggParamEditorsScope.indexedFields.length > 0;
@@ -136,30 +133,6 @@ uiModules
136133
.append(param.editor)
137134
.get(0);
138135
}
139-
140-
function getIndexedFields(param) {
141-
let fields = _.filter($scope.agg.vis.indexPattern.fields.raw, 'aggregatable');
142-
const fieldTypes = param.filterFieldTypes;
143-
144-
if (fieldTypes) {
145-
fields = $filter('fieldType')(fields, fieldTypes);
146-
fields = $filter('orderBy')(fields, ['type', 'name']);
147-
}
148-
149-
return new IndexedArray({
150-
151-
/**
152-
* @type {Array}
153-
*/
154-
index: ['name'],
155-
156-
/**
157-
* [group description]
158-
* @type {Array}
159-
*/
160-
initialSet: fields
161-
});
162-
}
163136
}
164137
};
165138
});

src/ui/public/agg_types/param_types/field.js

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,10 @@ import { SavedObjectNotFound } from 'ui/errors';
22
import _ from 'lodash';
33
import editorHtml from 'ui/agg_types/controls/field.html';
44
import AggTypesParamTypesBaseProvider from 'ui/agg_types/param_types/base';
5-
export default function FieldAggParamFactory(Private) {
5+
import 'ui/filters/field_type';
6+
import IndexedArray from 'ui/indexed_array';
67

8+
export default function FieldAggParamFactory(Private, $filter) {
79
let BaseAggParam = Private(AggTypesParamTypesBaseProvider);
810

911
_.class(FieldAggParam).inherits(BaseAggParam);
@@ -25,6 +27,28 @@ export default function FieldAggParamFactory(Private) {
2527
return field.name;
2628
};
2729

30+
/**
31+
* Get the options for this field from the indexPattern
32+
*/
33+
FieldAggParam.prototype.getFieldOptions = function (aggConfig) {
34+
const indexPattern = aggConfig.getIndexPattern();
35+
let fields = indexPattern.fields.raw;
36+
37+
fields = fields.filter(f => f.aggregatable);
38+
39+
if (this.filterFieldTypes) {
40+
fields = $filter('fieldType')(fields, this.filterFieldTypes);
41+
}
42+
43+
fields = $filter('orderBy')(fields, ['type', 'name']);
44+
45+
return new IndexedArray({
46+
index: ['name'],
47+
group: ['type'],
48+
initialSet: fields
49+
});
50+
};
51+
2852
/**
2953
* Called to read values from a database record into the
3054
* aggConfig object
@@ -33,7 +57,7 @@ export default function FieldAggParamFactory(Private) {
3357
* @return {field}
3458
*/
3559
FieldAggParam.prototype.deserialize = function (fieldName, aggConfig) {
36-
let field = aggConfig.vis.indexPattern.fields.byName[fieldName];
60+
const field = aggConfig.getIndexPattern().fields.byName[fieldName];
3761

3862
if (!field) {
3963
throw new SavedObjectNotFound('index-pattern-field', fieldName);
@@ -56,7 +80,7 @@ export default function FieldAggParamFactory(Private) {
5680
let field = aggConfig.getField();
5781

5882
if (!field) {
59-
throw new Error(`"${aggConfig.makeLabel()}" requires a field`);
83+
throw new TypeError('"field" is a required parameter');
6084
}
6185

6286
if (field.scripted) {

src/ui/public/vis/agg_config.js

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -148,13 +148,11 @@ export default function AggConfigFactory(Private, fieldTypeFilter) {
148148
* @return {object} the new params object
149149
*/
150150
AggConfig.prototype.resetParams = function () {
151-
let fieldParam = this.type && this.type.params.byName.field;
152151
let field;
152+
const fieldOptions = this.getFieldOptions();
153153

154-
if (fieldParam) {
155-
let prevField = this.params.field;
156-
let fieldOpts = fieldTypeFilter(this.vis.indexPattern.fields, fieldParam.filterFieldTypes);
157-
field = _.contains(fieldOpts, prevField) ? prevField : null;
154+
if (fieldOptions) {
155+
field = fieldOptions.byName[this.fieldName()] || null;
158156
}
159157

160158
return this.fillDefaults({ row: this.params.row, field: field });
@@ -286,6 +284,20 @@ export default function AggConfigFactory(Private, fieldTypeFilter) {
286284
return pre += this.type.makeLabel(this);
287285
};
288286

287+
AggConfig.prototype.getIndexPattern = function () {
288+
return this.vis.indexPattern;
289+
};
290+
291+
AggConfig.prototype.getFieldOptions = function () {
292+
const fieldParamType = this.type && this.type.params.byName.field;
293+
294+
if (!fieldParamType || !fieldParamType.getFieldOptions) {
295+
return null;
296+
}
297+
298+
return fieldParamType.getFieldOptions(this);
299+
};
300+
289301
AggConfig.prototype.fieldFormatter = function (contentType, defaultFormat) {
290302
let format = this.type && this.type.getFormat(this);
291303
if (format) return format.getConverterFor(contentType);

0 commit comments

Comments
 (0)