all files / src/visualizations/deckgl/layers/ common.js

26.47% Statements 9/34
0% Branches 0/10
0% Functions 0/7
21.43% Lines 6/28
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64                                                                                                                    
import dompurify from 'dompurify';
import { fitBounds } from 'viewport-mercator-project';
 
import sandboxedEval from '../../../modules/sandbox';
 
export function getBounds(points) {
  const latExt = d3.extent(points, d => d[1]);
  const lngExt = d3.extent(points, d => d[0]);
  return [
    [lngExt[0], latExt[0]],
    [lngExt[1], latExt[1]],
  ];
}
 
export function fitViewport(viewport, points, padding = 10) {
  try {
    const bounds = getBounds(points);
    return {
      ...viewport,
      ...fitBounds({
        height: viewport.height,
        width: viewport.width,
        padding,
        bounds,
      }),
    };
  } catch (e) {
    /* eslint no-console: 0 */
    console.error('Could not auto zoom', e);
    return viewport;
  }
}
 
export function commonLayerProps(formData, slice) {
  const fd = formData;
  let onHover;
  if (fd.js_tooltip) {
    const jsTooltip = sandboxedEval(fd.js_tooltip);
    onHover = (o) => {
      if (o.picked) {
        slice.setTooltip({
          content: dompurify.sanitize(jsTooltip(o)),
          x: o.x,
          y: o.y,
        });
      } else {
        slice.setTooltip(null);
      }
    };
  }
  let onClick;
  if (fd.js_onclick_href) {
    onClick = (o) => {
      const href = sandboxedEval(fd.js_onclick_href)(o);
      window.open(href);
    };
  }
  return {
    onClick,
    onHover,
    pickable: Boolean(onHover),
  };
}