From fc555b48ec0f5600a7f9ae25354ffaca2e0533de Mon Sep 17 00:00:00 2001
From: Thomas Tanghus <thomas@tanghus.net>
Date: Tue, 3 Apr 2012 07:29:47 +0200
Subject: [PATCH] Contacts: Added autocomplete using geonames.org.

---
 apps/contacts/css/contacts.css       |  1 +
 apps/contacts/js/contacts.js         | 88 +++++++++++++++++++++++++++-
 apps/contacts/templates/settings.php |  1 +
 3 files changed, 87 insertions(+), 3 deletions(-)

diff --git a/apps/contacts/css/contacts.css b/apps/contacts/css/contacts.css
index 9d238c36f3..4e7d8c285a 100644
--- a/apps/contacts/css/contacts.css
+++ b/apps/contacts/css/contacts.css
@@ -28,6 +28,7 @@ dl.form { width: 100%; float: left; clear: right; margin: 0; padding: 0; }
 .form dd { display: table-cell; clear: right; float: left; margin: 0; padding: 0px; white-space: nowrap; vertical-align: text-bottom; }
 
 .loading { background: url('../../../core/img/loading.gif') no-repeat center !important; /*cursor: progress; */ cursor: wait; }
+.ui-autocomplete-loading { background: url('../../../core/img/loading.gif') right center no-repeat; }
 .float { float: left; }
 .listactions { height: 1em; width:60px; float: left; clear: right; }
 .add,.edit,.delete,.mail, .globe { cursor: pointer; width: 20px; height: 20px; margin: 0; float: left; position:relative; display: none; }
diff --git a/apps/contacts/js/contacts.js b/apps/contacts/js/contacts.js
index e838d713d4..b72f3c9a67 100644
--- a/apps/contacts/js/contacts.js
+++ b/apps/contacts/js/contacts.js
@@ -908,10 +908,92 @@ Contacts={
 										container.remove();
 									}
 									//Contacts.UI.showHideContactInfo();
-								}/*,
+								},
 								open : function(event, ui) {
-									// load 'ADR' property - maybe :-P
-								}*/
+									$( "#adr_city" ).autocomplete({
+										source: function( request, response ) {
+											$.ajax({
+												url: "http://ws.geonames.org/searchJSON",
+												dataType: "jsonp",
+												data: {
+													featureClass: "P",
+													style: "full",
+													maxRows: 12,
+													name_startsWith: request.term
+												},
+												success: function( data ) {
+													response( $.map( data.geonames, function( item ) {
+														/*for(var key in item) {
+															console.log(key + ': ' + item[key]);
+														}*/
+														return {
+															label: item.name + (item.adminName1 ? ", " + item.adminName1 : "") + ", " + item.countryName,
+															value: item.name,
+															country: item.countryName
+														}
+													}));
+												}
+											});
+										},
+										minLength: 2,
+										select: function( event, ui ) {
+											if(ui.item) {
+												$('#adr_country').val(ui.item.country);
+											}
+											/*log( ui.item ?
+												"Selected: " + ui.item.label :
+												"Nothing selected, input was " + this.value);*/
+										},
+										open: function() {
+											$( this ).removeClass( "ui-corner-all" ).addClass( "ui-corner-top" );
+										},
+										close: function() {
+											$( this ).removeClass( "ui-corner-top" ).addClass( "ui-corner-all" );
+										}
+									});
+									$( "#adr_country" ).autocomplete({
+										source: function( request, response ) {
+											$.ajax({
+												url: "http://ws.geonames.org/searchJSON",
+												dataType: "jsonp",
+												data: {
+													/*featureClass: "A",*/
+													featureCode: "PCLI",
+													/*countryBias: "true",*/
+													/*style: "full",*/
+													maxRows: 12,
+													name_startsWith: request.term
+												},
+												success: function( data ) {
+													response( $.map( data.geonames, function( item ) {
+														for(var key in item) {
+															console.log(key + ': ' + item[key]);
+														}
+														return {
+															label: item.name,
+															value: item.name
+														}
+													}));
+												}
+											});
+										},
+										minLength: 2,
+										select: function( event, ui ) {
+											/*if(ui.item) {
+												$('#adr_country').val(ui.item.country);
+											}
+											log( ui.item ?
+												"Selected: " + ui.item.label :
+												"Nothing selected, input was " + this.value);*/
+										},
+										open: function() {
+											$( this ).removeClass( "ui-corner-all" ).addClass( "ui-corner-top" );
+										},
+										close: function() {
+											$( this ).removeClass( "ui-corner-top" ).addClass( "ui-corner-all" );
+										}
+									});
+								}
 							});
 						} else {
 							alert(jsondata.data.message);
diff --git a/apps/contacts/templates/settings.php b/apps/contacts/templates/settings.php
index f56de0ec8b..d77c4d3802 100644
--- a/apps/contacts/templates/settings.php
+++ b/apps/contacts/templates/settings.php
@@ -9,4 +9,5 @@
 		<dd><code><?php echo OC_Helper::linkToAbsolute('contacts', 'carddav.php'); ?>/principals/<?php echo OC_User::getUser(); ?></code>/</dd>
 		</dl>
 	</fieldset>
+	Powered by <a href="http://geonames.org/" target="_blank">geonames.org webservice</a>
 </form>
-- 
GitLab