UNPKG

11.1 kBJavaScriptView Raw
1'use strict';
2
3Object.defineProperty(exports, "__esModule", {
4 value: true
5});
6
7var _extends2 = require('babel-runtime/helpers/extends');
8
9var _extends3 = _interopRequireDefault(_extends2);
10
11var _objectWithoutProperties2 = require('babel-runtime/helpers/objectWithoutProperties');
12
13var _objectWithoutProperties3 = _interopRequireDefault(_objectWithoutProperties2);
14
15var _getPrototypeOf = require('babel-runtime/core-js/object/get-prototype-of');
16
17var _getPrototypeOf2 = _interopRequireDefault(_getPrototypeOf);
18
19var _classCallCheck2 = require('babel-runtime/helpers/classCallCheck');
20
21var _classCallCheck3 = _interopRequireDefault(_classCallCheck2);
22
23var _createClass2 = require('babel-runtime/helpers/createClass');
24
25var _createClass3 = _interopRequireDefault(_createClass2);
26
27var _possibleConstructorReturn2 = require('babel-runtime/helpers/possibleConstructorReturn');
28
29var _possibleConstructorReturn3 = _interopRequireDefault(_possibleConstructorReturn2);
30
31var _inherits2 = require('babel-runtime/helpers/inherits');
32
33var _inherits3 = _interopRequireDefault(_inherits2);
34
35var _react = require('react');
36
37var _react2 = _interopRequireDefault(_react);
38
39var _PropTypes = require('react-router/lib/PropTypes');
40
41function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
42
43/**
44 * @copyright 2016-present, Sitegen team
45 */
46
47var Link = function (_React$Component) {
48 (0, _inherits3.default)(Link, _React$Component);
49
50 function Link() {
51 var _Object$getPrototypeO;
52
53 var _temp, _this, _ret;
54
55 (0, _classCallCheck3.default)(this, Link);
56
57 for (var _len = arguments.length, args = Array(_len), _key = 0; _key < _len; _key++) {
58 args[_key] = arguments[_key];
59 }
60
61 return _ret = (_temp = (_this = (0, _possibleConstructorReturn3.default)(this, (_Object$getPrototypeO = (0, _getPrototypeOf2.default)(Link)).call.apply(_Object$getPrototypeO, [this].concat(args))), _this), _this.onClick = function (event) {
62 var allowTransition = true;
63
64 if (_this.props.onClick) {
65 _this.props.onClick(event);
66 }
67
68 if (isModifiedEvent(event) || !isLeftClickEvent(event)) {
69 return;
70 }
71
72 if (event.defaultPrevented === true) {
73 allowTransition = false;
74 }
75
76 // If target prop is set (e.g. to "_blank") let browser handle link.
77 /* istanbul ignore if: untestable with Karma */
78 if (_this.props.target) {
79 if (!allowTransition) {
80 event.preventDefault();
81 }
82
83 return;
84 }
85
86 event.preventDefault();
87
88 if (allowTransition) {
89 var href = _this.href;
90
91 if (href) {
92 _this.context.router.push({ pathname: href });
93 }
94 }
95 }, _temp), (0, _possibleConstructorReturn3.default)(_this, _ret);
96 }
97
98 (0, _createClass3.default)(Link, [{
99 key: 'render',
100 value: function render() {
101 var _props = this.props;
102 var Component = _props.Component;
103 var className = _props.className;
104 var activeClassName = _props.activeClassName;
105 var style = _props.style;
106 var activeStyle = _props.activeStyle;
107 var props = (0, _objectWithoutProperties3.default)(_props, ['Component', 'className', 'activeClassName', 'style', 'activeStyle']);
108
109 var href = this.href;
110 var router = this.context.router;
111
112 var external = isExternal(href);
113 var active = !external && href && router.isActive({ pathname: href }, true);
114 if (active && activeClassName) {
115 className = (className || '') + ' ' + activeClassName;
116 }
117 if (active && activeStyle) {
118 style = (0, _extends3.default)({}, style, { activeStyle: activeStyle });
119 }
120 return _react2.default.createElement(Component, (0, _extends3.default)({}, props, {
121 href: href,
122 active: active,
123 style: style,
124 className: className,
125 onClick: !external && this.onClick
126 }));
127 }
128 }, {
129 key: 'href',
130 get: function get() {
131 var href = this.props.href;
132
133 if (href && typeof __webpack_public_path__ !== 'undefined' && !isExternal(href)) {
134 href = join(__webpack_public_path__, href);
135 }
136 return href;
137 }
138 }]);
139 return Link;
140}(_react2.default.Component);
141
142Link.contextTypes = { router: _PropTypes.routerShape };
143Link.defaultProps = {
144 Component: 'a'
145};
146exports.default = Link;
147
148
149function isLeftClickEvent(event) {
150 return event.button === 0;
151}
152
153function isModifiedEvent(event) {
154 return !!(event.metaKey || event.altKey || event.ctrlKey || event.shiftKey);
155}
156
157function isExternal(href) {
158 return (/^(https?|mailto):/.exec(href)
159 );
160}
161
162function join() {
163 for (var _len2 = arguments.length, segments = Array(_len2), _key2 = 0; _key2 < _len2; _key2++) {
164 segments[_key2] = arguments[_key2];
165 }
166
167 return normalize(segments.join('/'));
168}
169
170function normalize(href) {
171 return href.replace(new RegExp('/+', 'g'), '/');
172}
173//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbIi9Vc2Vycy9hbmRyZXlwb3BwL1dvcmtzcGFjZS9zaXRlZ2VuL3NyYy9MaW5rLmpzIl0sIm5hbWVzIjpbXSwibWFwcGluZ3MiOiI7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7QUFJQTs7OztBQUNBOzs7Ozs7OztJQUVxQixJOzs7Ozs7Ozs7Ozs7Ozt3TkFpRG5CLE8sR0FBVSxVQUFDLEtBQUQsRUFBVztBQUNuQixVQUFJLGtCQUFrQixJQUF0Qjs7QUFFQSxVQUFJLE1BQUssS0FBTCxDQUFXLE9BQWYsRUFBd0I7QUFDdEIsY0FBSyxLQUFMLENBQVcsT0FBWCxDQUFtQixLQUFuQjtBQUNEOztBQUVELFVBQUksZ0JBQWdCLEtBQWhCLEtBQTBCLENBQUMsaUJBQWlCLEtBQWpCLENBQS9CLEVBQXdEO0FBQ3REO0FBQ0Q7O0FBRUQsVUFBSSxNQUFNLGdCQUFOLEtBQTJCLElBQS9CLEVBQXFDO0FBQ25DLDBCQUFrQixLQUFsQjtBQUNEOzs7O0FBSUQsVUFBSSxNQUFLLEtBQUwsQ0FBVyxNQUFmLEVBQXVCO0FBQ3JCLFlBQUksQ0FBQyxlQUFMLEVBQXNCO0FBQ3BCLGdCQUFNLGNBQU47QUFDRDs7QUFFRDtBQUNEOztBQUVELFlBQU0sY0FBTjs7QUFFQSxVQUFJLGVBQUosRUFBcUI7QUFDbkIsWUFBSSxPQUFPLE1BQUssSUFBaEI7O0FBRUEsWUFBSSxJQUFKLEVBQVU7QUFDUixnQkF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m9yZSBpZjogdW50ZXN0YWJsZSB3aXRoIEthcm1hICovXG4gICAgaWYgKHRoaXMucHJvcHMudGFyZ2V0KSB7XG4gICAgICBpZiAoIWFsbG93VHJhbnNpdGlvbikge1xuICAgICAgICBldmVudC5wcmV2ZW50RGVmYXVsdCgpO1xuICAgICAgfVxuXG4gICAgICByZXR1cm47XG4gICAgfVxuXG4gICAgZXZlbnQucHJldmVudERlZmF1bHQoKTtcblxuICAgIGlmIChhbGxvd1RyYW5zaXRpb24pIHtcbiAgICAgIGxldCBocmVmID0gdGhpcy5ocmVmO1xuXG4gICAgICBpZiAoaHJlZikge1xuICAgICAgICB0aGlzLmNvbnRleHQucm91dGVyLnB1c2goe3BhdGhuYW1lOiBocmVmfSk7XG4gICAgICB9XG4gICAgfVxuICB9O1xufVxuXG5mdW5jdGlvbiBpc0xlZnRDbGlja0V2ZW50KGV2ZW50KSB7XG4gIHJldHVybiBldmVudC5idXR0b24gPT09IDA7XG59XG5cbmZ1bmN0aW9uIGlzTW9kaWZpZWRFdmVudChldmVudCkge1xuICByZXR1cm4gISEoZXZlbnQubWV0YUtleSB8fCBldmVudC5hbHRLZXkgfHwgZXZlbnQuY3RybEtleSB8fCBldmVudC5zaGlmdEtleSk7XG59XG5cbmZ1bmN0aW9uIGlzRXh0ZXJuYWwoaHJlZikge1xuICByZXR1cm4gL14oaHR0cHM/fG1haWx0byk6Ly5leGVjKGhyZWYpO1xufVxuXG5mdW5jdGlvbiBqb2luKC4uLnNlZ21lbnRzKSB7XG4gIHJldHVybiBub3JtYWxpemUoc2VnbWVudHMuam9pbignLycpKTtcbn1cblxuZnVuY3Rpb24gbm9ybWFsaXplKGhyZWYpIHtcbiAgcmV0dXJuIGhyZWYucmVwbGFjZShuZXcgUmVnRXhwKCcvKycsICdnJyksICAnLycpO1xufVxuIl19
\No newline at end of file