Skip to content

Commit

Permalink
feat(riot-v5): handle page query param & refactor router & remove old…
Browse files Browse the repository at this point in the history
… code
  • Loading branch information
Joxit committed Mar 29, 2021
1 parent 603b586 commit 8ef4110
Show file tree
Hide file tree
Showing 10 changed files with 74 additions and 733 deletions.
5 changes: 2 additions & 3 deletions src/components/dialogs/add-registry-url.riot
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,7 @@
</material-popup>
<script>
import {
getRegistryServers,
updateHistory
getRegistryServers
} from '../../scripts/utils';
import router from '../../scripts/router';
Expand All @@ -58,7 +57,7 @@
router.home()
this.props.onServerChange(url);
this.props.onClose()
setTimeout(() => updateHistory(url), 100);
setTimeout(() => router.updateUrlQueryParam(url), 100);
}
}
</script>
Expand Down
5 changes: 2 additions & 3 deletions src/components/dialogs/change-registry-url.riot
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,7 @@
</material-popup>
<script>
import {
getRegistryServers,
updateHistory
getRegistryServers
} from '../../scripts/utils';
import router from '../../scripts/router';
export default {
Expand All @@ -52,7 +51,7 @@
router.home()
this.props.onServerChange(url);
this.props.onClose()
setTimeout(() => updateHistory(url), 100);
setTimeout(() => router.updateUrlQueryParam(url), 100);
},
getRegistryServers
}
Expand Down
4 changes: 1 addition & 3 deletions src/components/dialogs/remove-registry-url.riot
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,8 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
</material-popup>
<script>
import {
getRegistryServers,
updateHistory
getRegistryServers
} from '../../scripts/utils';
import router from '../../scripts/router';
export default {
remove(event) {
const url = event.currentTarget.attributes.url && event.currentTarget.attributes.url.value;
Expand Down
6 changes: 4 additions & 2 deletions src/components/tag-list/tag-list.riot
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.

<tag-table if="{ state.loadend }" tags="{state.tags}" asc="{state.asc}" page="{ state.page }"
show-content-digest="{props.showContentDigest}" is-image-remove-activated="{props.isImageRemoveActivated}"
onReverseOrder="{ onReverseOrder }" registry-url="{ props.registryUrl }" pull-url="{ props.pullUrl }" on-notify="{ props.onNotify }"></tag-table>
onReverseOrder="{ onReverseOrder }" registry-url="{ props.registryUrl }" pull-url="{ props.pullUrl }"
on-notify="{ props.onNotify }"></tag-table>

<pagination pages="{ getPageLabels(state.page, getNumPages(state.tags)) }" onPageUpdate="{onPageUpdate}"></pagination>

Expand Down Expand Up @@ -68,7 +69,7 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
tags: [],
loadend: false,
asc: true,
page: 1
page: router.getPageQueryParam() || 1
}
},
onMounted(props, state) {
Expand Down Expand Up @@ -117,6 +118,7 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
this.update({
page: page
});
router.updatePageQueryParam(page);
},
onResize() {
Expand Down
61 changes: 56 additions & 5 deletions src/scripts/router.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,28 +15,79 @@
* 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');
},
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'];
},
};
124 changes: 0 additions & 124 deletions src/scripts/script.js

This file was deleted.

44 changes: 0 additions & 44 deletions src/scripts/static.js

This file was deleted.

23 changes: 9 additions & 14 deletions src/scripts/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -124,16 +124,6 @@ export function getPageLabels(page, nPages) {
return pageLabels;
}

export function updateQueryString(qs) {
var search = '';
for (var key in qs) {
if (qs[key] !== undefined) {
search += (search.length > 0 ? '&' : '?') + key + '=' + qs[key];
}
}
history.pushState(null, '', search + window.location.hash);
}

export function stripHttps(url) {
if (!url) {
return '';
Expand Down Expand Up @@ -171,10 +161,15 @@ export function getRegistryServers(i) {
}

export function encodeURI(url) {
if (!url) { return; }
if (!url) {
return;
}
return url.indexOf('&') < 0 ? window.encodeURIComponent(url) : btoa(url);
};
}

export function updateHistory(url) {
updateQueryString({ url: encodeURI(url) })
export function decodeURI(url) {
if (!url) {
return;
}
return url.startsWith('http') ? window.decodeURIComponent(url) : atob(url);
}
Loading

0 comments on commit 8ef4110

Please sign in to comment.