mirror of https://github.com/rancher/dashboard.git
48 lines
1.1 KiB
JavaScript
48 lines
1.1 KiB
JavaScript
import VueRouter from 'vue-router';
|
|
import { isEqual } from 'lodash';
|
|
|
|
VueRouter.prototype.applyQuery = function(qp, defaults = {}) {
|
|
const query = queryParamsFor(this.currentRoute.query, qp, defaults);
|
|
|
|
if ( isEqual(query, this.currentRoute.query) ) {
|
|
return;
|
|
}
|
|
|
|
this.replace({ query }).catch((err) => {
|
|
if ( err?.name === 'NavigationDuplicated' ) {
|
|
// Do nothing, this is fine...
|
|
// https://github.com/vuejs/vue-router/issues/2872
|
|
} else {
|
|
throw err;
|
|
}
|
|
});
|
|
};
|
|
|
|
export function queryParamsFor(current, qp, defaults = {}) {
|
|
const query = Object.assign({}, current || {});
|
|
|
|
for ( const key of Object.keys(qp) ) {
|
|
const val = qp[key];
|
|
|
|
if ( typeof defaults[key] === 'undefined' ) {
|
|
// There is no default
|
|
query[key] = qp[key];
|
|
} else if ( defaults[key] === false ) {
|
|
// Value-less boolean flags
|
|
if ( val ) {
|
|
query[key] = null;
|
|
} else {
|
|
delete query[key];
|
|
}
|
|
} else if ( val === defaults[key] ) {
|
|
// The value is the default
|
|
delete query[key];
|
|
} else {
|
|
// The value is not the default
|
|
query[key] = val;
|
|
}
|
|
}
|
|
|
|
return query;
|
|
}
|