# all files / ol/ collection.js

100% Statements 69/69
100% Branches 6/6
100% Functions 15/15
100% Lines 69/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
    204
    205
    206
    207
    208
    209
    210
    211
    212
    213
    214
    215
    216
    217
    218
    219
    220
    221
    222
    223
    224
    225
    226
    227
    228
    229
    230
    231
    232
    233
    234
    235
    236
    237
    238
    239
    240
    241
    242
    243
    244
    245
    246
    247
    248
    249
    250
    251
    252
    253
    254
    255
    256
    257
    258
    259
    260
    261
    262
    263
    264
    265
    266
    267
    268
    269
    270
    271
    272
    273
    274
    275
    276
    277
    278
    279
    280
    281
    282

    1×
    1×
    1×

    1×
    1×
    1×

    1×

    1×

    2124×

    2124×

    1×

    1×

    1×

    626×

    626×

    626×

    1×

    1×
    167×
    290×

    1×
    11×
    11×
    17×

    11×

    1×
    474×

    1×
    416×

    1×
    40×

    1×
    810×

    1×
    1792×
    1792×
    1792×

    1×
    294×

    1×
    1784×
    1784×
    1784×

    1×
    32×
    32×
    32×
    147×
    30×

    2×

    1×
    326×
    326×
    326×
    326×

    326×

    1×
    5×
    5×
    3×
    3×
    3×

    3×

    2×
    2×
    3×

    2×

    1×
    2744×

     /**
     * An implementation of Google Maps' MVCArray.
     * @see https://developers.google.com/maps/documentation/javascript/reference
     */

    goog.provide('ol.Collection');
    goog.provide('ol.CollectionEvent');
    goog.provide('ol.CollectionEventType');

    goog.require('goog.array');
    goog.require('goog.events.Event');
    goog.require('ol.Object');

    /**
     * @enum {string}
     */
    ol.CollectionEventType = {
      /**
       * Triggered when an item is added to the collection.
       * @event ol.CollectionEvent#add
       * @api stable
       */
      ADD: 'add',
      /**
       * Triggered when an item is removed from the collection.
       * @event ol.CollectionEvent#remove
       * @api stable
       */
      REMOVE: 'remove'
    };

    /**
     * @classdesc
     * Events emitted by {@link ol.Collection} instances are instances of this
     * type.
     *
     * @constructor
     * @extends {goog.events.Event}
     * @implements {oli.CollectionEvent}
     * @param {ol.CollectionEventType} type Type.
     * @param {*=} opt_element Element.
     * @param {Object=} opt_target Target.
     */
    ol.CollectionEvent = function(type, opt_element, opt_target) {

      goog.base(this, type, opt_target);

      /**
       * The element that is added to or removed from the collection.
       * @type {*}
       * @api stable
       */
      this.element = opt_element;

    };
    goog.inherits(ol.CollectionEvent, goog.events.Event);

    /**
     * @enum {string}
     */
    ol.CollectionProperty = {
      LENGTH: 'length'
    };

    /**
     * @classdesc
     * An expanded version of standard JS Array, adding convenience methods for
     * manipulation. Add and remove changes to the Collection trigger a Collection
     * event. Note that this does not cover changes to the objects _within_ the
     * Collection; they trigger events on the appropriate object, not on the
     * Collection as a whole.
     *
     * @constructor
     * @extends {ol.Object}
     * @fires ol.CollectionEvent
     * @param {!Array.<T>=} opt_array Array.
     * @template T
     * @api stable
     */
    ol.Collection = function(opt_array) {

      goog.base(this);

      /**
       * @private
       * @type {!Array.<T>}
       */
      this.array_ = opt_array ? opt_array : [];

      this.updateLength_();

    };
    goog.inherits(ol.Collection, ol.Object);

    /**
     * Remove all elements from the collection.
     * @api stable
     */
    ol.Collection.prototype.clear = function() {
      while (this.getLength() > 0) {
        this.pop();
      }
    };

    /**
     * Add elements to the collection.  This pushes each item in the provided array
     * to the end of the collection.
     * @param {!Array.<T>} arr Array.
     * @return {ol.Collection.<T>} This collection.
     * @api stable
     */
    ol.Collection.prototype.extend = function(arr) {
      var i, ii;
      for (i = 0, ii = arr.length; i < ii; ++i) {
        this.push(arr[i]);
      }
      return this;
    };

    /**
     * Iterate over each element, calling the provided callback.
     * @param {function(this: S, T, number, Array.<T>): *} f The function to call
     *     for every element. This function takes 3 arguments (the element, the
     *     index and the array). The return value is ignored.
     * @param {S=} opt_this The object to use as `this` in `f`.
     * @template S
     * @api stable
     */
    ol.Collection.prototype.forEach = function(f, opt_this) {
      this.array_.forEach(f, opt_this);
    };

    /**
     * Get a reference to the underlying Array object. Warning: if the array
     * is mutated, no events will be dispatched by the collection, and the
     * collection's "length" property won't be in sync with the actual length
     * of the array.
     * @return {!Array.<T>} Array.
     * @api stable
     */
    ol.Collection.prototype.getArray = function() {
      return this.array_;
    };

    /**
     * Get the element at the provided index.
     * @param {number} index Index.
     * @return {T} Element.
     * @api stable
     */
    ol.Collection.prototype.item = function(index) {
      return this.array_[index];
    };

    /**
     * Get the length of this collection.
     * @return {number} The length of the array.
     * @observable
     * @api stable
     */
    ol.Collection.prototype.getLength = function() {
      return /** @type {number} */ (this.get(ol.CollectionProperty.LENGTH));
    };

    /**
     * Insert an element at the provided index.
     * @param {number} index Index.
     * @param {T} elem Element.
     * @api stable
     */
    ol.Collection.prototype.insertAt = function(index, elem) {
      goog.array.insertAt(this.array_, elem, index);
      this.updateLength_();
      this.dispatchEvent(
          new ol.CollectionEvent(ol.CollectionEventType.ADD, elem, this));
    };

    /**
     * Remove the last element of the collection and return it.
     * Return `undefined` if the collection is empty.
     * @return {T|undefined} Element.
     * @api stable
     */
    ol.Collection.prototype.pop = function() {
      return this.removeAt(this.getLength() - 1);
    };

    /**
     * Insert the provided element at the end of the collection.
     * @param {T} elem Element.
     * @return {number} Length.
     * @api stable
     */
    ol.Collection.prototype.push = function(elem) {
      var n = this.array_.length;
      this.insertAt(n, elem);
      return n;
    };

    /**
     * Remove the first occurrence of an element from the collection.
     * @param {T} elem Element.
     * @return {T|undefined} The removed element or undefined if none found.
     * @api stable
     */
    ol.Collection.prototype.remove = function(elem) {
      var arr = this.array_;
      var i, ii;
      for (i = 0, ii = arr.length; i < ii; ++i) {
        if (arr[i] === elem) {
          return this.removeAt(i);
        }
      }
      return undefined;
    };

    /**
     * Remove the element at the provided index and return it.
     * Return `undefined` if the collection does not contain this index.
     * @param {number} index Index.
     * @return {T|undefined} Value.
     * @api stable
     */
    ol.Collection.prototype.removeAt = function(index) {
      var prev = this.array_[index];
      goog.array.removeAt(this.array_, index);
      this.updateLength_();
      this.dispatchEvent(
          new ol.CollectionEvent(ol.CollectionEventType.REMOVE, prev, this));
      return prev;
    };

    /**
     * Set the element at the provided index.
     * @param {number} index Index.
     * @param {T} elem Element.
     * @api stable
     */
    ol.Collection.prototype.setAt = function(index, elem) {
      var n = this.getLength();
      if (index < n) {
        var prev = this.array_[index];
        this.array_[index] = elem;
        this.dispatchEvent(
            new ol.CollectionEvent(ol.CollectionEventType.REMOVE, prev, this));
        this.dispatchEvent(
            new ol.CollectionEvent(ol.CollectionEventType.ADD, elem, this));
      } else {
        var j;
        for (j = n; j < index; ++j) {
          this.insertAt(j, undefined);
        }
        this.insertAt(index, elem);
      }
    };

    /**
     * @private
     */
    ol.Collection.prototype.updateLength_ = function() {
      this.set(ol.CollectionProperty.LENGTH, this.array_.length);
    };