forked from vitalijm/acf-custom-field-locations-rule
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathacf-custom-field-location-rule.js
93 lines (85 loc) · 3.04 KB
/
acf-custom-field-location-rule.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
jQuery(document).ready(function($){
if (typeof acf == 'undefined') { return; }
var acfCustomFieldLocatioRule = acf.ajax.extend({
events: {
'change .acf-field-select select': '_custom_field_location_change_select',
'ready .acf-field-select select': '_custom_field_location_change_select',
'change .acf-field-radio input': '_custom_field_location_change_radio',
'ready .acf-field-radio input': '_custom_field_location_change_radio',
'change .acf-field-true-false input': '_custom_field_location_change_true_false',
'ready .acf-field-true-false input': '_custom_field_location_change_true_false',
'change .acf-field-checkbox input': '_custom_field_location_change_checkox',
'ready .acf-field-checkbox input': '_custom_field_location_change_checkox',
},
_custom_field_location_change_select: function(e){
//alert(typeof(e.$el));
//for (i in e.$el) {
// alert(i+':'+e.$el[i]);
//}
var $container = e.$el.closest('.acf-field');
var $field = $container.attr('data-key');
var $value = e.$el.val();
//alert($field+' : '+$value);
this.update($field, $value).fetch();
},
_custom_field_location_change_radio: function(e){
//alert(typeof(e.$el));
//for (i in e.$el) {
// alert(i+':'+e.$el[i]);
//}
// this fires on every radio button
// if there are 5 buttons in radio field it will fire 5 times
var $container = e.$el.closest('.acf-field');
var $field = $container.attr('data-key');
var $ul = e.$el.closest('ul');
var $inputs = $ul.find('input');
var $value = [];
for (i=0; i<$inputs.length; i++) {
if ($inputs[i].checked) {
$value.push($inputs[i].value);
}
}
//alert($field+' : '+$value);
this.update($field, $value).fetch();
},
_custom_field_location_change_true_false: function(e){
//alert(typeof(e.$el));
//for (i in e.$el) {
// alert(i+':'+e.$el[i]);
//}
var $container = e.$el.closest('.acf-field');
var $field = $container.attr('data-key');
var $value = '';
if (e.$el.prop('checked')) {
$value = 'checked';
}
//alert($field+' : '+$value);
this.update($field, $value).fetch();
},
_custom_field_location_change_checkox: function(e){
//alert(typeof(e.$el));
//for (i in e.$el) {
// alert(i+':'+e.$el[i]);
//}
// this will fire for every checkbox
// if there are 5 choices in a checkbox field it will fire 5 times
var $container = e.$el.closest('.acf-field');
var $field = $container.attr('data-key');
var $ul = e.$el.closest('ul');
var $inputs = $ul.find('input');
var $value = [];
for (i=0; i<$inputs.length; i++) {
if ($inputs[i].checked) {
$value.push($inputs[i].value);
}
}
//alert($field+' : '+$value);
this.update($field, $value).fetch();
}
});
// triger actions when page is loaded
$('.acf-field-select select').trigger('ready');
$('.acf-field-checkbox input').trigger('ready');
$('.acf-field-radio input').trigger('ready');
$('.acf-field-true-false input').trigger('ready');
});