location_field_plain.html 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. <div class="bx-form-input-location-__name__ 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 getLocationString = function (sMode) {
  14. var sCountry = $('select[name="__name___country"] :selected').text();
  15. var sCountryCode = $('select[name="__name___country"]').val();
  16. var sZip = $('input[name="__name___zip"]').val();
  17. var sState = $('input[name="__name___state"]').val();
  18. var sCity = $('input[name="__name___city"]').val();
  19. var sStreet = $('input[name="__name___street"]').val();
  20. var sNum = $('input[name="__name___street_number"]').val();
  21. if (!sCountryCode || !sCountryCode.length)
  22. return '';
  23. sRet = sZip.length ? sZip : '';
  24. if ('undefined' !== typeof(sMode) && 'zip_only' == sMode) {
  25. return sZip.length ? sRet : '';
  26. }
  27. if ('undefined' !== typeof(sMode) && 'city_only' == sMode) {
  28. return (sCity.length ? sCity + ', ': '') + (sState.length ? sState + ', ': '') + sCountry;
  29. }
  30. sRet = (sCity.length ? sCity + ', ': '') + (sState.length ? sState + ', ': '') + sRet;
  31. if (sCity.length)
  32. sRet = (sStreet.length && sNum.length ? sNum + ' ' : '') + (sStreet.length ? sStreet + ', ' : '') + sRet;
  33. if ('undefined' !== typeof(sMode) && 'address_wo_zip' == sMode) {
  34. return sCity.length && sStreet.length ?
  35. (sStreet.length && sNum.length ? sNum + ' ' : '') + (sStreet.length ? sStreet + ', ' : '') + (sCity.length ? sCity + ', ': '') + (sState.length ? sState : '') : '';
  36. }
  37. return sRet;
  38. }
  39. var geocode = function (a, i, sCountryCode) {
  40. var s = a[i];
  41. $.getJSON('__nominatim_server__/search/?q=' + encodeURIComponent(s) + '&countrycodes=' + sCountryCode + '&addressdetails=1&format=json&limit=1&email=__nominatim_email__', function (oResult) {
  42. glBxLocationFieldPlainGeocodingRequest = false;
  43. if (('object' === typeof oResult) && oResult.length && ('undefined' !== typeof oResult[0].lat) && ('undefined' !== typeof oResult[0].lon)) {
  44. $('input[name="__name___lat"]').val(oResult[0].lat);
  45. $('input[name="__name___lng"]').val(oResult[0].lon);
  46. if ('undefined' !== typeof(oResult[0].address)) {
  47. if ('undefined' !== typeof(oResult[0].address.state) && 0 == $('input[name="__name___state"]').val().length)
  48. $('input[name="__name___state"]').val(oResult[0].address.state);
  49. if (0 == $('input[name="__name___city"]').val().length) {
  50. if ('undefined' !== typeof(oResult[0].address.city))
  51. $('input[name="__name___city"]').val(oResult[0].address.city);
  52. else if ('undefined' !== typeof(oResult[0].address.town))
  53. $('input[name="__name___city"]').val(oResult[0].address.town);
  54. else if ('undefined' !== typeof(oResult[0].address.village))
  55. $('input[name="__name___city"]').val(oResult[0].address.village);
  56. }
  57. }
  58. if ('undefined' !== typeof(oResult[0].licence))
  59. $('.bx-form-input-location-__name__ .bx-form-input-location-copyright').html(oResult[0].licence);
  60. setLocationStatus('ok');
  61. }
  62. else {
  63. i += 1;
  64. if ('undefined' !== typeof(a[i])) {
  65. geocode(a, i, sCountryCode);
  66. }
  67. else {
  68. $('input[name="__name___lat"]').val(0);
  69. $('input[name="__name___lng"]').val(0);
  70. setLocationStatus('fail');
  71. }
  72. }
  73. });
  74. }
  75. var setLocationStatus = function (s) {
  76. $('.bx-form-input-location-__name__ .bx-form-input-location-status').hide();
  77. $('.bx-form-input-location-__name__ .bx-form-input-location-status.' + s).show();
  78. }
  79. setLocationStatus('undef');
  80. $(document).ready(function () {
  81. $('input[name^="__name__"],select[name^="__name__"]').on('change', function () {
  82. if ('undefined' !== typeof(glBxLocationFieldPlainGeocodingRequest) && glBxLocationFieldPlainGeocodingRequest) { // cancel previous request
  83. clearTimeout(glBxLocationFieldPlainGeocodingRequest);
  84. glBxLocationFieldPlainGeocodingRequest = null;
  85. }
  86. if ('hidden' == this.type)
  87. return;
  88. var s = getLocationString();
  89. if (!s) {
  90. setLocationStatus('undef');
  91. return;
  92. }
  93. var a = [s];
  94. if (s = getLocationString('address_wo_zip'))
  95. a.push(s);
  96. if (s = getLocationString('zip_only'))
  97. a.push(s);
  98. if (s = getLocationString('city_only'))
  99. a.push(s);
  100. glBxLocationFieldPlainGeocodingRequest = setTimeout(function () {
  101. setLocationStatus('loading');
  102. geocode(a, 0, $('select[name="__name___country"]').val());
  103. }, 1500);
  104. });
  105. if (getLocationString()) {
  106. if (parseFloat($('input[name="__name___lat"]').val()) && parseFloat($('input[name="__name___lng"]').val()))
  107. setLocationStatus('ok');
  108. else
  109. setLocationStatus('fail');
  110. }
  111. else {
  112. setLocationStatus('undef');
  113. }
  114. });
  115. })();
  116. </script>