location_field_plain.html 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. <div class="bx-form-input-location-__name__ __minimized__ bx-form-input-location-cont bx-clearfix">
  2. __inputs__
  3. <div class="bx-form-input-location-status-cont bx-def-font-small bx-def-font-grayed bx-def-margin-thd-top bx-clearfix">
  4. <div class="bx-form-input-location-status ok"><i class="sys-icon sys-colored col-green1-dark check-circle"></i> <bx_text:_sys_location_status_ok /></div>
  5. <div class="bx-form-input-location-status fail"><i class="sys-icon sys-colored col-red1-dark times-circle"></i> <bx_text:_sys_location_status_fail /></div>
  6. <div class="bx-form-input-location-status undef"><i class="sys-icon sys-colored question-circle"></i> <bx_text:_sys_location_status_undef /></div>
  7. <div class="bx-form-input-location-status loading"><i class="sys-icon sys-colored sync-alt"></i> <bx_text:_sys_loading /></div>
  8. <div class="bx-form-input-location-copyright"></div>
  9. </div>
  10. </div>
  11. <script>
  12. (function () {
  13. var eForm = $('#__form_id__');
  14. eForm.on('submit', function (ev) {
  15. if (typeof(glBxLocationDisableFormSubmit) !== 'undefined' && glBxLocationDisableFormSubmit)
  16. ev.preventDefault();
  17. });
  18. var disableFormSubmit = function (b) {
  19. glBxLocationDisableFormSubmit = b;
  20. if (b)
  21. eForm.find('[type=submit]').addClass('bx-btn-disabled').attr('disabled', true);
  22. else
  23. eForm.find('[type=submit]').removeClass('bx-btn-disabled').attr('disabled', false);
  24. }
  25. var getLocationParams = function (sMode) {
  26. var sCountry = eForm.find('select[name="__name___country"] :selected').text();
  27. var sCountryCode = eForm.find('select[name="__name___country"]').val();
  28. var sZip = eForm.find('input[name="__name___zip"]').val();
  29. var sState = eForm.find('input[name="__name___state"]').val();
  30. var sCity = eForm.find('input[name="__name___city"]').val();
  31. var sStreet = eForm.find('input[name="__name___street"]').val();
  32. var sNum = eForm.find('input[name="__name___street_number"]').val();
  33. if (!sCountryCode || !sCountryCode.length)
  34. return '';
  35. sRet = '&country=' + encodeURIComponent(sCountry);
  36. if (sZip !== undefined && sZip.length && sMode !== 'no_street_no_zip')
  37. sRet += '&postalcode=' + encodeURIComponent(sZip);
  38. if (sZip !== undefined && 'zip_only' == sMode)
  39. return sZip.length ? sRet : '';
  40. if (sState.length)
  41. sRet += '&state=' + encodeURIComponent(sState);
  42. if (sCity.length)
  43. sRet += ('county_as_city' == sMode ? '&county=' : '&city=') + encodeURIComponent(sCity);
  44. if (sStreet !== undefined && sStreet.length && sMode !== 'no_street_no_zip')
  45. sRet += '&street=' + (sNum.length ? encodeURIComponent(sNum) + ' ' : '') + encodeURIComponent(sStreet);
  46. return sRet;
  47. }
  48. var geocode = function (a, i) {
  49. var s = a[i];
  50. $.getJSON('__nominatim_server__/search?addressdetails=1' + s + '&format=json&limit=1&email=__nominatim_email__', function (oResult) {
  51. glBxLocationFieldPlainGeocodingRequest = false;
  52. if (('object' === typeof oResult) && oResult.length && ('undefined' !== typeof oResult[0].lat) && ('undefined' !== typeof oResult[0].lon)) {
  53. eForm.find('input[name="__name___lat"]').val(oResult[0].lat);
  54. eForm.find('input[name="__name___lng"]').val(oResult[0].lon);
  55. if ('undefined' !== typeof(oResult[0].address)) {
  56. if ('undefined' !== typeof(oResult[0].address.state) && 0 == eForm.find('input[name="__name___state"]').val().length)
  57. eForm.find('input[name="__name___state"]').val(oResult[0].address.state);
  58. else if (__normalize_names__ && eForm.find('input[name="__name___state"]').val() != oResult[0].address.state)
  59. eForm.find('input[name="__name___state"]').val(oResult[0].address.state);
  60. var sCity = '';
  61. if ('undefined' !== typeof(oResult[0].address.city))
  62. sCity = oResult[0].address.city;
  63. else if ('undefined' !== typeof(oResult[0].address.town))
  64. sCity = oResult[0].address.town;
  65. else if ('undefined' !== typeof(oResult[0].address.village))
  66. sCity = oResult[0].address.village;
  67. if (sCity && 0 == $('input[name="__name___city"]').val().length)
  68. eForm.find('input[name="__name___city"]').val(sCity);
  69. else if (__normalize_names__ && sCity && eForm.find('input[name="__name___city"]').val() != sCity)
  70. eForm.find('input[name="__name___city"]').val(sCity);
  71. }
  72. if ('undefined' !== typeof(oResult[0].licence))
  73. $('.bx-form-input-location-__name__ .bx-form-input-location-copyright').html(oResult[0].licence);
  74. setLocationStatus('ok');
  75. }
  76. else {
  77. i += 1;
  78. if ('undefined' !== typeof(a[i])) {
  79. geocode(a, i);
  80. }
  81. else {
  82. eForm.find('input[name="__name___lat"]').val(0);
  83. eForm.find('input[name="__name___lng"]').val(0);
  84. setLocationStatus('fail');
  85. }
  86. }
  87. });
  88. }
  89. var setLocationStatus = function (s) {
  90. eForm.find('.bx-form-input-location-__name__ .bx-form-input-location-status').hide();
  91. eForm.find('.bx-form-input-location-__name__ .bx-form-input-location-status.' + s).show();
  92. if ('loading' !== s)
  93. disableFormSubmit(false);
  94. }
  95. setLocationStatus('undef');
  96. $(document).ready(function () {
  97. eForm.find('input[name^="__name__"],select[name^="__name__"]').on('change', function () {
  98. if ('undefined' !== typeof(glBxLocationFieldPlainGeocodingRequest) && glBxLocationFieldPlainGeocodingRequest) { // cancel previous request
  99. clearTimeout(glBxLocationFieldPlainGeocodingRequest);
  100. glBxLocationFieldPlainGeocodingRequest = null;
  101. }
  102. if ('hidden' == this.type)
  103. return;
  104. var s = getLocationParams();
  105. if (!s) {
  106. setLocationStatus('undef');
  107. return;
  108. }
  109. var a = [s];
  110. if (s = getLocationParams('zip_only'))
  111. a.push(s);
  112. if (s = getLocationParams('no_street_no_zip'))
  113. a.push(s);
  114. disableFormSubmit(true);
  115. glBxLocationFieldPlainGeocodingRequest = setTimeout(function () {
  116. setLocationStatus('loading');
  117. geocode(a, 0);
  118. }, 1500);
  119. });
  120. if (getLocationParams()) {
  121. if (parseFloat(eForm.find('input[name="__name___lat"]').val()) && parseFloat(eForm.find('input[name="__name___lng"]').val()))
  122. setLocationStatus('ok');
  123. else
  124. setLocationStatus('fail');
  125. }
  126. else {
  127. setLocationStatus('undef');
  128. }
  129. });
  130. })();
  131. </script>