2021-07-20 12:54:19 +03:00
|
|
|
import Browser from 'mobiledoc-kit/utils/browser';
|
2021-07-20 20:33:39 +03:00
|
|
|
import Component from '@glimmer/component';
|
|
|
|
import {action} from '@ember/object';
|
2021-07-20 12:54:19 +03:00
|
|
|
import {formatTextReplacementHtml} from './koenig-text-replacement-html-input';
|
2021-08-25 17:13:00 +03:00
|
|
|
import {guidFor} from '@ember/object/internals';
|
2021-07-20 12:54:19 +03:00
|
|
|
import {isBlank} from '@ember/utils';
|
|
|
|
import {run} from '@ember/runloop';
|
2021-08-25 17:13:00 +03:00
|
|
|
import {schedule} from '@ember/runloop';
|
2021-07-26 19:00:11 +03:00
|
|
|
import {inject as service} from '@ember/service';
|
2021-07-20 12:54:19 +03:00
|
|
|
import {set} from '@ember/object';
|
2021-10-12 14:24:43 +03:00
|
|
|
import {task} from 'ember-concurrency';
|
2021-07-20 20:33:39 +03:00
|
|
|
import {tracked} from '@glimmer/tracking';
|
2021-07-20 12:54:19 +03:00
|
|
|
|
2021-07-20 20:33:39 +03:00
|
|
|
export default class KoenigCardEmailCtaComponent extends Component {
|
2021-08-20 16:33:23 +03:00
|
|
|
@service config;
|
2021-11-02 19:25:54 +03:00
|
|
|
@service feature;
|
2021-10-12 14:24:43 +03:00
|
|
|
@service store;
|
2021-08-23 20:46:00 +03:00
|
|
|
@service membersUtils;
|
2021-07-26 19:00:11 +03:00
|
|
|
@service ui;
|
|
|
|
|
|
|
|
@tracked buttonFocused = false;
|
|
|
|
@tracked contentFocused = false;
|
2021-10-12 14:24:43 +03:00
|
|
|
@tracked offers = null;
|
2021-07-20 20:33:39 +03:00
|
|
|
|
2021-08-25 17:13:00 +03:00
|
|
|
buttonTextInputId = 'button-text-input-' + guidFor(this);
|
|
|
|
urlInputId = 'url-input-' + guidFor(this);
|
|
|
|
|
2021-11-10 17:45:54 +03:00
|
|
|
get isEmpty() {
|
2021-11-10 17:50:02 +03:00
|
|
|
const {html, showButton, buttonText, buttonUrl} = this.args.payload;
|
|
|
|
|
|
|
|
if (!showButton) {
|
|
|
|
return isBlank(html);
|
|
|
|
}
|
2021-11-10 17:45:54 +03:00
|
|
|
|
|
|
|
return isBlank(html) && isBlank(buttonText) && isBlank(buttonUrl);
|
|
|
|
}
|
|
|
|
|
2021-07-20 20:33:39 +03:00
|
|
|
get formattedHtml() {
|
|
|
|
return formatTextReplacementHtml(this.args.payload.html);
|
|
|
|
}
|
|
|
|
|
2021-07-21 20:39:21 +03:00
|
|
|
get segments() {
|
|
|
|
return [{
|
2021-07-26 19:00:11 +03:00
|
|
|
name: 'free members',
|
2021-07-21 20:39:21 +03:00
|
|
|
filter: 'status:free'
|
|
|
|
}, {
|
2021-07-26 19:00:11 +03:00
|
|
|
name: 'paid members',
|
2021-07-21 20:39:21 +03:00
|
|
|
filter: 'status:-free'
|
|
|
|
}];
|
|
|
|
}
|
|
|
|
|
|
|
|
get selectedSegment() {
|
|
|
|
return this.segments.find(segment => segment.filter === this.args.payload.segment);
|
|
|
|
}
|
|
|
|
|
2021-07-20 20:33:39 +03:00
|
|
|
get toolbar() {
|
|
|
|
if (this.args.isEditing) {
|
2021-07-20 12:54:19 +03:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-11-02 19:25:54 +03:00
|
|
|
const items = [];
|
|
|
|
|
2021-11-03 17:52:05 +03:00
|
|
|
items.push({
|
|
|
|
buttonClass: 'fw4 flex items-center white',
|
|
|
|
icon: 'koenig/kg-edit',
|
|
|
|
iconClass: 'fill-white',
|
|
|
|
title: 'Edit',
|
|
|
|
text: '',
|
|
|
|
action: run.bind(this, this.args.editCard)
|
|
|
|
});
|
2021-11-02 19:25:54 +03:00
|
|
|
|
|
|
|
return {items};
|
2021-07-20 20:33:39 +03:00
|
|
|
}
|
2021-07-20 12:54:19 +03:00
|
|
|
|
2021-08-20 16:33:23 +03:00
|
|
|
get suggestedUrls() {
|
2021-08-23 20:46:00 +03:00
|
|
|
const urls = [];
|
|
|
|
|
|
|
|
urls.push(...[{
|
2021-08-20 16:33:23 +03:00
|
|
|
name: `Link to ${this.config.get('blogTitle')}`,
|
|
|
|
url: this.config.getSiteUrl('/')
|
|
|
|
}, {
|
2021-08-23 20:46:00 +03:00
|
|
|
name: 'Free email signup',
|
|
|
|
url: this.config.getSiteUrl('/#/portal/signup/free')
|
|
|
|
}]);
|
|
|
|
|
|
|
|
if (this.membersUtils.isStripeEnabled) {
|
|
|
|
urls.push(...[{
|
|
|
|
name: 'Paid subscription',
|
|
|
|
url: this.config.getSiteUrl('/#/portal/signup')
|
|
|
|
}, {
|
|
|
|
name: 'Upgrade or change plan',
|
|
|
|
url: this.config.getSiteUrl('/#/portal/account/plans')
|
|
|
|
}]);
|
|
|
|
}
|
|
|
|
|
2021-10-12 14:24:43 +03:00
|
|
|
if (this.offers) {
|
|
|
|
this.offers.forEach((offer) => {
|
|
|
|
urls.push(...[{
|
|
|
|
name: `Offer - ${offer.name}`,
|
|
|
|
url: this.config.getSiteUrl(offer.code)
|
|
|
|
}]);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2021-08-23 20:46:00 +03:00
|
|
|
return urls;
|
2021-08-20 16:33:23 +03:00
|
|
|
}
|
|
|
|
|
2021-07-20 20:33:39 +03:00
|
|
|
constructor() {
|
|
|
|
super(...arguments);
|
|
|
|
this.args.registerComponent(this);
|
2021-08-24 12:43:06 +03:00
|
|
|
|
|
|
|
const payloadDefaults = {
|
|
|
|
showButton: false,
|
2021-08-24 12:50:19 +03:00
|
|
|
showDividers: true,
|
2021-08-24 12:43:06 +03:00
|
|
|
segment: 'status:free',
|
2021-08-24 19:53:34 +03:00
|
|
|
alignment: 'left'
|
2021-08-24 12:43:06 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
Object.entries(payloadDefaults).forEach(([key, value]) => {
|
|
|
|
if (this.args.payload[key] === undefined) {
|
|
|
|
this._updatePayloadAttr(key, value);
|
|
|
|
}
|
|
|
|
});
|
2021-10-12 14:24:43 +03:00
|
|
|
this.fetchOffersTask.perform();
|
2021-07-20 20:33:39 +03:00
|
|
|
}
|
|
|
|
|
2021-08-24 12:12:52 +03:00
|
|
|
// required for snippet rects to be calculated - editor reaches in to component,
|
|
|
|
// expecting a non-Glimmer component with a .element property
|
|
|
|
@action
|
|
|
|
registerElement(element) {
|
|
|
|
this.element = element;
|
|
|
|
}
|
|
|
|
|
2021-07-20 20:33:39 +03:00
|
|
|
@action
|
|
|
|
updateHtml(html) {
|
|
|
|
this._updatePayloadAttr('html', html);
|
|
|
|
}
|
|
|
|
|
2021-07-21 20:39:21 +03:00
|
|
|
@action
|
|
|
|
setSegment(segment) {
|
|
|
|
this._updatePayloadAttr('segment', segment.filter);
|
|
|
|
}
|
|
|
|
|
2021-08-24 12:43:06 +03:00
|
|
|
@action
|
|
|
|
toggleButton() {
|
|
|
|
this._updatePayloadAttr('showButton', !this.args.payload.showButton);
|
2021-08-25 17:13:00 +03:00
|
|
|
|
|
|
|
if (this.args.payload.showButton) {
|
|
|
|
schedule('afterRender', this, function () {
|
|
|
|
document.getElementById(this.buttonTextInputId)?.focus();
|
|
|
|
});
|
|
|
|
}
|
2021-08-24 12:43:06 +03:00
|
|
|
}
|
|
|
|
|
2021-07-26 19:00:11 +03:00
|
|
|
@action
|
|
|
|
setButtonText(event) {
|
|
|
|
this._updatePayloadAttr('buttonText', event.target.value);
|
|
|
|
}
|
|
|
|
|
|
|
|
@action
|
2021-08-20 16:33:23 +03:00
|
|
|
setButtonUrl(url) {
|
|
|
|
this._updatePayloadAttr('buttonUrl', url);
|
2021-07-26 19:00:11 +03:00
|
|
|
}
|
|
|
|
|
2021-08-02 17:01:15 +03:00
|
|
|
@action
|
2021-08-24 19:53:34 +03:00
|
|
|
setAlignment(alignment, event) {
|
2021-08-02 17:01:15 +03:00
|
|
|
event.preventDefault();
|
2021-08-24 19:53:34 +03:00
|
|
|
this._updatePayloadAttr('alignment', alignment);
|
2021-08-02 17:01:15 +03:00
|
|
|
}
|
|
|
|
|
2021-07-20 20:33:39 +03:00
|
|
|
@action
|
2021-08-24 12:50:19 +03:00
|
|
|
toggleDividers() {
|
|
|
|
this._updatePayloadAttr('showDividers', !this.args.payload.showDividers);
|
2021-08-02 16:24:17 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
@action
|
2021-07-20 20:33:39 +03:00
|
|
|
registerEditor(textReplacementEditor) {
|
|
|
|
let commands = {
|
|
|
|
'META+ENTER': run.bind(this, this._enter, 'meta'),
|
|
|
|
'CTRL+ENTER': run.bind(this, this._enter, 'ctrl')
|
|
|
|
};
|
|
|
|
|
|
|
|
Object.keys(commands).forEach((str) => {
|
|
|
|
textReplacementEditor.registerKeyCommand({
|
|
|
|
str,
|
|
|
|
run() {
|
|
|
|
return commands[str](textReplacementEditor, str);
|
|
|
|
}
|
2021-07-20 12:54:19 +03:00
|
|
|
});
|
2021-07-20 20:33:39 +03:00
|
|
|
});
|
2021-07-20 12:54:19 +03:00
|
|
|
|
2021-07-20 20:33:39 +03:00
|
|
|
this._textReplacementEditor = textReplacementEditor;
|
2021-07-20 12:54:19 +03:00
|
|
|
|
2021-11-03 17:52:05 +03:00
|
|
|
run.scheduleOnce('afterRender', this, this._placeCursorAtEnd);
|
2021-07-20 20:33:39 +03:00
|
|
|
}
|
2021-07-20 12:54:19 +03:00
|
|
|
|
2021-07-20 20:33:39 +03:00
|
|
|
@action
|
|
|
|
leaveEditMode() {
|
2021-11-10 17:45:54 +03:00
|
|
|
if (this.isEmpty) {
|
2021-07-20 20:33:39 +03:00
|
|
|
// afterRender is required to avoid double modification of `isSelected`
|
|
|
|
// TODO: see if there's a way to avoid afterRender
|
|
|
|
run.scheduleOnce('afterRender', this, this.args.deleteCard);
|
2021-07-20 12:54:19 +03:00
|
|
|
}
|
2021-07-20 20:33:39 +03:00
|
|
|
}
|
2021-07-20 12:54:19 +03:00
|
|
|
|
2021-07-26 19:00:11 +03:00
|
|
|
@action
|
|
|
|
blurElement(event) {
|
|
|
|
event.preventDefault();
|
|
|
|
event.target.blur();
|
|
|
|
}
|
|
|
|
|
|
|
|
@action
|
|
|
|
focusElement(selector, event) {
|
|
|
|
event.preventDefault();
|
|
|
|
document.querySelector(selector)?.focus();
|
|
|
|
}
|
|
|
|
|
2021-10-12 14:24:43 +03:00
|
|
|
@task({restartable: true})
|
|
|
|
*fetchOffersTask() {
|
2021-10-18 12:18:01 +03:00
|
|
|
this.offers = yield this.store.query('offer', {limit: 'all', filter: 'status:active'});
|
2021-10-12 14:24:43 +03:00
|
|
|
}
|
|
|
|
|
2021-07-20 12:54:19 +03:00
|
|
|
_updatePayloadAttr(attr, value) {
|
2021-07-20 20:33:39 +03:00
|
|
|
let payload = this.args.payload;
|
2021-07-20 12:54:19 +03:00
|
|
|
|
|
|
|
set(payload, attr, value);
|
|
|
|
|
|
|
|
// update the mobiledoc and stay in edit mode
|
2021-07-20 20:33:39 +03:00
|
|
|
this.args.saveCard?.(payload, false);
|
|
|
|
}
|
2021-07-20 12:54:19 +03:00
|
|
|
|
|
|
|
/* key commands ----------------------------------------------------------*/
|
|
|
|
|
|
|
|
_enter(modifier) {
|
|
|
|
if (this.isEditing && (modifier === 'meta' || (modifier === 'crtl' && Browser.isWin()))) {
|
2021-07-20 20:33:39 +03:00
|
|
|
this.args.editCard?.();
|
2021-07-20 12:54:19 +03:00
|
|
|
}
|
2021-07-20 20:33:39 +03:00
|
|
|
}
|
2021-07-20 12:54:19 +03:00
|
|
|
|
|
|
|
_placeCursorAtEnd() {
|
|
|
|
if (!this._textReplacementEditor) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
let tailPosition = this._textReplacementEditor.post.tailPosition();
|
|
|
|
let rangeToSelect = tailPosition.toRange();
|
|
|
|
this._textReplacementEditor.selectRange(rangeToSelect);
|
|
|
|
}
|
2021-07-20 20:33:39 +03:00
|
|
|
}
|