-
Notifications
You must be signed in to change notification settings - Fork 4.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
ui: Support Route optional parameters/segments (#10212)
Moves our URLs with 'optional namespace segment' into a separately abstracted 'optional URL segment' feature
- Loading branch information
1 parent
56a165b
commit dd280ee
Showing
63 changed files
with
848 additions
and
450 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
```release-note:improvement | ||
ui: Add 'optional route segments' and move namespaces to use them | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,5 +6,6 @@ | |
{{/if}} | ||
|
||
{{yield (hash | ||
model=model | ||
model=this.model | ||
params=this.params | ||
)}} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,59 +1,73 @@ | ||
/*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 { observes } from '@ember-decorators/object'; | ||
import { hrefTo as _hrefTo } from 'ember-href-to/helpers/href-to'; | ||
import { inject as service } from '@ember/service'; | ||
import { action } from '@ember/object'; | ||
import { getOwner } from '@ember/application'; | ||
|
||
import transitionable from 'consul-ui/utils/routing/transitionable'; | ||
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('/'); | ||
}); | ||
|
||
export const hrefTo = function(container, params, hash = {}) { | ||
// TODO: consider getting this from @service('router')._router which is | ||
// private but we don't need getOwner, and it ensures setupRouter is called | ||
// How private is 'router:main'? If its less private maybe stick with it? | ||
const location = container.lookup('router:main').location; | ||
const router = container.lookup('service:router'); | ||
|
||
let _params = params.slice(0); | ||
let routeName = _params.shift(); | ||
let _hash = hash.params || {}; | ||
// a period means use the same routeName we are currently at and therefore | ||
// use transitionable to figure out all the missing params | ||
if (routeName === '.') { | ||
_params = transitionable(router.currentRoute, _hash, container); | ||
// _hash = {}; | ||
routeName = _params.shift(); | ||
} | ||
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 | ||
const currentRouteName = router.currentRouteName || ''; | ||
if (currentRouteName.startsWith('nspace.') && targetRouteName.startsWith('dc.')) { | ||
targetRouteName = `nspace.${targetRouteName}`; | ||
|
||
try { | ||
// if the routeName is a wildcard (*) route url encode all of the params | ||
if (isWildcard(routeName)) { | ||
_params = _params.map((item, i) => { | ||
return item | ||
.split('/') | ||
.map(encodeURIComponent) | ||
.join('/'); | ||
}); | ||
} | ||
return location.hrefTo(routeName, _params, _hash); | ||
} catch (e) { | ||
if (e.constructor === Error) { | ||
e.message = `${e.message} For "${params[0]}:${JSON.stringify(params.slice(1))}"`; | ||
} | ||
return _hrefTo(owned, [targetRouteName, ...rest]); | ||
throw e; | ||
} | ||
}; | ||
|
||
export default class HrefToHelper extends Helper { | ||
@service('router') router; | ||
|
||
init() { | ||
super.init(...arguments); | ||
this.router.on('routeWillChange', this.routeWillChange); | ||
} | ||
|
||
compute(params, hash) { | ||
let href; | ||
try { | ||
href = hrefTo(this, this.router, params, hash); | ||
} catch (e) { | ||
e.message = `${e.message} For "${params[0]}:${JSON.stringify(params.slice(1))}"`; | ||
throw e; | ||
} | ||
return href; | ||
return hrefTo(getOwner(this), params, hash); | ||
} | ||
|
||
@observes('router.currentURL') | ||
onURLChange() { | ||
@action | ||
routeWillChange(transition) { | ||
this.recompute(); | ||
} | ||
|
||
willDestroy() { | ||
this.router.off('routeWillChange', this.routeWillChange); | ||
super.willDestroy(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.