# all files / ol/layer/ layer.js

95.65% Statements 66/69
88.89% Branches 24/27
90.91% Functions 10/11
95.65% Lines 66/69

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    2041×

    1×
    1×
    1×
    1×
    1×
    1×
    1×
    1×
    1×

    1×

    416×
    416×

    416×

    416×

    416×

    416×

    416×
    1×

    416×

    416×
    416×

    1×

    1×
    343×

    1×

    1×
    290×
    290×
    290×

    1×
    2263×
    2263×

    1×
    373×
    373×

    1×
    1343×

    1×
    426×
    10×
    10×

    426×
    426×
    410×

    426×

    1×
    116×
    14×
    14×

    116×
    22×

    116×
    14×
    14×

    116×
    94×

    4×
    4×
    4×
    4×
    4×

    94×

    94×

    1×
    423×

     goog.provide('ol.layer.Layer');

    goog.require('ol.events');
    goog.require('ol.events.EventType');
    goog.require('ol');
    goog.require('ol.Object');
    goog.require('ol.layer.Base');
    goog.require('ol.layer.Property');
    goog.require('ol.obj');
    goog.require('ol.render.EventType');
    goog.require('ol.source.State');

    /**
     * @classdesc
     * Abstract base class; normally only used for creating subclasses and not
     * instantiated in apps.
     * A visual representation of raster or vector map data.
     * Layers group together those properties that pertain to how the data is to be
     * displayed, irrespective of the source of that data.
     *
     * Layers are usually added to a map with {@link ol.Map#addLayer}. Components
     * like {@link ol.interaction.Select} use unmanaged layers internally. These
     * unmanaged layers are associated with the map using
     * {@link ol.layer.Layer#setMap} instead.
     *
     * A generic `change` event is fired when the state of the source changes.
     *
     * @constructor
     * @abstract
     * @extends {ol.layer.Base}
     * @fires ol.render.Event
     * @param {olx.layer.LayerOptions} options Layer options.
     * @api
     */
    ol.layer.Layer = function(options) {

      var baseOptions = ol.obj.assign({}, options);
      delete baseOptions.source;

      ol.layer.Base.call(this, /** @type {olx.layer.BaseOptions} */ (baseOptions));

      /**
       * @private
       * @type {?ol.EventsKey}
       */
      this.mapPrecomposeKey_ = null;

      /**
       * @private
       * @type {?ol.EventsKey}
       */
      this.mapRenderKey_ = null;

      /**
       * @private
       * @type {?ol.EventsKey}
       */
      this.sourceChangeKey_ = null;

      if (options.map) {
        this.setMap(options.map);
      }

      ol.events.listen(this,
          ol.Object.getChangeEventType(ol.layer.Property.SOURCE),
          this.handleSourcePropertyChange_, this);

      var source = options.source ? options.source : null;
      this.setSource(source);
    };
    ol.inherits(ol.layer.Layer, ol.layer.Base);

    /**
     * Return `true` if the layer is visible, and if the passed resolution is
     * between the layer's minResolution and maxResolution. The comparison is
     * inclusive for `minResolution` and exclusive for `maxResolution`.
     * @param {ol.LayerState} layerState Layer state.
     * @param {number} resolution Resolution.
     * @return {boolean} The layer is visible at the given resolution.
     */
    ol.layer.Layer.visibleAtResolution = function(layerState, resolution) {
      return layerState.visible && resolution >= layerState.minResolution &&
          resolution < layerState.maxResolution;
    };

    /**
     * @inheritDoc
     */
    ol.layer.Layer.prototype.getLayersArray = function(opt_array) {
      var array = opt_array ? opt_array : [];
      array.push(this);
      return array;
    };

    /**
     * @inheritDoc
     */
    ol.layer.Layer.prototype.getLayerStatesArray = function(opt_states) {
      var states = opt_states ? opt_states : [];
      states.push(this.getLayerState());
      return states;
    };

    /**
     * Get the layer source.
     * @return {ol.source.Source} The layer source (or `null` if not yet set).
     * @observable
     * @api
     */
    ol.layer.Layer.prototype.getSource = function() {
      var source = this.get(ol.layer.Property.SOURCE);
      return /** @type {ol.source.Source} */ (source) || null;
    };

    /**
      * @inheritDoc
      */
    ol.layer.Layer.prototype.getSourceState = function() {
      var source = this.getSource();
      return !source ? ol.source.State.UNDEFINED : source.getState();
    };

    /**
     * @private
     */
    ol.layer.Layer.prototype.handleSourceChange_ = function() {
      this.changed();
    };

    /**
     * @private
     */
    ol.layer.Layer.prototype.handleSourcePropertyChange_ = function() {
      if (this.sourceChangeKey_) {
        ol.events.unlistenByKey(this.sourceChangeKey_);
        this.sourceChangeKey_ = null;
      }
      var source = this.getSource();
      if (source) {
        this.sourceChangeKey_ = ol.events.listen(source,
            ol.events.EventType.CHANGE, this.handleSourceChange_, this);
      }
      this.changed();
    };

    /**
     * Sets the layer to be rendered on top of other layers on a map. The map will
     * not manage this layer in its layers collection, and the callback in
     * {@link ol.Map#forEachLayerAtPixel} will receive `null` as layer. This
     * is useful for temporary layers. To remove an unmanaged layer from the map,
     * use `#setMap(null)`.
     *
     * To add the layer to a map and have it managed by the map, use
     * {@link ol.Map#addLayer} instead.
     * @param {ol.PluggableMap} map Map.
     * @api
     */
    ol.layer.Layer.prototype.setMap = function(map) {
      if (this.mapPrecomposeKey_) {
        ol.events.unlistenByKey(this.mapPrecomposeKey_);
        this.mapPrecomposeKey_ = null;
      }
      if (!map) {
        this.changed();
      }
      if (this.mapRenderKey_) {
        ol.events.unlistenByKey(this.mapRenderKey_);
        this.mapRenderKey_ = null;
      }
      if (map) {
        this.mapPrecomposeKey_ = ol.events.listen(
            map, ol.render.EventType.PRECOMPOSE, function(evt) {
              var layerState = this.getLayerState();
              layerState.managed = false;
              layerState.zIndex = Infinity;
              evt.frameState.layerStatesArray.push(layerState);
              evt.frameState.layerStates[ol.getUid(this)] = layerState;
            }, this);
        this.mapRenderKey_ = ol.events.listen(
            this, ol.events.EventType.CHANGE, map.render, map);
        this.changed();
      }
    };

    /**
     * Set the layer source.
     * @param {ol.source.Source} source The layer source.
     * @observable
     * @api
     */
    ol.layer.Layer.prototype.setSource = function(source) {
      this.set(ol.layer.Property.SOURCE, source);
    };