debian-mirror-gitlab/app/assets/javascripts/vue_shared/components/url_sync.vue

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

67 lines
1.9 KiB
Vue
Raw Normal View History

2020-06-23 00:09:42 +05:30
<script>
2023-04-23 21:23:45 +05:30
import { historyPushState, historyReplaceState } from '~/lib/utils/common_utils';
2022-11-25 23:54:43 +05:30
import { mergeUrlParams, setUrlParams } from '~/lib/utils/url_utility';
2023-04-23 21:23:45 +05:30
export const HISTORY_PUSH_UPDATE_METHOD = 'push';
export const HISTORY_REPLACE_UPDATE_METHOD = 'replace';
2022-11-25 23:54:43 +05:30
export const URL_SET_PARAMS_STRATEGY = 'set';
export const URL_MERGE_PARAMS_STRATEGY = 'merge';
2020-06-23 00:09:42 +05:30
2021-04-29 21:17:54 +05:30
/**
* Renderless component to update the query string,
* the update is done by updating the query property or
* by using updateQuery method in the scoped slot.
* note: do not use both prop and updateQuery method.
*/
2020-06-23 00:09:42 +05:30
export default {
props: {
query: {
type: Object,
2021-04-29 21:17:54 +05:30
required: false,
default: null,
2020-06-23 00:09:42 +05:30
},
2022-11-25 23:54:43 +05:30
urlParamsUpdateStrategy: {
type: String,
required: false,
default: URL_MERGE_PARAMS_STRATEGY,
validator: (value) => [URL_MERGE_PARAMS_STRATEGY, URL_SET_PARAMS_STRATEGY].includes(value),
},
2023-04-23 21:23:45 +05:30
historyUpdateMethod: {
type: String,
required: false,
default: HISTORY_PUSH_UPDATE_METHOD,
validator: (value) =>
[HISTORY_PUSH_UPDATE_METHOD, HISTORY_REPLACE_UPDATE_METHOD].includes(value),
},
2020-06-23 00:09:42 +05:30
},
watch: {
query: {
immediate: true,
deep: true,
handler(newQuery) {
2021-04-29 21:17:54 +05:30
if (newQuery) {
this.updateQuery(newQuery);
}
2020-06-23 00:09:42 +05:30
},
},
},
2021-04-29 21:17:54 +05:30
methods: {
updateQuery(newQuery) {
2022-11-25 23:54:43 +05:30
const url =
this.urlParamsUpdateStrategy === URL_SET_PARAMS_STRATEGY
2023-04-23 21:23:45 +05:30
? setUrlParams(this.query, window.location.href, true, true, true)
2022-11-25 23:54:43 +05:30
: mergeUrlParams(newQuery, window.location.href, { spreadArrays: true });
2023-04-23 21:23:45 +05:30
if (this.historyUpdateMethod === HISTORY_PUSH_UPDATE_METHOD) {
historyPushState(url);
} else {
historyReplaceState(url);
}
2021-04-29 21:17:54 +05:30
},
},
2020-06-23 00:09:42 +05:30
render() {
2021-04-29 21:17:54 +05:30
return this.$scopedSlots.default?.({ updateQuery: this.updateQuery });
2020-06-23 00:09:42 +05:30
},
};
</script>