Advertisement
Guest User

To

a guest
Aug 28th, 2014
181
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.     var Location = function() { };
  2.     Location.prototype = {
  3.         fill: function(item) {
  4.             if (!Location.isStorableData(item)) {
  5.                 return;
  6.             }
  7.             var geoname = {
  8.                 labels: [],
  9.                 value: item[this.store]
  10.             };
  11.  
  12.             for(var i = 0; i < this.store.length; i++) {
  13.                 this._mapItemValues(geoname, item, i);
  14.             }
  15.             geoname.label = geoname.labels.join(" - ");
  16.  
  17.             return geoname;
  18.         },
  19.         _mapItemValues: function(geoname, item, index) {
  20.             geoname[this.store[index]] = item[this.store[index]];
  21.             geoname.labels.push(item[this.labels[index]]);
  22.         }
  23.     };
  24.     Location.isStorableData = function(item) {
  25.         return item.fclass === City.code || item.fcode === State.code || item.fcode === Country.code;
  26.     };
  27.     Location.fill = function(item) {
  28.         return new City().fill(item) || new State().fill(item) || new Country().fill(item);
  29.     };
  30.  
  31.     var City = function() {
  32.         this.labels = ["countryName", "toponymName", "name"];
  33.         this.store = ["countryCode", "adminCode1", "name"];
  34.         this.value = "name";
  35.         this.code = "P";
  36.     };
  37.     City.prototype = new Location();
  38.  
  39.     var State = function() {
  40.         this.labels = ["countryName", "toponymName"];
  41.         this.store = ["countryCode", "adminCode1"];
  42.         this.value = "adminCode1";
  43.         this.code = "ADM1";
  44.     };
  45.     State.prototype = new Location();
  46.  
  47.     var Country = function() {
  48.         this.labels = ["countryName"];
  49.         this.store = ["countryCode"];
  50.         this.value = "countryCode";
  51.         this.code = "PCLI";
  52.     };
  53.     Country.prototype = new Location();
  54.  
  55.     //
  56.     // Filter
  57.     //
  58.     var LocationAutocompleteFilter = function($el) {
  59.         this.$el = $el;
  60.     };
  61.     LocationAutocompleteFilter.prototype = {
  62.         start: function(selectCallback) {
  63.             var self = this;
  64.             this.$el.autocomplete({
  65.                 minLength: 3,
  66.                 source: function(request, response) {
  67.                     self.remoteSource(request.term, response);
  68.                 },
  69.                 open: function() {
  70.                     $(this).removeClass("ui-corner-all").addClass("ui-corner-top");
  71.                 },
  72.                 focus: function(event, ui) {
  73.                     self.$el.val(ui.item.label);
  74.                     return false;
  75.                 },
  76.                 select: function(event, ui) {
  77.                     self.fillDOMWith(ui.item.label, {
  78.                         city: ui.item.name,
  79.                         state: ui.item.adminCode1,
  80.                         country: ui.item.countryCode
  81.                     });
  82.                     if (selectCallback) {
  83.                         selectCallback();
  84.                     }
  85.                     return false;
  86.                 },
  87.                 change: function() {
  88.                     if (self.$el.val() === "") {
  89.                         self.fillDOMWith("");
  90.                     }
  91.                     return false;
  92.                 },
  93.                 close: function() {
  94.                     $(this).removeClass("ui-corner-top").addClass("ui-corner-all");
  95.                 }
  96.             });
  97.         },
  98.         remoteSource: function(searchTerm, fillWith) {
  99.             $.ajax({
  100.                 url: "//ws.geonames.org/searchJSON",
  101.                 dataType: "jsonp",
  102.                 data: {
  103.                     maxRows: 15,
  104.                     q: searchTerm,
  105.                     username: this.$el.data('username'),
  106.                     lang: this.$el.data('locale')
  107.                 },
  108.                 success: function(data) {
  109.                     fillWith( $.map(data.geonames, function(item) {
  110.                         return Location.fill(item);
  111.                     }) );
  112.                 }
  113.             });
  114.         },
  115.         fillDOMWith: function(elValue, siblingsValues) {
  116.             this.$el.val(elValue);
  117.             this.$el.siblings("[name='group[city]']").val(siblingsValues.city || elValue);
  118.             this.$el.siblings("[name='group[state]']").val(siblingsValues.state || elValue);
  119.             this.$el.siblings("[name='group[country]']").val(siblingsValues.country || elValue);
  120.         }
  121.     };
  122.  
  123.     window.LocationAutocompleteFilter = LocationAutocompleteFilter;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement