Skip to content

Commit

Permalink
Init commit
Browse files Browse the repository at this point in the history
  • Loading branch information
kylekatarnls committed Jan 21, 2016
0 parents commit 79df63d
Show file tree
Hide file tree
Showing 6 changed files with 877 additions and 0 deletions.
674 changes: 674 additions & 0 deletions LICENSE

Large diffs are not rendered by default.

7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# [VICOPO](http://vicopo.selfbuild.fr/)

#### API HTTP et Script pour trouver les villes à partir d'un code postal et code postaux à partir d'une ville

Vicopo est un moyen léger et rapide rechercher une ville française et implémenter des propositions à la volée, l'autocomplétion d'un champ de ville ou de code postal et la conversion de l'un vers l'autre.

http://vicopo.selfbuild.fr/
136 changes: 136 additions & 0 deletions api.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
/**
* Vicopo
* @author https://github.com/kylekatarnls
* http://vicopo.selfbuild.fr
*/
jQuery(function ($) {
var _host = ~(location.protocol + '').indexOf('s')
? 'https://www.selfbuild.fr/vicopo'
: 'http://vicopo.selfbuild.fr';
var _cache = {};
var _sort = function (a, b) {
return a.city - b.city;
};
var _filter = function () {
return true;
};
$.extend({
vicopoSort: function ($sort) {
_sort = $sort;
},
vicopoFilter: function ($filter) {
_filter = $filter;
},
vicopoPrepare: function ($cities) {
$cities = $cities.filter(_filter);
return $cities.sort(_sort);
},
vicopo: function (_input, _done) {
_input = _input.trim();
return this.getVicopo(/^\d+$/.test(_input) ? 'code' : 'city', _input, _done);
},
codePostal: function (_input, _done) {
return this.getVicopo('code', _input, _done);
},
ville: function (_input, _done) {
return this.getVicopo('city', _input, _done);
},
getVicopo: function (_name, _input, _done) {
if(_input.length > 1) {
_input = _input.trim();
_cache[_name] = _cache[_name] || {};
if(_cache[_name][_input]) {
_done(_input, $.vicopoPrepare(_cache[_name][_input] || []), _name);
} else {
var _data = {};
_data[_name] = _input;
return $.getJSON(_host, _data, function (_answear) {
_cache[_name][_input] = _answear.cities;
_done(_answear.input, $.vicopoPrepare(_answear.cities || []), _name);
});
}
} else {
_done(_input, [], _name);
}
}
});
$.fn.extend({
vicopoClean: function () {
return $(this).each(function () {
var _removeList = [];
for(var $next = $(this).next(); $next.hasClass('vicopo-answear'); $next = $next.next()) {
_removeList.push($next[0]);
}
$(_removeList).remove();
});
},
vicopoTargets: function () {
var _targets = [];
$(this).each(function () {
var $target = $(this);
$('[data-vicopo]').each(function () {
if($target.is($(this).data('vicopo'))) {
_targets.push(this);
}
});
});
return $(_targets);
},
vicopoTarget: function () {
return $(this).vicopoTargets().first();
},
getVicopo: function (_method, _done) {
return $(this).keyup(function () {
var $input = $(this);
$[_method]($input.val(), function (_input, _cities, _name) {
if(_input == $input.val()) {
_done(_cities, _name, _input);
}
});
});
},
vicopo: function (_done) {
return $(this).getVicopo('vicopo', _done);
},
codePostal: function (_done) {
return $(this).getVicopo('codePostal', _done);
},
ville: function (_done) {
return $(this).getVicopo('ville', _done);
}
});
var _fields = 'input, textarea, select';
$(document).on('keyup change', _fields, function () {
var $target = $(this);
var _input = $target.val();
if($target.data('vicopo-value') !== _input) {
var _$targets = $target.data('vicopo-value', _input)
.vicopoTargets().each(function () {
var $repeater = $(this).hide().vicopoClean();
});
if(_input.length) {
$.vicopo(_input, function (_check, _cities) {
if(_check === _input) {
_$targets.each(function () {
var $repeater = $(this).vicopoClean();
var _$template = $repeater.clone();
_$template.show().removeAttr('data-vicopo');
var _$cities = [];
$.each(_cities, function () {
var $city = _$template.clone();
$city.addClass('vicopo-answear');
$city.find('[data-vicopo-code-postal]').text(this.code);
$city.find('[data-vicopo-ville]').text(this.city);
$city.find('[data-vicopo-val-code-postal]').val(this.code);
$city.find('[data-vicopo-val-ville]').val(this.city);
_$cities.push($city);
});
$repeater.after(_$cities);
});
}
});
}
}
});
$(_fields).trigger('keyup');
});
6 changes: 6 additions & 0 deletions api.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

26 changes: 26 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
{
"name": "vicopo",
"version": "1.0.3",
"description": "API HTTP et Script pour trouver les villes à partir d'un code postal et code postaux à partir d'une ville",
"main": "slick/slick.js",
"repository": {
"type": "git",
"url": "https://github.com/kylekatarnls/vicopo.git"
},
"keywords": [
"ville",
"code",
"postal",
"code-postal",
"jquery-plugin",
"ecosystem:jquery"
],
"author": "kylekatarnls",
"license": "GNU GPL v3.0",
"bugs": {
"url": "https://github.com/kylekatarnls/vicopo/issues"
},
"dependencies": {
"jquery": ">=1.7.2"
}
}
28 changes: 28 additions & 0 deletions vicopo.jquery.Json
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
{
"name": "vicopo",
"title": "vicopo",
"description": "API HTTP et Script pour trouver les villes à partir d'un code postal et code postaux à partir d'une ville",
"keywords": [
"ville",
"code",
"postal",
"code-postal"
],
"version": "1.0.4",
"author": {
"name": "kylekatarnls",
"url": "https://github.com/kylekatarnls"
},
"licenses": [{
"type": "GNU GPL v3.0",
"url": "https://github.com/kylekatarnls/vicopo/blob/master/LICENSE"
}],
"demo": "http://vicopo.selfbuild.fr/",
"bugs": "https://github.com/kylekatarnls/vicopo/issues",
"homepage": "http://vicopo.selfbuild.fr/",
"docs": "http://vicopo.selfbuild.fr/",
"download": "https://github.com/kylekatarnls/vicopo/archive/master.zip",
"dependencies": {
"jquery": ">=1.7"
}
}

0 comments on commit 79df63d

Please sign in to comment.