-
Notifications
You must be signed in to change notification settings - Fork 13
/
address-field.js
105 lines (89 loc) · 3.01 KB
/
address-field.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
;(function($) {
function split_acf_name(name) {
var matches = name.match(/\[.*?(?=\])/g);
for(var i=0; i<matches.length; i++)
matches[i] = matches[i].replace('[', '');
return matches;
}
function update_item_layout(item) {
if(item.parents('ul.missing').size() > 0) {
item.find('input').remove();
}
else if($('input', item).size() == 0) {
item.append(
$('<input type="hidden" />')
.val(item.attr('name'))
);
}
var field = item.closest('.field');
var key = field.find('input[name="address_layout_key"]').val();
$('ul.row li.item input', field).each(function() {
var $this = $(this);
var col = $this.closest('li.item').index();
var row = $this.closest('div.address_layout').children('ul.row').index($this.closest('ul.row'));
$this.val($this.closest('li.item').attr('name'));
$this.attr('name', 'fields['+key+'][address_layout]['+row+']['+col+']');
});
}
$('#acf_fields .address_enabled input[type="checkbox"]').live('change', function() {
var $this = $(this),
field = $this.closest('.field'),
matches = split_acf_name($this.attr('name')),
layout_item = $('li.item[name="' + matches[2] + '"]', field),
missing_row = $('ul.row.missing', field);
if($this.is(':checked')) {
layout_item.removeClass('disabled');
}
else {
layout_item
.addClass('disabled')
.remove()
.appendTo(missing_row);
}
update_item_layout(layout_item);
});
$('#acf_fields input.address_label[type="text"]').live('keyup', function() {
var $this = $(this),
field = $this.closest('.field'),
matches = split_acf_name($this.attr('name')),
layout_item = $('li.item[name="' + matches[2] + '"]', field);
layout_item.text($this.val());
});
function init_address_layout(address_layout) {
if( !address_layout.is('.address_layout') )
return;
if( $('ul.row', address_layout).data('sortable') )
return;
$('ul.row', address_layout).sortable({
connectWith: ".row",
placeholder: "placeholder",
start: function(event, ui) {
ui.placeholder.html(' ');
},
update: function(event, ui) {
update_item_layout(ui.item);
}
}).disableSelection();
}
//Initialize existing address fields or fields that have changed to address fields
$('#acf_fields .field_type select.select').live('change', function() {
var $this = $(this),
field = $this.closest('.field');
if($this.val() == 'address-field') {
//If address-field already exists, initialize it
if($('.field_option_address-field', field).exists() ) {
init_address_layout( $('.address_layout', field) );
}
}
});
//Listen to ajax requests and initialize and new address fields
$(document).bind('ajaxSuccess', function(e, xhr, settings) {
if( settings.url == ajaxurl && settings.data.indexOf('field_type=address-field') != -1 ) {
init_address_layout( $('.address_layout' ) );
}
});
//Trigger change even to initialize all address fields on document.ready event.
$(function(){
$('#acf_fields .field_type select.select').trigger('change');
})
})(jQuery)