/
admin.js
95 lines (83 loc) · 2.83 KB
/
admin.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
if(!window.jQuery)
{
console.log("jquery is not loaded");
}
function JeebCtr(curBtnUrl) {
this.curBtnUrl = curBtnUrl;
this.init();
};
JeebCtr.prototype.init = function () {
jQuery("#jeeb-form-table tbody tr:last").remove();
var culture = jQuery("#woocommerce_jeebpaymentgateway_btnLang").val();
var theme = jQuery("#woocommerce_jeebpaymentgateway_btnTheme").val();
this.load(culture, theme);
}
JeebCtr.prototype.onChange = function () {
var culture = jQuery("#woocommerce_jeebpaymentgateway_btnLang").val();
var theme = jQuery("#woocommerce_jeebpaymentgateway_btnTheme").val();
this.load(culture, theme);
};
JeebCtr.prototype.bind = function () {
const self = this;
jQuery("#woocommerce_jeebpaymentgateway_btnLang").change(function () {
self.onChange();
});
jQuery("#woocommerce_jeebpaymentgateway_btnTheme").change(function () {
self.onChange();
});
};
JeebCtr.prototype.load = function (culture, theme) {
const self = this;
const url = "https://jeeb.io/media/resources?culture=" + culture + "&theme=" + theme;
jQuery.ajax({
dataType: "json",
url: url,
success: function (response) {
self.populate(response.resources);
},
error: function (err) {
console.log(err);
}
});
};
JeebCtr.prototype.populate = function (buttons) {
var raw = "<tr valign=\"top\" id=\"jeeb-buttons-row\">" +
"<th scope=\"row\" class=\"titledesc\">" +
"<label for=\"woocommerce_jeebpaymentgateway_btnurl\">Checkout Button </label>" +
"</th>" +
"<td class=\"forminp jeeb-buttons-container\">" +
"<fieldset>" +
"{0}" +
"</fieldset>" +
"</td>" +
"</tr>";
var name = "woocommerce_jeebpaymentgateway_btnUrl";
var content = "";
var hasCurBtnUrl = !!this.curBtnUrl;
var curBtnIndex = -1;
if (hasCurBtnUrl) {
for (var index = 0; index < buttons.length; index++)
if (buttons[index].url === this.curBtnUrl) {
curBtnIndex = index;
break;
}
}
for (var index = 0; index < buttons.length; index++) {
var checked = curBtnIndex >= 0
? (curBtnIndex === index ? true : false)
: index === 0 ? true : false;
content += "<label>" +
"<input type=\"radio\" name=\"" + name + "\" value=\"" + buttons[index].url + "\"" + (checked ? "checked" : "") + ">" +
"<img src=\"" + buttons[index].url + "\">" +
"</label>" +
"<br/>";
}
raw = raw.replace("{0}", content)
jQuery("#jeeb-buttons-row").remove();
jQuery("#jeeb-form-table tbody").append(raw);
}
jQuery(document).ready(function() {
const curBtnUrl = jQuery("#jeebCurBtnUrl").val();
const jeeb = new JeebCtr(curBtnUrl);
jeeb.bind();
});