-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
128 lines (107 loc) · 2.69 KB
/
index.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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
const NO_VALID_PRICE = "No valid price.";
let tmpl = document.createElement("template");
tmpl.id = "etn-vendor-payment";
tmpl.innerHTML = `
<style>
:host {
display: block; /* by default, custom elements are display: inline */
contain: content; /* CSS containment FTW. */
}
#errors {
color: red;
}
#qr div {
position: relative;
box-sizing: content-box;
border: 4px solid #333333;
border-radius: 4px;
}
#qr img {
box-sizing: border-box;
border: 8px solid #FFFFFF;
width: 100%;
}
</style>
<slot></slot>
<slot id="errors"></slot>
<slot id="qr"></slot>
`;
export default class EtnVendorPayment extends HTMLElement {
constructor() {
super();
}
static get observedAttributes() {
return ["amount"];
}
attributeChangedCallback() {
if (!this.shadowRoot) {
return
}
this.render();
}
connectedCallback() {
this.attachShadow({ mode: "open" });
this.shadowRoot.appendChild(tmpl.content.cloneNode(true));
this.render();
}
buildPaymentString() {
return this.outletId + "/" + this.paymentId + "/" + this.amount
}
buildQrCode(paymentString) {
return (
"https://chart.googleapis.com/chart?cht=qr&chs=300x300&chld=L|0&chl=" +
encodeURI(
"https://link.electroneum.com/jWEpM5HcxP?vendor=" + paymentString
)
);
}
renderError() {
const errorsElement = this.shadowRoot.getElementById("errors");
let p = document.createElement("p");
p.innerHTML = NO_VALID_PRICE;
errorsElement.appendChild(p);
}
renderQr() {
const qrElement = this.shadowRoot.getElementById("qr")
let content = document.createElement("div");
let qrImg = document.createElement("img");
qrImg.src = this.buildQrCode(this.buildPaymentString());
content.appendChild(qrImg);
qrElement.appendChild(content);
}
render() {
this.removeChildrenElement('errors');
this.removeChildrenElement('qr');
if (this.amount <= 0) {
return this.renderError();
}
this.renderQr();
}
removeChildrenElement(selector) {
const element = this.shadowRoot.getElementById(selector)
while (element.firstChild) {
element.removeChild(element.firstChild);
}
};
get amount() {
if (!this.getAttribute("amount")) {
return 0;
}
return this.getAttribute("amount");
}
set amount(value) {
this.setAttribute("amount", value);
}
get outletId() {
return this.getAttribute("outletId");
}
set outletId(value) {
this.setAttribute("outletId", value);
}
get paymentId() {
return this.getAttribute("paymentId");
}
set paymentId(vlaue) {
this.setAttribute("paymentId");
}
}