# all files / src/ event.js

38.03% Statements 27/71
23.53% Branches 8/34
53.85% Functions 7/13
38.03% Lines 27/71

    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

    1×

    3×

    1×

    1×

    1×
    1×

    1×
    1×
    1×

    2×
    1×

    1×
    2×
    2×

    1×
    1×

    1×
    1×
    1×
    1×

    1×

    1×
    1×
    1×

    1×
    1×

    1×

     import {
        transaction
    } from './transaction'
    import {
        document
    } from './browser'
    import {
        isFn,
        options
    } from './util'

    export var eventMap = {}
    /**
     * 判定否为与事件相关
     *
     * @param {any} name
     * @returns
     */
    export function isEventName(name) {
        return /^on[A-Z]/.test(name)
    }

    function dispatchEvent(e) {
        e = new SyntheticEvent(e)
        var target = e.target
        var paths = []
        do {
            var events = target.__events
            if (events) {
                paths.push({
                    dom: target,
                    props: events
                })
            }
        } while ((target = target.parentNode) && target.nodeType === 1)
        // target --> parentNode --> body --> html
        var type = eventMap[e.type] || e.type

        var capitalized = capitalize(type)
        var bubble = 'on' + capitalized
        var captured = 'on' + capitalized + 'Capture'
        transaction.isInTransation = true
        triggerEventFlow(paths, captured, e)

        if (!e._stopPropagation) {

            triggerEventFlow(paths.reverse(), bubble, e)
        }
        transaction.isInTransation = false
        options.updateBatchNumber++
            transaction.dequeue()
    }

    function triggerEventFlow(paths, prop, e) {
        for (var i = paths.length; i--;) {
            var path = paths[i]
            var fn = path.props[prop]
            if (isFn(fn)) {
                e.currentTarget = path._hostNode
                fn.call(path._hostNode, e)
                if (e._stopPropagation) {
                    break
                }
            }
        }
    }

    function capitalize(str) {
        return str
            .charAt(0)
            .toUpperCase() + str.slice(1)
    }

    var globalEvents = {}
    export function addGlobalEventListener(name) {
        if (!globalEvents[name]) {
            globalEvents[name] = true
            addEvent(document, name, dispatchEvent)
        }
    }
    export function addEvent(el, type, fn) {
        Iif (el.addEventListener) {
            el.addEventListener(type, fn)
        } else Eif (el.attachEvent) {
            el.attachEvent('on' + type, fn)
        }
    }

    var eventLowerCache = {}
    var ron = /^on/
    var rcapture = /Capture$/
    export function getBrowserName(onStr) {
        var lower = eventLowerCache[onStr]
        if (lower) {
            return lower
        }
        var hump = onStr
            .replace(ron, '')
            .replace(rcapture, '')
        lower = hump.toLowerCase()
        eventLowerCache[onStr] = lower
        eventMap[lower] = hump
        return lower
    }

    export function SyntheticEvent(event) {
        if (event.originalEvent) {
            return event
        }
        for (var i in event) {
            Eif (!eventProto[i]) {
                this[i] = event[i]
            }
        }
        Eif (!this.target) {
            this.target = event.srcElement
        }
        var target = this.target
        this.fixEvent()
        this.timeStamp = new Date() - 0
        this.originalEvent = event
    }

    var eventProto = SyntheticEvent.prototype = {
        fixEvent: function () {}, //留给以后扩展用
        preventDefault: function () {
            var e = this.originalEvent || {}
            e.returnValue = this.returnValue = false
            if (e.preventDefault) {
                e.preventDefault()
            }
        },
        stopPropagation: function () {
            var e = this.originalEvent || {}
            e.cancelBubble = this._stopPropagation = true
            Iif (e.stopPropagation) {
                e.stopPropagation()
            }
        },
        stopImmediatePropagation: function () {
            this.stopPropagation()
            this.stopImmediate = true
        },
        toString: function () {
            return '[object Event]'
        }
    }