UNPKG

@roderickhsiao/react-i13n

Version:

[Experiment] React I13n provides a performant and scalable solution to application instrumentation.

149 lines (120 loc) 4.14 kB
"use strict"; exports.__esModule = true; exports["default"] = void 0; /** * Copyright 2015 - Present, Yahoo Inc. * Copyrights licensed under the New BSD License. See the accompanying LICENSE file for terms. */ var isLeftClickEvent = function isLeftClickEvent(e) { return e.button === 0; }; var isModifiedEvent = function isModifiedEvent(e) { return !!(e.metaKey || e.altKey || e.ctrlKey || e.shiftKey); }; var getLinkTarget = function getLinkTarget(target, props) { return props.target || (target === null || target === void 0 ? void 0 : target.target) || '_self'; }; var isNewWindow = function isNewWindow(target, props) { return getLinkTarget(target, props) === '_blank'; }; var isLink = function isLink(target) { return target.tagName === 'A'; }; var isButtonLike = function isButtonLike(target) { var tagName = target.tagName, type = target.type; if (tagName === 'BUTTON') { return true; } // input with submit or button type if (tagName === 'INPUT' && (type === 'submit' || type === 'button')) { return true; } }; var isDefaultRedirectLink = function 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 = function 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, options) { var _ref, _shouldFollowLink; if (options === void 0) { options = {}; } var target = e.target || e.srcElement; var isForm = isFormSubmit(target); var isRedirectLink = isDefaultRedirectLink(target); var isPreventDefault = true; var _options = options, executeEvent = _options.executeEvent, i13nNode = _options.i13nNode, _options$props = _options.props, props = _options$props === void 0 ? {} : _options$props, shouldFollowLink = _options.shouldFollowLink; if (!executeEvent) { return; } var follow = props.follow; 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: i13nNode, e: e }, function () { 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); } } } }); }; var _default = clickHandler; exports["default"] = _default;