location_field_plain.html 5.2 KB

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