-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathassets_js_lib_bitrequest_checkout.js
142 lines (129 loc) · 4.16 KB
/
assets_js_lib_bitrequest_checkout.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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
const html_node = document.documentElement,
b_url = "https://bitrequest.github.io";
document.addEventListener("DOMContentLoaded", function() {
checkout();
window.addEventListener("message", crossframe);
keyup();
closeloader_trigger();
});
// Handles the checkout process when a checkout button is clicked
function checkout() {
document.addEventListener("click", function(e) {
const target = e.target;
if (!target.matches(".br_checkout")) return;
e.preventDefault();
const checkout_url = target.getAttribute("href"),
br_frame = document.querySelector("#br_framebox iframe");
if (br_frame) {
showloader();
br_frame.setAttribute("src", checkout_url);
} else {
append_iframe(checkout_url);
showloader();
}
});
}
// Appends an iframe to the body with the given source URL
function append_iframe(framesrc) {
const div = document.createElement("div");
div.innerHTML = "<div id='br_framebox'><iframe src='" + framesrc + "'></iframe></div><div id='br_loadbox'><div id='br_loadpanel'><div id='br_loader'></div><p>Loading request...</p></div></div>";
document.body.appendChild(div.firstElementChild);
iframe_loaded();
}
// Sets up a load event listener for the iframe
function iframe_loaded() {
const requestframe = document.querySelector("#br_framebox iframe");
requestframe.addEventListener("load", function() {
const frame_source = requestframe.getAttribute("src");
if (frame_source) {
if (frame_source === b_url) {
return
}
showframe();
}
});
}
// Handles cross-frame communication
function crossframe(e) {
const data = e.data;
switch (data) {
case "close_loader":
closeloader();
break;
case "close_request_confirm":
setTimeout(closeframe_confirm, 200);
break;
case "close_request":
setTimeout(closeframe, 200);
break;
default:
if (data.id === "result") {
result_callback(data.data);
}
}
}
// Placeholder function for handling result data
function result_callback(post_data) {
// overwrite this function for your callback
console.log("overwrite this function for your callback");
console.log(post_data);
}
// Shows the iframe by adding CSS classes
function showframe() {
html_node.classList.add("showframe", "zoomframe");
}
// Prompts for confirmation before closing the frame
function closeframe_confirm() {
if (confirm("Close request?")) {
closeframe();
}
}
// Closes the iframe by removing CSS classes
function closeframe() {
if (html_node.classList.contains("zoomframe")) {
html_node.classList.remove("zoomframe");
setTimeout(function() {
html_node.classList.remove("showframe");
const iframe = document.querySelector("#br_framebox iframe");
if (iframe) {
iframe.setAttribute("src", b_url);
}
}, 400);
}
}
// Shows the loader by adding CSS classes
function showloader() {
html_node.classList.add("slide_loader", "fade_loader");
}
// Sets up a click event listener to close the loader
function closeloader_trigger() {
document.addEventListener("click", function(e) {
if (e.target.matches("#br_loadbox")) {
closeloader();
}
});
}
// Closes the loader by removing CSS classes
function closeloader() {
if (html_node.classList.contains("fade_loader")) {
html_node.classList.remove("fade_loader");
setTimeout(function() {
html_node.classList.remove("slide_loader");
}, 1000);
}
}
// Sets up a keyup event listener for the ESC key
function keyup() {
document.addEventListener("keyup", function(e) {
if (e.key === "Escape" || e.keyCode === 27) {
if (html_node.classList.contains("slide_loader")) {
closeloader();
return
}
if (html_node.classList.contains("showframe")) {
closeframe_confirm();
return
}
}
});
}