forked from preactjs/preact-router
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmatch.js
More file actions
99 lines (74 loc) · 3.85 KB
/
match.js
File metadata and controls
99 lines (74 loc) · 3.85 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
'use strict';
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.Link = exports.Match = undefined;
var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; };
var _preact = require('preact');
var _preactRouter = require('preact-router');
function _objectWithoutProperties(obj, keys) { var target = {}; for (var i in obj) { if (keys.indexOf(i) >= 0) continue; if (!Object.prototype.hasOwnProperty.call(obj, i)) continue; target[i] = obj[i]; } return target; }
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; }
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; }
function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; }
var Match = exports.Match = function (_Component) {
_inherits(Match, _Component);
function Match(props, context) {
_classCallCheck(this, Match);
var _this = _possibleConstructorReturn(this, _Component.call(this, props));
_this.update = function (url) {
_this.nextUrl = url;
_this.setState({});
};
_this.baseUrl = props.basePath || '';
if (props.path) {
var segments = (0, _preactRouter.segmentize)(props.path);
segments.forEach(function (segment) {
if (segment.indexOf(':') == -1) {
_this.baseUrl = _this.baseUrl + '/' + segment;
}
});
}
if (context && context['preact-router-base']) {
_this.baseUrl = context['preact-router-base'] + _this.baseUrl;
}
return _this;
}
Match.prototype.componentDidMount = function componentDidMount() {
_preactRouter.subscribers.push(this.update);
};
Match.prototype.componentWillUnmount = function componentWillUnmount() {
_preactRouter.subscribers.splice(_preactRouter.subscribers.indexOf(this.update) >>> 0, 1);
};
Match.prototype.getChildContext = function getChildContext() {
return _defineProperty({}, 'preact-router-base', this.baseUrl);
};
Match.prototype.render = function render(props) {
var url = this.nextUrl || (0, _preactRouter.getCurrentUrl)(),
path = url.replace(/\?.+$/, ''),
route = props.basePath && !!props.path.indexOf(props.basePath) ? props.basePath + props.path : props.path;
this.nextUrl = null;
return props.children({
url: url,
path: path,
matches: (0, _preactRouter.exec)(path, route, {}) !== false
});
};
return Match;
}(_preact.Component);
var Link = function Link(_ref2) {
var activeClassName = _ref2.activeClassName,
path = _ref2.path,
props = _objectWithoutProperties(_ref2, ['activeClassName', 'path']);
return (0, _preact.h)(
Match,
{ path: path || props.href },
function (_ref3) {
var matches = _ref3.matches;
return (0, _preact.h)(_preactRouter.Link, _extends({}, props, { 'class': [props.class || props.className, matches && activeClassName].filter(Boolean).join(' ') }));
}
);
};
exports.Link = Link;
exports.default = Match;
Match.Link = Link;