# all files / lib/utilities/eventGroup/ EventGroup.js

86.99% Statements 107/123
76.14% Branches 67/88
86.67% Functions 13/15
86.99% Lines 107/123

    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

    1×

    1×
    15×
    15×
    15×

    1×
    15×
    15×
    3×
    3×
    3×
    3×
    3×

    12×
    14×
    14×
    14×
    22×
    22×
    22×
    14×
    14×
    14×

    14×

    15×

    1×
    3×
    3×

    1×
    3×
    3×

    1×

    1×
    34×

    1×
    7×
    7×
    7×
    7×

    1×

    1×
    20×
    20×
    1×
    1×
    2×

    19×
    19×

    19×
    19×

    19×
    19×
    19×
    19×
    9×
    7×
    7×
    7×

    7×

    7×
    7×
    7×
    7×
    1×
    1×
    1×

    7×

    9×
    9×

    9×

    10×
    14×
    14×
    14×

    14×

    14×

    10×

    19×

    1×
    10×
    15×
    15×

    14×
    14×
    14×

    14×
    12×
    11×
    11×

    1×
    1×

    12×
    11×

    14×
    4×
    4×

    14×

    1×
    12×

    1×
    1×
    1×

    1×
    2×

    1×
    1×

    1×

     /* tslint:disable:no-string-literal */
    "use strict";
    /** An instance of EventGroup allows anything with a handle to it to trigger events on it.
     *  If the target is an HTMLElement, the event will be attached to the element and can be
     *  triggered as usual (like clicking for onclick).
     *  The event can be triggered by calling EventGroup.raise() here. If the target is an
     *  HTMLElement, the event gets raised and is handled by the browser. Otherwise, it gets
     *  handled here in EventGroup, and the handler is called in the context of the parent
     *  (which is passed in in the constructor).
     */
    var EventGroup = (function () {
        /** parent: the context in which events attached to non-HTMLElements are called */
        function EventGroup(parent) {
            this._id = EventGroup._uniqueId++;
            this._parent = parent;
            this._eventRecords = [];
        }
        /** For IE8, bubbleEvent is ignored here and must be dealt with by the handler.
         *  Events raised here by default have bubbling set to false and cancelable set to true.
         *  This applies also to built-in events being raised manually here on HTMLElements,
         *  which may lead to unexpected behavior if it differs from the defaults.
         */
        EventGroup.raise = function (target, eventName, eventArgs, bubbleEvent) {
            var retVal;
            if (EventGroup._isElement(target)) {
                Eif (document.createEvent) {
                    var ev = document.createEvent('HTMLEvents');
                    ev.initEvent(eventName, bubbleEvent, true);
                    ev['args'] = eventArgs;
                    retVal = target.dispatchEvent(ev);
                }
                else if (document['createEventObject']) {
                    var evObj = document['createEventObject'](eventArgs);
                    // cannot set cancelBubble on evObj, fireEvent will overwrite it
                    target.fireEvent('on' + eventName, evObj);
                }
            }
            else {
                while (target && retVal !== false) {
                    var events = target.__events__;
                    var eventRecords = events ? events[eventName] : null;
                    for (var id in eventRecords) {
                        Eif (eventRecords.hasOwnProperty(id)) {
                            var eventRecordList = eventRecords[id];
                            for (var listIndex = 0; retVal !== false && listIndex < eventRecordList.length; listIndex++) {
                                var record = eventRecordList[listIndex];
                                Eif (record.objectCallback) {
                                    retVal = record.objectCallback.call(record.parent, eventArgs);
                                }
                            }
                        }
                    }
                    // If the target has a parent, bubble the event up.
                    target = bubbleEvent ? target.parent : null;
                }
            }
            return retVal;
        };
        EventGroup.isObserved = function (target, eventName) {
            var events = target && target.__events__;
            return !!events && !!events[eventName];
        };
        /** Check to see if the target has declared support of the given event. */
        EventGroup.isDeclared = function (target, eventName) {
            var declaredEvents = target && target.__declaredEvents;
            return !!declaredEvents && !!declaredEvents[eventName];
        };
        EventGroup.stopPropagation = function (event) {
            if (event.stopPropagation) {
                event.stopPropagation();
            }
            else {
                event.cancelBubble = true;
            }
        };
        EventGroup._isElement = function (target) {
            return !!target && (target instanceof HTMLElement || target.addEventListener);
        };
        EventGroup.prototype.dispose = function () {
            Eif (!this._isDisposed) {
                this._isDisposed = true;
                this.off();
                this._parent = null;
            }
        };
        /** On the target, attach a set of events, where the events object is a name to function mapping. */
        EventGroup.prototype.onAll = function (target, events, useCapture) {
            for (var eventName in events) {
                if (events.hasOwnProperty(eventName)) {
                    this.on(target, eventName, events[eventName], useCapture);
                }
            }
        };
        /** On the target, attach an event whose handler will be called in the context of the parent
         * of this instance of EventGroup.
         */
        EventGroup.prototype.on = function (target, eventName, callback, useCapture) {
            var _this = this;
            if (eventName.indexOf(',') > -1) {
                var events = eventName.split(/[ ,]+/);
                for (var i = 0; i < events.length; i++) {
                    this.on(target, events[i], callback, useCapture);
                }
            }
            else {
                var parent_1 = this._parent;
                var eventRecord = {
                    target: target,
                    eventName: eventName,
                    parent: parent_1,
                    callback: callback,
                    objectCallback: null,
                    elementCallback: null,
                    useCapture: useCapture
                };
                // Initialize and wire up the record on the target, so that it can call the callback if the event fires.
                var events = (target.__events__ = target.__events__ || {});
                events[eventName] = events[eventName] || {
                    count: 0
                };
                events[eventName][this._id] = events[eventName][this._id] || [];
                events[eventName][this._id].push(eventRecord);
                events[eventName].count++;
                if (EventGroup._isElement(target)) {
                    var processElementEvent = function () {
                        var args = [];
                        for (var _i = 0; _i < arguments.length; _i++) {
                            args[_i - 0] = arguments[_i];
                        }
                        Iif (_this._isDisposed) {
                            return;
                        }
                        var result;
                        try {
                            result = callback.apply(parent_1, args);
                            if (result === false && args[0] && args[0].preventDefault) {
                                var e = args[0];
                                e.preventDefault();
                                e.cancelBubble = true;
                            }
                        }
                        catch (e) {
                        }
                        return result;
                    };
                    eventRecord.elementCallback = processElementEvent;
                    Eif (target.addEventListener) {
                        /* tslint:disable:ban-native-functions */
                        target.addEventListener(eventName, processElementEvent, useCapture);
                    }
                    else if (target.attachEvent) {
                        target.attachEvent('on' + eventName, processElementEvent);
                    }
                }
                else {
                    var processObjectEvent = function () {
                        var args = [];
                        for (var _i = 0; _i < arguments.length; _i++) {
                            args[_i - 0] = arguments[_i];
                        }
                        Iif (_this._isDisposed) {
                            return;
                        }
                        return callback.apply(parent_1, args);
                    };
                    eventRecord.objectCallback = processObjectEvent;
                }
                // Remember the record locally, so that it can be removed.
                this._eventRecords.push(eventRecord);
            }
        };
        EventGroup.prototype.off = function (target, eventName, callback, useCapture) {
            for (var i = 0; i < this._eventRecords.length; i++) {
                var eventRecord = this._eventRecords[i];
                if ((!target || target === eventRecord.target) &&
                    (!eventName || eventName === eventRecord.eventName) &&
                    (!callback || callback === eventRecord.callback) &&
                    ((typeof useCapture !== 'boolean') || useCapture === eventRecord.useCapture)) {
                    var events = eventRecord.target.__events__;
                    var targetArrayLookup = events[eventRecord.eventName];
                    var targetArray = targetArrayLookup ? targetArrayLookup[this._id] : null;
                    // We may have already target's entries, so check for null.
                    if (targetArray) {
                        if (targetArray.length === 1 || !callback) {
                            targetArrayLookup.count -= targetArray.length;
                            delete events[eventRecord.eventName][this._id];
                        }
                        else {
                            targetArrayLookup.count--;
                            targetArray.splice(targetArray.indexOf(eventRecord), 1);
                        }
                        if (!targetArrayLookup.count) {
                            delete events[eventRecord.eventName];
                        }
                    }
                    if (eventRecord.elementCallback) {
                        Eif (eventRecord.target.removeEventListener) {
                            eventRecord.target.removeEventListener(eventRecord.eventName, eventRecord.elementCallback, eventRecord.useCapture);
                        }
                        else if (eventRecord.target.detachEvent) {
                            eventRecord.target.detachEvent('on' + eventRecord.eventName, eventRecord.elementCallback);
                        }
                    }
                    this._eventRecords.splice(i--, 1);
                }
            }
        };
        /** Trigger the given event in the context of this instance of EventGroup. */
        EventGroup.prototype.raise = function (eventName, eventArgs, bubbleEvent) {
            return EventGroup.raise(this._parent, eventName, eventArgs, bubbleEvent);
        };
        /** Declare an event as being supported by this instance of EventGroup. */
        EventGroup.prototype.declare = function (event) {
            var declaredEvents = this._parent.__declaredEvents = this._parent.__declaredEvents || {};
            Iif (typeof event === 'string') {
                declaredEvents[event] = true;
            }
            else {
                for (var i = 0; i < event.length; i++) {
                    declaredEvents[event[i]] = true;
                }
            }
        };
        EventGroup._uniqueId = 0;
        return EventGroup;
    }());
    exports.EventGroup = EventGroup;

    //# sourceMappingURL=EventGroup.js.map