layer/layerRec/shared.js

  1. 'use strict';
  2. // TODO revisit if we still need rv- in these constants.
  3. const states = { // these are used as css classes; hence the `rv` prefix
  4. NEW: 'rv-new',
  5. REFRESH: 'rv-refresh',
  6. LOADING: 'rv-loading',
  7. LOADED: 'rv-loaded', // TODO maybe loaded and default are the same?
  8. DEFAULT: 'rv-default',
  9. ERROR: 'rv-error'
  10. };
  11. // these match strings in the client.
  12. const clientLayerType = {
  13. ESRI_DYNAMIC: 'esriDynamic',
  14. ESRI_FEATURE: 'esriFeature',
  15. ESRI_IMAGE: 'esriImage',
  16. ESRI_TILE: 'esriTile',
  17. ESRI_GROUP: 'esriGroup',
  18. ESRI_RASTER: 'esriRaster',
  19. OGC_WMS: 'ogcWms',
  20. UNRESOLVED: 'unresolved',
  21. UNKNOWN: 'unknown'
  22. };
  23. /**
  24. * Takes an array of (possibly pending) legend data and constructs an array of default
  25. * symbology objects. As each legend item loads, the symbology objects are updated.
  26. *
  27. * @function makeSymbologyArray
  28. * @param {Array} legendData list of promises that resolve with legend data (svg and labels)
  29. * @returns {Array} a list of symbology objects.
  30. */
  31. function makeSymbologyArray(legendData) {
  32. return legendData.map(item => {
  33. const symbologyItem = {
  34. svgcode: null,
  35. name: null
  36. };
  37. // file-based layers don't have symbology labels, default to ''
  38. // legend items are promises
  39. item.then(data => {
  40. symbologyItem.svgcode = data.svgcode;
  41. symbologyItem.name = data.label || '';
  42. });
  43. return symbologyItem;
  44. });
  45. }
  46. /**
  47. * Splits an indexed map server url into an object with .rootUrl and .index
  48. * properties.
  49. *
  50. * @function parseUrlIndex
  51. * @param {String} url an indexed map server url
  52. * @returns {Object} the url split into the server root and the index.
  53. */
  54. function parseUrlIndex(url) {
  55. // break url into root and index
  56. // note we are returning index as a string for now.
  57. const result = {
  58. rootUrl: url,
  59. index: '0'
  60. };
  61. const re = /\/(\d+)\/?$/;
  62. const matches = url.match(re);
  63. if (matches) {
  64. result.index = matches[1];
  65. result.rootUrl = url.substr(0, url.length - matches[0].length); // will drop trailing slash
  66. } else {
  67. // give up, dont crash with error.
  68. // default configuration will make sense for non-feature urls,
  69. // even though they should not be using this.
  70. console.warn('Cannot extract layer index from url ' + url);
  71. }
  72. return result;
  73. }
  74. /**
  75. * Takes a specific layer state and determines if the layer can be considered
  76. * loaded or not.
  77. *
  78. * @function layerLoaded
  79. * @param {String} state a layer state
  80. * @returns {Boolean} if the layer is loaded or not
  81. */
  82. function layerLoaded(state) {
  83. switch (state) {
  84. case states.ERROR:
  85. case states.LOADING:
  86. case states.NEW:
  87. return false;
  88. default:
  89. return true;
  90. }
  91. }
  92. /**
  93. * @class IdentifyResult
  94. */
  95. class IdentifyResult {
  96. /**
  97. * @param {Object} proxy proxy to the logical layer containing the results (i.e. a feature class)
  98. */
  99. constructor (proxy) {
  100. // TODO revisit what should be in this class, and what belongs in the app
  101. // also what can be abstacted to come from layerRec
  102. this.isLoading = true;
  103. this.requestId = -1;
  104. this.requester = {
  105. proxy
  106. };
  107. this.data = [];
  108. }
  109. }
  110. module.exports = () => ({
  111. states,
  112. clientLayerType,
  113. makeSymbologyArray,
  114. IdentifyResult,
  115. parseUrlIndex,
  116. layerLoaded
  117. });