-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwidget-generator.js
89 lines (82 loc) · 2.86 KB
/
widget-generator.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
const productLinkInput = flight.component(function() {
this.after('initialize', function() {
this.$node.find('input').on('input', event => {
const productLink = event.target.value;
this.trigger(document, 'productLinkChanged', { productLink });
});
});
});
productLinkInput.attachTo('#product-link-input');
const DisplayStyleChooser = flight.component(function() {
this.doSomething = function(event) {
if (event.target.value) {
const selectedDisplayStyle = event.target.value;
this.trigger(document, 'displayStyleSelected', { selectedDisplayStyle });
}
};
this.after('initialize', function() {
this.on('click', this.doSomething);
});
});
DisplayStyleChooser.attachTo('#display-style-chooser');
const ButtonTextInput = flight.component(function() {
this.after('initialize', function() {
this.on(document, 'displayStyleSelected', (event, data) => {
if (data.selectedDisplayStyle === 'overlay') {
this.$node.show();
} else {
this.$node.hide();
}
});
this.$node.find('input').on('input', event => {
const buttonText = event.target.value || 'Buy my product!';
this.trigger(document, 'buttonTextChanged', { buttonText });
});
});
});
ButtonTextInput.attachTo('#button-text-input');
const WidgetCodeSnippet = flight.component(function() {
this.snippetData = {
productLink: '',
buttonText: 'Buy my product',
displayStyle: 'overlay'
};
this.generateCodeSnippet = function() {
if (!this.snippetData.productLink) {
alert('Enter a valid link to a Gumroad product page.');
return;
}
let buttonText = 'Loading';
let buttonClassName = '';
if (this.snippetData.displayStyle === 'overlay') {
buttonText = this.snippetData.buttonText;
buttonClassName = 'gumroad-button';
}
const codeSnippet = `<a class="${buttonClassName}" href="${
this.snippetData.productLink
}" target="_blank" data-display-style="${this.snippetData.displayStyle}">
${buttonText}
</a>
<script src="https://gumroad.now.sh/gumroadDev.js" />`;
this.$node.find('textarea').val(codeSnippet);
this.$node.find('#preview').html('');
$('.gumroad-iframe-wrapper').remove();
this.$node.find('#preview').html(codeSnippet);
this.$node.find('#preview').show();
};
this.after('initialize', function() {
this.on(document, 'productLinkChanged', (event, data) => {
this.snippetData.productLink = data.productLink;
this.generateCodeSnippet();
});
this.on(document, 'buttonTextChanged', (event, data) => {
this.snippetData.buttonText = data.buttonText;
this.generateCodeSnippet();
});
this.on(document, 'displayStyleSelected', (event, data) => {
this.snippetData.displayStyle = data.selectedDisplayStyle;
this.generateCodeSnippet();
});
});
});
WidgetCodeSnippet.attachTo('#widget-code-sinippet');