<!DOCTYPE html>
<html>
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <title>{{name}} - TileServer GL</title>
  {{#is_vector}}
  <link rel="stylesheet" type="text/css" href="{{public_url}}maplibre-gl.css{{&key_query}}" />
  <link rel="stylesheet" type="text/css" href="{{public_url}}maplibre-gl-inspect.css{{&key_query}}" />
  <script>if (typeof Symbol !== 'undefined') { document.write('<script src="{{public_url}}maplibre-gl.js{{&key_query}}"><\/script>'); } else { document.write('<script src="{{public_url}}maplibre-gl-compat.js{{&key_query}}"><\/script>'); }</script>
  <script>if (typeof Symbol !== 'undefined') { document.write('<script src="{{public_url}}maplibre-gl-inspect.min.js{{&key_query}}"><\/script>'); } else { document.write('<script src="{{public_url}}maplibre-gl-inspect-compat.min.js{{&key_query}}"><\/script>'); }</script>
  <style>
    body {background:#fff;color:#333;font-family:Arial, sans-serif;}
    #map {position:absolute;top:0;left:0;right:250px;bottom:0;}
    h1 {position:absolute;top:5px;right:0;width:240px;margin:0;line-height:20px;font-size:20px;}
    #layerList {position:absolute;top:35px;right:0;bottom:0;width:240px;overflow:auto;}
    #layerList div div {width:15px;height:15px;display:inline-block;}
  </style>
  {{/is_vector}}
  {{^is_vector}}
  <link rel="stylesheet" type="text/css" href="{{public_url}}leaflet.css{{&key_query}}" />
  <script src="{{public_url}}leaflet.js{{&key_query}}"></script>
  <script src="{{public_url}}leaflet-hash.js{{&key_query}}"></script>
  <script src="{{public_url}}L.TileLayer.NoGap.js{{&key_query}}"></script>
  <style>
    body { margin:0; padding:0; }
    #map { position:absolute; top:0; bottom:0; width:100%; }
  </style>
  {{/is_vector}}
</head>
<body>
  {{#is_vector}}
  <h1>{{name}}</h1>
  <div id="map"></div>
  <div id="layerList"></div>
  <pre id="propertyList"></pre>
  <script>
  var map = new maplibregl.Map({
    container: 'map',
    hash: true,
    maplibreLogo: true,
    style: {
      version: 8,
      sources: {
        'vector_layer_': {
          type: 'vector',
          url: '{{public_url}}data/{{id}}.json{{&key_query}}'
        }
      },
      layers: []
    }
  });
  map.addControl(new maplibregl.NavigationControl());
  var inspect = new MaplibreInspect({
    showInspectMap: true,
    showInspectButton: false
  });
  map.addControl(inspect);
  map.on('styledata', function() {
    var layerList = document.getElementById('layerList');
    layerList.innerHTML = '';
    Object.keys(inspect.sources).forEach(function(sourceId) {
      var layerIds = inspect.sources[sourceId];
      layerIds.forEach(function(layerId) {
        var item = document.createElement('div');
        item.innerHTML = '<div style="' +
          'background:' + inspect.assignLayerColor(layerId) + ';' +
        '"></div> ' + layerId;
        layerList.appendChild(item);
      });
    })
  });
  </script>
  {{/is_vector}}
  {{^is_vector}}
  <h1 style="display:none;">{{name}}</h1>
  <div id='map'></div>
  <script>
	var map = L.map('map', { zoomControl: false });
	new L.Control.Zoom({ position: 'topright' }).addTo(map);

	var tile_urls = [], tile_attribution, tile_minzoom, tile_maxzoom;
	var url = '{{public_url}}data/{{id}}.json{{&key_query}}';
	var req = new XMLHttpRequest();
	req.overrideMimeType("application/json");
	req.open('GET', url, true);
	req.onload = function() {
		var jsonResponse = JSON.parse(req.responseText);
		for (key in jsonResponse) {
		  var keyl = key.toLowerCase();
			switch(keyl) {
			  case "tiles":
				tile_urls = jsonResponse[key];
				break;
			  case "attribution":
				tile_attribution = jsonResponse[key];
				break;
			  case "minzoom":
				tile_minzoom = jsonResponse[key];
				break;
			  case "maxzoom":
				tile_maxzoom = jsonResponse[key];
				break;
			}
		}

		for (tile_url in tile_urls) {
			L.tileLayer(tile_urls[tile_url], {
				minZoom: tile_minzoom,
				maxZoom: tile_maxzoom,
				attribution: tile_attribution
			}).addTo(map);
		}
		
		map.eachLayer(function(layer) {
		  // do not add scale prefix even if retina display is detected
		  layer.scalePrefix = '.';
		});
	};
	req.send(null);

    setTimeout(function() {
      new L.Hash(map);
    }, 0);
  </script>
  {{/is_vector}}
</body>
</html>
