You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

OutboundLink.js 7.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. function _typeof(obj) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (obj) { return typeof obj; } : function (obj) { return obj && "function" == typeof Symbol && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }, _typeof(obj); }
  2. var _excluded = ["to", "target"];
  3. function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; }
  4. function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { _defineProperty(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; }
  5. function _objectWithoutProperties(source, excluded) { if (source == null) return {}; var target = _objectWithoutPropertiesLoose(source, excluded); var key, i; if (Object.getOwnPropertySymbols) { var sourceSymbolKeys = Object.getOwnPropertySymbols(source); for (i = 0; i < sourceSymbolKeys.length; i++) { key = sourceSymbolKeys[i]; if (excluded.indexOf(key) >= 0) continue; if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue; target[key] = source[key]; } } return target; }
  6. function _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } return target; }
  7. function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
  8. function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } }
  9. function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); Object.defineProperty(Constructor, "prototype", { writable: false }); return Constructor; }
  10. function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function"); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, writable: true, configurable: true } }); Object.defineProperty(subClass, "prototype", { writable: false }); if (superClass) _setPrototypeOf(subClass, superClass); }
  11. function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf ? Object.setPrototypeOf.bind() : function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); }
  12. function _createSuper(Derived) { var hasNativeReflectConstruct = _isNativeReflectConstruct(); return function _createSuperInternal() { var Super = _getPrototypeOf(Derived), result; if (hasNativeReflectConstruct) { var NewTarget = _getPrototypeOf(this).constructor; result = Reflect.construct(Super, arguments, NewTarget); } else { result = Super.apply(this, arguments); } return _possibleConstructorReturn(this, result); }; }
  13. function _possibleConstructorReturn(self, call) { if (call && (_typeof(call) === "object" || typeof call === "function")) { return call; } else if (call !== void 0) { throw new TypeError("Derived constructors may only return object or undefined"); } return _assertThisInitialized(self); }
  14. function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; }
  15. function _isNativeReflectConstruct() { if (typeof Reflect === "undefined" || !Reflect.construct) return false; if (Reflect.construct.sham) return false; if (typeof Proxy === "function") return true; try { Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); return true; } catch (e) { return false; } }
  16. function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf.bind() : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); }
  17. function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
  18. import React, { Component } from 'react';
  19. import PropTypes from 'prop-types';
  20. import warn from '../utils/console/warn';
  21. var NEWTAB = '_blank';
  22. var MIDDLECLICK = 1;
  23. var OutboundLink = /*#__PURE__*/function (_Component) {
  24. _inherits(OutboundLink, _Component);
  25. var _super = _createSuper(OutboundLink);
  26. function OutboundLink() {
  27. var _this;
  28. _classCallCheck(this, OutboundLink);
  29. for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
  30. args[_key] = arguments[_key];
  31. }
  32. _this = _super.call.apply(_super, [this].concat(args));
  33. _defineProperty(_assertThisInitialized(_this), "handleClick", function (event) {
  34. var _this$props = _this.props,
  35. target = _this$props.target,
  36. eventLabel = _this$props.eventLabel,
  37. to = _this$props.to,
  38. onClick = _this$props.onClick,
  39. trackerNames = _this$props.trackerNames;
  40. var eventMeta = {
  41. label: eventLabel
  42. };
  43. var sameTarget = target !== NEWTAB;
  44. var normalClick = !(event.ctrlKey || event.shiftKey || event.metaKey || event.button === MIDDLECLICK);
  45. if (sameTarget && normalClick) {
  46. event.preventDefault();
  47. OutboundLink.trackLink(eventMeta, function () {
  48. window.location.href = to;
  49. }, trackerNames);
  50. } else {
  51. OutboundLink.trackLink(eventMeta, function () {}, trackerNames);
  52. }
  53. if (onClick) {
  54. onClick(event);
  55. }
  56. });
  57. return _this;
  58. }
  59. _createClass(OutboundLink, [{
  60. key: "render",
  61. value: function render() {
  62. var _this$props2 = this.props,
  63. href = _this$props2.to,
  64. target = _this$props2.target,
  65. oldProps = _objectWithoutProperties(_this$props2, _excluded);
  66. var props = _objectSpread(_objectSpread({}, oldProps), {}, {
  67. target: target,
  68. href: href,
  69. onClick: this.handleClick
  70. });
  71. if (target === NEWTAB) {
  72. props.rel = "".concat(props.rel ? props.rel : '', " noopener noreferrer").trim();
  73. }
  74. delete props.eventLabel;
  75. delete props.trackerNames;
  76. return /*#__PURE__*/React.createElement('a', props);
  77. }
  78. }]);
  79. return OutboundLink;
  80. }(Component);
  81. _defineProperty(OutboundLink, "trackLink", function () {
  82. warn('ga tracking not enabled');
  83. });
  84. export { OutboundLink as default };
  85. OutboundLink.propTypes = {
  86. eventLabel: PropTypes.string.isRequired,
  87. target: PropTypes.string,
  88. to: PropTypes.string,
  89. onClick: PropTypes.func,
  90. trackerNames: PropTypes.arrayOf(PropTypes.string)
  91. };
  92. OutboundLink.defaultProps = {
  93. target: null,
  94. to: null,
  95. onClick: null,
  96. trackerNames: null
  97. };