yzt
2023-05-26 de4278af2fd46705a40bac58ec01122db6b7f3d7
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
define(['../on', '../_base/window', '../dom-construct', '../domReady!'], function(on, baseWin, domConstruct){
    // summary:
    //      This sub module provide an event factory for delayed events (like debounce or throttle)
    // module:
    //      dojo/on/asyncEventListener
 
 
    //Testing is the browser support async event access
    //If not we need to clone the event, otherwise accessing the event properties
    //will trigger a JS error (invalid member)
    var testNode = domConstruct.create('div', null, baseWin.body()),
        testEvent,
        requiresClone;
    on.once(testNode, 'click', function(e){
        testEvent = e;
    });
    testNode.click();
    try{
        requiresClone = testEvent.clientX === undefined;
    }catch(e){
        requiresClone = true;
    }finally{
        domConstruct.destroy(testNode);
    }
 
    function clone(arg){
        // summary:
        //      clone the event
        // description:
        //      Used if the browser provides a corrupted event (comming from a node) when passed to an async function
        var argCopy = {},
            i;
        for(i in arg){
            argCopy[i] = arg[i];
        }
        return argCopy;
    }
 
    return function(listener){
        if(requiresClone){
            return function(e){
                //lang.clone fail to clone events, so we use a custom function
                listener.call(this, clone(e));
            };
        }
        return listener;
    };
});