globalListener.js 3.0 KB
Newer Older
S
sushuang 已提交
1 2
import * as zrUtil from 'zrender/src/core/util';
import env from 'zrender/src/core/env';
3
import {makeInner} from '../../util/model';
P
pah100 已提交
4

5
var inner = makeInner();
S
sushuang 已提交
6
var each = zrUtil.each;
P
pah100 已提交
7

S
sushuang 已提交
8 9 10 11 12 13 14
/**
 * @param {string} key
 * @param {module:echarts/ExtensionAPI} api
 * @param {Function} handler
 *      param: {string} currTrigger
 *      param: {Array.<number>} point
 */
S
sushuang 已提交
15
export function register(key, api, handler) {
S
sushuang 已提交
16 17 18 19 20
    if (env.node) {
        return;
    }

    var zr = api.getZr();
21
    inner(zr).records || (inner(zr).records = {});
P
pah100 已提交
22

S
sushuang 已提交
23
    initGlobalListeners(zr, api);
P
pah100 已提交
24

25
    var record = inner(zr).records[key] || (inner(zr).records[key] = {});
S
sushuang 已提交
26
    record.handler = handler;
S
sushuang 已提交
27
}
P
pah100 已提交
28

S
sushuang 已提交
29
function initGlobalListeners(zr, api) {
30
    if (inner(zr).initialized) {
S
sushuang 已提交
31 32
        return;
    }
P
pah100 已提交
33

34
    inner(zr).initialized = true;
P
pah100 已提交
35

S
sushuang 已提交
36 37 38 39 40 41 42 43 44
    useHandler('click', zrUtil.curry(doEnter, 'click'));
    useHandler('mousemove', zrUtil.curry(doEnter, 'mousemove'));
    // useHandler('mouseout', onLeave);
    useHandler('globalout', onLeave);

    function useHandler(eventType, cb) {
        zr.on(eventType, function (e) {
            var dis = makeDispatchAction(api);

45
            each(inner(zr).records, function (record) {
S
sushuang 已提交
46
                record && cb(record, e, dis.dispatchAction);
P
pah100 已提交
47
            });
S
sushuang 已提交
48 49 50

            dispatchTooltipFinally(dis.pendings, api);
        });
P
pah100 已提交
51
    }
S
sushuang 已提交
52
}
P
pah100 已提交
53

S
sushuang 已提交
54 55 56
function dispatchTooltipFinally(pendings, api) {
    var showLen = pendings.showTip.length;
    var hideLen = pendings.hideTip.length;
P
tweak  
pah100 已提交
57

S
sushuang 已提交
58 59 60
    var actuallyPayload;
    if (showLen) {
        actuallyPayload = pendings.showTip[showLen - 1];
P
pah100 已提交
61
    }
S
sushuang 已提交
62 63
    else if (hideLen) {
        actuallyPayload = pendings.hideTip[hideLen - 1];
P
pah100 已提交
64
    }
S
sushuang 已提交
65 66 67
    if (actuallyPayload) {
        actuallyPayload.dispatchAction = null;
        api.dispatchAction(actuallyPayload);
P
pah100 已提交
68
    }
S
sushuang 已提交
69
}
P
pah100 已提交
70

S
sushuang 已提交
71 72 73 74 75 76 77
function onLeave(record, e, dispatchAction) {
    record.handler('leave', null, dispatchAction);
}

function doEnter(currTrigger, record, e, dispatchAction) {
    record.handler(currTrigger, e, dispatchAction);
}
P
pah100 已提交
78

S
sushuang 已提交
79 80 81 82 83 84 85 86 87 88 89 90 91 92
function makeDispatchAction(api) {
    var pendings = {
        showTip: [],
        hideTip: []
    };
    // FIXME
    // better approach?
    // 'showTip' and 'hideTip' can be triggered by axisPointer and tooltip,
    // which may be conflict, (axisPointer call showTip but tooltip call hideTip);
    // So we have to add "final stage" to merge those dispatched actions.
    var dispatchAction = function (payload) {
        var pendingList = pendings[payload.type];
        if (pendingList) {
            pendingList.push(payload);
P
pah100 已提交
93
        }
S
sushuang 已提交
94 95 96
        else {
            payload.dispatchAction = dispatchAction;
            api.dispatchAction(payload);
P
pah100 已提交
97 98 99
        }
    };

S
sushuang 已提交
100 101 102 103 104 105 106 107 108 109
    return {
        dispatchAction: dispatchAction,
        pendings: pendings
    };
}

/**
 * @param {string} key
 * @param {module:echarts/ExtensionAPI} api
 */
S
sushuang 已提交
110
export function unregister(key, api) {
S
sushuang 已提交
111 112 113 114
    if (env.node) {
        return;
    }
    var zr = api.getZr();
115
    var record = (inner(zr).records || {})[key];
S
sushuang 已提交
116
    if (record) {
117
        inner(zr).records[key] = null;
S
sushuang 已提交
118
    }
S
sushuang 已提交
119
}