mirror of
https://github.com/Joxit/docker-registry-ui.git
synced 2025-04-29 16:39:54 +03:00
feat(riot-v5): handle page query param & refactor router & remove old code
This commit is contained in:
parent
603b5861fa
commit
8ef411059c
10 changed files with 74 additions and 733 deletions
|
@ -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'];
|
||||
},
|
||||
};
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue