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(),
|
2021-10-22 13:29:55 +03:00
|
|
|
store: service(),
|
2020-04-21 09:54:29 +03:00
|
|
|
|
2021-03-23 14:59:52 +03:00
|
|
|
billingRouteRoot: '#/pro',
|
2020-04-21 09:54:29 +03:00
|
|
|
billingWindowOpen: false,
|
2020-05-22 05:44:37 +03:00
|
|
|
subscription: null,
|
|
|
|
previousRoute: null,
|
2021-09-30 15:54:54 +03:00
|
|
|
action: null,
|
2021-10-22 13:29:55 +03:00
|
|
|
ownerUser: null,
|
2020-04-22 06:35:56 +03:00
|
|
|
|
2020-05-22 05:44:37 +03:00
|
|
|
init() {
|
|
|
|
this._super(...arguments);
|
|
|
|
|
2021-03-04 23:39:33 +03:00
|
|
|
if (this.config.get('hostSettings.billing.url')) {
|
2020-05-22 05:44:37 +03:00
|
|
|
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() {
|
2021-10-28 13:09:36 +03:00
|
|
|
// initiate getting owner user in the background
|
|
|
|
this.getOwnerUser();
|
|
|
|
|
2021-03-04 23:39:33 +03:00
|
|
|
let url = this.config.get('hostSettings.billing.url');
|
2020-05-22 05:44:37 +03:00
|
|
|
|
|
|
|
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
|
|
|
},
|
|
|
|
|
2021-10-22 13:29:55 +03:00
|
|
|
async getOwnerUser() {
|
|
|
|
if (!this.get('ownerUser')) {
|
|
|
|
// Try to receive the owner user from the store
|
|
|
|
let user = this.store.peekAll('user').findBy('isOwnerOnly', true);
|
|
|
|
|
|
|
|
if (!user) {
|
|
|
|
// load it when it's not there yet
|
2021-10-28 13:09:36 +03:00
|
|
|
await this.store.findAll('user', {reload: true});
|
2021-10-22 13:29:55 +03:00
|
|
|
user = this.store.peekAll('user').findBy('isOwnerOnly', true);
|
|
|
|
}
|
|
|
|
this.set('ownerUser', user);
|
|
|
|
}
|
|
|
|
return this.get('ownerUser');
|
|
|
|
},
|
|
|
|
|
2021-09-30 15:54:54 +03:00
|
|
|
// Sends a route update to a child route in the BMA, because we can't control
|
|
|
|
// navigating to it otherwise
|
|
|
|
sendRouteUpdate() {
|
|
|
|
const action = this.get('action');
|
|
|
|
|
|
|
|
if (action) {
|
|
|
|
if (action === 'checkout') {
|
|
|
|
this.getBillingIframe().contentWindow.postMessage({
|
|
|
|
query: 'routeUpdate',
|
|
|
|
response: this.get('checkoutRoute')
|
|
|
|
}, '*');
|
|
|
|
}
|
|
|
|
|
|
|
|
this.set('action', null);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
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,
|
2021-03-23 14:59:52 +03:00
|
|
|
// for example: by entering "/pro" route in the URL or using history navigation (back and forward)
|
2021-09-10 17:22:52 +03:00
|
|
|
toggleProWindow(value) {
|
2021-09-30 15:54:54 +03:00
|
|
|
if (this.get('billingWindowOpen') && value && !this.get('action')) {
|
2021-09-10 17:22:52 +03:00
|
|
|
// don't attempt to open again
|
|
|
|
return;
|
2020-05-22 05:44:37 +03:00
|
|
|
}
|
|
|
|
|
2021-09-30 15:54:54 +03:00
|
|
|
this.sendRouteUpdate();
|
|
|
|
|
2020-05-22 05:44:37 +03:00
|
|
|
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) {
|
2021-10-22 13:29:55 +03:00
|
|
|
// initiate getting owner user in the background
|
|
|
|
this.getOwnerUser();
|
|
|
|
|
2021-09-10 17:22:52 +03:00
|
|
|
if (this.get('billingWindowOpen')) {
|
|
|
|
// don't attempt to open again
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-05-22 05:44:37 +03:00
|
|
|
this.set('previousRoute', currentRoute);
|
|
|
|
|
|
|
|
// Ensures correct "getIframeURL" calculation when syncing iframe location
|
2021-09-10 17:22:52 +03:00
|
|
|
// in toggleProWindow
|
2021-03-23 14:59:52 +03:00
|
|
|
window.location.hash = childRoute || '/pro';
|
2020-05-22 05:44:37 +03:00
|
|
|
|
2021-09-30 15:54:54 +03:00
|
|
|
this.sendRouteUpdate();
|
|
|
|
|
2021-03-23 14:59:52 +03:00
|
|
|
this.router.transitionTo(childRoute || '/pro');
|
2020-05-22 05:44:37 +03:00
|
|
|
},
|
|
|
|
|
|
|
|
getBillingIframe() {
|
|
|
|
return document.getElementById('billing-frame');
|
|
|
|
}
|
2020-04-21 09:54:29 +03:00
|
|
|
});
|