@roderickhsiao/react-i13n
Version:
[Experiment] React I13n provides a performant and scalable solution to application instrumentation.
134 lines (108 loc) • 3.63 kB
JavaScript
/**
* Copyright 2015 - Present, Yahoo Inc.
* Copyrights licensed under the New BSD License. See the accompanying LICENSE file for terms.
*/
var isLeftClickEvent = e => e.button === 0;
var isModifiedEvent = e => !!(e.metaKey || e.altKey || e.ctrlKey || e.shiftKey);
var getLinkTarget = (target, props) => props.target || (target === null || target === void 0 ? void 0 : target.target) || '_self';
var isNewWindow = (target, props) => getLinkTarget(target, props) === '_blank';
var isLink = target => target.tagName === 'A';
var isButtonLike = target => {
var {
tagName,
type
} = target;
if (tagName === 'BUTTON') {
return true;
} // input with submit or button type
if (tagName === 'INPUT' && (type === 'submit' || type === 'button')) {
return true;
}
};
var isDefaultRedirectLink = target => {
// if it's a
// 1. link
// 2. button
// 3. input with submit or button type
// then redirect it by default, otherwise
if (isLink(target) || isButtonLike(target)) {
return true;
}
return false;
};
var isFormSubmit = target => {
// if it's a
// 1. button
// 2. input with submit or button type
if (isButtonLike(target)) {
return true;
}
return false;
};
/**
* clickHandler which integrate the beacon event, send out beacon event then redirect user to the destination.
* @param {Object} e the click event
* @method ClickHandler
*/
var clickHandler = function clickHandler(e) {
var _ref, _shouldFollowLink;
var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
var target = e.target || e.srcElement;
var isForm = isFormSubmit(target);
var isRedirectLink = isDefaultRedirectLink(target);
var isPreventDefault = true;
var {
executeEvent,
i13nNode,
props = {},
shouldFollowLink
} = options;
if (!executeEvent) {
return;
}
var {
follow
} = props;
var href = props.href || target.href; // if users disable the redirect by follow, force set it as false
isRedirectLink = (_ref = (_shouldFollowLink = shouldFollowLink === null || shouldFollowLink === void 0 ? void 0 : shouldFollowLink(props)) !== null && _shouldFollowLink !== void 0 ? _shouldFollowLink : follow) !== null && _ref !== void 0 ? _ref : isRedirectLink; // 1. not a link or button
// 2. if it is an anchor but no href or hash link
// 3. button without form submit
// Do not trigger navigate action. Let browser handle it natively.
if (!isDefaultRedirectLink(target) || isLink(target) && (!href || href && href[0] === '#') || isButtonLike(target) && !target.form) {
isRedirectLink = false;
isPreventDefault = false;
} // this is a click with a modifier or not a left-click
// let browser handle it natively
if (isModifiedEvent(e) || !isLeftClickEvent(e) || isNewWindow(target, props)) {
isPreventDefault = false;
isRedirectLink = false;
}
if (isPreventDefault) {
if (e.preventDefault) {
e.preventDefault();
} else {
e.returnValue = false;
}
}
executeEvent('click', {
i13nNode,
e
}, () => {
if (isRedirectLink) {
if (isForm) {
var _target$form;
(_target$form = target.form) === null || _target$form === void 0 ? void 0 : _target$form.submit();
} else {
var linkTarget = getLinkTarget(target, props);
if (linkTarget === '_top') {
window.top.location.href = href;
} else if (linkTarget === '_parent') {
window.parent.location.href = href;
} else {
window.location.assign(href);
}
}
}
});
};
export default clickHandler;