# all files / ol/ object.js

100% Statements 54/54
87.5% Branches 7/8
100% Functions 10/10
100% Lines 54/54

    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
    2351×
    1×
    1×

    1×
    1×
    1×

    1×

    1×
    35130×

    35130×

    35130×

    1×

    1×
    7083×

    7083×

    7083×

    7083×
    2×

    1×

    1×

    1×
    20305×

    1×
    10414×
    10414×
    9829×

    10414×

    1×
    8×

    1×
    114×
    114×
    114×
    639×

    114×

    1×
    17565×
    17565×
    17565×
    17565×
    17565×

    1×
    17562×
    17562×
    17562×

    1×
    1785×
    1785×
    11790×

    1×
    1×
    1×
    1×
    1×

     goog.provide('ol.Object');
    goog.provide('ol.ObjectEvent');
    goog.provide('ol.ObjectEventType');

    goog.require('goog.events');
    goog.require('goog.events.Event');
    goog.require('ol.Observable');

    /**
     * @enum {string}
     */
    ol.ObjectEventType = {
      /**
       * Triggered when a property is changed.
       * @event ol.ObjectEvent#propertychange
       * @api stable
       */
      PROPERTYCHANGE: 'propertychange'
    };

    /**
     * @classdesc
     * Events emitted by {@link ol.Object} instances are instances of this type.
     *
     * @param {string} type The event type.
     * @param {string} key The property name.
     * @param {*} oldValue The old value for `key`.
     * @extends {goog.events.Event}
     * @implements {oli.ObjectEvent}
     * @constructor
     */
    ol.ObjectEvent = function(type, key, oldValue) {
      goog.base(this, type);

      /**
       * The name of the property whose value is changing.
       * @type {string}
       * @api stable
       */
      this.key = key;

      /**
       * The old value. To get the new value use `e.target.get(e.key)` where
       * `e` is the event object.
       * @type {*}
       * @api stable
       */
      this.oldValue = oldValue;

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

    /**
     * @classdesc
     * Abstract base class; normally only used for creating subclasses and not
     * instantiated in apps.
     * Most non-trivial classes inherit from this.
     *
     * This extends {@link ol.Observable} with observable properties, where each
     * property is observable as well as the object as a whole.
     *
     * Classes that inherit from this have pre-defined properties, to which you can
     * add your owns. The pre-defined properties are listed in this documentation as
     * 'Observable Properties', and have their own accessors; for example,
     * {@link ol.Map} has a `target` property, accessed with `getTarget()`  and
     * changed with `setTarget()`. Not all properties are however settable. There
     * are also general-purpose accessors `get()` and `set()`. For example,
     * `get('target')` is equivalent to `getTarget()`.
     *
     * The `set` accessors trigger a change event, and you can monitor this by
     * registering a listener. For example, {@link ol.View} has a `center`
     * property, so `view.on('change:center', function(evt) {...});` would call the
     * function whenever the value of the center property changes. Within the
     * function, `evt.target` would be the view, so `evt.target.getCenter()` would
     * return the new center.
     *
     * You can add your own observable properties with
     * `object.set('prop', 'value')`, and retrieve that with `object.get('prop')`.
     * You can listen for changes on that property value with
     * `object.on('change:prop', listener)`. You can get a list of all
     * properties with {@link ol.Object#getProperties object.getProperties()}.
     *
     * Note that the observable properties are separate from standard JS properties.
     * You can, for example, give your map object a title with
     * `map.title='New title'` and with `map.set('title', 'Another title')`. The
     * first will be a `hasOwnProperty`; the second will appear in
     * `getProperties()`. Only the second is observable.
     *
     * Properties can be deleted by using the unset method. E.g.
     * object.unset('foo').
     *
     * @constructor
     * @extends {ol.Observable}
     * @param {Object.<string, *>=} opt_values An object with key-value pairs.
     * @fires ol.ObjectEvent
     * @api
     */
    ol.Object = function(opt_values) {
      goog.base(this);

      // Call goog.getUid to ensure that the order of objects' ids is the same as
      // the order in which they were created.  This also helps to ensure that
      // object properties are always added in the same order, which helps many
      // JavaScript engines generate faster code.
      goog.getUid(this);

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

      if (opt_values !== undefined) {
        this.setProperties(opt_values);
      }
    };
    goog.inherits(ol.Object, ol.Observable);

    /**
     * @private
     * @type {Object.<string, string>}
     */
    ol.Object.changeEventTypeCache_ = {};

    /**
     * @param {string} key Key name.
     * @return {string} Change name.
     */
    ol.Object.getChangeEventType = function(key) {
      return ol.Object.changeEventTypeCache_.hasOwnProperty(key) ?
          ol.Object.changeEventTypeCache_[key] :
          (ol.Object.changeEventTypeCache_[key] = 'change:' + key);
    };

    /**
     * Gets a value.
     * @param {string} key Key name.
     * @return {*} Value.
     * @api stable
     */
    ol.Object.prototype.get = function(key) {
      var value;
      if (this.values_.hasOwnProperty(key)) {
        value = this.values_[key];
      }
      return value;
    };

    /**
     * Get a list of object property names.
     * @return {Array.<string>} List of property names.
     * @api stable
     */
    ol.Object.prototype.getKeys = function() {
      return Object.keys(this.values_);
    };

    /**
     * Get an object of all property names and values.
     * @return {Object.<string, *>} Object.
     * @api stable
     */
    ol.Object.prototype.getProperties = function() {
      var properties = {};
      var key;
      for (key in this.values_) {
        properties[key] = this.values_[key];
      }
      return properties;
    };

    /**
     * @param {string} key Key name.
     * @param {*} oldValue Old value.
     */
    ol.Object.prototype.notify = function(key, oldValue) {
      var eventType;
      eventType = ol.Object.getChangeEventType(key);
      this.dispatchEvent(new ol.ObjectEvent(eventType, key, oldValue));
      eventType = ol.ObjectEventType.PROPERTYCHANGE;
      this.dispatchEvent(new ol.ObjectEvent(eventType, key, oldValue));
    };

    /**
     * Sets a value.
     * @param {string} key Key name.
     * @param {*} value Value.
     * @api stable
     */
    ol.Object.prototype.set = function(key, value) {
      var oldValue = this.values_[key];
      this.values_[key] = value;
      this.notify(key, oldValue);
    };

    /**
     * Sets a collection of key-value pairs.  Note that this changes any existing
     * properties and adds new ones (it does not remove any existing properties).
     * @param {Object.<string, *>} values Values.
     * @api stable
     */
    ol.Object.prototype.setProperties = function(values) {
      var key;
      for (key in values) {
        this.set(key, values[key]);
      }
    };

    /**
     * Unsets a property.
     * @param {string} key Key name.
     * @api stable
     */
    ol.Object.prototype.unset = function(key) {
      Eif (key in this.values_) {
        var oldValue = this.values_[key];
        delete this.values_[key];
        this.notify(key, oldValue);
      }
    };