Commit d87d232e authored by Michael Underwood's avatar Michael Underwood
Browse files

Make building provided methods more automatic

parent 3a4dde3a
Loading
Loading
Loading
Loading
+3 −19
Original line number Diff line number Diff line
@@ -18,13 +18,9 @@ import {
  propsBinder,
  debounce,
  resetWebpackIcon,
  provideMethodsFromBuilders,
} from "../utils.js";
import {
  buildAddMapLayer,
  buildRegisterLayerControl,
  buildRemoveMapLayer,
  buildMapPropSetters,
} from "../functions/map";
import { mapMethodBuilders, buildMapPropSetters } from "../functions/map";

export default {
  props: {
@@ -174,9 +170,7 @@ export default {
      markerZoomAnimation: props.markerZoomAnimation,
    };

    provide("addMapLayer", buildAddMapLayer(blueprint));
    provide("removeMapLayer", buildRemoveMapLayer(blueprint));
    provide("registerLayerControl", buildRegisterLayerControl(blueprint));
    provideMethodsFromBuilders(provide, mapMethodBuilders, blueprint);

    const eventHandlers = {
      moveEndHandler() {
@@ -218,16 +212,6 @@ export default {
      resetWebpackIcon(Icon);
      options.crs = options.crs || CRS.EPSG3857;

      /*
      const methods = {
        fitBounds(bounds) {
          blueprint.mapRef.fitBounds(bounds, {
            animate: this.noBlockingAnimations ? false : null,
          });
        },
      };
      */

      blueprint.mapRef = map(root.value, options);

      const setters = buildMapPropSetters(blueprint, props);
+65 −52
Original line number Diff line number Diff line
import { latLng, latLngBounds } from "leaflet";

export const buildAddMapLayer = (blueprint) => {
export const mapMethodBuilders = {
  buildAddMapLayer(blueprint) {
    return (layer) => {
      if (!blueprint.ready) {
        return;
@@ -22,9 +23,9 @@ export const buildAddMapLayer = (blueprint) => {
        blueprint.mapRef.addLayer(layer.mapObject);
      }
    };
};
  },

export const buildRemoveMapLayer = (blueprint) => {
  buildRemoveMapLayer(blueprint) {
    return (layer) => {
      if (!blueprint.ready) {
        return;
@@ -43,9 +44,9 @@ export const buildRemoveMapLayer = (blueprint) => {
      }
      blueprint.mapRef.removeLayer(layer.mapObject);
    };
};
  },

export const buildRegisterLayerControl = (blueprint) => {
  buildRegisterLayerControl(blueprint) {
    return (lControlLayer) => {
      if (!blueprint.ready) {
        return;
@@ -57,6 +58,18 @@ export const buildRegisterLayerControl = (blueprint) => {
      });
      blueprint.layersToAdd = [];
    };
  },

  buildFitBounds(blueprint) {
    return (bounds) => {
      if (!blueprint.ready) {
        return;
      }
      blueprint.mapRef.fitBounds(bounds, {
        animate: this.noBlockingAnimations ? false : null,
      });
    };
  },
};

export const buildMapPropSetters = (blueprint, props) => {
+31 −0
Original line number Diff line number Diff line
@@ -22,6 +22,24 @@ export const capitalizeFirstLetter = (string) => {
  return string.charAt(0).toUpperCase() + string.slice(1);
};

export const lowercaseFirstLetter = (string) => {
  if (!string || typeof string.charAt !== "function") {
    return string;
  }
  return string.charAt(0).toLowerCase() + string.slice(1);
};

export const getMethodNameFromBuilder = (string) => {
  if (!string || typeof string.match !== "function") {
    return string;
  }
  const match = string.match(/^build(.+)/);
  if (!match || !Array.isArray(match) || match.length < 2) {
    return string;
  }
  return lowercaseFirstLetter(match[1]);
};

export const propsBinder = (methods, leafletElement, props, setOptions) => {
  for (const key in props) {
    const setMethodName = "set" + capitalizeFirstLetter(key);
@@ -50,6 +68,19 @@ export const propsBinder = (methods, leafletElement, props, setOptions) => {
  }
};

export const provideMethodsFromBuilders = (
  provide,
  methodBuilders,
  blueprint
) => {
  for (const builder in methodBuilders) {
    provide(
      getMethodNameFromBuilder(builder),
      methodBuilders[builder](blueprint)
    );
  }
};

export const remapEvents = (onEvent) => {
  const result = {};
  for (const eventName in onEvent) {