-
Notifications
You must be signed in to change notification settings - Fork 7
/
settings.js
148 lines (130 loc) · 5.26 KB
/
settings.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
143
144
145
146
147
148
(function($) {
'use strict';
/**
* @param {string} service_slug
* @param {boolean} is_setup
*/
function on_price_service_change(service_slug, is_setup) {
console.debug('Price service selected:', service_slug);
// Disable all non-common conditional fields
var conditional_fields = [
'#woocommerce_nimiq_gateway_fee_nim',
'#woocommerce_nimiq_gateway_fee_btc',
'#woocommerce_nimiq_gateway_fee_eth',
// '#conditional_field_id',
];
$(conditional_fields.join(',')).closest('tr').addClass('hidden');
// Enable service-specific fields
switch (service_slug) {
case 'coingecko':
$('#woocommerce_nimiq_gateway_fee_nim, ' +
'#woocommerce_nimiq_gateway_fee_btc, ' +
'#woocommerce_nimiq_gateway_fee_eth')
.closest('tr').removeClass('hidden');
break;
case 'fastspot':
break;
// case '':
// $('#conditional_field_id').closest('tr').removeClass('hidden'); break;
}
price_service = service_slug;
if (!is_setup) toggle_common_fields();
}
/**
* @param {string} service_slug
* @param {boolean} is_setup
*/
function on_validation_service_change(service_slug, is_setup) {
console.debug('Validation service selected:', service_slug);
// Disable all non-common conditional fields
var conditional_fields = [
'#woocommerce_nimiq_gateway_jsonrpc_nimiq_url',
'#woocommerce_nimiq_gateway_jsonrpc_nimiq_username',
'#woocommerce_nimiq_gateway_jsonrpc_nimiq_password',
// '#conditional_field_id',
];
$(conditional_fields.join(',')).closest('tr').addClass('hidden');
// Enable service-specific fields
switch (service_slug) {
case 'nimiq_watch':
break;
case 'json_rpc_nim':
$('#woocommerce_nimiq_gateway_jsonrpc_nimiq_url, ' +
'#woocommerce_nimiq_gateway_jsonrpc_nimiq_username, ' +
'#woocommerce_nimiq_gateway_jsonrpc_nimiq_password')
.closest('tr').removeClass('hidden');
break;
// case '':
// $('#conditional_field_id').closest('tr').removeClass('hidden'); break;
}
validation_service = service_slug;
if (!is_setup) toggle_common_fields();
}
function toggle_common_fields() {
// Disable all conditional fields
var common_fields = [
'#woocommerce_nimiq_gateway_nimiqx_api_key',
// '#conditional_field_id',
];
$(common_fields.join(',')).closest('tr').addClass('hidden');
// Enable required fields
if (price_service === 'nimiqx' || validation_service === 'nimiqx') {
$('#woocommerce_nimiq_gateway_nimiqx_api_key')
.closest('tr').removeClass('hidden');
}
}
// Set up field toggle event handlers
const $price_service_select = $('#woocommerce_nimiq_gateway_price_service');
const $validation_service_select = $('#woocommerce_nimiq_gateway_validation_service_nim');
let price_service = $price_service_select.val();
let validation_service = $validation_service_select.val();
$price_service_select.on('change', function(event) {
on_price_service_change(event.target.value);
});
$validation_service_select.on('change', function(event) {
on_validation_service_change(event.target.value);
});
on_price_service_change(price_service, true);
on_validation_service_change(validation_service, true);
toggle_common_fields();
// Add click listener to toggle advanced section
$('#woocommerce_nimiq_gateway_section_advanced').click(function() {
console.log('boop')
$('#woocommerce_nimiq_gateway_section_advanced + p + table').toggle('fast');
});
// Add image preview to shop logo field
const $shop_logo_url = $('#woocommerce_nimiq_gateway_shop_logo_url');
function update_shop_logo_preview() {
const src = $shop_logo_url.val() || $shop_logo_url.data('site-icon');
console.log(src);
const $preview = $('#nimiq_shop_logo_preview');
if ($preview.length) {
$preview.attr('src', src);
} else {
$shop_logo_url.after('<img id="nimiq_shop_logo_preview" src="' + src + '">');
}
}
$shop_logo_url.on('input', update_shop_logo_preview);
update_shop_logo_preview();
// Add change listener for Bitcoin xpub
const $bitcoin_xpub = $('#woocommerce_nimiq_gateway_bitcoin_xpub');
$bitcoin_xpub.on('input', function() {
const xpub = $bitcoin_xpub.val();
if (!xpub) return;
let type;
switch (xpub.substr(0, 4)) {
case 'xpub':
case 'tpub':
type = 'bip-44';
break;
case 'zpub':
case 'vpub':
type = 'bip-84';
break;
default: break; // TODO Show error feedback to user
}
$('#woocommerce_nimiq_gateway_bitcoin_xpub_type').val(type);
});
// Add asterix to required fields
$('.required').after('<span>*</span>');
})(jQuery);