feat(riot-v5): handle page query param & refactor router & remove old code

This commit is contained in:
Joxit 2021-03-29 21:49:37 +02:00
parent 603b5861fa
commit 8ef411059c
No known key found for this signature in database
GPG key ID: F526592B8E012263
10 changed files with 74 additions and 733 deletions

View file

@ -15,23 +15,60 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
import { router, getCurrentRoute } from '@riotjs/route';
import { encodeURI } from './utils';
function baseUrl() {
return getCurrentRoute().replace(/#!(.*)/, '');
function getQueryParams() {
const queries = {};
window.location.search
.slice(1)
.split('&')
.forEach((qs) => {
const splitIndex = qs.indexOf('=');
queries[qs.slice(0, splitIndex)] = splitIndex < 0 ? '' : qs.slice(splitIndex + 1);
});
return queries;
}
function updateQueryParams(qs) {
const queryParams = getQueryParams();
for (let key in qs) {
if (qs[key] === null) {
delete queryParams[key];
} else {
queryParams[key] = qs[key];
}
}
return queryParams;
}
function toSearchString(queries) {
let search = [];
for (let key in queries) {
if (queries[key] !== undefined) {
search.push(`${key}=${queries[key]}`);
}
}
return search.length === 0 ? '' : `?${search.join('&')}`;
}
function baseUrl(qs) {
const location = window.location;
const queryParams = updateQueryParams(qs);
return location.origin + location.pathname + toSearchString(queryParams);
}
export default {
home() {
router.push(baseUrl());
router.push(baseUrl({ page: null }));
},
taglist(image) {
router.push(`${baseUrl()}#!/taglist/${image}`);
router.push(`${baseUrl({ page: null })}#!/taglist/${image}`);
},
getTagListImage() {
return getCurrentRoute().replace(/^.*(#!)?\/?taglist\//, '');
},
history(image, tag) {
router.push(`${baseUrl()}#!/taghistory/image/${image}/tag/${tag}`);
router.push(`${baseUrl({ page: null })}#!/taghistory/image/${image}/tag/${tag}`);
},
getTagHistoryImage() {
return getCurrentRoute().replace(/^.*(#!)?\/?taghistory\/image\/(.*)\/tag\/(.*)\/?$/, '$2');
@ -39,4 +76,18 @@ export default {
getTagHistoryTag() {
return getCurrentRoute().replace(/^.*(#!)?\/?taghistory\/image\/(.*)\/tag\/(.*)\/?$/, '$3');
},
updateQueryString(qs) {
const search = toSearchString(updateQueryParams(qs));
history.pushState(null, '', search + window.location.hash);
},
updateUrlQueryParam(url) {
this.updateQueryString({ url: encodeURI(url) });
},
updatePageQueryParam(page) {
this.updateQueryString({ page });
},
getPageQueryParam() {
const queries = getQueryParams();
return queries['page'];
},
};