# all files / ol/source/ cartodb.js

26.92% Statements 14/52
0% Branches 0/17
0% Functions 0/8
26.92% Lines 14/52

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

    1×
    1×
    1×
    1×

    1×

    1×

    1×

    1×

    1×

    1×

    1×

    1×

    1×

     goog.provide('ol.source.CartoDB');

    goog.require('ol');
    goog.require('ol.obj');
    goog.require('ol.source.State');
    goog.require('ol.source.XYZ');

    /**
     * @classdesc
     * Layer source for the CartoDB tiles.
     *
     * @constructor
     * @extends {ol.source.XYZ}
     * @param {olx.source.CartoDBOptions} options CartoDB options.
     * @api
     */
    ol.source.CartoDB = function(options) {

      /**
       * @type {string}
       * @private
       */
      this.account_ = options.account;

      /**
       * @type {string}
       * @private
       */
      this.mapId_ = options.map || '';

      /**
       * @type {!Object}
       * @private
       */
      this.config_ = options.config || {};

      /**
       * @type {!Object.<string, CartoDBLayerInfo>}
       * @private
       */
      this.templateCache_ = {};

      ol.source.XYZ.call(this, {
        attributions: options.attributions,
        cacheSize: options.cacheSize,
        crossOrigin: options.crossOrigin,
        logo: options.logo,
        maxZoom: options.maxZoom !== undefined ? options.maxZoom : 18,
        minZoom: options.minZoom,
        projection: options.projection,
        state: ol.source.State.LOADING,
        wrapX: options.wrapX
      });
      this.initializeMap_();
    };
    ol.inherits(ol.source.CartoDB, ol.source.XYZ);

    /**
     * Returns the current config.
     * @return {!Object} The current configuration.
     * @api
     */
    ol.source.CartoDB.prototype.getConfig = function() {
      return this.config_;
    };

    /**
     * Updates the carto db config.
     * @param {Object} config a key-value lookup. Values will replace current values
     *     in the config.
     * @api
     */
    ol.source.CartoDB.prototype.updateConfig = function(config) {
      ol.obj.assign(this.config_, config);
      this.initializeMap_();
    };

    /**
     * Sets the CartoDB config
     * @param {Object} config In the case of anonymous maps, a CartoDB configuration
     *     object.
     * If using named maps, a key-value lookup with the template parameters.
     * @api
     */
    ol.source.CartoDB.prototype.setConfig = function(config) {
      this.config_ = config || {};
      this.initializeMap_();
    };

    /**
     * Issue a request to initialize the CartoDB map.
     * @private
     */
    ol.source.CartoDB.prototype.initializeMap_ = function() {
      var paramHash = JSON.stringify(this.config_);
      if (this.templateCache_[paramHash]) {
        this.applyTemplate_(this.templateCache_[paramHash]);
        return;
      }
      var mapUrl = 'https://' + this.account_ + '.cartodb.com/api/v1/map';

      if (this.mapId_) {
        mapUrl += '/named/' + this.mapId_;
      }

      var client = new XMLHttpRequest();
      client.addEventListener('load', this.handleInitResponse_.bind(this, paramHash));
      client.addEventListener('error', this.handleInitError_.bind(this));
      client.open('POST', mapUrl);
      client.setRequestHeader('Content-type', 'application/json');
      client.send(JSON.stringify(this.config_));
    };

    /**
     * Handle map initialization response.
     * @param {string} paramHash a hash representing the parameter set that was used
     *     for the request
     * @param {Event} event Event.
     * @private
     */
    ol.source.CartoDB.prototype.handleInitResponse_ = function(paramHash, event) {
      var client = /** @type {XMLHttpRequest} */ (event.target);
      // status will be 0 for file:// urls
      if (!client.status || client.status >= 200 && client.status < 300) {
        var response;
        try {
          response = /** @type {CartoDBLayerInfo} */(JSON.parse(client.responseText));
        } catch (err) {
          this.setState(ol.source.State.ERROR);
          return;
        }
        this.applyTemplate_(response);
        this.templateCache_[paramHash] = response;
        this.setState(ol.source.State.READY);
      } else {
        this.setState(ol.source.State.ERROR);
      }
    };

    /**
     * @private
     * @param {Event} event Event.
     */
    ol.source.CartoDB.prototype.handleInitError_ = function(event) {
      this.setState(ol.source.State.ERROR);
    };

    /**
     * Apply the new tile urls returned by carto db
     * @param {CartoDBLayerInfo} data Result of carto db call.
     * @private
     */
    ol.source.CartoDB.prototype.applyTemplate_ = function(data) {
      var tilesUrl = 'https://' + data.cdn_url.https + '/' + this.account_ +
          '/api/v1/map/' + data.layergroupid + '/{z}/{x}/{y}.png';
      this.setUrl(tilesUrl);
    };