watergis/mapbox-gl-legend

Using mapbox-gl-legend and mapbox-gl-export together causes error

Closed this issue · 7 comments

Describe the bug
When using mapbox-gl-legend and mapbox-gl-export together (using mapbox-gl-js 2.6.0), no buttons appear and I get the following error message:
Uncaught TypeError: watergis.Size is undefined

To Reproduce
Just use mapbox-gl-legend and mapbox-gl-export together and look at the console output.

I used the following code, you can try it on https://codepen.io/pen/:

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Display a map on a webpage</title>
<meta name="viewport" content="initial-scale=1,maximum-scale=1,user-scalable=no">
<link href="https://api.mapbox.com/mapbox-gl-js/v2.6.0/mapbox-gl.css" rel="stylesheet">
<script src="https://api.mapbox.com/mapbox-gl-js/v2.6.0/mapbox-gl.js"></script>
<link href='https://watergis.github.io/mapbox-gl-export/mapbox-gl-export.css' rel='stylesheet' />
<script src="https://watergis.github.io/mapbox-gl-export/mapbox-gl-export.js"></script>
<link href='https://watergis.github.io/mapbox-gl-legend/mapbox-gl-legend.css' rel='stylesheet' />
<script src="https://watergis.github.io/mapbox-gl-legend/mapbox-gl-legend.js"></script>
<style>
body { margin: 0; padding: 0; }
#map { position: absolute; top: 0; bottom: 0; width: 100%; }
</style>
</head>
<body>
<div id="map"></div>
<script>
	// TO MAKE THE MAP APPEAR YOU MUST
	// ADD YOUR ACCESS TOKEN FROM
	// https://account.mapbox.com
	mapboxgl.accessToken = 'pk.eyJ1IjoidWJpbGFicyIsImEiOiJjaXc3dGQzb2wwMDEwMnlwNXlsejAyOXF6In0.2C7DfhUhiVWgE_ek5N5GzQ';
const map = new mapboxgl.Map({
container: 'map', // container ID
style: 'mapbox://styles/mapbox/streets-v11', // style URL
center: [-74.5, 40], // starting position [lng, lat]
zoom: 9 // starting zoom
});


  map.addControl(new watergis.MapboxExportControl({
      PageSize: watergis.Size.A3,
      PageOrientation: watergis.PageOrientation.Portrait,
      Format: watergis.Format.PNG,
      DPI: watergis.DPI[96],
      Crosshair: true,
      PrintableArea: true,
  }), 'top-right');
  
  
map.on('load', function() {
    const targets = {
        'pipeline': 'Pipeline',
        'pipeline_annotation': 'Pipeline Label', 
        'meter': 'Water Meter',
        'flow meter': 'Flow Meter', 
        'valve': 'Valve', 
        'firehydrant': 'Fire Hydrant', 
        'washout': 'Washout',
        'tank': 'Tank', 
        'tank_annotation': 'Tank Label', 
        'wtp': 'WTP', 
        'wtp_annotation': 'WTP Label', 
        'intake': 'Intake', 
        'intake_annotation': 'Intake Label', 
        'parcels': 'Parcels', 
        'parcels_annotation': 'Parcels Label', 
        'village': 'Village', 
        'village_annotation': 'Village Label', 
        'dma': 'DMA',
        'dma-annotation': 'DMA Label', 
        'contour-line': 'Countour',
        'contour-label': 'Contour Label',
        'hillshade': 'Hillshade'
    };
    map.addControl(new watergis.MapboxLegendControl(targets, {
        showDefault: false, 
        showCheckbox: false, 
        onlyRendered: true,
        reverseOrder: true
    }), 'top-right');
})
</script>
 
</body>
</html>

Expected behavior
Works together without causing error.

Desktop (please complete the following information):
Tested using Chrome and Firefox

Only initializing both like you can see above or only initializing mapbox-gl-export causes this error. When I only init mapbox-gl-legend, but have both scripts in my html, I don't get any error.

Hello @JinIgarashi, are you able to reproduce this problem or have you found out what could cause this issue?
Thank you very much!

@AgentSmith0 Sorry, I have no time to fix the issue. But welcome your pull request to fix it. Thank you.

I guess this is caused by the watergis namespace in the js files.
Have a look at the compiled js file here (for better understanding unminify it): https://watergis.github.io/mapbox-gl-legend/mapbox-gl-legend.js
Here are the first lines:

!(function (e, t) {
    "object" == typeof exports && "object" == typeof module
        ? (module.exports = t(require("mapboxgl")))
        : "function" == typeof define && define.amd
        ? define(["mapboxgl"], t)
        : "object" == typeof exports
        ? (exports.watergis = t(require("mapboxgl")))
        : (e.watergis = t(e.mapboxgl));
})(self, function (e) {

Maybe you are able to fix this issue. Thank you!

Thank you very much!

Yes, my issue is now solved. Thanks!