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. // legend data is our modified legend structure.
  24. // it is similar to esri's server output, but all individual
  25. // items are promises.
  26. // TODO proper docs
  27. function makeSymbologyArray(legendData) {
  28. return legendData.map(item => {
  29. const symbologyItem = {
  30. svgcode: null,
  31. name: null
  32. };
  33. // file-based layers don't have symbology labels, default to ''
  34. // legend items are promises
  35. item.then(data => {
  36. symbologyItem.svgcode = data.svgcode;
  37. symbologyItem.name = data.label || '';
  38. });
  39. return symbologyItem;
  40. });
  41. }
  42. function parseUrlIndex(url) {
  43. // break url into root and index
  44. // note we are returning index as a string for now.
  45. const result = {
  46. rootUrl: url,
  47. index: '0'
  48. };
  49. const re = /\/(\d+)\/?$/;
  50. const matches = url.match(re);
  51. if (matches) {
  52. result.index = matches[1];
  53. result.rootUrl = url.substr(0, url.length - matches[0].length); // will drop trailing slash
  54. } else {
  55. // give up, dont crash with error.
  56. // default configuration will make sense for non-feature urls,
  57. // even though they should not be using this.
  58. console.warn('Cannot extract layer index from url ' + url);
  59. }
  60. return result;
  61. }
  62. /**
  63. * @class IdentifyResult
  64. */
  65. class IdentifyResult {
  66. /**
  67. * @param {Object} proxy proxy to the logical layer containing the results (i.e. a feature class)
  68. */
  69. constructor (proxy) {
  70. // TODO revisit what should be in this class, and what belongs in the app
  71. // also what can be abstacted to come from layerRec
  72. this.isLoading = true;
  73. this.requestId = -1;
  74. this.requester = {
  75. proxy
  76. };
  77. this.data = [];
  78. }
  79. }
  80. module.exports = () => ({
  81. states,
  82. clientLayerType,
  83. makeSymbologyArray,
  84. IdentifyResult,
  85. parseUrlIndex
  86. });