fix tab fragments

This commit is contained in:
Dennis Eichhorn 2021-07-17 21:11:40 +02:00
parent e96311893f
commit c8a180fa8a
2 changed files with 17 additions and 19 deletions

View File

@ -65,7 +65,7 @@ export class Tab
for (let i = 0; i < length; ++i) { for (let i = 0; i < length; ++i) {
nodes[i].addEventListener('click', function (evt) nodes[i].addEventListener('click', function (evt)
{ {
let fragmentString = window.location.href.includes('#') ? HttpUri.parseUrl(window.location.href).fragment : ''; let fragmentString = this.querySelector('label').getAttribute('for');
/* Change Tab */ /* Change Tab */
/* Remove selected tab */ /* Remove selected tab */
@ -87,9 +87,10 @@ export class Tab
let fragments = fragmentString.split('&'); let fragments = fragmentString.split('&');
const index = fragments.indexOf(this.getElementsByTagName('label')[0].getAttribute('for')); const index = fragments.indexOf(this.getElementsByTagName('label')[0].getAttribute('for'));
/**
if (index > -1) { if (index > -1) {
fragments.splice(index, 1); fragments.splice(index, 1);
} }*/
// find old active and remove it // find old active and remove it
fragmentString = fragments.join('&'); fragmentString = fragments.join('&');
@ -99,7 +100,7 @@ export class Tab
/* Add selected tab */ /* Add selected tab */
window.history.replaceState(null, '', window.history.replaceState(null, '',
UriFactory.build( UriFactory.build(
'{%}#' + (fragmentString === '' ? '' : fragmentString + '&') + this.getElementsByTagName('label')[0].getAttribute('for') '{%}#' + (fragmentString === '' ? '' : fragmentString)
) )
); );
}); });

View File

@ -128,13 +128,12 @@ export class UriFactory
{ {
// @todo: there is a bug for uris which have a parameter without a value and a fragment e.g. ?debug#something. // @todo: there is a bug for uris which have a parameter without a value and a fragment e.g. ?debug#something.
// The fragment is ignored in such a case. // The fragment is ignored in such a case.
if (url.includes('?')) { const parsed = HttpUri.parseUrl(url);
const parsed = HttpUri.parseUrl(url); const pars = [];
if (url.includes('?')) {
// unique queries // unique queries
const parts = typeof parsed.query === 'undefined' ? [] : parsed.query.replace(/\?/g, '&').split('&'), const parts = typeof parsed.query === 'undefined' ? [] : parsed.query.replace(/\?/g, '&').split('&');
full = url.split('?')[0],
pars = [];
let comps = {}, let comps = {},
spl = null, spl = null,
@ -152,20 +151,18 @@ export class UriFactory
pars.push(a); pars.push(a);
} }
} }
url = full + '?' + pars.join('&');
} }
// unique fragments const fragments = typeof parsed.fragment !== 'undefined' ? parsed.fragment.split('#') : null;
const fragments = url.match(/\#[a-zA-Z0-9\-,]+/g),
fragLength = fragments !== null ? fragments.length : 0;
// @todo: don't remove fragments, some might be used = none tab fragments url = (typeof parsed.scheme !== 'undefined' ? parsed.scheme + '://' : '')
+ (typeof parsed.username !== 'undefined' ? parsed.username + ':' : '')
for (let i = 0; i < fragLength - 1; ++i) { + (typeof parsed.password !== 'undefined' ? parsed.password + '@' : '')
// remove all from old url + (typeof parsed.host !== 'undefined' ? parsed.host : '')
url = url.replace(fragments[i], ''); + (typeof parsed.port !== 'undefined' ? parsed.port : '')
} + (typeof parsed.path !== 'undefined' ? parsed.path : '')
+ (typeof parsed.query !== 'undefined' ? '?' + pars.join('&') : '')
+ (typeof parsed.fragment !== 'undefined' ? '#' + fragments[fragments.length - 1] : '');
return url; return url;
}; };