layer/layerRec/wmsFC.js

  1. 'use strict';
  2. const basicFC = require('./basicFC.js')();
  3. /**
  4. * Searches for a layer title defined by a wms.
  5. * @function getWMSLayerTitle
  6. * @private
  7. * @param {Object} wmsLayer esri layer object for the wms
  8. * @param {String} wmsLayerId layers id as defined in the wms (i.e. not wmsLayer.id)
  9. * @return {String} layer title as defined on the service, '' if no title defined
  10. */
  11. function getWMSLayerTitle(wmsLayer, wmsLayerId) {
  12. // TEST STATUS none
  13. // TODO move this to ogc.js module?
  14. // crawl esri layerInfos (which is a nested structure),
  15. // returns sublayer that has matching id or null if not found.
  16. // written as function to allow recursion
  17. const crawlSubLayers = (subLayerInfos, wmsLayerId) => {
  18. let targetEntry = null;
  19. // we use .some to allow the search to stop when we find something
  20. subLayerInfos.some(layerInfo => {
  21. // wms ids are stored in .name
  22. if (layerInfo.name === wmsLayerId) {
  23. // found it. save it and exit the search
  24. targetEntry = layerInfo;
  25. return true;
  26. } else if (layerInfo.subLayers) {
  27. // search children. if in children, will exit search, else will continue
  28. return crawlSubLayers(layerInfo.subLayers, wmsLayerId);
  29. } else {
  30. // continue search
  31. return false;
  32. }
  33. });
  34. return targetEntry;
  35. };
  36. // init search on root layerInfos, then process result
  37. const match = crawlSubLayers(wmsLayer.layerInfos, wmsLayerId);
  38. if (match && match.title) {
  39. return match.title;
  40. } else {
  41. return ''; // falsy!
  42. }
  43. }
  44. /**
  45. * @class WmsFC
  46. */
  47. class WmsFC extends basicFC.BasicFC {
  48. // this will actively download / refresh the internal symbology
  49. loadSymbology () {
  50. const configLayerEntries = this._parent.config.layerEntries;
  51. const gApi = this._parent._apiRef;
  52. const legendArray = gApi.layer.ogc
  53. .getLegendUrls(this._parent._layer, configLayerEntries.map(le => le.id))
  54. .map((imageUri, idx) => {
  55. const symbologyItem = {
  56. name: null,
  57. svgcode: null
  58. };
  59. // config specified name || server specified name || config id
  60. const name = configLayerEntries[idx].name ||
  61. getWMSLayerTitle(this._parent._layer, configLayerEntries[idx].id) ||
  62. configLayerEntries[idx].id;
  63. gApi.symbology.generateWMSSymbology(name, imageUri).then(data => {
  64. symbologyItem.name = data.name;
  65. symbologyItem.svgcode = data.svgcode;
  66. });
  67. return symbologyItem;
  68. });
  69. this.symbology = legendArray;
  70. return Promise.resolve();
  71. }
  72. }
  73. module.exports = () => ({
  74. WmsFC
  75. });