-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsmart-route.js
314 lines (254 loc) · 9.86 KB
/
smart-route.js
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
import React, { useEffect, useRef, useState } from 'react'
import { getAddressArray } from './utilities/get-address-array'
import { getAddressFromArray } from './utilities/get-address-from-array'
import { getAncestorNodeAddress } from './utilities/get-ancestor-node-address'
import { getCurrentNodeAddress } from './utilities/get-current-node-address'
import { getNodeAddress } from './utilities/get-node-address'
import { getParentNodeAddress } from './utilities/get-parent-node-address'
import { getRelativeAddress } from './utilities/get-relative-address'
import { getRootNodeAddress } from './utilities/get-root-node-address'
import { getVariableAddressKey } from './utilities/get-variable-address-key'
import { isVariableAddress } from './utilities/is-variable-address'
import { removeLeadingSlash } from './utilities/remove-leading-slash'
import { removeTrailingSlash } from './utilities/remove-trailing-slash'
import { shouldRouteActivate } from './utilities/should-route-activate'
const useComponentWillMount = (callback) => {
const mounted = useRef(false);
useEffect(() => {
if (mounted.current) {
return;
}
callback();
mounted.current = true
}, []);
};
function SmartRoute(props) {
const {
children,
defaultChild,
navSvc = { _index: 0 },
nodeAddress = '',
onMount,
onNavSvcInit,
...restProps
} = props;
const [changeCount, setChangeCount] = useState(0);
const [isActive, setIsActive] = useState(false);
const [isHandling, setIsHandling] = useState(false);
const [isMounted, setIsMounted] = useState(false);
const [targetAddress, setTargetAddress] = useState('');
const [variableKey, setVariableKey] = useState('');
let _timeoutId;
function initializeSmartRoute() {
const nodeIndex = navSvc._index;
const adjacentIndex = (navSvc._rootAddress === '') ? nodeIndex - 1 : nodeIndex; // todo check if ever nodeIndex
if (nodeIndex > 0 && shouldRouteActivate(navSvc._targetAddress, adjacentIndex, nodeAddress)) {
setIsActive(true);
} else if (nodeIndex === 0) {
setIsActive(true);
const pathname = window?.location.pathname;
setTargetAddress((pathname !== '/') ? removeTrailingSlash(pathname) : nodeAddress);
}
};
useComponentWillMount(() => initializeSmartRoute());
useEffect(() => {
const nodeIndex = navSvc._index;
if (nodeIndex === 0) {
_bindAllElements();
onNavSvcInit && onNavSvcInit({
_index: navSvc._index - 1,
_rootAddress: nodeAddress,
_targetAddress: targetAddress,
_setTargetAddress: _setTargetAddress,
changeCount: changeCount,
setFocus: setFocus,
getFocus: getFocus,
getSelf: getSelf,
getRoot: getRoot
});
} else if (defaultChild && nodeIndex === getAddressArray(navSvc._targetAddress).length - 1) {
setFocus(defaultChild);
}
setIsMounted(true);
}, []);
useEffect(() => {
if (!nodeAddress) {
return;
}
setVariableKey(isVariableAddress(nodeAddress) ? getVariableAddressKey(nodeAddress) : '');
}, [nodeAddress]);
useEffect(() => {
const nextTarget = navSvc._targetAddress;
const nodeIndex = navSvc._index;
if (nextTarget == null) {
return;
}
if (nodeIndex === getAddressArray(nextTarget).length - 1 && !!defaultChild) {
setFocus(defaultChild);
} else {
const adjIndex = (navSvc._rootAddress === '') ? nodeIndex - 1 : nodeIndex;
setIsActive(shouldRouteActivate(nextTarget, adjIndex, nodeAddress));
}
}, [navSvc, nodeAddress]);
useEffect(() => {
if (isActive && navSvc._targetAddress !== targetAddress && !!onFocusChange) {
onFocusChange(navSvc._targetAddress || targetAddress);
}
return () => {
const nodeIndex = navSvc._index;
if (!!window && nodeIndex === 0) {
_unbindAllElements();
}
clearTimeout(_timeoutId);
setIsMounted(false);
}
}, []);
function getRoot() {
return navSvc._rootAddress || nodeAddress;
};
function getSelf() {
const navSvc = navSvc;
const targetAddressArr = getAddressArray(navSvc._targetAddress || targetAddress);
const index = (!navSvc._rootAddress) ? navSvc._index - 1 : navSvc._index;
index -= (nodeAddress === '.') ? 1 : 0;
return targetAddressArr[index];
};
function getFocus() {
return navSvc._targetAddress || targetAddress;
};
function setFocus(reqAddress, isPopState) {
reqAddress = (reqAddress === '/') ? './' : reqAddress;
//const navSvc = navSvc || {};
const nodeIndex = navSvc?._index;
const rootAddress = (nodeIndex > 0) ? navSvc._rootAddress : nodeAddress;
let targetAddress = navSvc?._targetAddress || targetAddress;
const hashIndex = targetAddress?.indexOf('#');
targetAddress = (hashIndex < 0) ? targetAddress : targetAddress?.substring(0, hashIndex);
const nextAddress = (() => {
let targetAddrArr = (!targetAddress) ? [] : getAddressArray(targetAddress);
if (rootAddress === getAddressArray(reqAddress)[0]) { // returns reqAddress as nextAddress if address is absolute
return reqAddress;
} else if (reqAddress === './') { // returns root node
return rootAddress;
}
const adjIndex = (rootAddress === '') ? nodeIndex - 1 : nodeIndex;
for (let i = targetAddrArr.length - 1; i > adjIndex; i--) { // aligns address to position of node requesting new focus
targetAddrArr.pop();
}
if (!reqAddress) {
return reqAddress;
} else if (reqAddress.substring(0, 2) === '..') { // parses relative-to-ancestor focus
let navAddress = '/' + reqAddress;
for (let i = 0; i < reqAddress.length; i += 3) {
if (reqAddress.substring(i, i + 2) !== '..') {
break;
} else {
targetAddrArr.pop();
navAddress = navAddress.substring(3);
}
}
return getAddressFromArray(targetAddrArr) + navAddress;
} else if (reqAddress[0] === '.') { // parses self focus
return getAddressFromArray(targetAddrArr);
} else if (reqAddress[0] !== '/') { // parses relative-to-self focus
targetAddrArr.pop();
return getAddressFromArray(targetAddrArr) + '/' + reqAddress;
} else { // parses relative-to-descendant focus
return getAddressFromArray(targetAddrArr) + reqAddress;
}
})();
let addrChanged = false;
if (nodeIndex !== 0) {
addrChanged = navSvc._setTargetAddress(nextAddress, isPopState);
} else {
if (nextAddress === targetAddress) { // escapes address change when already at root
return addrChanged;
}
addrChanged = _setTargetAddress(nextAddress, isPopState);
}
return addrChanged;
};
function _setTargetAddress(_targetAddress, isPopState) {
if (!isMounted) { // _setTargetAddress may be called before root is mounted, so
_timeoutId = setTimeout((_targetAddress, isPopState) => { // it awaits for root to mount before setting a new target
_setTargetAddress(_targetAddress, isPopState);
}, 100, _targetAddress, isPopState);
return;
}
if (targetAddress === _targetAddress) { // route change is aborted if the route is the same
return false;
}
const targetAddressArr = getAddressArray(_targetAddress);
const relativeAddress = getRelativeAddress(_targetAddress);
window && !isPopState &&
window.history.pushState(
targetAddressArr,
relativeAddress,
window.location.origin + _targetAddress + window.location.search);
setTargetAddress(_targetAddress);
setIsHandling(true);
setChangeCount(changeCount + 1);
return true;
};
function _handlePopstate(e) {
const pathname = window.location.pathname;
const _targetAddress = (pathname === '/') ? '' : pathname;
_setTargetAddress(_targetAddress, true);
};
function _handleLayerClick(e) {
let target = e.target || e.srcElement;
if (target.tagName !== 'A') {
return;
}
setFocus(target.href);
};
function _bindAllElements() {
window.addEventListener('popstate', _handlePopstate);
};
function _unbindAllElements() {
window.removeEventListener('popstate', _handlePopstate);
};
if (!isActive) {
return null;
}
const childrenWithProps = React.Children.map(children, child => {
if (!child) {
return;
}
const nextProps = {};
const childProps = child.props;
for (let i = 0, keys = Object.keys(childProps); i < keys.length; i++) {
const key = keys[i];
nextProps[key] = childProps[key];
}
for (let i = 0, keys = Object.keys(restProps); i < keys.length; i++) {
const key = keys[i];
nextProps[key] = restProps[key];
}
let
rootAddress = nodeAddress,
nextTargetAddress = targetAddress,
nextChangeCount = changeCount,
targetAddressCb = _setTargetAddress;
if (navSvc._index > 0) {
rootAddress = navSvc._rootAddress;
nextTargetAddress = navSvc._targetAddress;
nextChangeCount = navSvc.changeCount;
targetAddressCb = navSvc._setTargetAddress;
}
nextProps.navSvc = {
_index: navSvc._index + 1,
_rootAddress: rootAddress,
_targetAddress: targetAddress,
_setTargetAddress: targetAddressCb,
changeCount: changeCount,
setFocus: setFocus,
getFocus: getFocus,
getSelf: getSelf,
getRoot: getRoot
};
return React.cloneElement(child, nextProps);
});
return childrenWithProps;
};
export default SmartRoute;