open-consul/ui-v2/app/helpers/href-to.js
John Cowen 12c4bddcac
ui: Ensure URLs for tabs change when selecting a different DC (#8397)
* ui: Add URLs to tabs to we can assert them

* Add dcs to the service show page, temporarily

There is an ongoing PR which removes the need to do this by adding a dcs
property to the navigation, hence temporary

* Add a step to assert on whether text _contains_ rather than exact match

* Test whether the URL of the instance tab changes when the user switches dcs using the main navigation menu

* Recompute href-to's on URL change, just like is-href
2020-07-29 16:09:40 +02:00

49 lines
1.6 KiB
JavaScript

/*eslint ember/no-observers: "warn"*/
// TODO: Remove ^
// This helper requires `ember-href-to` for the moment at least
// It's similar code but allows us to check on the type of route
// (dynamic or wildcard) and encode or not depending on the type
import { inject as service } from '@ember/service';
import Helper from '@ember/component/helper';
import { observer } from '@ember/object';
import { hrefTo as _hrefTo } from 'ember-href-to/helpers/href-to';
import wildcard from 'consul-ui/utils/routing/wildcard';
import { routes } from 'consul-ui/router';
const isWildcard = wildcard(routes);
export const hrefTo = function(owned, router, [targetRouteName, ...rest], namedArgs) {
if (isWildcard(targetRouteName)) {
rest = rest.map(function(item, i) {
return item
.split('/')
.map(encodeURIComponent)
.join('/');
});
}
if (namedArgs.params) {
return _hrefTo(owned, namedArgs.params);
} else {
// we don't check to see if nspaces are enabled here as routes
// with beginning with 'nspace' only exist if nspaces are enabled
// this globally converts non-nspaced href-to's to nspace aware
// href-to's only if you are within a namespace
if (router.currentRouteName.startsWith('nspace.') && targetRouteName.startsWith('dc.')) {
targetRouteName = `nspace.${targetRouteName}`;
}
return _hrefTo(owned, [targetRouteName, ...rest]);
}
};
export default Helper.extend({
router: service('router'),
compute(params, hash) {
return hrefTo(this, this.router, params, hash);
},
onURLChange: observer('router.currentURL', function() {
this.recompute();
}),
});