PageRenderTime 29ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/ajax/libs/typeahead-addresspicker/0.1.2/typeahead-addresspicker.js

https://gitlab.com/Mirros/cdnjs
JavaScript | 230 lines | 205 code | 25 blank | 0 comment | 36 complexity | 29ebb393e9e7738c00b6b9dd403854de MD5 | raw file
  1. (function() {
  2. var __bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; },
  3. __hasProp = {}.hasOwnProperty,
  4. __extends = function(child, parent) { for (var key in parent) { if (__hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor(); child.__super__ = parent.prototype; return child; };
  5. (function($) {
  6. this.AddressPickerResult = (function() {
  7. function AddressPickerResult(placeResult, fromReverseGeocoding) {
  8. this.placeResult = placeResult;
  9. this.fromReverseGeocoding = fromReverseGeocoding != null ? fromReverseGeocoding : false;
  10. this.latitude = this.placeResult.geometry.location.lat();
  11. this.longitude = this.placeResult.geometry.location.lng();
  12. }
  13. AddressPickerResult.prototype.addressTypes = function() {
  14. var component, type, types, _i, _j, _len, _len1, _ref, _ref1;
  15. types = [];
  16. _ref = this.addressComponents();
  17. for (_i = 0, _len = _ref.length; _i < _len; _i++) {
  18. component = _ref[_i];
  19. _ref1 = component.types;
  20. for (_j = 0, _len1 = _ref1.length; _j < _len1; _j++) {
  21. type = _ref1[_j];
  22. if (types.indexOf(type) === -1) {
  23. types.push(type);
  24. }
  25. }
  26. }
  27. return types;
  28. };
  29. AddressPickerResult.prototype.addressComponents = function() {
  30. return this.placeResult.address_components || [];
  31. };
  32. AddressPickerResult.prototype.address = function() {
  33. return this.placeResult.formatted_address;
  34. };
  35. AddressPickerResult.prototype.nameForType = function(type, shortName) {
  36. var component, _i, _len, _ref;
  37. if (shortName == null) {
  38. shortName = false;
  39. }
  40. _ref = this.addressComponents();
  41. for (_i = 0, _len = _ref.length; _i < _len; _i++) {
  42. component = _ref[_i];
  43. if (component.types.indexOf(type) !== -1) {
  44. return (shortName ? component.short_name : component.long_name);
  45. }
  46. }
  47. return null;
  48. };
  49. AddressPickerResult.prototype.lat = function() {
  50. return this.latitude;
  51. };
  52. AddressPickerResult.prototype.lng = function() {
  53. return this.longitude;
  54. };
  55. AddressPickerResult.prototype.setLatLng = function(latitude, longitude) {
  56. this.latitude = latitude;
  57. this.longitude = longitude;
  58. };
  59. AddressPickerResult.prototype.isAccurate = function() {
  60. return !this.placeResult.geometry.viewport;
  61. };
  62. AddressPickerResult.prototype.isReverseGeocoding = function() {
  63. return this.fromReverseGeocoding;
  64. };
  65. return AddressPickerResult;
  66. })();
  67. return this.AddressPicker = (function(_super) {
  68. __extends(AddressPicker, _super);
  69. function AddressPicker(options) {
  70. if (options == null) {
  71. options = {};
  72. }
  73. this.markerDragged = __bind(this.markerDragged, this);
  74. this.updateBoundsForPlace = __bind(this.updateBoundsForPlace, this);
  75. this.updateMap = __bind(this.updateMap, this);
  76. this.options = $.extend({
  77. local: [],
  78. datumTokenizer: function(d) {
  79. return Bloodhound.tokenizers.whitespace(d.num);
  80. },
  81. queryTokenizer: Bloodhound.tokenizers.whitespace,
  82. autocompleteService: {
  83. types: ["geocode"]
  84. },
  85. zoomForLocation: 16,
  86. reverseGeocoding: false,
  87. placeDetails: true
  88. }, options);
  89. AddressPicker.__super__.constructor.call(this, this.options);
  90. if (this.options.map) {
  91. this.initMap();
  92. }
  93. this.placeService = new google.maps.places.PlacesService(document.createElement('div'));
  94. }
  95. AddressPicker.prototype.bindDefaultTypeaheadEvent = function(typeahead) {
  96. typeahead.bind("typeahead:selected", this.updateMap);
  97. return typeahead.bind("typeahead:cursorchanged", this.updateMap);
  98. };
  99. AddressPicker.prototype.initMap = function() {
  100. var markerOptions, _ref, _ref1;
  101. if ((_ref = this.options) != null ? (_ref1 = _ref.map) != null ? _ref1.gmap : void 0 : void 0) {
  102. this.map = this.options.map.gmap;
  103. } else {
  104. this.mapOptions = $.extend({
  105. zoom: 3,
  106. center: new google.maps.LatLng(0, 0),
  107. mapTypeId: google.maps.MapTypeId.ROADMAP,
  108. boundsForLocation: this.updateBoundsForPlace
  109. }, this.options.map);
  110. this.map = new google.maps.Map($(this.mapOptions.id)[0], this.mapOptions);
  111. }
  112. this.lastResult = null;
  113. markerOptions = $.extend({
  114. draggable: true,
  115. visible: false,
  116. position: this.map.getCenter(),
  117. map: this.map
  118. }, this.options.marker || {});
  119. this.marker = new google.maps.Marker(markerOptions);
  120. if (markerOptions.draggable) {
  121. return google.maps.event.addListener(this.marker, 'dragend', this.markerDragged);
  122. }
  123. };
  124. AddressPicker.prototype.get = function(query, cb) {
  125. var service;
  126. service = new google.maps.places.AutocompleteService();
  127. this.options.autocompleteService.input = query;
  128. return service.getPlacePredictions(this.options.autocompleteService, (function(_this) {
  129. return function(predictions) {
  130. $(_this).trigger('addresspicker:predictions', [predictions]);
  131. return cb(predictions);
  132. };
  133. })(this));
  134. };
  135. AddressPicker.prototype.updateMap = function(event, place) {
  136. if (this.placeDetails) {
  137. return this.placeService.getDetails(place, (function(_this) {
  138. return function(response) {
  139. var _ref;
  140. _this.lastResult = new AddressPickerResult(response);
  141. if (_this.marker) {
  142. _this.marker.setPosition(response.geometry.location);
  143. _this.marker.setVisible(true);
  144. }
  145. if (_this.map) {
  146. if ((_ref = _this.mapOptions) != null) {
  147. _ref.boundsForLocation(response);
  148. }
  149. }
  150. return $(_this).trigger('addresspicker:selected', _this.lastResult);
  151. };
  152. })(this));
  153. } else {
  154. return $(this).trigger('addresspicker:selected', place);
  155. }
  156. };
  157. AddressPicker.prototype.updateBoundsForPlace = function(response) {
  158. if (response.geometry.viewport) {
  159. return this.map.fitBounds(response.geometry.viewport);
  160. } else {
  161. this.map.setCenter(response.geometry.location);
  162. return this.map.setZoom(this.options.zoomForLocation);
  163. }
  164. };
  165. AddressPicker.prototype.markerDragged = function() {
  166. if (this.options.reverseGeocoding) {
  167. return this.reverseGeocode(this.marker.getPosition());
  168. } else {
  169. if (this.lastResult) {
  170. this.lastResult.setLatLng(this.marker.getPosition().lat(), this.marker.getPosition().lng());
  171. } else {
  172. this.lastResult = new AddressPickerResult({
  173. geometry: {
  174. location: this.marker.getPosition()
  175. }
  176. });
  177. }
  178. return $(this).trigger('addresspicker:selected', this.lastResult);
  179. }
  180. };
  181. AddressPicker.prototype.reverseGeocode = function(position) {
  182. if (this.geocoder == null) {
  183. this.geocoder = new google.maps.Geocoder();
  184. }
  185. return this.geocoder.geocode({
  186. location: position
  187. }, (function(_this) {
  188. return function(results) {
  189. if (results && results.length > 0) {
  190. _this.lastResult = new AddressPickerResult(results[0], true);
  191. return $(_this).trigger('addresspicker:selected', _this.lastResult);
  192. }
  193. };
  194. })(this));
  195. };
  196. AddressPicker.prototype.getGMap = function() {
  197. return this.map;
  198. };
  199. AddressPicker.prototype.getGMarker = function() {
  200. return this.marker;
  201. };
  202. return AddressPicker;
  203. })(Bloodhound);
  204. })(jQuery);
  205. }).call(this);