-
Notifications
You must be signed in to change notification settings - Fork 8.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Color field format #4594
Color field format #4594
Changes from 15 commits
acf6e36
3cbe961
d7691a3
97c0f4a
003017a
b1f7438
1608756
f836516
fa26f82
b1d40dc
91356e5
2895dab
d58d14d
fee4e0d
f86145f
8221872
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
describe('Color Format', function () { | ||
var fieldFormats; | ||
var ColorFormat; | ||
var expect = require('expect.js'); | ||
var ngMock = require('ngMock'); | ||
|
||
beforeEach(ngMock.module('kibana')); | ||
beforeEach(ngMock.inject(function (Private) { | ||
fieldFormats = Private(require('ui/registry/field_formats')); | ||
ColorFormat = fieldFormats.getType('color'); | ||
|
||
})); | ||
|
||
it('should add colors if the value is in range', function () { | ||
var colorer = new ColorFormat({ | ||
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 () { | ||
var colorer = new ColorFormat({ | ||
colors: [{ | ||
range: '100150', | ||
text: 'blue', | ||
background: 'yellow' | ||
}] | ||
}); | ||
expect(colorer.convert(99, 'html')).to.eql('99'); | ||
}); | ||
}); |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,6 +12,7 @@ var formatIds = [ | |
'ip', | ||
'number', | ||
'percent', | ||
'color', | ||
'string', | ||
'url', | ||
'_source' | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
<div class="form-group"> | ||
<div ng-repeat="color in editor.formatParams.colors"> | ||
<div class="editor-color"> | ||
<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> | ||
<input | ||
ng-model="color.range" | ||
class="form-control"> | ||
</div> | ||
<div class="form-group"> | ||
<label>Font Color</label> | ||
<input | ||
ng-model="color.text" | ||
colorpicker | ||
type="text" | ||
class="form-control"> | ||
</div> | ||
<div class="form-group"> | ||
<label>Background Color</label> | ||
<input | ||
ng-model="color.background" | ||
colorpicker | ||
type="text" | ||
class="form-control"> | ||
</div> | ||
<div class="form-group"> | ||
<label>Example</label> | ||
<div class="form-control" | ||
ng-style="{color: color.text, 'background-color': color.background}" | ||
value="formatted"> | ||
123456 | ||
</div> | ||
</div> | ||
|
||
</div> | ||
<hr> | ||
</div> | ||
|
||
<button aria-label="Add Color" ng-click="addColor()" tooltip="Add Color" tooltip-append-to-body="true" type="button" class="btn btn-primary btn-xs"> | ||
<span class="sr-only">Add Color</span> | ||
<i aria-hidden="true" class="fa fa-plus"></i> Add Color | ||
</button> | ||
|
||
</div> |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
.editor-color { | ||
display: flex; | ||
position: relative; | ||
> .form-group { | ||
flex: 1 1 1%; | ||
padding-right: 5px; | ||
&:last-child { | ||
padding-right: 0; | ||
} | ||
} | ||
} | ||
|
||
.editor-color-remove { | ||
position: absolute; | ||
right: 0; | ||
top: -10px; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
define(function (require) { | ||
return function _StringProvider(Private) { | ||
var _ = require('lodash'); | ||
var FieldFormat = Private(require('ui/index_patterns/_field_format/FieldFormat')); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same as above |
||
|
||
require('ui/stringify/editors/color.less'); | ||
|
||
_.class(_Color).inherits(FieldFormat); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can't you use new es6 class and inherits here? Will probably be way shorter. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'd like to leave this one for consistency with the other formatters if possible. It would be a fairly substantial looking change. Thoughts? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That's cool. |
||
function _Color(params) { | ||
_Color.Super.call(this, params); | ||
} | ||
|
||
_Color.id = 'color'; | ||
_Color.title = 'Color'; | ||
_Color.fieldType = [ | ||
'number' | ||
]; | ||
|
||
_Color.editor = { | ||
template: require('ui/stringify/editors/color.html'), | ||
controller($scope) { | ||
$scope.addColor = function () { | ||
$scope.editor.formatParams.colors.push({}); | ||
}; | ||
|
||
$scope.removeColor = function (index) { | ||
$scope.editor.formatParams.colors.splice(index, 1); | ||
}; | ||
} | ||
}; | ||
|
||
_Color.paramDefaults = { | ||
colors: [{ | ||
range: `${Number.NEGATIVE_INFINITY}:${Number.POSITIVE_INFINITY}`, | ||
text: '#000000', | ||
background: '#ffffff' | ||
}] | ||
}; | ||
|
||
_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); | ||
}); | ||
|
||
if (!color) return _.asPrettyString(val); | ||
|
||
const styleColor = color.text ? `color: ${color.text};` : ''; | ||
const styleBackgroundColor = color.background ? `background-color: ${color.background};` : ''; | ||
return `<span style="${styleColor}${styleBackgroundColor}">${_.escape(val)}</span>`; | ||
} | ||
}; | ||
|
||
return _Color; | ||
}; | ||
}); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Const?