Source: map/utils/LayerLegend.js

  1. import { Window } from "../../ui/Window.js";
  2. import { WMSLayer } from "../layer/WMSLayer/WMSLayer.js";
  3. export { LayerLegend }
  4. /**
  5. * @memberof vef.map.layer.utils
  6. */
  7. class LayerLegend extends Window {
  8. constructor(target, layerManager) {
  9. super(target, {
  10. height: "unset",
  11. width: "250px",
  12. open: false,
  13. draggable: true,
  14. title: "LEGEND",
  15. responsive: true
  16. })
  17. this.activeLayer_ = null;
  18. this.activeLegend_ = null;
  19. this.layerManager_ = layerManager;
  20. }
  21. setOptions_() {
  22. // content height
  23. this.content_.style.maxHeight = "500px";
  24. this.content_.style.overflowY = "auto";
  25. this.setOptions({
  26. top: (window.innerHeight - this.getElement().clientHeight - 10) + "px",
  27. left: "480px",
  28. width: "280px"
  29. });
  30. this.getElement().style.visibility = "visible";
  31. }
  32. setLayer_(layer, hide) {
  33. if (this.activeLegend_) this.activeLegend_.remove();
  34. this.activeLayer_ = layer;
  35. this.setTitle(layer.title);
  36. const img = layer.getLegendGraphic();
  37. img.style = "max-width: 100%;";
  38. // set image but hide window until the image is loaded
  39. this.setContent(img);
  40. if (hide) this.getElement().style.visibility = "hidden";
  41. super.open();
  42. // update position on image loading
  43. if (!img.complete) {
  44. img.onload = () => this.setOptions_()
  45. } else {
  46. this.setOptions_();
  47. }
  48. this.activeLegend_ = img;
  49. }
  50. open(layer) {
  51. if (layer instanceof WMSLayer) {
  52. this.setLayer_(layer, true);
  53. if (this.layerManager_) {
  54. const dragover = (e) => {
  55. e.dataTransfer.dropEffect = "move";
  56. e.preventDefault();
  57. };
  58. const drop = (e) => {
  59. const id = e.dataTransfer.getData("layer_id");
  60. if (id) {
  61. const l = this.layerManager_.getLayerById(id);
  62. if (l && (l instanceof WMSLayer)) {
  63. this.setLayer_(l, false);
  64. }
  65. }
  66. };
  67. this.content_.addEventListener('dragover', dragover);
  68. this.content_.addEventListener('drop', drop);
  69. }
  70. } else {
  71. this.activeLayer_ = null;
  72. this.activeLegend_ = null;
  73. this.close();
  74. }
  75. }
  76. getActiveLayer() {
  77. return this.activeLayer_;
  78. }
  79. }