Skip to content

Commit

Permalink
Backport PR #8597
Browse files Browse the repository at this point in the history
---------

**Commit 1:**
#6537 add color formatting for string fields

* Original sha: ca87c48
* Authored by Marcel Hallmann <[email protected]> on 2016-09-29T12:53:19Z

**Commit 2:**
#6537 adjust test

* Original sha: 23e2ce1
* Authored by marcelhallmann <[email protected]> on 2016-10-05T08:21:24Z

**Commit 3:**
Merge remote-tracking branch 'upstream/master'

* Original sha: bdf66fb
* Authored by Marcel Hallmann <[email protected]> on 2016-10-07T11:19:00Z

**Commit 4:**
#6537 adjust check

* Original sha: a4bf798
* Authored by Marcel Hallmann <[email protected]> on 2016-10-08T17:58:51Z

**Commit 5:**
#6537 add simple check to enable coloring in viz (data table), too

* Original sha: e67f6eb
* Authored by Marcel Hallmann <[email protected]> on 2016-10-08T18:01:12Z

**Commit 6:**
#6537 call the field formatter instead of the toString() function

* Original sha: 7dc7fd5
* Authored by Marcel Hallmann <[email protected]> on 2016-10-08T18:47:39Z

**Commit 7:**
#6537 add some tests for coloring string fields

* Original sha: 9733fe5
* Authored by Marcel Hallmann <[email protected]> on 2016-10-08T18:48:46Z

**Commit 8:**
#6537 better default value for regex field

* Original sha: fed9df2
* Authored by Marcel Hallmann <[email protected]> on 2016-10-08T19:13:40Z

**Commit 9:**
Merge branch 'master' of github.com:elastic/kibana into pr/8597

* Original sha: b583db7
* Authored by spalger <[email protected]> on 2016-10-27T19:31:25Z

**Commit 10:**
[stringify] track field type in params

* Original sha: c84a61b
* Authored by spalger <[email protected]> on 2016-10-27T21:04:19Z
  • Loading branch information
