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. // TODO move this to ogc.js module?
  13. let targetEntry = null;
  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. // we use .some to allow the search to stop when we find something
  19. subLayerInfos.some(layerInfo => {
  20. // wms ids are stored in .name
  21. if (layerInfo.name === wmsLayerId) {
  22. // found it. save it and exit the search
  23. targetEntry = layerInfo;
  24. return true;
  25. } else if (layerInfo.subLayers) {
  26. // search children. if in children, will exit search, else will continue
  27. return crawlSubLayers(layerInfo.subLayers, wmsLayerId);
  28. } else {
  29. // continue search
  30. return false;
  31. }
  32. });
  33. return targetEntry;
  34. };
  35. // init search on root layerInfos, then process result
  36. const match = crawlSubLayers(wmsLayer.layerInfos, wmsLayerId);
  37. if (match && match.title) {
  38. return match.title;
  39. } else {
  40. return ''; // falsy!
  41. }
  42. }
  43. /**
  44. * @class WmsFC
  45. */
  46. class WmsFC extends basicFC.BasicFC {
  47. /**
  48. * Download or refresh the internal symbology for the FC.
  49. *
  50. * @function loadSymbology
  51. * @returns {Promise} resolves when symbology has been downloaded
  52. */
  53. loadSymbology () {
  54. const configLayerEntries = this._parent.config.layerEntries;
  55. const gApi = this._parent._apiRef;
  56. const legendArray = gApi.layer.ogc
  57. .getLegendUrls(this._parent._layer, configLayerEntries.map(le => le.id))
  58. .map((imageUri, idx) => {
  59. const symbologyItem = {
  60. name: null,
  61. svgcode: null
  62. };
  63. // config specified name || server specified name || config id
  64. const name = configLayerEntries[idx].name ||
  65. getWMSLayerTitle(this._parent._layer, configLayerEntries[idx].id) ||
  66. configLayerEntries[idx].id;
  67. gApi.symbology.generateWMSSymbology(name, imageUri).then(data => {
  68. symbologyItem.name = data.name;
  69. symbologyItem.svgcode = data.svgcode;
  70. });
  71. return symbologyItem;
  72. });
  73. this.symbology = legendArray;
  74. return Promise.resolve();
  75. }
  76. }
  77. module.exports = () => ({
  78. WmsFC
  79. });