import { template , escape , keys } from 'lodash' ; import { shortenDottedString } from '../../utils' ; import { OSD_FIELD_TYPES } from '../../osd_field_types/types' ; import { FieldFormat } from '../field_format' ; import { TextContextTypeConvert , HtmlContextTypeConvert , FIELD_FORMAT_IDS } from '../types' ; import { UI_SETTINGS } from '../../constants' ; param { string } html return { string } function noWhiteSpace ( html ) { const TAGS_WITH_WS = />\s+</g ; return html . replace ( TAGS_WITH_WS , '><' ) ; } const templateHtml = `
  <dl class="source truncate-by-height">
    <% defPairs.forEach(function (def) { %>
      <dt><%- def[0] %>:</dt>
      <dd><%= def[1] %></dd>
      <%= ' ' %>
    <% }); %>
  </dl>` ; const doTemplate = template ( noWhiteSpace ( templateHtml ) ) ; export class SourceFormat extends FieldFormat { static id = FIELD_FORMAT_IDS . _SOURCE ; static title = '_source' ; static fieldType = OSD_FIELD_TYPES . _SOURCE ; textConvert : TextContextTypeConvert = ( value ) => JSON . stringify ( value ) ; htmlConvert : HtmlContextTypeConvert = ( value , options = { } ) => { const { field , hit , indexPattern } = options ; if ( ! field ) { const converter = this . getConverterFor ( 'text' ) as Function ; return escape ( converter ( value ) ) ; } const highlights = ( hit && hit . highlight ) || { } ; const formatted = indexPattern . formatHit ( hit ) ; const highlightPairs = [ ] ; const sourcePairs = [ ] ; const isShortDots = this . getConfig ! ( UI_SETTINGS . SHORT_DOTS_ENABLE ) ; keys ( formatted ) . forEach ( ( key ) => { const pairs = highlights [ key ] ? highlightPairs : sourcePairs ; const newField = isShortDots ? shortenDottedString ( key ) : key ; const val = formatted [ key ] ; pairs . push ( [ newField , val ] ) ; } , [ ] ) ; return doTemplate ( { defPairs : highlightPairs . concat ( sourcePairs ) } ) ; } ; }