elastic-jasper committed Oct 28, 2016
1 parent fa11c03 commit 2479bc9
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 33 deletions.
67 changes: 47 additions & 20 deletions src/ui/public/stringify/__tests__/_color.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,31 +9,58 @@ describe('Color Format', function () {
beforeEach(ngMock.inject(function (Private) {
fieldFormats = Private(RegistryFieldFormatsProvider);
ColorFormat = fieldFormats.getType('color');

}));

it('should add colors if the value is in range', function () {
let colorer = new ColorFormat({
colors: [{
range: '100:150',
text: 'blue',
background: 'yellow'
}]
context('field is a number', () => {
it('should add colors if the value is in range', function () {
let colorer = new ColorFormat({
fieldType: 'number',
colors: [{
range: '100:150',
text: 'blue',
background: 'yellow'
}]
});
expect(colorer.convert(99, 'html')).to.eql('99');
expect(colorer.convert(100, 'html')).to.eql('<span style="color: blue;background-color: yellow;">100</span>');
expect(colorer.convert(150, 'html')).to.eql('<span style="color: blue;background-color: yellow;">150</span>');
expect(colorer.convert(151, 'html')).to.eql('151');
});

it('should not convert invalid ranges', function () {
let colorer = new ColorFormat({
fieldType: 'number',
colors: [{
range: '100150',
text: 'blue',
background: 'yellow'
}]
});
expect(colorer.convert(99, 'html')).to.eql('99');
});
expect(colorer.convert(99, 'html')).to.eql('99');
expect(colorer.convert(100, 'html')).to.eql('<span style="color: blue;background-color: yellow;">100</span>');
expect(colorer.convert(150, 'html')).to.eql('<span style="color: blue;background-color: yellow;">150</span>');
expect(colorer.convert(151, 'html')).to.eql('151');
});

it('should not convert invalid ranges', function () {
let colorer = new ColorFormat({
colors: [{
range: '100150',
text: 'blue',
background: 'yellow'
}]
context('field is a string', () => {
it('should add colors if the regex matches', function () {
let colorer = new ColorFormat({
fieldType: 'string',
colors: [{
regex: 'A.*',
text: 'blue',
background: 'yellow'
}]
});

let converter = colorer.getConverterFor('html');
expect(converter('B', 'html')).to.eql('B');
expect(converter('AAA', 'html')).to.eql('<span style="color: blue;background-color: yellow;">AAA</span>');
expect(converter('AB', 'html')).to.eql('<span style="color: blue;background-color: yellow;">AB</span>');
expect(converter('a', 'html')).to.eql('a');

expect(converter('B', 'html')).to.eql('B');
expect(converter('AAA', 'html')).to.eql('<span style="color: blue;background-color: yellow;">AAA</span>');
expect(converter('AB', 'html')).to.eql('<span style="color: blue;background-color: yellow;">AB</span>');
expect(converter('a', 'html')).to.eql('a');
});
expect(colorer.convert(99, 'html')).to.eql('99');
});
});
14 changes: 8 additions & 6 deletions src/ui/public/stringify/editors/color.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,18 @@
<button ng-if="editor.formatParams.colors.length > 1" aria-label="Remove Color" ng-click="removeColor($index)" tooltip="Remove Color" tooltip-append-to-body="true" type="button" class="btn btn-xs btn-danger editor-color-remove">
<i aria-hidden="true" class="fa fa-times"></i>
</button>
<div class="form-group">
<label>Range
<small>
(min:max)
</small>
</label>
<div class="form-group" ng-if="editor.formatParams.fieldType === 'number'">
<label>Range <small>(min:max)</small></label>
<input
ng-model="color.range"
class="form-control">
</div>
<div class="form-group" ng-if="editor.formatParams.fieldType === 'string'">
<label>Pattern <small>(regular expression)</small></label>
<input
ng-model="color.regex"
class="form-control">
</div>
<div class="form-group">
<label>Font Color</label>
<input
Expand Down
36 changes: 29 additions & 7 deletions src/ui/public/stringify/types/color.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ export default function ColorFormatProvider(Private) {
const convertTemplate = _.template('<span style="<%- style %>"><%- val %></span>');
const DEFAULT_COLOR = {
range: `${Number.NEGATIVE_INFINITY}:${Number.POSITIVE_INFINITY}`,
regex: '<insert regex>',
text: '#000000',
background: '#ffffff'
};
Expand All @@ -20,12 +21,17 @@ export default function ColorFormatProvider(Private) {
_Color.id = 'color';
_Color.title = 'Color';
_Color.fieldType = [
'number'
'number',
'string'
];

_Color.editor = {
template: colorTemplate,
controller($scope) {
$scope.$watch('editor.field.type', type => {
$scope.editor.formatParams.fieldType = type;
});

$scope.addColor = function () {
$scope.editor.formatParams.colors.push(_.cloneDeep(DEFAULT_COLOR));
};
Expand All @@ -38,17 +44,32 @@ export default function ColorFormatProvider(Private) {


_Color.paramDefaults = {
fieldType: null, // populated by editor, see controller below
colors: [_.cloneDeep(DEFAULT_COLOR)]
};

_Color.prototype.findColorRuleForVal = function (val) {
switch (this.param('fieldType')) {
case 'string':
return _.findLast(this.param('colors'), (colorParam) => {
return new RegExp(colorParam.regex).test(val);
});

case 'number':
return _.findLast(this.param('colors'), ({ range }) => {
if (!range) return;
const [start, end] = range.split(':');
return val >= Number(start) && val <= Number(end);
});

default:
return null;
}
};

_Color.prototype._convert = {
html(val) {
const color = _.findLast(this.param('colors'), ({ range }) => {
if (!range) return;
const [start, end] = range.split(':');
return val >= Number(start) && val <= Number(end);
});

const color = this.findColorRuleForVal(val);
if (!color) return _.asPrettyString(val);

let style = '';
Expand All @@ -58,5 +79,6 @@ export default function ColorFormatProvider(Private) {
}
};


return _Color;
};

0 comments on commit 2479bc9

Please sign in to comment.