Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- var Location = function() { };
- Location.prototype = {
- fill: function(item) {
- if (!Location.isStorableData(item)) {
- return;
- }
- var geoname = {
- labels: [],
- value: item[this.store]
- };
- for(var i = 0; i < this.store.length; i++) {
- this._mapItemValues(geoname, item, i);
- }
- geoname.label = geoname.labels.join(" - ");
- return geoname;
- },
- _mapItemValues: function(geoname, item, index) {
- geoname[this.store[index]] = item[this.store[index]];
- geoname.labels.push(item[this.labels[index]]);
- }
- };
- Location.isStorableData = function(item) {
- return item.fclass === City.code || item.fcode === State.code || item.fcode === Country.code;
- };
- Location.fill = function(item) {
- return new City().fill(item) || new State().fill(item) || new Country().fill(item);
- };
- var City = function() {
- this.labels = ["countryName", "toponymName", "name"];
- this.store = ["countryCode", "adminCode1", "name"];
- this.value = "name";
- this.code = "P";
- };
- City.prototype = new Location();
- var State = function() {
- this.labels = ["countryName", "toponymName"];
- this.store = ["countryCode", "adminCode1"];
- this.value = "adminCode1";
- this.code = "ADM1";
- };
- State.prototype = new Location();
- var Country = function() {
- this.labels = ["countryName"];
- this.store = ["countryCode"];
- this.value = "countryCode";
- this.code = "PCLI";
- };
- Country.prototype = new Location();
- //
- // Filter
- //
- var LocationAutocompleteFilter = function($el) {
- this.$el = $el;
- };
- LocationAutocompleteFilter.prototype = {
- start: function(selectCallback) {
- var self = this;
- this.$el.autocomplete({
- minLength: 3,
- source: function(request, response) {
- self.remoteSource(request.term, response);
- },
- open: function() {
- $(this).removeClass("ui-corner-all").addClass("ui-corner-top");
- },
- focus: function(event, ui) {
- self.$el.val(ui.item.label);
- return false;
- },
- select: function(event, ui) {
- self.fillDOMWith(ui.item.label, {
- city: ui.item.name,
- state: ui.item.adminCode1,
- country: ui.item.countryCode
- });
- if (selectCallback) {
- selectCallback();
- }
- return false;
- },
- change: function() {
- if (self.$el.val() === "") {
- self.fillDOMWith("");
- }
- return false;
- },
- close: function() {
- $(this).removeClass("ui-corner-top").addClass("ui-corner-all");
- }
- });
- },
- remoteSource: function(searchTerm, fillWith) {
- $.ajax({
- url: "//ws.geonames.org/searchJSON",
- dataType: "jsonp",
- data: {
- maxRows: 15,
- q: searchTerm,
- username: this.$el.data('username'),
- lang: this.$el.data('locale')
- },
- success: function(data) {
- fillWith( $.map(data.geonames, function(item) {
- return Location.fill(item);
- }) );
- }
- });
- },
- fillDOMWith: function(elValue, siblingsValues) {
- this.$el.val(elValue);
- this.$el.siblings("[name='group[city]']").val(siblingsValues.city || elValue);
- this.$el.siblings("[name='group[state]']").val(siblingsValues.state || elValue);
- this.$el.siblings("[name='group[country]']").val(siblingsValues.country || elValue);
- }
- };
- window.LocationAutocompleteFilter = LocationAutocompleteFilter;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement