Skip to content

Commit

Permalink
de-angularize indexPatterns Field and FieldList (#20589)
Browse files Browse the repository at this point in the history
* de-angularize indexPatterns Field and FieldList

* do not remove shortDots angular filter

* remove ui/chrome mock in _index_pattern test
  • Loading branch information
nreese authored Jul 13, 2018
1 parent 92b432e commit 16e9a7e
Show file tree
Hide file tree
Showing 9 changed files with 93 additions and 135 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,12 @@ import _ from 'lodash';
import $ from 'jquery';
import rison from 'rison-node';
import { fieldCalculator } from './lib/field_calculator';
import { IndexPatternsFieldListProvider } from 'ui/index_patterns/_field_list';
import { FieldList } from 'ui/index_patterns/_field_list';
import { uiModules } from 'ui/modules';
import fieldChooserTemplate from './field_chooser.html';
const app = uiModules.get('apps/discover');

app.directive('discFieldChooser', function ($location, globalState, config, $route, Private) {
const FieldList = Private(IndexPatternsFieldListProvider);

app.directive('discFieldChooser', function ($location, globalState, config, $route) {
return {
restrict: 'E',
scope: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
* under the License.
*/

import { IndexPatternsFieldProvider } from 'ui/index_patterns/_field';
import { Field } from 'ui/index_patterns/_field';
import { RegistryFieldFormatEditorsProvider } from 'ui/registry/field_format_editors';
import { KbnUrlProvider } from 'ui/url';
import uiRoutes from 'ui/routes';
Expand Down Expand Up @@ -90,7 +90,6 @@ uiRoutes
},
controllerAs: 'fieldSettings',
controller: function FieldEditorPageController($scope, $route, $timeout, $http, Private, docTitle, config) {
const Field = Private(IndexPatternsFieldProvider);
const getConfig = (...args) => config.get(...args);
const fieldFormatEditors = Private(RegistryFieldFormatEditorsProvider);
const kbnUrl = Private(KbnUrlProvider);
Expand Down
3 changes: 1 addition & 2 deletions src/test_utils/public/stub_index_pattern.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,11 @@ import { formatHit } from 'ui/index_patterns/_format_hit';
import { getComputedFields } from 'ui/index_patterns/_get_computed_fields';
import { fieldFormats } from 'ui/registry/field_formats';
import { IndexPatternsFlattenHitProvider } from 'ui/index_patterns/_flatten_hit';
import { IndexPatternsFieldListProvider } from 'ui/index_patterns/_field_list';
import { FieldList } from 'ui/index_patterns/_field_list';

export default function (Private) {

const flattenHit = Private(IndexPatternsFlattenHitProvider);
const FieldList = Private(IndexPatternsFieldListProvider);
const IndexPattern = Private(IndexPatternProvider);

function StubIndexPattern(pattern, timeField, fields) {
Expand Down
12 changes: 0 additions & 12 deletions src/ui/public/index_patterns/__tests__/_index_pattern.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,18 +63,6 @@ jest.mock('../_intervals', () => ({
IndexPatternsIntervalsProvider: jest.fn(),
}));

jest.mock('../_field_list', () => ({
IndexPatternsFieldListProvider: jest.fn().mockImplementation((pattern) => {
return {
byName: {
id: { value: pattern.id },
title: { value: pattern.title },
},
every: jest.fn(),
};
})
}));

jest.mock('../_flatten_hit', () => ({
IndexPatternsFlattenHitProvider: jest.fn(),
}));
Expand Down
173 changes: 84 additions & 89 deletions src/ui/public/index_patterns/_field.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,102 +21,97 @@ import { ObjDefine } from '../utils/obj_define';
import { FieldFormat } from '../../field_formats/field_format';
import { fieldFormats } from '../registry/field_formats';
import { getKbnFieldType } from '../../../utils';
import { shortenDottedString } from '../../../core_plugins/kibana/common/utils/shorten_dotted_string';
import { toastNotifications } from 'ui/notify';
import chrome from 'ui/chrome';

export function IndexPatternsFieldProvider(Private, shortDotsFilter, $rootScope, Notifier) {
const notify = new Notifier({ location: 'IndexPattern Field' });
export function Field(indexPattern, spec) {
// unwrap old instances of Field
if (spec instanceof Field) spec = spec.$$spec;

// construct this object using ObjDefine class, which
// extends the Field.prototype but gets it's properties
// defined using the logic below
const obj = new ObjDefine(spec, Field.prototype);

function Field(indexPattern, spec) {
// unwrap old instances of Field
if (spec instanceof Field) spec = spec.$$spec;
if (spec.name === '_source') {
spec.type = '_source';
}

// construct this object using ObjDefine class, which
// extends the Field.prototype but gets it's properties
// defined using the logic below
const obj = new ObjDefine(spec, Field.prototype);
// find the type for this field, fallback to unknown type
let type = getKbnFieldType(spec.type);
if (spec.type && !type) {
toastNotifications.addDanger({
title: `Unknown field type ${spec.type}`,
text: `Field ${spec.name} in indexPattern ${indexPattern.title} is using an unknown field type.`
});
}

if (spec.name === '_source') {
spec.type = '_source';
}
if (!type) type = getKbnFieldType('unknown');

// find the type for this field, fallback to unknown type
let type = getKbnFieldType(spec.type);
if (spec.type && !type) {
notify.error(
'Unknown field type "' + spec.type + '"' +
' for field "' + spec.name + '"' +
' in indexPattern "' + indexPattern.title + '"'
);
}
let format = spec.format;
if (!format || !(format instanceof FieldFormat)) {
format = indexPattern.fieldFormatMap[spec.name] || fieldFormats.getDefaultInstance(spec.type);
}

if (!type) type = getKbnFieldType('unknown');
const indexed = !!spec.indexed;
const scripted = !!spec.scripted;
const searchable = !!spec.searchable || scripted;
const aggregatable = !!spec.aggregatable || scripted;
const readFromDocValues = !!spec.readFromDocValues && !scripted;
const sortable = spec.name === '_score' || ((indexed || aggregatable) && type.sortable);
const filterable = spec.name === '_id' || scripted || ((indexed || searchable) && type.filterable);
const visualizable = aggregatable;

obj.fact('name');
obj.fact('type');
obj.writ('count', spec.count || 0);

// scripted objs
obj.fact('scripted', scripted);
obj.writ('script', scripted ? spec.script : null);
obj.writ('lang', scripted ? (spec.lang || 'painless') : null);

// stats
obj.fact('searchable', searchable);
obj.fact('aggregatable', aggregatable);
obj.fact('readFromDocValues', readFromDocValues);

// usage flags, read-only and won't be saved
obj.comp('format', format);
obj.comp('sortable', sortable);
obj.comp('filterable', filterable);
obj.comp('visualizable', visualizable);

// computed values
obj.comp('indexPattern', indexPattern);
obj.comp('displayName', chrome.getUiSettingsClient().get('shortDots:enable') ? shortenDottedString(spec.name) : spec.name);
obj.comp('$$spec', spec);

// conflict info
obj.writ('conflictDescriptions');

return obj.create();
}

let format = spec.format;
if (!format || !(format instanceof FieldFormat)) {
format = indexPattern.fieldFormatMap[spec.name] || fieldFormats.getDefaultInstance(spec.type);
Object.defineProperties(Field.prototype, {
indexed: {
get() {
throw new Error('field.indexed has been removed, see https://github.com/elastic/kibana/pull/11969');
}
},
analyzed: {
get() {
throw new Error('field.analyzed has been removed, see https://github.com/elastic/kibana/pull/11969');
}
},
doc_values: {
get() {
throw new Error('field.doc_values has been removed, see https://github.com/elastic/kibana/pull/11969');
}
},
});

const indexed = !!spec.indexed;
const scripted = !!spec.scripted;
const searchable = !!spec.searchable || scripted;
const aggregatable = !!spec.aggregatable || scripted;
const readFromDocValues = !!spec.readFromDocValues && !scripted;
const sortable = spec.name === '_score' || ((indexed || aggregatable) && type.sortable);
const filterable = spec.name === '_id' || scripted || ((indexed || searchable) && type.filterable);
const visualizable = aggregatable;

obj.fact('name');
obj.fact('type');
obj.writ('count', spec.count || 0);

// scripted objs
obj.fact('scripted', scripted);
obj.writ('script', scripted ? spec.script : null);
obj.writ('lang', scripted ? (spec.lang || 'painless') : null);

// stats
obj.fact('searchable', searchable);
obj.fact('aggregatable', aggregatable);
obj.fact('readFromDocValues', readFromDocValues);

// usage flags, read-only and won't be saved
obj.comp('format', format);
obj.comp('sortable', sortable);
obj.comp('filterable', filterable);
obj.comp('visualizable', visualizable);

// computed values
obj.comp('indexPattern', indexPattern);
obj.comp('displayName', shortDotsFilter(spec.name));
obj.comp('$$spec', spec);

// conflict info
obj.writ('conflictDescriptions');

return obj.create();
}

Object.defineProperties(Field.prototype, {
indexed: {
get() {
throw new Error('field.indexed has been removed, see https://github.com/elastic/kibana/pull/11969');
}
},
analyzed: {
get() {
throw new Error('field.analyzed has been removed, see https://github.com/elastic/kibana/pull/11969');
}
},
doc_values: {
get() {
throw new Error('field.doc_values has been removed, see https://github.com/elastic/kibana/pull/11969');
}
},
});

Field.prototype.routes = {
edit: '/management/kibana/indices/{{indexPattern.id}}/field/{{name}}'
};

return Field;
}
Field.prototype.routes = {
edit: '/management/kibana/indices/{{indexPattern.id}}/field/{{name}}'
};
14 changes: 4 additions & 10 deletions src/ui/public/index_patterns/_field_list.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,22 +18,16 @@
*/

import { IndexedArray } from '../indexed_array';
import { IndexPatternsFieldProvider } from './_field';
import { createLegacyClass } from '../utils/legacy_class';
import { Field } from './_field';

export function IndexPatternsFieldListProvider(Private) {
const Field = Private(IndexPatternsFieldProvider);

createLegacyClass(FieldList).inherits(IndexedArray);
function FieldList(indexPattern, specs) {
FieldList.Super.call(this, {
export class FieldList extends IndexedArray {
constructor(indexPattern, specs) {
super({
index: ['name'],
group: ['type'],
initialSet: specs.map(function (field) {
return new Field(indexPattern, field);
})
});
}

return FieldList;
}
3 changes: 1 addition & 2 deletions src/ui/public/index_patterns/_index_pattern.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import { getComputedFields } from './_get_computed_fields';
import { formatHit } from './_format_hit';
import { IndexPatternsGetProvider } from './_get';
import { IndexPatternsIntervalsProvider } from './_intervals';
import { IndexPatternsFieldListProvider } from './_field_list';
import { FieldList } from './_field_list';
import { IndexPatternsFlattenHitProvider } from './_flatten_hit';
import { IndexPatternsPatternCacheProvider } from './_pattern_cache';
import { FieldsFetcherProvider } from './fields_fetcher_provider';
Expand All @@ -53,7 +53,6 @@ export function IndexPatternProvider(Private, config, Promise, confirmModalPromi
const fieldsFetcher = Private(FieldsFetcherProvider);
const intervals = Private(IndexPatternsIntervalsProvider);
const mappingSetup = Private(UtilsMappingSetupProvider);
const FieldList = Private(IndexPatternsFieldListProvider);
const flattenHit = Private(IndexPatternsFlattenHitProvider);
const patternCache = Private(IndexPatternsPatternCacheProvider);
const isUserAwareOfUnsupportedTimePattern = Private(IsUserAwareOfUnsupportedTimePatternProvider);
Expand Down
13 changes: 0 additions & 13 deletions src/ui/public/index_patterns/_object.tmpl.html

This file was deleted.

1 change: 0 additions & 1 deletion src/ui/public/index_patterns/index_patterns.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
* under the License.
*/

import '../filters/short_dots';
import { IndexPatternMissingIndices } from '../errors';
import { IndexPatternProvider } from './_index_pattern';
import { IndexPatternsPatternCacheProvider } from './_pattern_cache';
Expand Down

0 comments on commit 16e9a7e

Please sign in to comment.