Skip to content

Commit

Permalink
Remove private module dependencies from field formatters (#12239)
Browse files Browse the repository at this point in the history
* remove Private module loader from FieldFormat

* remove dependency on bound_to_config_obj

* update field_formats registry getDefaultInstanc and getInstance to pass getConfig to FieldFormat constructors

* make previous changes work

* remove paramDefaults state from FieldFormat class, instead use getter

* remove use of shortDotsFilter in source field formatter

* fix broken tests

* remove angular dependency from field formatters

* fix _conformance test

* getConfig service and replace with inline function

* remove unneeded ngMock from __tests__/_field_format.js

* use JSON.stringify in content_types instead of aggressive_parse.tojson because function is only converting an array of strings to into a string

* pass getParamDefaults to _numeral in opts argument

* update field format editors to handle moving paramDefaults from class to instance via getter

* remove unneeded reference to self

* change to arrow functions

* change content_type export from object to only the setup function

* remove unneeded function block around simple arrow function
  • Loading branch information
nreese authored Jun 19, 2017
1 parent 65c4bb2 commit 86d32da
Show file tree
Hide file tree
Showing 26 changed files with 291 additions and 300 deletions.
3 changes: 1 addition & 2 deletions src/ui/public/agg_types/buckets/range.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
import { AggTypesBucketsBucketAggTypeProvider } from 'ui/agg_types/buckets/_bucket_agg_type';
import { AggTypesBucketsCreateFilterRangeProvider } from 'ui/agg_types/buckets/create_filter/range';
import { IndexPatternsFieldFormatProvider } from 'ui/index_patterns/_field_format/field_format';
import { FieldFormat } from 'ui/index_patterns/_field_format/field_format';
import { RangeKeyProvider } from './range_key';
import rangesTemplate from 'ui/agg_types/controls/ranges.html';

export function AggTypesBucketsRangeProvider(Private) {
const BucketAggType = Private(AggTypesBucketsBucketAggTypeProvider);
const createFilter = Private(AggTypesBucketsCreateFilterRangeProvider);
const FieldFormat = Private(IndexPatternsFieldFormatProvider);
const RangeKey = Private(RangeKeyProvider);

const keyCaches = new WeakMap();
Expand Down
9 changes: 6 additions & 3 deletions src/ui/public/field_editor/field_editor.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ import { getKbnTypeNames } from '../../../utils';

uiModules
.get('kibana', ['colorpicker.module'])
.directive('fieldEditor', function (Private, $sce, confirmModal) {
.directive('fieldEditor', function (Private, $sce, confirmModal, config) {
const getConfig = (...args) => config.get(...args);
const fieldFormats = Private(RegistryFieldFormatsProvider);
const Field = Private(IndexPatternsFieldProvider);
const getEnabledScriptingLanguages = Private(GetEnabledScriptingLanguagesProvider);
Expand Down Expand Up @@ -111,12 +112,14 @@ uiModules
if (!changedFormat || !missingFormat) return;

// reset to the defaults, but make sure it's an object
self.formatParams = _.assign({}, _.cloneDeep(getFieldFormatType().paramDefaults));
const FieldFormat = getFieldFormatType();
const paramDefaults = new FieldFormat({}, getConfig).getParamDefaults();
self.formatParams = _.assign({}, _.cloneDeep(paramDefaults));
});

$scope.$watch('editor.formatParams', function () {
const FieldFormat = getFieldFormatType();
self.field.format = new FieldFormat(self.formatParams);
self.field.format = new FieldFormat(self.formatParams, getConfig);
}, true);

$scope.$watch('editor.field.type', function (newValue) {
Expand Down
4 changes: 2 additions & 2 deletions src/ui/public/field_format_editor/numeral/numeral.html
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,14 @@
<label>
Numeral.js format pattern
<small>
(Default: "{{ editor.field.format.type.paramDefaults.pattern }}")
(Default: "{{ editor.field.format.getParamDefaults().pattern }}")
</small>
</label>
</div>

<field-format-editor-pattern
ng-model="editor.formatParams.pattern"
placeholder="editor.field.format.type.paramDefaults.pattern"
placeholder="editor.field.format.getParamDefaults().pattern"
inputs="cntrl.sampleInputs">
</field-format-editor-pattern>

19 changes: 11 additions & 8 deletions src/ui/public/index_patterns/__tests__/_field_format.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,12 @@
import _ from 'lodash';
import expect from 'expect.js';
import ngMock from 'ng_mock';
import { IndexPatternsFieldFormatProvider } from 'ui/index_patterns/_field_format/field_format';
import { FieldFormat } from 'ui/index_patterns/_field_format/field_format';

describe('FieldFormat class', function () {

let FieldFormat;
let TestFormat;

beforeEach(ngMock.module('kibana'));
beforeEach(ngMock.inject(function (Private) {
FieldFormat = Private(IndexPatternsFieldFormatProvider);
beforeEach(function () {

TestFormat = function (params) {
TestFormat.Super.call(this, params);
Expand All @@ -21,7 +17,7 @@ describe('FieldFormat class', function () {
TestFormat.prototype._convert = _.asPrettyString;

_.class(TestFormat).inherits(FieldFormat);
}));
});

describe('params', function () {
it('accepts its params via the constructor', function () {
Expand Down Expand Up @@ -53,7 +49,9 @@ describe('FieldFormat class', function () {
});

it('removes param values that match the defaults', function () {
TestFormat.paramDefaults = { foo: 'bar' };
TestFormat.prototype.getParamDefaults = function () {
return { foo: 'bar' };
};

const f = new TestFormat({ foo: 'bar', baz: 'bar' });
const ser = JSON.parse(JSON.stringify(f));
Expand Down Expand Up @@ -154,6 +152,11 @@ describe('FieldFormat class', function () {
const f = new TestFormat();
expect(f.convert()).to.be(' - ');
});

it('formats a list of values as text', function () {
const f = new TestFormat();
expect(f.convert(['one', 'two', 'three'])).to.be('["one","two","three"]');
});
});

});
Expand Down
3 changes: 1 addition & 2 deletions src/ui/public/index_patterns/_field.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
import { ObjDefine } from 'ui/utils/obj_define';
import { IndexPatternsFieldFormatProvider } from 'ui/index_patterns/_field_format/field_format';
import { FieldFormat } from 'ui/index_patterns/_field_format/field_format';
import { RegistryFieldFormatsProvider } from 'ui/registry/field_formats';
import { getKbnFieldType } from '../../../utils';

export function IndexPatternsFieldProvider(Private, shortDotsFilter, $rootScope, Notifier) {
const notify = new Notifier({ location: 'IndexPattern Field' });
const FieldFormat = Private(IndexPatternsFieldFormatProvider);
const fieldFormats = Private(RegistryFieldFormatsProvider);

function Field(indexPattern, spec) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import sinon from 'sinon';
import expect from 'expect.js';
import { escape } from 'lodash';

import { IndexPatternsFieldFormatContentTypesProvider } from '../content_types';
import { contentTypesSetup } from '../content_types';

describe('index_patterns/_field_format/content_types', () => {

Expand All @@ -27,15 +27,14 @@ describe('index_patterns/_field_format/content_types', () => {

beforeEach(ngMock.module('testApp'));
beforeEach(ngMock.inject(($injector) => {
const contentTypes = new IndexPatternsFieldFormatContentTypesProvider();
const $rootScope = $injector.get('$rootScope');
const $compile = $injector.get('$compile');

$rootScope.callMe = callMe;

render = (convert) => {
const $el = $('<div>');
const { html } = contentTypes.setup({ _convert: { html: convert } });
const { html } = contentTypesSetup({ _convert: { html: convert } });
$compile($el.html(html(`
<!-- directive: test-directive -->
<div></div>
Expand Down
101 changes: 46 additions & 55 deletions src/ui/public/index_patterns/_field_format/content_types.js
Original file line number Diff line number Diff line change
@@ -1,73 +1,64 @@
import _ from 'lodash';
import angular from 'angular';
import { getHighlightHtml } from 'ui/highlight';

export function IndexPatternsFieldFormatContentTypesProvider() {

const types = {
html: function (format, convert) {
function recurse(value, field, hit) {
if (value == null) {
return _.asPrettyString(value);
}

if (!value || typeof value.map !== 'function') {
return convert.call(format, value, field, hit);
}

const subVals = value.map(function (v) {
return recurse(v, field, hit);
});
const useMultiLine = subVals.some(function (sub) {
return sub.indexOf('\n') > -1;
});

return subVals.join(',' + (useMultiLine ? '\n' : ' '));
const types = {
html: function (format, convert) {
function recurse(value, field, hit) {
if (value == null) {
return _.asPrettyString(value);
}

return function (...args) {
return `<span ng-non-bindable>${recurse(...args)}</span>`;
};
},
if (!value || typeof value.map !== 'function') {
return convert.call(format, value, field, hit);
}

text: function (format, convert) {
return function recurse(value) {
if (!value || typeof value.map !== 'function') {
return convert.call(format, value);
}
const subVals = value.map(v => {
return recurse(v, field, hit);
});
const useMultiLine = subVals.some(sub => {
return sub.indexOf('\n') > -1;
});

// format a list of values. In text contexts we just use JSON encoding
return angular.toJson(value.map(recurse), true);
};
return subVals.join(',' + (useMultiLine ? '\n' : ' '));
}
};

function fallbackText(value) {
return _.asPrettyString(value);
}
return function (...args) {
return `<span ng-non-bindable>${recurse(...args)}</span>`;
};
},

function fallbackHtml(value, field, hit) {
const formatted = _.escape(this.convert(value, 'text'));
text: function (format, convert) {
return function recurse(value) {
if (!value || typeof value.map !== 'function') {
return convert.call(format, value);
}

if (!hit || !hit.highlight || !hit.highlight[field.name]) {
return formatted;
} else {
return getHighlightHtml(formatted, hit.highlight[field.name]);
}
// format a list of values. In text contexts we just use JSON encoding
return JSON.stringify(value.map(recurse));
};
}
};

function setup(format) {
const src = format._convert || {};
const converters = format._convert = {};
function fallbackText(value) {
return _.asPrettyString(value);
}

converters.text = types.text(format, src.text || fallbackText);
converters.html = types.html(format, src.html || fallbackHtml);
function fallbackHtml(value, field, hit) {
const formatted = _.escape(this.convert(value, 'text'));

return format._convert;
if (!hit || !hit.highlight || !hit.highlight[field.name]) {
return formatted;
} else {
return getHighlightHtml(formatted, hit.highlight[field.name]);
}
}

export function contentTypesSetup(format) {
const src = format._convert || {};
const converters = format._convert = {};

converters.text = types.text(format, src.text || fallbackText);
converters.html = types.html(format, src.html || fallbackHtml);

return {
types: types,
setup: setup
};
return format._convert;
}
Loading

0 comments on commit 86d32da

Please sign in to comment.