# all files / ol/ tile.js

95.83% Statements 46/48
91.67% Branches 11/12
88.89% Functions 8/9
95.83% Lines 46/48

    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
    1641×

    1×
    1×
    1×
    1×

    1×

    646×

    646×

    646×

    646×

    646×

    1×

    1×
    350×

    1×
    300×

    1×
    64×

    60×

    4×

    4×
    8×
    4×

    4×

    1×
    9×
    1×

    8×
    8×

    8×
    20×

    8×
    8×
    12×

    4×
    8×

    8×

    12×

    1×
    61×

    1×
    2132×

    1×
    104×
    104×

    1×
     goog.provide('ol.Tile');

    goog.require('ol');
    goog.require('ol.TileState');
    goog.require('ol.events.EventTarget');
    goog.require('ol.events.EventType');

    /**
     * @classdesc
     * Base class for tiles.
     *
     * @constructor
     * @abstract
     * @extends {ol.events.EventTarget}
     * @param {ol.TileCoord} tileCoord Tile coordinate.
     * @param {ol.TileState} state State.
     */
    ol.Tile = function(tileCoord, state) {

      ol.events.EventTarget.call(this);

      /**
       * @type {ol.TileCoord}
       */
      this.tileCoord = tileCoord;

      /**
       * @protected
       * @type {ol.TileState}
       */
      this.state = state;

      /**
       * An "interim" tile for this tile. The interim tile may be used while this
       * one is loading, for "smooth" transitions when changing params/dimensions
       * on the source.
       * @type {ol.Tile}
       */
      this.interimTile = null;

      /**
       * A key assigned to the tile. This is used by the tile source to determine
       * if this tile can effectively be used, or if a new tile should be created
       * and this one be used as an interim tile for this new tile.
       * @type {string}
       */
      this.key = '';

    };
    ol.inherits(ol.Tile, ol.events.EventTarget);

    /**
     * @protected
     */
    ol.Tile.prototype.changed = function() {
      this.dispatchEvent(ol.events.EventType.CHANGE);
    };

    /**
     * @return {string} Key.
     */
    ol.Tile.prototype.getKey = function() {
      return this.key + '/' + this.tileCoord;
    };

    /**
     * Get the interim tile most suitable for rendering using the chain of interim
     * tiles. This corresponds to the  most recent tile that has been loaded, if no
     * such tile exists, the original tile is returned.
     * @return {!ol.Tile} Best tile for rendering.
     */
    ol.Tile.prototype.getInterimTile = function() {
      if (!this.interimTile) {
        //empty chain
        return this;
      }
      var tile = this.interimTile;

      // find the first loaded tile and return it. Since the chain is sorted in
      // decreasing order of creation time, there is no need to search the remainder
      // of the list (all those tiles correspond to older requests and will be
      // cleaned up by refreshInterimChain)
      do {
        if (tile.getState() == ol.TileState.LOADED) {
          return tile;
        }
        tile = tile.interimTile;
      } while (tile);

      // we can not find a better tile
      return this;
    };

    /**
     * Goes through the chain of interim tiles and discards sections of the chain
     * that are no longer relevant.
     */
    ol.Tile.prototype.refreshInterimChain = function() {
      if (!this.interimTile) {
        return;
      }

      var tile = this.interimTile;
      var prev = this;

      do {
        if (tile.getState() == ol.TileState.LOADED) {
          //we have a loaded tile, we can discard the rest of the list
          //we would could abort any LOADING tile request
          //older than this tile (i.e. any LOADING tile following this entry in the chain)
          tile.interimTile = null;
          break;
        } else if (tile.getState() == ol.TileState.LOADING) {
          //keep this LOADING tile any loaded tiles later in the chain are
          //older than this tile, so we're still interested in the request
          prev = tile;
        } else Eif (tile.getState() == ol.TileState.IDLE) {
          //the head of the list is the most current tile, we don't need
          //to start any other requests for this chain
          prev.interimTile = tile.interimTile;
        } else {
          prev = tile;
        }
        tile = prev.interimTile;
      } while (tile);
    };

    /**
     * Get the tile coordinate for this tile.
     * @return {ol.TileCoord} The tile coordinate.
     * @api
     */
    ol.Tile.prototype.getTileCoord = function() {
      return this.tileCoord;
    };

    /**
     * @return {ol.TileState} State.
     */
    ol.Tile.prototype.getState = function() {
      return this.state;
    };

    /**
     * @param {ol.TileState} state State.
     */
    ol.Tile.prototype.setState = function(state) {
      this.state = state;
      this.changed();
    };

    /**
     * Load the image or retry if loading previously failed.
     * Loading is taken care of by the tile queue, and calling this method is
     * only needed for preloading or for reloading in case of an error.
     * @abstract
     * @api
     */
    ol.Tile.prototype.load = function() {};