all files / dom/event/ compact.js

100% Statements 40/40
100% Branches 30/30
100% Functions 8/8
100% Lines 40/40
17 statements, 5 functions, 14 branches Ignored     
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                  23×                         62× 62×   62×                            
import { avalon, eventHooks, modern, document, root } from '../../seed/core'
import { canBubbleUp } from './canBubbleUp'
import { avEvent } from './share'
export {
    avEvent
}
/* istanbul ignore if */
Iif (!modern) {
    delete canBubbleUp.change
    delete canBubbleUp.select
}
/* istanbul ignore next */
avalon._nativeBind = modern ? function (el, type, fn, capture) {
    el.addEventListener(type, fn, !!capture)
} : function (el, type, fn) {
    el.attachEvent('on' + type, fn)
}
/* istanbul ignore next */
avalon._nativeUnBind = modern ? function (el, type, fn, a) {
    el.removeEventListener(type, fn, !!a)
} : function (el, type, fn) {
    el.detachEvent('on' + type, fn)
}
/* istanbul ignore next */
avalon.fireDom = function (elem, type, opts) {
    Eif (document.createEvent) {
        var hackEvent = document.createEvent('Events')
        hackEvent.initEvent(type, true, true, opts)
        avalon.shadowCopy(hackEvent, opts)
        elem.dispatchEvent(hackEvent)
    } else if (root.contains(elem)) {//IE6-8触发事件必须保证在DOM树中,否则报'SCRIPT16389: 未指明的错误'
        hackEvent = document.createEventObject()
        if (opts)
            avalon.shadowCopy(hackEvent, opts)
        try {
            elem.fireEvent('on' + type, hackEvent)
        } catch (e) {
            avalon.log('fireDom', type, 'args error')
        }
    }
}
 
var rmouseEvent = /^(?:mouse|contextmenu|drag)|click/
/* istanbul ignore next */
avEvent.prototype.fixEvent = function () {
    var event = this
    if (event.which == null && event.type.indexOf('key') === 0) {
        event.which = event.charCode != null ? event.charCode : event.keyCode
    }
    if (rmouseEvent.test(event.type) && !('pageX' in event)) {
        var DOC = event.target.ownerDocument || document
        var box = DOC.compatMode === 'BackCompat' ? DOC.body : DOC.documentElement
        event.pageX = event.clientX + (box.scrollLeft >> 0) - (box.clientLeft >> 0)
        event.pageY = event.clientY + (box.scrollTop >> 0) - (box.clientTop >> 0)
        event.wheelDeltaY = ~~event.wheelDelta
        event.wheelDeltaX = 0
    }
}
 
//针对IE6-8修正input
/* istanbul ignore if */
Iif (!('oninput' in document.createElement('input'))) {
    eventHooks.input = {
        type: 'propertychange',
        fix: function (elem, fn) {
            return function (e) {
                if (e.propertyName === 'value') {
                    e.type = 'input'
                    return fn.apply(this, arguments)
                }
            }
        }
    }
}