view client/src/components/identify/formatter.js @ 4230:f3b733fd119a

Identify: Improve formatter for Identify tool The main usecase of the formatter is providing a key-value structure for displaying information in the identify box. Therefore the assumption seems justified, that a value displayed in the identify tool should be human readable. This isn't the case for values, which are javascript objects like "Feature". That said, it seems valid to exclude such values, which are instances of "Feature". This improves the solution of ommiting one specific "feature" to a more general solution of omitting every "feature".
author Thomas Junk <thomas.junk@intevation.de>
date Tue, 20 Aug 2019 17:15:06 +0200
parents bb66e144dece
children 57c38087fe18
line wrap: on
line source

import Feature from "ol/Feature";

const formatter = {
  all(p) {
    if (p.key === "objnam") p.key = "Name";
    if (p.key === "objname") p.key = "Name";
    if (p.key === "staging_done" || p.key === "fa_critical")
      p.val = p.val ? "yes" : "no";
    if (
      (p.key === "date_info" ||
        p.key === "fa_date_info" ||
        p.key === "gm_measuredate") &&
      p.val !== null
    ) {
      p.val = new Date(p.val).toLocaleString();
    }

    // remove certain props
    let propsToRemove = ["bbox"];
    if (propsToRemove.indexOf(p.key) !== -1) return null;

    return p;
  },
  bottlenecks_geoserver: {
    label: "Bottleneck",
    props: p => {
      if (p.key === "bottleneck_id") p.key = "ID";
      if (p.key === "responsible_country") p.key = "Responsible Country";
      if (p.key === "fa_date_info") p.key = "Fairway Date";
      if (p.key === "fa_critical") p.key = "Fairway Critical";
      if (p.key === "gauge_objname") p.key = "Reference Gauge";
      if (p.key === "source_organization") p.key = "Source Organization";
      if (p.key === "gm_measuredate") p.key = "Gauge Waterlevel Date";
      if (p.key === "gm_waterlevel") p.key = "Gauge Waterlevel";
      if (p.key === "gm_n_14d") p.key = "G.W. Count in Last 14 Days";
      // remove certain props
      //    * gauge_obj is another feature object and we want to stay flat
      //      for display (if left in causes a
      //        TypeError: Converting circular structure to JSON
      //      somewhere later)
      if (p.val instanceof Feature) {
        return null;
      }
      let propsToRemove = ["nobjnm", "reference_water_levels", "fa_data"];
      if (propsToRemove.indexOf(p.key) !== -1) return null;

      return p;
    }
  },
  fairway_dimensions: {
    label: "Fairway Dimensions"
  },
  waterway_area: {
    label: "Waterway Area"
  },
  distance_marks_geoserver: {
    label: "Distance Mark"
  },
  distance_marks_ashore_geoserver: {
    label: "Distance Mark ashore"
  },
  waterway_axis: {
    label: "Waterway Axis"
  },
  waterway_profiles: {
    label: "Waterway Profile"
  },
  stretches_geoserver: {
    label: "Stretch",
    props: p => {
      if (p.key === "gm_measuredate") p.key = "Min. Gauge Waterlevel Date";
      if (p.key === "gm_n_14d") p.key = "Min. G.W. Count in Last 14 Days";

      return p;
    }
  },
  sections_geoserver: {
    label: "Section",
    props: p => {
      if (p.key === "gm_measuredate") p.key = "Min. Gauge Waterlevel Date";
      if (p.key === "gm_n_14d") p.key = "Min. G.W. Count in Last 14 Days";

      return p;
    }
  },
  gauges_geoserver: {
    label: "Gauge",
    props: p => {
      if (p.key === "gm_measuredate") p.key = "Latest Waterlevel Date";
      if (p.key === "gm_waterlevel") p.key = "Latest Waterlevel";
      if (p.key === "gm_n_14d") p.key = "Measurement Count in Last 14 Days";

      // remove certain props
      let propsToRemove = ["nsc_data"];
      if (propsToRemove.indexOf(p.key) !== -1) return null;

      return p;
    }
  }
};

export { formatter };