2020-04-21 09:54:29 +03:00
|
|
|
import Service from '@ember/service';
|
|
|
|
import {inject as service} from '@ember/service';
|
|
|
|
|
|
|
|
export default Service.extend({
|
2020-05-22 05:44:37 +03:00
|
|
|
router: service(),
|
2020-04-21 09:54:29 +03:00
|
|
|
config: service(),
|
|
|
|
ghostPaths: service(),
|
|
|
|
|
2020-05-22 05:44:37 +03:00
|
|
|
billingRouteRoot: '#/billing',
|
2020-04-21 09:54:29 +03:00
|
|
|
billingWindowOpen: false,
|
2020-05-22 05:44:37 +03:00
|
|
|
subscription: null,
|
|
|
|
previousRoute: null,
|
2020-04-22 06:35:56 +03:00
|
|
|
|
2020-05-22 05:44:37 +03:00
|
|
|
init() {
|
|
|
|
this._super(...arguments);
|
|
|
|
|
|
|
|
if (this.config.get('billingUrl')) {
|
|
|
|
window.addEventListener('message', (event) => {
|
|
|
|
if (event && event.data && event.data.route) {
|
|
|
|
this.handleRouteChangeInIframe(event.data.route);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2020-04-22 06:35:56 +03:00
|
|
|
},
|
2020-04-21 09:54:29 +03:00
|
|
|
|
2020-05-22 05:44:37 +03:00
|
|
|
handleRouteChangeInIframe(destinationRoute) {
|
|
|
|
if (this.get('billingWindowOpen')) {
|
|
|
|
let billingRoute = this.get('billingRouteRoot');
|
|
|
|
|
|
|
|
if (destinationRoute !== '/') {
|
|
|
|
billingRoute += destinationRoute;
|
|
|
|
}
|
2020-04-21 09:54:29 +03:00
|
|
|
|
2020-05-22 05:44:37 +03:00
|
|
|
if (window.location.hash !== billingRoute) {
|
|
|
|
window.history.replaceState(window.history.state, '', billingRoute);
|
|
|
|
}
|
2020-04-21 09:54:29 +03:00
|
|
|
}
|
2020-05-22 05:44:37 +03:00
|
|
|
},
|
2020-04-21 09:54:29 +03:00
|
|
|
|
2020-05-22 05:44:37 +03:00
|
|
|
getIframeURL() {
|
|
|
|
let url = this.config.get('billingUrl');
|
|
|
|
|
|
|
|
if (window.location.hash && window.location.hash.includes(this.get('billingRouteRoot'))) {
|
|
|
|
let destinationRoute = window.location.hash.replace(this.get('billingRouteRoot'), '');
|
|
|
|
|
|
|
|
if (destinationRoute) {
|
|
|
|
url += destinationRoute;
|
|
|
|
}
|
2020-04-22 06:35:56 +03:00
|
|
|
}
|
|
|
|
|
2020-04-21 09:54:29 +03:00
|
|
|
return url;
|
2020-05-22 05:44:37 +03:00
|
|
|
},
|
|
|
|
|
|
|
|
// Controls billing window modal visibility and sync of the URL visible in browser
|
|
|
|
// and the URL opened on the iframe. It is responsible to non user triggered iframe opening,
|
|
|
|
// for example: by entering "/billing" route in the URL or using history navigation (back and forward)
|
|
|
|
setBillingWindowOpen(value) {
|
|
|
|
let billingIframe = this.getBillingIframe();
|
|
|
|
|
|
|
|
if (billingIframe && value) {
|
|
|
|
billingIframe.contentWindow.location.replace(this.getIframeURL());
|
|
|
|
}
|
|
|
|
|
|
|
|
this.set('billingWindowOpen', value);
|
|
|
|
},
|
|
|
|
|
|
|
|
// Controls navigation to billing window modal which is triggered from the application UI.
|
|
|
|
// For example: pressing "View Billing" link in navigation menu. It's main side effect is
|
|
|
|
// remembering the route from which the action has been triggered - "previousRoute" so it
|
|
|
|
// could be reused when closing billing window
|
|
|
|
openBillingWindow(currentRoute, childRoute) {
|
|
|
|
this.set('previousRoute', currentRoute);
|
|
|
|
|
|
|
|
// Ensures correct "getIframeURL" calculation when syncing iframe location
|
|
|
|
// in setBillingWindowOpen
|
|
|
|
window.location.hash = childRoute || '/billing';
|
|
|
|
|
|
|
|
this.router.transitionTo(childRoute || '/billing');
|
|
|
|
},
|
|
|
|
|
|
|
|
closeBillingWindow() {
|
|
|
|
this.set('billingWindowOpen', false);
|
|
|
|
|
|
|
|
let transitionRoute = this.get('previousRoute') || '/';
|
|
|
|
this.router.transitionTo(transitionRoute);
|
|
|
|
},
|
|
|
|
|
|
|
|
getBillingIframe() {
|
|
|
|
return document.getElementById('billing-frame');
|
|
|
|
}
|
2020-04-21 09:54:29 +03:00
|
|
|
});
|