[stringify/source] added basic _source format

This commit is contained in:
Spencer Alger 2015-04-30 05:37:26 -07:00
parent c90996e7e7
commit ed667d8661
4 changed files with 87 additions and 0 deletions

View file

@ -7,4 +7,5 @@ define(function (require) {
fieldFormats.register(require('components/stringify/types/Number'));
fieldFormats.register(require('components/stringify/types/Percent'));
fieldFormats.register(require('components/stringify/types/String'));
fieldFormats.register(require('components/stringify/types/Source'));
});

View file

@ -0,0 +1,59 @@
define(function (require) {
return function _StringProvider(Private, shortDotsFilter, highlightFilter) {
var _ = require('lodash');
var FieldFormat = Private(require('components/index_patterns/_field_format/FieldFormat'));
var noWhiteSpace = require('utils/no_white_space');
var template = _.template(noWhiteSpace(require('text!components/stringify/types/_source.html')));
_(Source).inherits(FieldFormat);
function Source(params) {
var self = this;
// _source converters are weird and override the _convert object
// that is setup by the FieldFormat constructor
Source.prototype._convert = {};
Source.Super.call(self, params);
function sourceToText(source) {
return _.escape(JSON.stringify(source));
}
function sourceToHtml(source, indexPattern, hit) {
if (!indexPattern) return sourceToText(source);
var highlights = (hit && hit.highlights) || {};
var formatted = indexPattern.formatHit(hit);
var highlightPairs = [];
var sourcePairs = [];
_.keys(formatted).forEach(function (key) {
var pairs = sourcePairs;
var field = shortDotsFilter(key);
var val = formatted[key];
if (highlights[key]) {
pairs = highlightPairs;
val = highlightFilter(val, highlights[key]);
}
pairs.push([field, val]);
}, []);
return template({ defPairs: highlightPairs.concat(sourcePairs) });
}
self._convert = {
text: sourceToText,
html: sourceToHtml
};
}
Source.id = '_source';
Source.title = '_source';
Source.fieldType = '_source';
return Source;
};
});

View file

@ -0,0 +1,7 @@
<dl class="source truncate-by-height">
<% defPairs.forEach(function (def) { %>
<dt><%- def[0] %>:</dt>
<dd><%= def[1] %></dd>
<%= ' ' %>
<% }); %>
</dl>

View file

@ -183,7 +183,27 @@ define(function (require) {
});
});
});
describe('Source format', function () {
var indexPattern;
var hits;
var format;
var convertHtml;
beforeEach(inject(function (Private) {
indexPattern = Private(require('fixtures/stubbed_logstash_index_pattern'));
hits = Private(require('fixtures/hits'));
format = fieldFormats.getInstance('_source');
convertHtml = format.getConverterFor('html');
}));
it('uses the hit source, indexPattern, and the hit itself to create a <dl>', function () {
var hit = _.first(hits);
var $dl = $(convertHtml(hit._source, indexPattern, hit));
expect($dl.is('dl')).to.be.ok();
expect($dl.find('dt')).to.have.length(_.keys(indexPattern.flattenHit(hit)).length);
});
});
});
});