# all files / ol/ object.js

98.11% Statements 52/53
78.57% Branches 11/14
100% Functions 10/10
98.11% Lines 52/53

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

    1×
    1×
    1×
    1×
    1×

    1×
    14326×

    14326×

    14326×

    14326×
    2×

    1×

    1×

    1×
    28448×

    1×
    36556×
    36556×
    35009×

    36556×

    1×
    13×

    1×
    152×

    1×
    23503×
    23503×
    23503×
    23503×
    23503×

    1×
    23737×

    23737×
    23737×
    23737×
    23500×

    1×
    3547×
    3547×
    9573×

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

    1×
    47006×

    47006×

    47006×

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

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

    /**
     * @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.Object.Event
     * @api
     */
    ol.Object = function(opt_values) {
      ol.Observable.call(this);

      // Call ol.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.
      ol.getUid(this);

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

      if (opt_values !== undefined) {
        this.setProperties(opt_values);
      }
    };
    ol.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
     */
    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
     */
    ol.Object.prototype.getKeys = function() {
      return Object.keys(this.values_);
    };

    /**
     * Get an object of all property names and values.
     * @return {Object.<string, *>} Object.
     * @api
     */
    ol.Object.prototype.getProperties = function() {
      return ol.obj.assign({}, this.values_);
    };

    /**
     * @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.Object.Event(eventType, key, oldValue));
      eventType = ol.ObjectEventType.PROPERTYCHANGE;
      this.dispatchEvent(new ol.Object.Event(eventType, key, oldValue));
    };

    /**
     * Sets a value.
     * @param {string} key Key name.
     * @param {*} value Value.
     * @param {boolean=} opt_silent Update without triggering an event.
     * @api
     */
    ol.Object.prototype.set = function(key, value, opt_silent) {
      Iif (opt_silent) {
        this.values_[key] = value;
      } else {
        var oldValue = this.values_[key];
        this.values_[key] = value;
        if (oldValue !== 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.
     * @param {boolean=} opt_silent Update without triggering an event.
     * @api
     */
    ol.Object.prototype.setProperties = function(values, opt_silent) {
      var key;
      for (key in values) {
        this.set(key, values[key], opt_silent);
      }
    };

    /**
     * Unsets a property.
     * @param {string} key Key name.
     * @param {boolean=} opt_silent Unset without triggering an event.
     * @api
     */
    ol.Object.prototype.unset = function(key, opt_silent) {
      Eif (key in this.values_) {
        var oldValue = this.values_[key];
        delete this.values_[key];
        Eif (!opt_silent) {
          this.notify(key, oldValue);
        }
      }
    };

    /**
     * @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 {ol.events.Event}
     * @implements {oli.Object.Event}
     * @constructor
     */
    ol.Object.Event = function(type, key, oldValue) {
      ol.events.Event.call(this, type);

      /**
       * The name of the property whose value is changing.
       * @type {string}
       * @api
       */
      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
       */
      this.oldValue = oldValue;

    };
    ol.inherits(ol.Object.Event, ol.events.Event);