layer/layerRec/dynamicFC.js

  1. 'use strict';
  2. const attribFC = require('./attribFC.js')();
  3. /**
  4. * @class DynamicFC
  5. */
  6. class DynamicFC extends attribFC.AttribFC {
  7. // dynamic child variant for feature class object.
  8. // deals with stuff specific to dynamic children (i.e. virtual layer on client)
  9. /**
  10. * Create an feature class object for a feature class that is a child of a dynamic layer
  11. * @param {Object} parent the Record object that this Feature Class belongs to
  12. * @param {String} idx the service index of this Feature Class. an integer in string format. use '0' for non-indexed sources.
  13. * @param {Object} layerPackage a layer package object from the attribute module for this feature class
  14. * @param {Object} config the config object for this sublayer
  15. */
  16. constructor (parent, idx, layerPackage, config) {
  17. super(parent, idx, layerPackage, config);
  18. this.opacity = config.state.opacity;
  19. // visibility is kept stateful by the parent. keeping an internal property
  20. // just means we would need to keep it in synch.
  21. // the DynamicRecord onLoad handler will set the initial state, so don't do it here.
  22. // will also cache scale levels to avoid asynching code. initialize here with no limits,
  23. // then update when layer loads
  24. this._scaleSet = {
  25. minScale: 0,
  26. maxScale: 0
  27. };
  28. }
  29. get supportsOpacity () { return this._parent._isTrueDynamic; }
  30. get opacity () { return this._opacity; }
  31. set opacity (value) {
  32. // avoid parent/child update loops by only doing work if value changed
  33. if (this._opacity !== value) {
  34. this._opacity = value;
  35. if (this.supportsOpacity) {
  36. // only attempt to set the layer if we support that kind of magic.
  37. // instead of being consistent, esri using value from 0 to 100 for sublayer transparency where 100 is fully transparent
  38. const optionsArray = [];
  39. const drawingOptions = new this._parent._apiRef.layer.LayerDrawingOptions();
  40. drawingOptions.transparency = (value - 1) * -100;
  41. optionsArray[this._idx] = drawingOptions;
  42. this._parent._layer.setLayerDrawingOptions(optionsArray);
  43. } else {
  44. // update the opacity on the parent and any sibling children
  45. this._parent.synchOpacity(value);
  46. }
  47. }
  48. }
  49. // returns an object with minScale and maxScale values for the feature class
  50. getScaleSet () {
  51. return this._scaleSet;
  52. }
  53. get geomType () { return this._geometryType; }
  54. set geomType (value) { this._geometryType = value; }
  55. get featureCount () { return this._fcount; }
  56. set featureCount (value) { this._fcount = value; }
  57. setVisibility (value) {
  58. // update visible layers array
  59. const vLayers = this._parent._layer.visibleLayers.concat();
  60. const intIdx = parseInt(this._idx);
  61. const vIdx = vLayers.indexOf(intIdx);
  62. let dirty = false;
  63. let layerVisChange = false;
  64. if (value && vIdx === -1) {
  65. // check for first added case
  66. if (vLayers.length === 1 && vLayers[0] === -1) {
  67. vLayers.pop();
  68. layerVisChange = true;
  69. }
  70. // was invisible, now visible
  71. vLayers.push(intIdx);
  72. dirty = true;
  73. } else if (!value && vIdx > -1) {
  74. // was visible, now invisible
  75. vLayers.splice(vIdx, 1);
  76. if (vLayers.length === 0) {
  77. vLayers.push(-1); // code for no layers
  78. layerVisChange = true;
  79. }
  80. dirty = true;
  81. }
  82. if (dirty) {
  83. this._parent._layer.setVisibleLayers(vLayers);
  84. if (layerVisChange) {
  85. this._parent._layer.setVisibility(value);
  86. }
  87. }
  88. // TODO add a timer or something to cache requests.
  89. // use setVisibileLayers(arry, true) to stall the redraw
  90. // then when timer runs out, call layer.refresh
  91. }
  92. // TODO extend this function to other FC's? do they need it?
  93. getVisibility () {
  94. return this._parent._layer.visibleLayers.indexOf(parseInt(this._idx)) > -1;
  95. }
  96. }
  97. module.exports = () => ({
  98. DynamicFC
  99. });