Skip to content

Commit 91786aa

Browse files
committed
fix: use vanilla js to interact with URL (#172)
* refactor: use vanilla js to interact with URL * chore: drop @neovici/cosmoz-page-router dependency
1 parent 1047af2 commit 91786aa

File tree

3 files changed

+19145
-95
lines changed

3 files changed

+19145
-95
lines changed

cosmoz-data-nav.js

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,6 @@ import { Debouncer } from '@polymer/polymer/lib/utils/debounce';
1111
import { animationFrame } from '@polymer/polymer/lib/utils/async';
1212
import { flush } from '@polymer/polymer/lib/utils/flush';
1313

14-
import '@neovici/cosmoz-page-router/cosmoz-page-location';
15-
1614
import { hauntedPolymer } from '@neovici/cosmoz-utils';
1715

1816
import { useDataNav } from './lib/use-data-nav.js';
@@ -21,6 +19,20 @@ const _async = window.requestIdleCallback || window.requestAnimationFrame || win
2119
_hasDeadline = 'IdleDeadline' in window,
2220
_asyncPeriod = (cb, timeout = 1500) => {
2321
_async(() => cb(), _hasDeadline && { timeout });
22+
},
23+
24+
hashUrl = () => new URL(location.hash.replace(/^#!?/iu, '').replace('%23', '#'), location.origin),
25+
getHashParam = param => new URLSearchParams(hashUrl().hash.replace('#', '')).get(param),
26+
setHashParam = (param, value) => {
27+
const
28+
url = hashUrl(),
29+
searchParams = new URLSearchParams(url.hash.replace('#', ''));
30+
31+
searchParams.set(param, value);
32+
33+
const newUrl = '#!' + Object.assign(url, { hash: searchParams }).href.replace(location.origin, '');
34+
35+
history.replaceState(null, '', newUrl);
2436
};
2537

2638
class CosmozDataNav extends hauntedPolymer('haunted', useDataNav)(PolymerElement) {
@@ -64,7 +76,6 @@ class CosmozDataNav extends hauntedPolymer('haunted', useDataNav)(PolymerElement
6476
display: none;
6577
}
6678
</style>
67-
<cosmoz-page-location id="location" route-hash="{{ _routeHashParams }}"></cosmoz-page-location>
6879
<div id="items">
6980
<slot name="items"></slot>
7081
</div>
@@ -250,14 +261,6 @@ class CosmozDataNav extends hauntedPolymer('haunted', useDataNav)(PolymerElement
250261
type: String
251262
},
252263

253-
/**
254-
* The route hash parameters extracted by the `cosmoz-page-location`
255-
* element.
256-
*/
257-
_routeHashParams: {
258-
type: Object
259-
},
260-
261264
/**
262265
*
263266
*/
@@ -941,7 +944,7 @@ class CosmozDataNav extends hauntedPolymer('haunted', useDataNav)(PolymerElement
941944
const hashParam = this.hashParam,
942945
idPath = this.idPath;
943946

944-
if (!hashParam || !idPath || !this._routeHashParams || !this.items.length) {
947+
if (!hashParam || !idPath || !this.items.length) {
945948
return;
946949
}
947950

@@ -950,30 +953,28 @@ class CosmozDataNav extends hauntedPolymer('haunted', useDataNav)(PolymerElement
950953
return;
951954
}
952955
const itemId = this._getItemId(item),
953-
path = ['_routeHashParams', hashParam],
954-
hashValue = this.get(path);
956+
hashValue = getHashParam(hashParam);
955957

956958
if (itemId === hashValue) {
957959
return;
958960
}
959961

960-
this.set(path, itemId);
962+
setHashParam(hashParam, itemId);
961963
}
962964

963965
_updateSelectedFromHash() {
964966
const hashParam = this.hashParam,
965967
idPath = this.idPath;
966968

967-
if (!(hashParam && idPath && this._routeHashParams)) {
969+
if (!(hashParam && idPath)) {
968970
return;
969971
}
970972

971973
if (this._readFromHashOnce && !this.maintainSelection) {
972974
return;
973975
}
974976

975-
const path = ['_routeHashParams', hashParam],
976-
hashValue = this.get(path);
977+
const hashValue = getHashParam(hashParam);
977978

978979
if (!hashValue) {
979980
this._readFromHashOnce = true;

0 commit comments

Comments
 (0)