mirror of
https://github.com/opensourcepos/opensourcepos.git
synced 2026-01-08 01:17:53 -05:00
Externalize nominatim autcomplete functionality
Add chosen language to nominatim request
This commit is contained in:
@@ -20,6 +20,7 @@
|
||||
<script src="<?php echo base_url();?>js/jquery.autocomplete.js" type="text/javascript" language="javascript" charset="UTF-8"></script>
|
||||
<script src="<?php echo base_url();?>js/jquery.validate-1.13.1-min.js" type="text/javascript" language="javascript" charset="UTF-8"></script>
|
||||
<script src="<?php echo base_url();?>js/jquery.jkey-1.1.js" type="text/javascript" language="javascript" charset="UTF-8"></script>
|
||||
<script src="<?php echo base_url();?>js/nominatim.autocomplete.js" type="text/javascript" language="javascript" charset="UTF-8"></script>
|
||||
<script src="<?php echo base_url();?>js/thickbox.js" type="text/javascript" language="javascript" charset="UTF-8"></script>
|
||||
<script src="<?php echo base_url();?>js/common.js" type="text/javascript" language="javascript" charset="UTF-8"></script>
|
||||
<script src="<?php echo base_url();?>js/manage_tables.js" type="text/javascript" language="javascript" charset="UTF-8"></script>
|
||||
|
||||
@@ -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 : '<?php echo $this->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"]));
|
||||
|
||||
});
|
||||
</script>
|
||||
@@ -10,17 +10,6 @@ $(document).ready(function()
|
||||
enable_delete('<?php echo $this->lang->line($controller_name."_confirm_delete")?>','<?php echo $this->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
|
||||
|
||||
@@ -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(){
|
||||
|
||||
115
js/nominatim.autocomplete.js
Normal file
115
js/nominatim.autocomplete.js
Normal file
@@ -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);
|
||||
Reference in New Issue
Block a user