[UI] Added utilities to get and set args on the URL fragment.

This commit is contained in:
Fabio Manganiello 2023-10-18 02:56:31 +02:00
parent a30c07fbb5
commit 6f01c35a0f
Signed by untrusted user: blacklight
GPG key ID: D90FBA7F76362774
3 changed files with 40 additions and 1 deletions

View file

@ -9,6 +9,7 @@ import Notification from "@/utils/Notification";
import Screen from "@/utils/Screen";
import Text from "@/utils/Text";
import Types from "@/utils/Types";
import Url from "@/utils/Url";
export default {
name: "Utils",
@ -23,6 +24,7 @@ export default {
Screen,
Text,
Types,
Url,
],
}
</script>

View file

@ -4,7 +4,7 @@ $header-height: 3.5em;
display: flex;
align-items: center;
padding: .75em .25em;
box-shadow: 0 2.5px 2px -1px $default-shadow-color;
border-bottom: 1px solid $default-shadow-color;
cursor: pointer;
&:hover {

View file

@ -0,0 +1,37 @@
<script>
export default {
name: "Url",
methods: {
parseUrlFragment() {
return window.location.href.replace(/.*#(\w+)[?;]?.*/, '$1')
},
getUrlArgs() {
return window.location.href
.replace(/.*#/, '')
.replace(/.*\?/, '')
.split(/[&;]/)
.reduce((acc, obj) => {
const tokens = obj.split('=')
acc[tokens[0]] = tokens[1]
return acc
}, {})
},
setUrlArgs(args) {
window.location.href = (
`/#${this.parseUrlFragment()}?${this.fragmentFromArgs(args)}`
)
},
fragmentFromArgs(args) {
return Object.entries(args)
.map(
([key, value]) => `${encodeURIComponent(key)}=${encodeURIComponent(value)}`
)
.join('&')
},
},
}
</script>