# all files / ol/ observable.js

42.86% Statements 18/42
0% Branches 0/8
28.57% Functions 2/7
42.86% Lines 18/42

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

    1×
    1×
    1×
    1×

    1×

    43×

    43×

    1×

    1×

    1×
    37×
    37×

    1×

    1×

    1×

    1×

    1×

     goog.provide('ol.Observable');

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

    /**
     * @classdesc
     * Abstract base class; normally only used for creating subclasses and not
     * instantiated in apps.
     * An event target providing convenient methods for listener registration
     * and unregistration. A generic `change` event is always available through
     * {@link ol.Observable#changed}.
     *
     * @constructor
     * @extends {ol.events.EventTarget}
     * @fires ol.events.Event
     * @struct
     * @api
     */
    ol.Observable = function() {

      ol.events.EventTarget.call(this);

      /**
       * @private
       * @type {number}
       */
      this.revision_ = 0;

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

    /**
     * Removes an event listener using the key returned by `on()` or `once()`.
     * @param {ol.EventsKey|Array.<ol.EventsKey>} key The key returned by `on()`
     *     or `once()` (or an array of keys).
     * @api
     */
    ol.Observable.unByKey = function(key) {
      if (Array.isArray(key)) {
        for (var i = 0, ii = key.length; i < ii; ++i) {
          ol.events.unlistenByKey(key[i]);
        }
      } else {
        ol.events.unlistenByKey(/** @type {ol.EventsKey} */ (key));
      }
    };

    /**
     * Increases the revision counter and dispatches a 'change' event.
     * @api
     */
    ol.Observable.prototype.changed = function() {
      ++this.revision_;
      this.dispatchEvent(ol.events.EventType.CHANGE);
    };

    /**
     * Dispatches an event and calls all listeners listening for events
     * of this type. The event parameter can either be a string or an
     * Object with a `type` property.
     *
     * @param {{type: string,
     *     target: (EventTarget|ol.events.EventTarget|undefined)}|ol.events.Event|
     *     string} event Event object.
     * @function
     * @api
     */
    ol.Observable.prototype.dispatchEvent;

    /**
     * Get the version number for this object.  Each time the object is modified,
     * its version number will be incremented.
     * @return {number} Revision.
     * @api
     */
    ol.Observable.prototype.getRevision = function() {
      return this.revision_;
    };

    /**
     * Listen for a certain type of event.
     * @param {string|Array.<string>} type The event type or array of event types.
     * @param {function(?): ?} listener The listener function.
     * @param {Object=} opt_this The object to use as `this` in `listener`.
     * @return {ol.EventsKey|Array.<ol.EventsKey>} Unique key for the listener. If
     *     called with an array of event types as the first argument, the return
     *     will be an array of keys.
     * @api
     */
    ol.Observable.prototype.on = function(type, listener, opt_this) {
      if (Array.isArray(type)) {
        var len = type.length;
        var keys = new Array(len);
        for (var i = 0; i < len; ++i) {
          keys[i] = ol.events.listen(this, type[i], listener, opt_this);
        }
        return keys;
      } else {
        return ol.events.listen(
            this, /** @type {string} */ (type), listener, opt_this);
      }
    };

    /**
     * Listen once for a certain type of event.
     * @param {string|Array.<string>} type The event type or array of event types.
     * @param {function(?): ?} listener The listener function.
     * @param {Object=} opt_this The object to use as `this` in `listener`.
     * @return {ol.EventsKey|Array.<ol.EventsKey>} Unique key for the listener. If
     *     called with an array of event types as the first argument, the return
     *     will be an array of keys.
     * @api
     */
    ol.Observable.prototype.once = function(type, listener, opt_this) {
      if (Array.isArray(type)) {
        var len = type.length;
        var keys = new Array(len);
        for (var i = 0; i < len; ++i) {
          keys[i] = ol.events.listenOnce(this, type[i], listener, opt_this);
        }
        return keys;
      } else {
        return ol.events.listenOnce(
            this, /** @type {string} */ (type), listener, opt_this);
      }
    };

    /**
     * Unlisten for a certain type of event.
     * @param {string|Array.<string>} type The event type or array of event types.
     * @param {function(?): ?} listener The listener function.
     * @param {Object=} opt_this The object which was used as `this` by the
     * `listener`.
     * @api
     */
    ol.Observable.prototype.un = function(type, listener, opt_this) {
      if (Array.isArray(type)) {
        for (var i = 0, ii = type.length; i < ii; ++i) {
          ol.events.unlisten(this, type[i], listener, opt_this);
        }
        return;
      } else {
        ol.events.unlisten(this, /** @type {string} */ (type), listener, opt_this);
      }
    };