2021-05-26 16:43:46 +00:00
|
|
|
import { env } from 'consul-ui/env';
|
|
|
|
const OPTIONAL = {};
|
ui: Adds Partitions to the HTTP layer (#10447)
This PR mainly adds partition to our HTTP adapter. Additionally and perhaps most importantly, we've also taken the opportunity to move our 'conditional namespaces' deeper into the app.
The reason for doing this was, we like that namespaces should be thought of as required instead of conditional, 'special' things and would like the same thinking to be applied to partitions.
Now, instead of using code throughout the app throughout the adapters to add/remove namespaces or partitions depending on whether they are enabled or not. As a UI engineer you just pretend that namespaces and partitions are always enabled, and we remove them for you deeper in the app, out of the way of you forgetting to treat these properties as a special case.
Notes:
Added a PartitionAbility while we were there (not used as yet)
Started to remove the CONSTANT variables we had just for property names. I prefer that our adapters are as readable and straightforwards as possible, it just looks like HTTP.
We'll probably remove our formatDatacenter method we use also at some point, it was mainly too make it look the same as our previous formatNspace, but now we don't have that, it instead now looks different!
We enable parsing of partition in the UIs URL, but this is feature flagged so still does nothing just yet.
All of the test changes were related to the fact that we were treating client.url as a function rather than a method, and now that we reference this in client.url (etc) it needs binding to client.
2021-09-15 17:09:55 +00:00
|
|
|
if (env('CONSUL_PARTITIONS_ENABLED')) {
|
|
|
|
OPTIONAL.partition = /^-([a-zA-Z0-9]([a-zA-Z0-9-]{0,62}[a-zA-Z0-9])?)$/;
|
|
|
|
}
|
|
|
|
|
2021-05-26 16:43:46 +00:00
|
|
|
if (env('CONSUL_NSPACES_ENABLED')) {
|
|
|
|
OPTIONAL.nspace = /^~([a-zA-Z0-9]([a-zA-Z0-9-]{0,62}[a-zA-Z0-9])?)$/;
|
|
|
|
}
|
|
|
|
|
|
|
|
const trailingSlashRe = /\/$/;
|
2021-07-14 17:49:01 +00:00
|
|
|
|
|
|
|
// see below re: ember double slashes
|
|
|
|
// const moreThan1SlashRe = /\/{2,}/g;
|
2021-05-26 16:43:46 +00:00
|
|
|
|
|
|
|
const _uuid = function() {
|
|
|
|
return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, c => {
|
|
|
|
const r = (Math.random() * 16) | 0;
|
|
|
|
return (c === 'x' ? r : (r & 3) | 8).toString(16);
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
// let popstateFired = false;
|
|
|
|
/**
|
|
|
|
* Register a callback to be invoked whenever the browser history changes,
|
|
|
|
* including using forward and back buttons.
|
|
|
|
*/
|
|
|
|
const route = function(e) {
|
|
|
|
const path = e.state.path;
|
|
|
|
const url = this.getURLForTransition(path);
|
|
|
|
// Ignore initial page load popstate event in Chrome
|
|
|
|
// if (!popstateFired) {
|
|
|
|
// popstateFired = true;
|
|
|
|
// if (url === this._previousURL) {
|
|
|
|
// return;
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
if (url === this._previousURL) {
|
|
|
|
if (path === this._previousPath) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
this._previousPath = e.state.path;
|
|
|
|
// async
|
|
|
|
this.container.lookup('route:application').refresh();
|
|
|
|
}
|
|
|
|
if (typeof this.callback === 'function') {
|
|
|
|
// TODO: Can we use `settled` or similar to make this `route` method async?
|
|
|
|
// not async
|
|
|
|
this.callback(url);
|
|
|
|
}
|
|
|
|
// used for webkit workaround
|
|
|
|
this._previousURL = url;
|
|
|
|
this._previousPath = e.state.path;
|
|
|
|
};
|
|
|
|
export default class FSMWithOptionalLocation {
|
|
|
|
// extend FSMLocation
|
|
|
|
implementation = 'fsm-with-optional';
|
|
|
|
|
|
|
|
baseURL = '';
|
|
|
|
/**
|
|
|
|
* Set from router:main._setupLocation (-internals/routing/lib/system/router)
|
|
|
|
* Will be pre-pended to path upon state change
|
|
|
|
*/
|
|
|
|
rootURL = '/';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Path is the 'application path' i.e. the path/URL with no root/base URLs
|
|
|
|
* but potentially with optional parameters (these are remove when getURL is called)
|
|
|
|
*/
|
|
|
|
path = '/';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sneaky undocumented property used in ember's main router used to skip any
|
|
|
|
* setup of location from the main router. We currently don't need this but
|
|
|
|
* document it here incase we ever do.
|
|
|
|
*/
|
|
|
|
cancelRouterSetup = false;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Used to store our 'optional' segments should we have any
|
|
|
|
*/
|
|
|
|
optional = {};
|
|
|
|
|
|
|
|
static create() {
|
|
|
|
return new this(...arguments);
|
|
|
|
}
|
|
|
|
|
|
|
|
constructor(owner, doc, env) {
|
|
|
|
this.container = Object.entries(owner)[0][1];
|
|
|
|
|
|
|
|
// add the route/state change handler
|
|
|
|
this.route = route.bind(this);
|
|
|
|
|
|
|
|
this.doc = typeof doc === 'undefined' ? this.container.lookup('service:-document') : doc;
|
|
|
|
this.env = typeof env === 'undefined' ? this.container.lookup('service:env') : env;
|
|
|
|
|
|
|
|
const base = this.doc.querySelector('base[href]');
|
|
|
|
if (base !== null) {
|
|
|
|
this.baseURL = base.getAttribute('href');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @internal
|
|
|
|
* Called from router:main._setupLocation (-internals/routing/lib/system/router)
|
|
|
|
* Used to set state on first call to setURL
|
|
|
|
*/
|
|
|
|
initState() {
|
|
|
|
this.location = this.location || this.doc.defaultView.location;
|
|
|
|
this.machine = this.machine || this.doc.defaultView.history;
|
|
|
|
this.doc.defaultView.addEventListener('popstate', this.route);
|
|
|
|
|
|
|
|
const state = this.machine.state;
|
|
|
|
const url = this.getURL();
|
|
|
|
const href = this.formatURL(url);
|
|
|
|
|
|
|
|
if (state && state.path === href) {
|
|
|
|
// preserve existing state
|
|
|
|
// used for webkit workaround, since there will be no initial popstate event
|
|
|
|
this._previousPath = href;
|
|
|
|
this._previousURL = url;
|
|
|
|
} else {
|
|
|
|
this.dispatch('replace', href);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
getURLFrom(url) {
|
2021-07-14 17:49:01 +00:00
|
|
|
// remove trailing slashes if they exist
|
2021-05-26 16:43:46 +00:00
|
|
|
url = url || this.location.pathname;
|
|
|
|
this.rootURL = this.rootURL.replace(trailingSlashRe, '');
|
|
|
|
this.baseURL = this.baseURL.replace(trailingSlashRe, '');
|
|
|
|
// remove baseURL and rootURL from start of path
|
|
|
|
return url
|
|
|
|
.replace(new RegExp(`^${this.baseURL}(?=/|$)`), '')
|
2021-07-14 17:49:01 +00:00
|
|
|
.replace(new RegExp(`^${this.rootURL}(?=/|$)`), '');
|
|
|
|
// ember default locations remove double slashes here e.g. '//'
|
|
|
|
// .replace(moreThan1SlashRe, '/'); // remove extra slashes
|
2021-05-26 16:43:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
getURLForTransition(url) {
|
|
|
|
this.optional = {};
|
|
|
|
url = this.getURLFrom(url)
|
|
|
|
.split('/')
|
|
|
|
.filter((item, i) => {
|
|
|
|
if (i < 3) {
|
|
|
|
let found = false;
|
|
|
|
Object.entries(OPTIONAL).reduce((prev, [key, re]) => {
|
|
|
|
const res = re.exec(item);
|
|
|
|
if (res !== null) {
|
|
|
|
prev[key] = {
|
|
|
|
value: item,
|
|
|
|
match: res[1],
|
|
|
|
};
|
|
|
|
found = true;
|
|
|
|
}
|
|
|
|
return prev;
|
|
|
|
}, this.optional);
|
|
|
|
return !found;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
})
|
|
|
|
.join('/');
|
|
|
|
return url;
|
|
|
|
}
|
|
|
|
|
|
|
|
optionalParams() {
|
|
|
|
let optional = this.optional || {};
|
2021-09-15 18:50:11 +00:00
|
|
|
return ['partition', 'nspace'].reduce((prev, item) => {
|
2021-05-26 16:43:46 +00:00
|
|
|
let value = '';
|
|
|
|
if (typeof optional[item] !== 'undefined') {
|
|
|
|
value = optional[item].match;
|
|
|
|
}
|
|
|
|
prev[item] = value;
|
|
|
|
return prev;
|
|
|
|
}, {});
|
|
|
|
}
|
|
|
|
|
|
|
|
// public entrypoints for app hrefs/URLs
|
|
|
|
|
|
|
|
// visit and transitionTo can't be async/await as they return promise-like
|
|
|
|
// non-promises that get re-wrapped by the addition of async/await
|
|
|
|
visit() {
|
|
|
|
return this.transitionTo(...arguments);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Turns a routeName into a full URL string for anchor hrefs etc.
|
|
|
|
*/
|
|
|
|
hrefTo(routeName, params, hash) {
|
|
|
|
if (typeof hash.dc !== 'undefined') {
|
|
|
|
delete hash.dc;
|
|
|
|
}
|
|
|
|
if (typeof hash.nspace !== 'undefined') {
|
|
|
|
hash.nspace = `~${hash.nspace}`;
|
|
|
|
}
|
ui: Adds Partitions to the HTTP layer (#10447)
This PR mainly adds partition to our HTTP adapter. Additionally and perhaps most importantly, we've also taken the opportunity to move our 'conditional namespaces' deeper into the app.
The reason for doing this was, we like that namespaces should be thought of as required instead of conditional, 'special' things and would like the same thinking to be applied to partitions.
Now, instead of using code throughout the app throughout the adapters to add/remove namespaces or partitions depending on whether they are enabled or not. As a UI engineer you just pretend that namespaces and partitions are always enabled, and we remove them for you deeper in the app, out of the way of you forgetting to treat these properties as a special case.
Notes:
Added a PartitionAbility while we were there (not used as yet)
Started to remove the CONSTANT variables we had just for property names. I prefer that our adapters are as readable and straightforwards as possible, it just looks like HTTP.
We'll probably remove our formatDatacenter method we use also at some point, it was mainly too make it look the same as our previous formatNspace, but now we don't have that, it instead now looks different!
We enable parsing of partition in the UIs URL, but this is feature flagged so still does nothing just yet.
All of the test changes were related to the fact that we were treating client.url as a function rather than a method, and now that we reference this in client.url (etc) it needs binding to client.
2021-09-15 17:09:55 +00:00
|
|
|
if (typeof hash.partition !== 'undefined') {
|
|
|
|
hash.partition = `-${hash.partition}`;
|
|
|
|
}
|
2021-05-26 16:43:46 +00:00
|
|
|
if (typeof this.router === 'undefined') {
|
|
|
|
this.router = this.container.lookup('router:main');
|
|
|
|
}
|
|
|
|
let withOptional = true;
|
|
|
|
switch (true) {
|
|
|
|
case routeName === 'settings':
|
|
|
|
case routeName.startsWith('docs.'):
|
|
|
|
withOptional = false;
|
ui: Adds initial CRUD for partitions (#11188)
* Add `is` and `test` helpers in a similar vein to `can`
Adds 2 new helpers in a similar vein to ember-cans can:
- `is` allows you to use vocab/phrases such as (is "something model") which calls isSomething() on the models ability.
- `test` allows you to use vocab/phrases such as (test "is something model") or (test "can something model")which calls isSomething() / canSomething() on the models ability. Mostly using the is helper and the can helper. It's basically the is/can helper combined.
* Adds TextInput component + related modifiers/helpers/machines/services (#11189)
Adds a few new components/modifiers/helpers to aid building forms.
- state-chart helper, used in lieu of a more generic approach for requiring our statecharts.
- A few modifications to our existing disabled modifier.
- A new 'validation' modifier, a super small form validation approach built to make use of state charts (optionally). Eventually we should be able to replace our current validation approach (ember-changeset-validations + extra deps) with this.
- A new TextInput component, which is the first of our new components specifically to make it easy to build forms with validations. This is still a WIP, I left some comments in pointing out where this one would be progressed, but as we don't need the planned functionality yet, I left it where it was. All of this will be fleshed out more at a later date.
Documentation is included for all of ^
* ui: Adds initial CRUD for partitions (#11190)
Adds basic CRUD support for partitions. Engineering-wise probably the biggest takeaway here is that we needed to write very little javascript code to add this entire feature, and the little javascript we did need to write was very straightforwards. Everything is pretty much just HTML. Another note to make is that both ember-changeset and ember-data (model layer things) are now completely abstracted away from the view layer of the application.
New components:
- Consul::Partition::Form
- Consul::Partition::List
- Consul::Partition::Notifications
- Consul::Partition::SearchBar
- Consul::Partition::Selector
See additional documentation here for more details
New Route templates:
- index.hbs partition listing/searching/filtering
- edit.hbs partition editing and creation
Additionally:
There is some additional debug work here for better observability and to prevent any errors regarding our href-to usage when a dc is not available in our documentation site.
Our softDelete functionality has been DRYed out a little to be used across two repos.
isLinkable was removed from our ListCollection component for lists like upstream and service listing, and instead use our new is helper from within the ListCollection, meaning we've added a few more lighterweight templateOnly components.
* ui: Exclude all debug-like files from the build (#11211)
This PR adds **/*-debug.* to our test/prod excluded files (realised I needed to add test-support.js also so added that here as its more or less the same thing). Conditionally juggling ES6 static imports (specifically debug ones) for this was also getting a little hairy, so I moved it all to use the same approach as our conditional routes. All in all it brings the vendor build back down to ~430kb gzipped.
2021-10-08 15:29:30 +00:00
|
|
|
break;
|
|
|
|
}
|
2021-11-24 14:53:12 +00:00
|
|
|
if (this.router.currentRouteName.startsWith('docs.')) {
|
2021-11-17 15:59:26 +00:00
|
|
|
// If we are in docs, then add a default dc as there won't be one in the
|
|
|
|
// URL
|
|
|
|
params.unshift(env('CONSUL_DATACENTER_PRIMARY'));
|
2021-11-24 14:53:12 +00:00
|
|
|
if (routeName.startsWith('dc')) {
|
2021-11-17 15:59:26 +00:00
|
|
|
// if its an app URL replace it with debugging instead of linking
|
|
|
|
return `console://${routeName} <= ${JSON.stringify(params)}`;
|
ui: Adds initial CRUD for partitions (#11188)
* Add `is` and `test` helpers in a similar vein to `can`
Adds 2 new helpers in a similar vein to ember-cans can:
- `is` allows you to use vocab/phrases such as (is "something model") which calls isSomething() on the models ability.
- `test` allows you to use vocab/phrases such as (test "is something model") or (test "can something model")which calls isSomething() / canSomething() on the models ability. Mostly using the is helper and the can helper. It's basically the is/can helper combined.
* Adds TextInput component + related modifiers/helpers/machines/services (#11189)
Adds a few new components/modifiers/helpers to aid building forms.
- state-chart helper, used in lieu of a more generic approach for requiring our statecharts.
- A few modifications to our existing disabled modifier.
- A new 'validation' modifier, a super small form validation approach built to make use of state charts (optionally). Eventually we should be able to replace our current validation approach (ember-changeset-validations + extra deps) with this.
- A new TextInput component, which is the first of our new components specifically to make it easy to build forms with validations. This is still a WIP, I left some comments in pointing out where this one would be progressed, but as we don't need the planned functionality yet, I left it where it was. All of this will be fleshed out more at a later date.
Documentation is included for all of ^
* ui: Adds initial CRUD for partitions (#11190)
Adds basic CRUD support for partitions. Engineering-wise probably the biggest takeaway here is that we needed to write very little javascript code to add this entire feature, and the little javascript we did need to write was very straightforwards. Everything is pretty much just HTML. Another note to make is that both ember-changeset and ember-data (model layer things) are now completely abstracted away from the view layer of the application.
New components:
- Consul::Partition::Form
- Consul::Partition::List
- Consul::Partition::Notifications
- Consul::Partition::SearchBar
- Consul::Partition::Selector
See additional documentation here for more details
New Route templates:
- index.hbs partition listing/searching/filtering
- edit.hbs partition editing and creation
Additionally:
There is some additional debug work here for better observability and to prevent any errors regarding our href-to usage when a dc is not available in our documentation site.
Our softDelete functionality has been DRYed out a little to be used across two repos.
isLinkable was removed from our ListCollection component for lists like upstream and service listing, and instead use our new is helper from within the ListCollection, meaning we've added a few more lighterweight templateOnly components.
* ui: Exclude all debug-like files from the build (#11211)
This PR adds **/*-debug.* to our test/prod excluded files (realised I needed to add test-support.js also so added that here as its more or less the same thing). Conditionally juggling ES6 static imports (specifically debug ones) for this was also getting a little hairy, so I moved it all to use the same approach as our conditional routes. All in all it brings the vendor build back down to ~430kb gzipped.
2021-10-08 15:29:30 +00:00
|
|
|
}
|
2021-05-26 16:43:46 +00:00
|
|
|
}
|
2021-11-17 15:59:26 +00:00
|
|
|
const router = this.router._routerMicrolib;
|
2021-11-24 14:53:12 +00:00
|
|
|
let url;
|
|
|
|
try {
|
|
|
|
url = router.generate(routeName, ...params, {
|
|
|
|
queryParams: {},
|
|
|
|
});
|
|
|
|
} catch (e) {
|
|
|
|
// if the previous generation throws due to params not being available
|
|
|
|
// its probably due to the view wanting to re-render even though we are
|
|
|
|
// leaving the view and the router has already moved the state to old
|
|
|
|
// state so try again with the old state to avoid errors
|
|
|
|
params = Object.values(router.oldState.params).reduce((prev, item) => {
|
|
|
|
return prev.concat(Object.keys(item).length > 0 ? item : []);
|
|
|
|
}, []);
|
|
|
|
url = router.generate(routeName, ...params);
|
|
|
|
}
|
2021-05-26 16:43:46 +00:00
|
|
|
return this.formatURL(url, hash, withOptional);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Takes a full browser URL including rootURL and optional (a full href) and
|
|
|
|
* performs an ember transition/refresh and browser location update using that
|
|
|
|
*/
|
|
|
|
transitionTo(url) {
|
2021-11-24 14:53:12 +00:00
|
|
|
if (this.router.currentRouteName.startsWith('docs') && url.startsWith('console://')) {
|
ui: Adds initial CRUD for partitions (#11188)
* Add `is` and `test` helpers in a similar vein to `can`
Adds 2 new helpers in a similar vein to ember-cans can:
- `is` allows you to use vocab/phrases such as (is "something model") which calls isSomething() on the models ability.
- `test` allows you to use vocab/phrases such as (test "is something model") or (test "can something model")which calls isSomething() / canSomething() on the models ability. Mostly using the is helper and the can helper. It's basically the is/can helper combined.
* Adds TextInput component + related modifiers/helpers/machines/services (#11189)
Adds a few new components/modifiers/helpers to aid building forms.
- state-chart helper, used in lieu of a more generic approach for requiring our statecharts.
- A few modifications to our existing disabled modifier.
- A new 'validation' modifier, a super small form validation approach built to make use of state charts (optionally). Eventually we should be able to replace our current validation approach (ember-changeset-validations + extra deps) with this.
- A new TextInput component, which is the first of our new components specifically to make it easy to build forms with validations. This is still a WIP, I left some comments in pointing out where this one would be progressed, but as we don't need the planned functionality yet, I left it where it was. All of this will be fleshed out more at a later date.
Documentation is included for all of ^
* ui: Adds initial CRUD for partitions (#11190)
Adds basic CRUD support for partitions. Engineering-wise probably the biggest takeaway here is that we needed to write very little javascript code to add this entire feature, and the little javascript we did need to write was very straightforwards. Everything is pretty much just HTML. Another note to make is that both ember-changeset and ember-data (model layer things) are now completely abstracted away from the view layer of the application.
New components:
- Consul::Partition::Form
- Consul::Partition::List
- Consul::Partition::Notifications
- Consul::Partition::SearchBar
- Consul::Partition::Selector
See additional documentation here for more details
New Route templates:
- index.hbs partition listing/searching/filtering
- edit.hbs partition editing and creation
Additionally:
There is some additional debug work here for better observability and to prevent any errors regarding our href-to usage when a dc is not available in our documentation site.
Our softDelete functionality has been DRYed out a little to be used across two repos.
isLinkable was removed from our ListCollection component for lists like upstream and service listing, and instead use our new is helper from within the ListCollection, meaning we've added a few more lighterweight templateOnly components.
* ui: Exclude all debug-like files from the build (#11211)
This PR adds **/*-debug.* to our test/prod excluded files (realised I needed to add test-support.js also so added that here as its more or less the same thing). Conditionally juggling ES6 static imports (specifically debug ones) for this was also getting a little hairy, so I moved it all to use the same approach as our conditional routes. All in all it brings the vendor build back down to ~430kb gzipped.
2021-10-08 15:29:30 +00:00
|
|
|
console.log(`location.transitionTo: ${url.substr(10)}`);
|
|
|
|
return true;
|
|
|
|
}
|
2021-11-17 15:59:26 +00:00
|
|
|
const previousOptional = Object.entries(this.optionalParams());
|
2021-05-26 16:43:46 +00:00
|
|
|
const transitionURL = this.getURLForTransition(url);
|
|
|
|
if (this._previousURL === transitionURL) {
|
2021-11-17 15:59:26 +00:00
|
|
|
// probably an optional parameter change as the Ember URLs are the same
|
|
|
|
// whereas the entire URL is different
|
2021-05-26 16:43:46 +00:00
|
|
|
this.dispatch('push', url);
|
|
|
|
return Promise.resolve();
|
|
|
|
// this.setURL(url);
|
|
|
|
} else {
|
2021-11-17 15:59:26 +00:00
|
|
|
const currentOptional = this.optionalParams();
|
2021-11-24 14:53:12 +00:00
|
|
|
if (previousOptional.some(([key, value]) => currentOptional[key] !== value)) {
|
2021-11-17 15:59:26 +00:00
|
|
|
// an optional parameter change and a normal param change as the Ember
|
|
|
|
// URLs are different and we know the optional params changed
|
|
|
|
// TODO: Consider changing the above previousURL === transitionURL to
|
|
|
|
// use the same 'check the optionalParams' approach
|
|
|
|
this.dispatch('push', url);
|
|
|
|
}
|
2021-05-26 16:43:46 +00:00
|
|
|
// use ember to transition, which will eventually come around to use location.setURL
|
|
|
|
return this.container.lookup('router:main').transitionTo(transitionURL);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
|
|
|
|
// Ember location interface
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the current `location.pathname` without `rootURL` or `baseURL`
|
|
|
|
*/
|
|
|
|
getURL() {
|
|
|
|
const search = this.location.search || '';
|
|
|
|
let hash = '';
|
|
|
|
if (typeof this.location.hash !== 'undefined') {
|
|
|
|
hash = this.location.hash.substr(0);
|
|
|
|
}
|
|
|
|
const url = this.getURLForTransition(this.location.pathname);
|
|
|
|
return `${url}${search}${hash}`;
|
|
|
|
}
|
|
|
|
|
|
|
|
formatURL(url, optional, withOptional = true) {
|
|
|
|
if (url !== '') {
|
|
|
|
// remove trailing slashes if they exists
|
|
|
|
this.rootURL = this.rootURL.replace(trailingSlashRe, '');
|
|
|
|
this.baseURL = this.baseURL.replace(trailingSlashRe, '');
|
|
|
|
} else if (this.baseURL[0] === '/' && this.rootURL[0] === '/') {
|
|
|
|
// if baseURL and rootURL both start with a slash
|
|
|
|
// ... remove trailing slash from baseURL if it exists
|
|
|
|
this.baseURL = this.baseURL.replace(trailingSlashRe, '');
|
|
|
|
}
|
|
|
|
|
|
|
|
if (withOptional) {
|
|
|
|
const temp = url.split('/');
|
|
|
|
if (Object.keys(optional || {}).length === 0) {
|
|
|
|
optional = undefined;
|
|
|
|
}
|
|
|
|
optional = Object.values(optional || this.optional || {});
|
2021-09-15 18:50:11 +00:00
|
|
|
optional = optional.filter(item => Boolean(item)).map(item => item.value || item, []);
|
2021-05-26 16:43:46 +00:00
|
|
|
temp.splice(...[1, 0].concat(optional));
|
|
|
|
url = temp.join('/');
|
|
|
|
}
|
|
|
|
|
|
|
|
return `${this.baseURL}${this.rootURL}${url}`;
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
* Change URL takes an ember application URL
|
|
|
|
*/
|
|
|
|
changeURL(type, path) {
|
|
|
|
this.path = path;
|
|
|
|
const state = this.machine.state;
|
|
|
|
path = this.formatURL(path);
|
|
|
|
|
|
|
|
if (!state || state.path !== path) {
|
|
|
|
this.dispatch(type, path);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
setURL(path) {
|
|
|
|
// this.optional = {};
|
|
|
|
this.changeURL('push', path);
|
|
|
|
}
|
|
|
|
|
|
|
|
replaceURL(path) {
|
|
|
|
this.changeURL('replace', path);
|
|
|
|
}
|
|
|
|
|
|
|
|
onUpdateURL(callback) {
|
|
|
|
this.callback = callback;
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Dispatch takes a full actual browser URL with all the rootURL and optional
|
|
|
|
* params if they exist
|
|
|
|
*/
|
|
|
|
dispatch(event, path) {
|
|
|
|
const state = {
|
|
|
|
path: path,
|
|
|
|
uuid: _uuid(),
|
|
|
|
};
|
|
|
|
this.machine[`${event}State`](state, null, path);
|
|
|
|
// popstate listeners only run from a browser action not when a state change
|
|
|
|
// is called directly, so manually call the popstate listener.
|
|
|
|
// https://developer.mozilla.org/en-US/docs/Web/API/Window/popstate_event#the_history_stack
|
|
|
|
this.route({ state: state });
|
|
|
|
}
|
|
|
|
|
|
|
|
willDestroy() {
|
|
|
|
this.doc.defaultView.removeEventListener('popstate', this.route);
|
|
|
|
}
|
|
|
|
}
|