{"size": 2338, "ext": "js", "max_stars_repo_path": "src/legacy/core_plugins/kibana/common/field_formats/types/source.js", "max_stars_repo_name": "albertzaharovits/kibana", "max_stars_repo_stars_event_min_datetime": "2018-03-09T08:37:23.000Z", "max_stars_repo_stars_event_max_datetime": "2022-03-18T10:43:13.000Z", "max_issues_repo_path": "src/legacy/core_plugins/kibana/common/field_formats/types/source.js", "max_issues_repo_name": "albertzaharovits/kibana", "max_issues_repo_issues_event_min_datetime": "2018-03-29T09:02:07.000Z", "max_issues_repo_issues_event_max_datetime": "2021-04-23T14:07:09.000Z", "max_forks_repo_path": "src/legacy/core_plugins/kibana/common/field_formats/types/source.js", "max_forks_repo_name": "albertzaharovits/kibana", "max_forks_repo_forks_event_min_datetime": "2016-03-08T15:23:59.000Z", "max_forks_repo_forks_event_max_datetime": "2018-02-06T17:21:14.000Z"}
{"max_stars_count": 12, "max_issues_count": 41, "max_forks_count": 11, "avg_line_length": 32.9295774648, "max_line_length": 72, "alphanum_fraction": 0.6685201027}
/*
 * Licensed to Elasticsearch B.V. under one or more contributor
 * license agreements. See the NOTICE file distributed with
 * this work for additional information regarding copyright
 * ownership. Elasticsearch B.V. licenses this file to you under
 * the Apache License, Version 2.0 (the "License"); you may
 * not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */

import _ from 'lodash';
import { noWhiteSpace } from '../../utils/no_white_space';
import { shortenDottedString } from '../../utils/shorten_dotted_string';

const templateHtml = `
  <dl class="source truncate-by-height">
    <% defPairs.forEach(function (def) { %>
      <dt><%- def[0] %>:</dt>
      <dd><%= def[1] %></dd>
      <%= ' ' %>
    <% }); %>
  </dl>`;
const template = _.template(noWhiteSpace(templateHtml));

export function createSourceFormat(FieldFormat) {
  class SourceFormat extends FieldFormat {
    constructor(params, getConfig) {
      super(params);

      this.getConfig = getConfig;
    }

    static id = '_source';
    static title = '_source';
    static fieldType = '_source';
  }

  SourceFormat.prototype._convert = {
    text: (value) => JSON.stringify(value),
    html: function sourceToHtml(source, field, hit) {
      if (!field) return _.escape(this.getConverterFor('text')(source));

      const highlights = (hit && hit.highlight) || {};
      const formatted = field.indexPattern.formatHit(hit);
      const highlightPairs = [];
      const sourcePairs = [];

      const isShortDots = this.getConfig('shortDots:enable');
      _.keys(formatted).forEach((key) => {
        const pairs = highlights[key] ? highlightPairs : sourcePairs;
        const field = isShortDots ? shortenDottedString(key) : key;
        const val = formatted[key];
        pairs.push([field, val]);
      }, []);

      return template({ defPairs: highlightPairs.concat(sourcePairs) });
    }
  };

  return SourceFormat;
}
