ant-design-vue/components/_util/css-animation/Event.js

131 lines
3.2 KiB
JavaScript
Raw Normal View History

2018-12-12 14:09:30 +00:00
const START_EVENT_NAME_MAP = {
transitionstart: {
transition: 'transitionstart',
WebkitTransition: 'webkitTransitionStart',
MozTransition: 'mozTransitionStart',
OTransition: 'oTransitionStart',
msTransition: 'MSTransitionStart',
},
animationstart: {
animation: 'animationstart',
WebkitAnimation: 'webkitAnimationStart',
MozAnimation: 'mozAnimationStart',
OAnimation: 'oAnimationStart',
msAnimation: 'MSAnimationStart',
},
2019-01-12 03:33:27 +00:00
};
2018-12-12 14:09:30 +00:00
const END_EVENT_NAME_MAP = {
2018-02-04 03:08:04 +00:00
transitionend: {
transition: 'transitionend',
WebkitTransition: 'webkitTransitionEnd',
MozTransition: 'mozTransitionEnd',
OTransition: 'oTransitionEnd',
msTransition: 'MSTransitionEnd',
},
animationend: {
animation: 'animationend',
WebkitAnimation: 'webkitAnimationEnd',
MozAnimation: 'mozAnimationEnd',
OAnimation: 'oAnimationEnd',
msAnimation: 'MSAnimationEnd',
},
2019-01-12 03:33:27 +00:00
};
2018-02-04 03:08:04 +00:00
2019-01-12 03:33:27 +00:00
const startEvents = [];
const endEvents = [];
2018-02-04 03:08:04 +00:00
2019-01-12 03:33:27 +00:00
function detectEvents() {
const testEl = document.createElement('div');
const style = testEl.style;
2018-02-04 03:08:04 +00:00
if (!('AnimationEvent' in window)) {
2019-01-12 03:33:27 +00:00
delete START_EVENT_NAME_MAP.animationstart.animation;
delete END_EVENT_NAME_MAP.animationend.animation;
2018-02-04 03:08:04 +00:00
}
if (!('TransitionEvent' in window)) {
2019-01-12 03:33:27 +00:00
delete START_EVENT_NAME_MAP.transitionstart.transition;
delete END_EVENT_NAME_MAP.transitionend.transition;
2018-02-04 03:08:04 +00:00
}
2019-01-12 03:33:27 +00:00
function process(EVENT_NAME_MAP, events) {
2018-12-12 14:09:30 +00:00
for (const baseEventName in EVENT_NAME_MAP) {
if (EVENT_NAME_MAP.hasOwnProperty(baseEventName)) {
2019-01-12 03:33:27 +00:00
const baseEvents = EVENT_NAME_MAP[baseEventName];
2018-12-12 14:09:30 +00:00
for (const styleName in baseEvents) {
if (styleName in style) {
2019-01-12 03:33:27 +00:00
events.push(baseEvents[styleName]);
break;
2018-12-12 14:09:30 +00:00
}
2018-02-04 03:08:04 +00:00
}
}
}
}
2018-12-12 14:09:30 +00:00
2019-01-12 03:33:27 +00:00
process(START_EVENT_NAME_MAP, startEvents);
process(END_EVENT_NAME_MAP, endEvents);
2018-02-04 03:08:04 +00:00
}
if (typeof window !== 'undefined' && typeof document !== 'undefined') {
2019-01-12 03:33:27 +00:00
detectEvents();
2018-02-04 03:08:04 +00:00
}
2019-01-12 03:33:27 +00:00
function addEventListener(node, eventName, eventListener) {
node.addEventListener(eventName, eventListener, false);
2018-02-04 03:08:04 +00:00
}
2019-01-12 03:33:27 +00:00
function removeEventListener(node, eventName, eventListener) {
node.removeEventListener(eventName, eventListener, false);
2018-02-04 03:08:04 +00:00
}
const TransitionEvents = {
2018-12-12 14:09:30 +00:00
// Start events
startEvents,
2019-01-12 03:33:27 +00:00
addStartEventListener(node, eventListener) {
2018-12-12 14:09:30 +00:00
if (startEvents.length === 0) {
2019-01-12 03:33:27 +00:00
window.setTimeout(eventListener, 0);
return;
2018-12-12 14:09:30 +00:00
}
2019-01-12 03:33:27 +00:00
startEvents.forEach(startEvent => {
addEventListener(node, startEvent, eventListener);
});
2018-12-12 14:09:30 +00:00
},
2019-01-12 03:33:27 +00:00
removeStartEventListener(node, eventListener) {
2018-12-12 14:09:30 +00:00
if (startEvents.length === 0) {
2019-01-12 03:33:27 +00:00
return;
2018-12-12 14:09:30 +00:00
}
2019-01-12 03:33:27 +00:00
startEvents.forEach(startEvent => {
removeEventListener(node, startEvent, eventListener);
});
2018-12-12 14:09:30 +00:00
},
// End events
endEvents,
2019-01-12 03:33:27 +00:00
addEndEventListener(node, eventListener) {
2018-02-04 03:08:04 +00:00
if (endEvents.length === 0) {
2019-01-12 03:33:27 +00:00
window.setTimeout(eventListener, 0);
return;
2018-02-04 03:08:04 +00:00
}
2019-01-12 03:33:27 +00:00
endEvents.forEach(endEvent => {
addEventListener(node, endEvent, eventListener);
});
2018-02-04 03:08:04 +00:00
},
2019-01-12 03:33:27 +00:00
removeEndEventListener(node, eventListener) {
2018-02-04 03:08:04 +00:00
if (endEvents.length === 0) {
2019-01-12 03:33:27 +00:00
return;
2018-02-04 03:08:04 +00:00
}
2019-01-12 03:33:27 +00:00
endEvents.forEach(endEvent => {
removeEventListener(node, endEvent, eventListener);
});
2018-02-04 03:08:04 +00:00
},
2019-01-12 03:33:27 +00:00
};
2018-02-04 03:08:04 +00:00
2019-01-12 03:33:27 +00:00
export default TransitionEvents;