1 |
|
$(function() {
|
2 |
|
function geoloc_prefill(element_type, element_value)
|
|
1 |
function geoloc_prefill(element_type, element_value, widget_name=null)
|
3 |
2 |
{
|
4 |
|
$('div[data-geolocation="' + element_type +'"] input').val(element_value);
|
5 |
|
$('div[data-geolocation="' + element_type +'"] textarea').val(element_value);
|
6 |
|
var $options = $('div[data-geolocation="' + element_type +'"] option');
|
|
3 |
var selector = 'div[data-geolocation="' + element_type +'"]:first';
|
|
4 |
if (widget_name) {
|
|
5 |
selector = 'div[data-widget-name="' + widget_name + '"] ~ ' + selector;
|
|
6 |
}
|
|
7 |
$(selector).find('input').val(element_value);
|
|
8 |
$(selector).find('textarea').val(element_value);
|
|
9 |
var $options = $(selector).find('option');
|
7 |
10 |
if ($options.length == 0) return;
|
8 |
11 |
|
9 |
12 |
var slugified_value = $.slugify(element_value);
|
... | ... | |
24 |
27 |
}
|
25 |
28 |
}
|
26 |
29 |
|
|
30 |
function init_sync_from_template_address() {
|
|
31 |
const widget_selector = '.JsonpSingleSelectWidget.template-address';
|
|
32 |
const hidden_parts_selector = '.hide-address-parts';
|
|
33 |
$(widget_selector + ' select').on('change', function() {
|
|
34 |
var data = $(this).select2('data');
|
|
35 |
var widget_name = $(this).parents('div.widget').data('widget-name');
|
|
36 |
if (data) {
|
|
37 |
var number_and_street = null;
|
|
38 |
var address = undefined;
|
|
39 |
if (typeof data[0].address == "object") {
|
|
40 |
address = data[0].address;
|
|
41 |
} else {
|
|
42 |
address = data[0];
|
|
43 |
}
|
|
44 |
var road = address.road || address.nom_rue;
|
|
45 |
var house_number = address.house_number || address.numero;
|
|
46 |
var city = address.city || address.nom_commune;
|
|
47 |
var postcode = address.postcode || address.code_postal;
|
|
48 |
if (house_number && road) {
|
|
49 |
number_and_street = house_number + ' ' + road;
|
|
50 |
} else {
|
|
51 |
number_and_street = road;
|
|
52 |
}
|
|
53 |
geoloc_prefill('number-and-street', number_and_street, widget_name);
|
|
54 |
geoloc_prefill('house', house_number, widget_name);
|
|
55 |
geoloc_prefill('road', road, widget_name);
|
|
56 |
geoloc_prefill('city', city, widget_name);
|
|
57 |
geoloc_prefill('postcode', postcode, widget_name);
|
|
58 |
geoloc_prefill('country', address.country, widget_name);
|
|
59 |
}
|
|
60 |
});
|
|
61 |
|
|
62 |
$('input.wcs-manual-address').on('change', function() {
|
|
63 |
var widget = $(this).parents('div.widget');
|
|
64 |
widget.nextUntil(widget_selector, 'div[data-geolocation]').find('input').attr('readonly', this.checked ? null : 'readonly');
|
|
65 |
widget.nextUntil(widget_selector, 'div[data-geolocation]').find('textarea').attr('readonly', this.checked ? null : 'readonly');
|
|
66 |
if (this.checked) {
|
|
67 |
widget.nextUntil(widget_selector, 'div[data-geolocation]:not(.template-address)').attr('hidden', null)
|
|
68 |
} else if (widget.is(hidden_parts_selector)) {
|
|
69 |
widget.nextUntil(widget_selector, 'div[data-geolocation]:not(.template-address)').attr('hidden', 'hidden')
|
|
70 |
}
|
|
71 |
});
|
|
72 |
|
|
73 |
if ($(widget_selector).length) {
|
|
74 |
$('div[data-geolocation] input, div[data-geolocation] textarea').attr('readonly', 'readonly')
|
|
75 |
if ($(widget_selector + hidden_parts_selector).length) {
|
|
76 |
$(widget_selector + hidden_parts_selector).nextUntil(widget_selector, 'div[data-geolocation]:not(.template-address)').attr('hidden', 'hidden');
|
|
77 |
}
|
|
78 |
}
|
|
79 |
}
|
|
80 |
|
|
81 |
|
|
82 |
$(function() {
|
|
83 |
init_sync_from_template_address();
|
|
84 |
$('form').on('wcs:block-row-added', function() {
|
|
85 |
init_sync_from_template_address();
|
|
86 |
});
|
27 |
87 |
$(document).on('set-geolocation', function(event, coords, options) {
|
28 |
88 |
$.getJSON(WCS_ROOT_URL + '/api/reverse-geocoding?lat=' + coords.lat + '&lon=' + coords.lng, function(data) {
|
29 |
89 |
unset_sync_callback()
|
... | ... | |
67 |
127 |
set_sync_callback()
|
68 |
128 |
});
|
69 |
129 |
});
|
70 |
|
$('.JsonpSingleSelectWidget.template-address select').on('change', function() {
|
71 |
|
var data = $('.JsonpSingleSelectWidget.template-address select').select2('data');
|
72 |
|
if (data) {
|
73 |
|
var number_and_street = null;
|
74 |
|
var address = undefined;
|
75 |
|
if (typeof data[0].address == "object") {
|
76 |
|
address = data[0].address;
|
77 |
|
} else {
|
78 |
|
address = data[0];
|
79 |
|
}
|
80 |
|
var road = address.road || address.nom_rue;
|
81 |
|
var house_number = address.house_number || address.numero;
|
82 |
|
var city = address.city || address.nom_commune;
|
83 |
|
var postcode = address.postcode || address.code_postal;
|
84 |
|
if (house_number && road) {
|
85 |
|
number_and_street = house_number + ' ' + road;
|
86 |
|
} else {
|
87 |
|
number_and_street = road;
|
88 |
|
}
|
89 |
|
geoloc_prefill('number-and-street', number_and_street);
|
90 |
|
geoloc_prefill('house', house_number);
|
91 |
|
geoloc_prefill('road', road);
|
92 |
|
geoloc_prefill('city', city);
|
93 |
|
geoloc_prefill('postcode', postcode);
|
94 |
|
geoloc_prefill('country', address.country);
|
95 |
|
}
|
96 |
|
});
|
|
130 |
|
97 |
131 |
if ($('.qommon-map').length == 0 && $('.JsonpSingleSelectWidget.template-address').length == 0) {
|
98 |
132 |
/* if there's no map on the page, we do the geolocation without leaflet. */
|
99 |
133 |
if (navigator.geolocation) {
|
... | ... | |
113 |
147 |
);
|
114 |
148 |
}
|
115 |
149 |
}
|
116 |
|
if ($('.JsonpSingleSelectWidget.template-address').length) {
|
117 |
|
$('div[data-geolocation] input, div[data-geolocation] textarea').attr('readonly', 'readonly')
|
118 |
|
if ($('.JsonpSingleSelectWidget.template-address.hide-address-parts').length) {
|
119 |
|
$('div[data-geolocation]:not(.template-address)').attr('hidden', 'hidden')
|
120 |
|
}
|
121 |
|
}
|
122 |
|
|
123 |
|
$('#wcs-manual-address').on('change', function() {
|
124 |
|
$('div[data-geolocation] input, div[data-geolocation] textarea').attr('readonly', this.checked ? null : 'readonly');
|
125 |
|
if (this.checked) {
|
126 |
|
$('div[data-geolocation]:not(.template-address)').attr('hidden', null)
|
127 |
|
} else if ($('.JsonpSingleSelectWidget.template-address.hide-address-parts').length) {
|
128 |
|
$('div[data-geolocation]:not(.template-address)').attr('hidden', 'hidden')
|
129 |
|
}
|
130 |
|
});
|
131 |
150 |
|
132 |
151 |
function set_sync_callback() {
|
133 |
152 |
var $map = $('.qommon-map');
|