Source: map-google-maps/src/main/resources/META-INF/resources/js/GoogleMapsSearch.es.js

  1. /**
  2. * Copyright (c) 2000-present Liferay, Inc. All rights reserved.
  3. *
  4. * This library is free software; you can redistribute it and/or modify it under
  5. * the terms of the GNU Lesser General Public License as published by the Free
  6. * Software Foundation; either version 2.1 of the License, or (at your option)
  7. * any later version.
  8. *
  9. * This library is distributed in the hope that it will be useful, but WITHOUT
  10. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  11. * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
  12. * details.
  13. */
  14. import {isInputNode} from 'map-common/js/validators.es';
  15. import State, {Config} from 'metal-state';
  16. /**
  17. * GoogleMapsSearch
  18. * @review
  19. */
  20. class GoogleMapsSearch extends State {
  21. /**
  22. * Creates a new search handler using Google Map's API
  23. * @param {Array} args List of arguments to be passed to State
  24. * @review
  25. */
  26. constructor(...args) {
  27. super(...args);
  28. const inputNode = this.inputNode;
  29. this._handlePlaceChanged = this._handlePlaceChanged.bind(this);
  30. this._autocomplete = new google.maps.places.Autocomplete(inputNode);
  31. this._bindUI();
  32. }
  33. /**
  34. * Removes the listeners that have been added to the search input.
  35. * @review
  36. */
  37. destructor() {
  38. this._eventHandlers.forEach((item) => {
  39. google.maps.event.removeListener(item);
  40. });
  41. }
  42. /**
  43. * Adds listeners for the created map object.
  44. * It listens for a custom 'place_changed' event and executes
  45. * GoogleMapsSearch._handlePlaceChanged.
  46. * @protected
  47. * @review
  48. */
  49. _bindUI() {
  50. this._eventHandlers = [
  51. google.maps.event.addListener(
  52. this._autocomplete,
  53. 'place_changed',
  54. this._handlePlaceChanged
  55. ),
  56. google.maps.event.addDomListener(
  57. this.inputNode,
  58. 'keydown',
  59. (event) => {
  60. if (event.keyCode === 13) {
  61. event.preventDefault();
  62. }
  63. }
  64. ),
  65. ];
  66. }
  67. /**
  68. * Gets the new place that has been processed by Google Maps and emits a
  69. * 'search' event with the location information and the address.
  70. * @protected
  71. * @review
  72. */
  73. _handlePlaceChanged() {
  74. const place = this._autocomplete.getPlace();
  75. if (place && typeof place === 'object' && place.geometry) {
  76. const geolocation = place.geometry.location;
  77. this.emit('search', {
  78. position: {
  79. address: place.formatted_address,
  80. location: {
  81. lat: geolocation.lat(),
  82. lng: geolocation.lng(),
  83. },
  84. },
  85. });
  86. }
  87. }
  88. }
  89. /**
  90. * State definition.
  91. * @review
  92. * @static
  93. * @type {!Object}
  94. */
  95. GoogleMapsSearch.STATE = {
  96. /**
  97. * Input element that will be used for searching addresses.
  98. * @review
  99. * @type {HTMLInputElement}
  100. */
  101. inputNode: Config.validator(isInputNode).value(null),
  102. };
  103. export default GoogleMapsSearch;
  104. export {GoogleMapsSearch};