2020-07-07 07:22:42 +03:00
|
|
|
import $ from 'jquery';
|
2020-06-19 19:06:49 +03:00
|
|
|
import ModalComponent from 'ghost-admin/components/modal-base';
|
2020-07-07 07:22:42 +03:00
|
|
|
import copyTextToClipboard from 'ghost-admin/utils/copy-text-to-clipboard';
|
2020-06-19 19:06:49 +03:00
|
|
|
import {computed} from '@ember/object';
|
2021-05-12 14:33:36 +03:00
|
|
|
import {htmlSafe} from '@ember/template';
|
2020-07-07 07:22:42 +03:00
|
|
|
import {run} from '@ember/runloop';
|
2020-06-19 19:06:49 +03:00
|
|
|
import {inject as service} from '@ember/service';
|
2020-07-07 07:22:42 +03:00
|
|
|
import {task, timeout} from 'ember-concurrency';
|
2020-07-21 20:12:55 +03:00
|
|
|
const ICON_EXTENSIONS = ['gif', 'jpg', 'jpeg', 'png', 'svg'];
|
|
|
|
|
2020-06-19 19:06:49 +03:00
|
|
|
export default ModalComponent.extend({
|
2020-06-25 23:45:47 +03:00
|
|
|
config: service(),
|
2021-01-28 20:28:19 +03:00
|
|
|
membersUtils: service(),
|
|
|
|
settings: service(),
|
2021-05-04 18:43:31 +03:00
|
|
|
store: service(),
|
2021-01-28 20:28:19 +03:00
|
|
|
|
2020-06-25 23:45:47 +03:00
|
|
|
page: 'signup',
|
2020-07-07 07:22:42 +03:00
|
|
|
iconExtensions: null,
|
|
|
|
isShowModalLink: true,
|
|
|
|
customIcon: null,
|
2020-07-30 16:12:44 +03:00
|
|
|
showLinksPage: false,
|
2020-07-30 13:56:07 +03:00
|
|
|
showLeaveSettingsModal: false,
|
2020-11-23 12:38:29 +03:00
|
|
|
freeSignupRedirect: undefined,
|
|
|
|
paidSignupRedirect: undefined,
|
2021-05-04 18:43:31 +03:00
|
|
|
prices: null,
|
2021-01-28 20:28:19 +03:00
|
|
|
|
2020-06-25 23:45:47 +03:00
|
|
|
confirm() {},
|
2020-06-29 20:37:11 +03:00
|
|
|
|
2021-05-04 18:43:31 +03:00
|
|
|
filteredPrices: computed('prices', 'settings.portalPlans.[]', function () {
|
2021-05-18 15:59:47 +03:00
|
|
|
const portalPlans = this.settings.get('portalPlans');
|
2021-05-06 15:25:16 +03:00
|
|
|
const prices = this.prices || [];
|
|
|
|
return prices.filter((d) => {
|
2021-05-04 18:43:31 +03:00
|
|
|
return d.amount !== 0 && d.type === 'recurring';
|
|
|
|
}).map((price) => {
|
|
|
|
return {
|
|
|
|
...price,
|
|
|
|
checked: !!portalPlans.find(d => d === price.id)
|
|
|
|
};
|
|
|
|
});
|
|
|
|
}),
|
|
|
|
|
2021-05-06 15:25:16 +03:00
|
|
|
hasPaidPriceChecked: computed('prices', 'settings.portalPlans.[]', function () {
|
2021-05-18 15:59:47 +03:00
|
|
|
const portalPlans = this.settings.get('portalPlans');
|
2021-05-06 15:25:16 +03:00
|
|
|
const prices = this.prices || [];
|
|
|
|
return prices.filter((d) => {
|
|
|
|
return d.amount !== 0 && d.type === 'recurring';
|
|
|
|
}).some((price) => {
|
|
|
|
return !!portalPlans.find(d => d === price.id);
|
|
|
|
});
|
|
|
|
}),
|
|
|
|
|
2020-07-07 07:22:42 +03:00
|
|
|
backgroundStyle: computed('settings.accentColor', function () {
|
2021-05-18 15:59:47 +03:00
|
|
|
let color = this.settings.get('accentColor') || '#ffffff';
|
2020-07-07 07:22:42 +03:00
|
|
|
return htmlSafe(`background-color: ${color}`);
|
|
|
|
}),
|
|
|
|
|
|
|
|
showModalLinkOrAttribute: computed('isShowModalLink', function () {
|
|
|
|
if (this.isShowModalLink) {
|
2020-10-12 14:56:37 +03:00
|
|
|
return `#/portal`;
|
2020-07-07 07:22:42 +03:00
|
|
|
}
|
|
|
|
return `data-portal`;
|
|
|
|
}),
|
|
|
|
|
2021-05-18 15:59:47 +03:00
|
|
|
portalPreviewUrl: computed('page', 'membersUtils.{isFreeChecked,isMonthlyChecked,isYearlyChecked}', 'settings.{portalName,portalButton,portalButtonIcon,portalButtonSignupText,portalButtonStyle,accentColor,portalPlans.[]}', function () {
|
|
|
|
const options = this.getProperties(['page']);
|
2021-01-28 21:41:03 +03:00
|
|
|
return this.membersUtils.getPortalPreviewUrl(options);
|
2020-06-25 23:45:47 +03:00
|
|
|
}),
|
|
|
|
|
2020-07-07 07:22:42 +03:00
|
|
|
showIconSetting: computed('selectedButtonStyle', function () {
|
|
|
|
const selectedButtonStyle = this.get('selectedButtonStyle.name') || '';
|
|
|
|
return selectedButtonStyle.includes('icon');
|
|
|
|
}),
|
|
|
|
|
2020-07-16 16:21:14 +03:00
|
|
|
showButtonTextSetting: computed('selectedButtonStyle', function () {
|
|
|
|
const selectedButtonStyle = this.get('selectedButtonStyle.name') || '';
|
|
|
|
return selectedButtonStyle.includes('text');
|
|
|
|
}),
|
|
|
|
|
2020-07-07 07:22:42 +03:00
|
|
|
selectedButtonStyle: computed('settings.portalButtonStyle', function () {
|
|
|
|
return this.buttonStyleOptions.find((buttonStyle) => {
|
|
|
|
return (buttonStyle.name === this.settings.get('portalButtonStyle'));
|
|
|
|
});
|
|
|
|
}),
|
|
|
|
|
2020-06-19 19:06:49 +03:00
|
|
|
init() {
|
|
|
|
this._super(...arguments);
|
2020-08-11 09:34:42 +03:00
|
|
|
this.set('hidePreviewFrame', true);
|
2020-07-07 07:22:42 +03:00
|
|
|
this.buttonStyleOptions = [
|
|
|
|
{name: 'icon-and-text', label: 'Icon and text'},
|
|
|
|
{name: 'icon-only', label: 'Icon only'},
|
|
|
|
{name: 'text-only', label: 'Text only'}
|
|
|
|
];
|
|
|
|
this.iconExtensions = ICON_EXTENSIONS;
|
2021-05-18 15:59:47 +03:00
|
|
|
|
2020-07-08 18:23:29 +03:00
|
|
|
const portalButtonIcon = this.settings.get('portalButtonIcon') || '';
|
2021-05-18 15:59:47 +03:00
|
|
|
if (portalButtonIcon && !this.membersUtils.defaultIconKeys.includes(portalButtonIcon)) {
|
2020-07-21 20:12:55 +03:00
|
|
|
this.set('customIcon', this.settings.get('portalButtonIcon'));
|
2020-07-07 07:22:42 +03:00
|
|
|
}
|
2021-05-18 15:59:47 +03:00
|
|
|
|
2021-05-04 18:43:31 +03:00
|
|
|
this.getAvailablePrices.perform();
|
2020-10-12 14:56:37 +03:00
|
|
|
this.siteUrl = this.config.get('blogUrl');
|
2020-06-19 19:06:49 +03:00
|
|
|
},
|
|
|
|
|
2020-08-11 09:34:42 +03:00
|
|
|
didInsertElement() {
|
|
|
|
this._super(...arguments);
|
2021-05-18 15:59:47 +03:00
|
|
|
this.settings.get('errors').clear();
|
2020-08-11 09:34:42 +03:00
|
|
|
run.later(this, function () {
|
2021-05-17 14:40:08 +03:00
|
|
|
if (!this.isDestroyed && !this.isDestroying) {
|
|
|
|
this.set('hidePreviewFrame', false);
|
|
|
|
}
|
2020-08-11 09:34:42 +03:00
|
|
|
}, 1200);
|
|
|
|
},
|
|
|
|
|
2020-06-19 19:06:49 +03:00
|
|
|
actions: {
|
|
|
|
toggleFreePlan(isChecked) {
|
|
|
|
this.updateAllowedPlan('free', isChecked);
|
|
|
|
},
|
|
|
|
toggleMonthlyPlan(isChecked) {
|
|
|
|
this.updateAllowedPlan('monthly', isChecked);
|
|
|
|
},
|
|
|
|
toggleYearlyPlan(isChecked) {
|
|
|
|
this.updateAllowedPlan('yearly', isChecked);
|
|
|
|
},
|
2021-05-04 18:43:31 +03:00
|
|
|
togglePlan(priceId, event) {
|
|
|
|
this.updateAllowedPlan(priceId, event.target.checked);
|
|
|
|
},
|
2020-07-07 07:22:42 +03:00
|
|
|
togglePortalButton(showButton) {
|
|
|
|
this.settings.set('portalButton', showButton);
|
2020-06-19 19:06:49 +03:00
|
|
|
},
|
|
|
|
|
|
|
|
togglePortalName(showSignupName) {
|
|
|
|
this.settings.set('portalName', showSignupName);
|
|
|
|
},
|
|
|
|
|
2020-11-20 12:53:08 +03:00
|
|
|
setPaidSignupRedirect(url) {
|
2020-11-23 12:38:29 +03:00
|
|
|
this.set('paidSignupRedirect', url);
|
2020-11-19 12:59:51 +03:00
|
|
|
},
|
|
|
|
|
2020-11-20 12:53:08 +03:00
|
|
|
setFreeSignupRedirect(url) {
|
2020-11-23 12:38:29 +03:00
|
|
|
this.set('freeSignupRedirect', url);
|
2020-11-19 12:59:51 +03:00
|
|
|
},
|
|
|
|
|
2020-06-19 19:06:49 +03:00
|
|
|
confirm() {
|
|
|
|
return this.saveTask.perform();
|
|
|
|
},
|
|
|
|
|
|
|
|
isPlanSelected(plan) {
|
|
|
|
const allowedPlans = this.settings.get('portalPlans');
|
|
|
|
return allowedPlans.includes(plan);
|
2020-06-25 23:45:47 +03:00
|
|
|
},
|
|
|
|
|
|
|
|
switchPreviewPage(page) {
|
2020-07-30 16:12:44 +03:00
|
|
|
if (page === 'links') {
|
|
|
|
this.set('showLinksPage', true);
|
|
|
|
this.set('page', '');
|
|
|
|
} else {
|
|
|
|
this.set('showLinksPage', false);
|
|
|
|
this.set('page', page);
|
|
|
|
}
|
2020-07-07 07:22:42 +03:00
|
|
|
},
|
|
|
|
|
2020-10-16 16:34:22 +03:00
|
|
|
switchToSignupPage() {
|
|
|
|
if (this.showLinksPage) {
|
|
|
|
this.set('showLinksPage', false);
|
|
|
|
this.set('page', 'signup');
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2020-07-07 07:22:42 +03:00
|
|
|
setButtonStyle(buttonStyle) {
|
2020-07-30 13:56:07 +03:00
|
|
|
this.settings.set('portalButtonStyle', buttonStyle.name);
|
2020-07-07 07:22:42 +03:00
|
|
|
},
|
|
|
|
setSignupButtonText(event) {
|
2020-07-30 13:56:07 +03:00
|
|
|
this.settings.set('portalButtonSignupText', event.target.value);
|
2020-07-07 07:22:42 +03:00
|
|
|
},
|
|
|
|
/**
|
|
|
|
* Fired after an image upload completes
|
|
|
|
* @param {string} property - Property name to be set on `this.settings`
|
|
|
|
* @param {UploadResult[]} results - Array of UploadResult objects
|
|
|
|
* @return {string} The URL that was set on `this.settings.property`
|
|
|
|
*/
|
|
|
|
imageUploaded(property, results) {
|
|
|
|
if (results[0]) {
|
|
|
|
this.set('customIcon', results[0].url);
|
2020-07-30 13:56:07 +03:00
|
|
|
this.settings.set('portalButtonIcon', results[0].url);
|
2020-07-07 07:22:42 +03:00
|
|
|
}
|
|
|
|
},
|
|
|
|
/**
|
|
|
|
* Opens a file selection dialog - Triggered by "Upload Image" buttons,
|
|
|
|
* searches for the hidden file input within the .gh-setting element
|
|
|
|
* containing the clicked button then simulates a click
|
|
|
|
* @param {MouseEvent} event - MouseEvent fired by the button click
|
|
|
|
*/
|
|
|
|
triggerFileDialog(event) {
|
|
|
|
// simulate click to open file dialog
|
|
|
|
// using jQuery because IE11 doesn't support MouseEvent
|
|
|
|
$(event.target)
|
|
|
|
.closest('.gh-setting-action')
|
|
|
|
.find('input[type="file"]')
|
|
|
|
.click();
|
|
|
|
},
|
|
|
|
|
2020-08-19 18:00:06 +03:00
|
|
|
deleteCustomIcon() {
|
|
|
|
this.set('customIcon', null);
|
2021-05-18 15:59:47 +03:00
|
|
|
this.settings.set('portalButtonIcon', this.membersUtils.defaultIconKeys[0]);
|
2020-08-19 18:00:06 +03:00
|
|
|
},
|
|
|
|
|
2020-07-07 07:22:42 +03:00
|
|
|
selectDefaultIcon(icon) {
|
2020-07-30 13:56:07 +03:00
|
|
|
this.settings.set('portalButtonIcon', icon);
|
|
|
|
},
|
|
|
|
|
|
|
|
closeLeaveSettingsModal() {
|
|
|
|
this.set('showLeaveSettingsModal', false);
|
|
|
|
},
|
|
|
|
|
2020-10-22 13:39:00 +03:00
|
|
|
openStripeSettings() {
|
|
|
|
this.model.openStripeSettings();
|
|
|
|
this.closeModal();
|
|
|
|
},
|
|
|
|
|
2020-07-30 13:56:07 +03:00
|
|
|
leaveSettings() {
|
|
|
|
this.closeModal();
|
2020-11-23 12:38:29 +03:00
|
|
|
},
|
|
|
|
|
|
|
|
validateFreeSignupRedirect() {
|
|
|
|
return this._validateSignupRedirect(this.get('freeSignupRedirect'), 'membersFreeSignupRedirect');
|
|
|
|
},
|
|
|
|
|
|
|
|
validatePaidSignupRedirect() {
|
|
|
|
return this._validateSignupRedirect(this.get('paidSignupRedirect'), 'membersPaidSignupRedirect');
|
2020-06-19 19:06:49 +03:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
updateAllowedPlan(plan, isChecked) {
|
2021-04-27 20:03:13 +03:00
|
|
|
const portalPlans = this.settings.get('portalPlans') || [];
|
|
|
|
const allowedPlans = [...portalPlans];
|
2020-06-19 19:06:49 +03:00
|
|
|
|
|
|
|
if (!isChecked) {
|
|
|
|
this.settings.set('portalPlans', allowedPlans.filter(p => p !== plan));
|
|
|
|
} else {
|
|
|
|
allowedPlans.push(plan);
|
2021-04-27 20:03:13 +03:00
|
|
|
this.settings.set('portalPlans', allowedPlans);
|
2020-06-19 19:06:49 +03:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2020-11-20 12:53:08 +03:00
|
|
|
_validateSignupRedirect(url, type) {
|
2020-11-23 12:38:29 +03:00
|
|
|
let errMessage = `Please enter a valid URL`;
|
2021-05-18 15:59:47 +03:00
|
|
|
this.settings.get('errors').remove(type);
|
|
|
|
this.settings.get('hasValidated').removeObject(type);
|
2020-11-20 12:53:08 +03:00
|
|
|
|
2020-11-23 12:38:29 +03:00
|
|
|
if (url === null) {
|
2021-05-18 15:59:47 +03:00
|
|
|
this.settings.get('errors').add(type, errMessage);
|
|
|
|
this.settings.get('hasValidated').pushObject(type);
|
2020-11-23 12:38:29 +03:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (url === undefined) {
|
|
|
|
// Not initialised
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-11-20 12:53:08 +03:00
|
|
|
if (url.href.startsWith(this.siteUrl)) {
|
|
|
|
const path = url.href.replace(this.siteUrl, '');
|
|
|
|
this.settings.set(type, path);
|
2020-11-23 12:38:29 +03:00
|
|
|
} else {
|
|
|
|
this.settings.set(type, url.href);
|
2020-11-20 12:53:08 +03:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2020-07-07 07:22:42 +03:00
|
|
|
copyLinkOrAttribute: task(function* () {
|
|
|
|
copyTextToClipboard(this.showModalLinkOrAttribute);
|
|
|
|
yield timeout(this.isTesting ? 50 : 3000);
|
|
|
|
}),
|
|
|
|
|
2020-06-19 19:06:49 +03:00
|
|
|
saveTask: task(function* () {
|
2020-11-23 12:38:29 +03:00
|
|
|
this.send('validateFreeSignupRedirect');
|
|
|
|
this.send('validatePaidSignupRedirect');
|
2021-05-18 15:59:47 +03:00
|
|
|
if (this.settings.get('errors').length !== 0) {
|
2020-11-23 12:38:29 +03:00
|
|
|
return;
|
|
|
|
}
|
2020-06-19 19:06:49 +03:00
|
|
|
yield this.settings.save();
|
|
|
|
this.closeModal();
|
2021-05-04 18:43:31 +03:00
|
|
|
}).drop(),
|
|
|
|
|
|
|
|
getAvailablePrices: task(function* () {
|
|
|
|
const products = yield this.store.query('product', {include: 'stripe_prices'});
|
|
|
|
const product = products.firstObject;
|
|
|
|
const prices = product.get('stripePrices');
|
2021-05-07 16:44:04 +03:00
|
|
|
const activePrices = prices.filter((d) => {
|
|
|
|
return !!d.active;
|
|
|
|
});
|
|
|
|
this.set('prices', activePrices);
|
2020-06-19 19:06:49 +03:00
|
|
|
}).drop()
|
|
|
|
});
|