Skip to content

Commit

Permalink
[stringify] track field type in params
Browse files Browse the repository at this point in the history
  • Loading branch information
spalger committed Oct 27, 2016
1 parent b583db7 commit 4f1ec19
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 24 deletions.
6 changes: 1 addition & 5 deletions src/ui/public/directives/rows.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,7 @@ module.directive('kbnRows', function ($compile, $rootScope, getAppState, Private
if (contents.type === 'bucket' && contents.aggConfig.getField() && contents.aggConfig.getField().filterable) {
$cell = createAggConfigResultCell(contents);
}

let formatter = contents.aggConfig.fieldFormatter('html');
contents = formatter(contents.value,
contents.aggConfig.params && contents.aggConfig.params.field && contents.aggConfig.params.field.type);

contents = contents.toString('html');
}

if (_.isObject(contents)) {
Expand Down
12 changes: 4 additions & 8 deletions src/ui/public/stringify/editors/color.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,14 @@
<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" ng-if="'number' === editor.field.type ">
<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="'string' === editor.field.type">
<label>Regex</label>
<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">
Expand Down
32 changes: 21 additions & 11 deletions src/ui/public/stringify/types/color.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ export default function ColorFormatProvider(Private) {
const FieldFormat = Private(IndexPatternsFieldFormatProvider);
const convertTemplate = _.template('<span style="<%- style %>"><%- val %></span>');
const DEFAULT_COLOR = {
fieldType: null, // populated by editor, see controller below
range: `${Number.NEGATIVE_INFINITY}:${Number.POSITIVE_INFINITY}`,
regex: '<insert regex>',
text: '#000000',
Expand All @@ -21,12 +22,17 @@ export default function ColorFormatProvider(Private) {
_Color.id = 'color';
_Color.title = 'Color';
_Color.fieldType = [
'number', 'string'
'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 @@ -42,24 +48,28 @@ export default function ColorFormatProvider(Private) {
colors: [_.cloneDeep(DEFAULT_COLOR)]
};

_Color.prototype._convert = {
html(val, field) {

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

else {
color = _.findLast(this.param('colors'), ({ range }) => {
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 = this.findColorRuleForVal(val);
if (!color) return _.asPrettyString(val);

let style = '';
Expand Down

0 comments on commit 4f1ec19

Please sign in to comment.