diff --git a/application/views/partial/header.php b/application/views/partial/header.php index e7e819a29..d47a69eda 100644 --- a/application/views/partial/header.php +++ b/application/views/partial/header.php @@ -20,6 +20,7 @@ + diff --git a/application/views/people/form_basic_info.php b/application/views/people/form_basic_info.php index 4fe50af4f..65bde7e43 100644 --- a/application/views/people/form_basic_info.php +++ b/application/views/people/form_basic_info.php @@ -141,122 +141,34 @@ //validation and submit handling $(document).ready(function() { - - var handle_auto_completion = function(fields) { - return function(event, results, formatted) { - if (results != null && results.length > 0) { - // handle auto completion - for(var i in fields) { - $("#" + fields[i]).val(results[i]); + nominatim.init({ + fields : { + postcode : { + dependencies : ["postcode", "city", "state", "country"], + response : { + field : 'postalcode', + format: ["postcode", "village|town|hamlet|city_district|city", "state", "country"] } - return false; + }, + + city : { + dependencies : ["postcode", "city", "state", "country"], + response : { + format: ["postcode", "village|town|hamlet|city_district|city", "state", "country"] + } + }, + + state : { + dependencies : ["state", "country"] + }, + + country : { + dependencies : ["state", "country"] } - return true; - }; - }; - - var set_field_values = function(results) { - return results[0] + ' - ' + results[1]; - }; - - var create_parser = function(field_name, parse_format) - { - var parse_field = function(format, address) - { - var fields = []; - $.each(format.split("|"), function(key, value) - { - if (address[value] && fields.length < 2 && $.inArray(address[value], fields) === -1) - { - fields.push(address[value]); - } - }); - return fields[0] + (fields[1] ? ' (' + fields[1] + ')' : ''); - }; - - return function(data) - { - var parsed = []; - $.each(data, function(index, value) - { - var address = value.address; - var row = []; - $.each(parse_format, function(key, format) - { - row.push(parse_field(format, address)); - }); - parsed[index] = { - data: row, - value: address[field_name], - result: address[field_name] - }; - }); - return parsed; - }; - }; - - var request_params = function(id, key) - { - return function() { - var result = { - format: 'json', - limit: 5, - addressdetails: 1, - country: window['sessionStorage'] ? sessionStorage['country'] : '' - }; - result[key || id] = $("#"+id).val(); - return result; - } - - }; - // TODO make endpoint configurable - var url = http_s('nominatim.openstreetmap.org/search'); - var handle_city_completion = handle_auto_completion(["postcode", "city", "state", "country"]); - $("#postcode").autocomplete(url,{ - max:100, - minChars:3, - delay:500, - formatItem: set_field_values, - type: 'GET', - dataType:'json', - extraParams: request_params("postcode", "postalcode"), - parse: create_parser('postcode', ["postcode", "village|city_district|town|hamlet|city|county", "state", "country"]) + + }, + language : 'config->item('language');?>' }); - $("#postcode").result(handle_city_completion); - - $("#city").autocomplete(url,{ - max:100, - minChars:2, - delay:500, - formatItem: set_field_values, - type: 'GET', - dataType:'json', - extraParams: request_params("city"), - parse: create_parser('city', ["postcode", "village|city_district|town|hamlet|city|county", "state", "country"]) - }); - $("#city").result(handle_city_completion); - - $("#state").autocomplete(url, { - max:100, - minChars:2, - delay:500, - type: 'GET', - dataType:'json', - extraParams: request_params("state"), - parse: create_parser('state', ["state", "country"]), - }); - $("#state").result(handle_auto_completion(["state", "country"])); - - $("#country").autocomplete(url,{ - max:100, - minChars:2, - delay:500, - type: 'GET', - dataType:'json', - extraParams: request_params("country"), - parse: create_parser('country', ["country"]), - }); - $("#country").result(handle_auto_completion(["country"])); }); \ No newline at end of file diff --git a/application/views/people/manage.php b/application/views/people/manage.php index d7de07eb6..cff723357 100644 --- a/application/views/people/manage.php +++ b/application/views/people/manage.php @@ -10,17 +10,6 @@ $(document).ready(function() enable_delete('lang->line($controller_name."_confirm_delete")?>','lang->line($controller_name."_none_selected")?>'); }); -if (window.sessionStorage && !sessionStorage['country']) -{ - jQuery.ajax({ - type: "GET", - url: http_s('ipinfo.io/json'), - success: function(response) { - sessionStorage['country'] = response.country; - }, dataType: 'jsonp' - }); -} - function init_table_sorting() { //Only init if there is more than one row diff --git a/js/common.js b/js/common.js index 68ba35624..6e96d626f 100644 --- a/js/common.js +++ b/js/common.js @@ -36,11 +36,6 @@ function set_feedback(text, classname, keep_displayed) } } -function http_s(url) -{ - return document.location.protocol + '//' + url; -} - //keylisteners $.each(['customers', 'items', 'reports', 'receivings', 'sales', 'employees', 'config', 'giftcards'], function(key, value) { $(window).jkey('f' + (key+1), function(){ diff --git a/js/nominatim.autocomplete.js b/js/nominatim.autocomplete.js new file mode 100644 index 000000000..710a271af --- /dev/null +++ b/js/nominatim.autocomplete.js @@ -0,0 +1,115 @@ +(function($) { + + function http_s(url) + { + return document.location.protocol + '//' + url; + } + + if (window.sessionStorage && !sessionStorage['country']) + { + $.ajax({ + type: "GET", + url: http_s('ipinfo.io/json'), + success: function(response) { + sessionStorage['country'] = response.country; + }, dataType: 'jsonp' + }); + } + + var url = http_s('nominatim.openstreetmap.org/search'); + + var handle_auto_completion = function(fields) { + return function(event, results, formatted) { + if (results != null && results.length > 0) { + // handle auto completion + for(var i in fields) { + $("#" + fields[i]).val(results[i]); + } + return false; + } + return true; + }; + }; + + var set_field_values = function(results) { + return results[0] + ' - ' + results[1]; + }; + + var create_parser = function(field_name, parse_format) + { + var parse_field = function(format, address) + { + var fields = []; + $.each(format.split("|"), function(key, value) + { + if (address[value] && fields.length < 2 && $.inArray(address[value], fields) === -1) + { + fields.push(address[value]); + } + }); + return fields[0] + (fields[1] ? ' (' + fields[1] + ')' : ''); + }; + + return function(data) + { + var parsed = []; + $.each(data, function(index, value) + { + var address = value.address; + var row = []; + $.each(parse_format, function(key, format) + { + row.push(parse_field(format, address)); + }); + parsed[index] = { + data: row, + value: address[field_name], + result: address[field_name] + }; + }); + return parsed; + }; + }; + + var request_params = function(id, key, language) + { + return function() { + var result = { + format: 'json', + limit: 5, + addressdetails: 1, + country: window['sessionStorage'] ? sessionStorage['country'] : 'be', + 'accept-language' : language || navigator.language + }; + result[key || id] = $("#"+id).val(); + return result; + } + + }; + + var nominatim = { + + init : function(options) { + + $.each(options.fields, function(key, value) + { + var handle_field_completion = handle_auto_completion(value.dependencies); + $("#" + key).autocomplete(url,{ + max:100, + minChars:3, + delay:500, + formatItem: set_field_values, + type: 'GET', + dataType:'json', + extraParams: request_params(key, value.response && value.response.field, options.language), + parse: create_parser(key, (value.response && value.response.format) || value.dependencies) + }); + $("#" + key).result(handle_field_completion); + }); + } + + }; + + window['nominatim'] = nominatim; + +})(jQuery); \ No newline at end of file