info-window.js.html 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="utf-8">
  5. <title>JSDoc: Source: directives/info-window.js</title>
  6. <script src="scripts/prettify/prettify.js"> </script>
  7. <script src="scripts/prettify/lang-css.js"> </script>
  8. <!--[if lt IE 9]>
  9. <script src="//html5shiv.googlecode.com/svn/trunk/html5.js"></script>
  10. <![endif]-->
  11. <link type="text/css" rel="stylesheet" href="styles/prettify-tomorrow.css">
  12. <link type="text/css" rel="stylesheet" href="styles/jsdoc-default.css">
  13. </head>
  14. <body>
  15. <div id="main">
  16. <h1 class="page-title">Source: directives/info-window.js</h1>
  17. <section>
  18. <article>
  19. <pre class="prettyprint source linenums"><code>/*jshint -W030*/
  20. /**
  21. * @ngdoc directive
  22. * @name info-window
  23. * @requires Attr2Options
  24. * @requires $compile
  25. * @description
  26. * Defines infoWindow and provides compile method
  27. *
  28. * Requires: map directive
  29. *
  30. * Restrict To: Element
  31. *
  32. * @param {Boolean} visible Indicates to show it when map is initialized
  33. * @param {Boolean} visible-on-marker Indicates to show it on a marker when map is initialized
  34. * @param {String} &amp;lt;InfoWindowOption> Any InfoWindow options,
  35. * https://developers.google.com/maps/documentation/javascript/reference?csw=1#InfoWindowOptions
  36. * @param {String} &amp;lt;InfoWindowEvent> Any InfoWindow events, https://developers.google.com/maps/documentation/javascript/reference
  37. * @example
  38. * Usage:
  39. * &lt;map MAP_ATTRIBUTES>
  40. * &lt;info-window id="foo" ANY_OPTIONS ANY_EVENTS">&lt;/info-window>
  41. * &lt;/map>
  42. *
  43. * Example:
  44. * &lt;map center="41.850033,-87.6500523" zoom="3">
  45. * &lt;info-window id="1" position="41.850033,-87.6500523" >
  46. * &lt;div ng-non-bindable>
  47. * Chicago, IL&lt;br/>
  48. * LatLng: {{chicago.lat()}}, {{chicago.lng()}}, &lt;br/>
  49. * World Coordinate: {{worldCoordinate.x}}, {{worldCoordinate.y}}, &lt;br/>
  50. * Pixel Coordinate: {{pixelCoordinate.x}}, {{pixelCoordinate.y}}, &lt;br/>
  51. * Tile Coordinate: {{tileCoordinate.x}}, {{tileCoordinate.y}} at Zoom Level {{map.getZoom()}}
  52. * &lt;/div>
  53. * &lt;/info-window>
  54. * &lt;/map>
  55. */
  56. ngMap.directive('infoWindow', ['Attr2Options', '$compile', '$timeout', function(Attr2Options, $compile, $timeout) {
  57. var parser = Attr2Options;
  58. var getInfoWindow = function(options, events, element) {
  59. var infoWindow;
  60. /**
  61. * set options
  62. */
  63. if (options.position &amp;&amp;
  64. !(options.position instanceof google.maps.LatLng)) {
  65. var address = options.position;
  66. delete options.position;
  67. infoWindow = new google.maps.InfoWindow(options);
  68. var callback = function() {
  69. infoWindow.open(infoWindow.map);
  70. }
  71. parser.setDelayedGeoLocation(infoWindow, 'setPosition', address, {callback: callback});
  72. } else {
  73. infoWindow = new google.maps.InfoWindow(options);
  74. }
  75. /**
  76. * set events
  77. */
  78. if (Object.keys(events).length > 0) {
  79. console.log("infoWindow events", events);
  80. }
  81. for (var eventName in events) {
  82. if (eventName) {
  83. google.maps.event.addListener(infoWindow, eventName, events[eventName]);
  84. }
  85. }
  86. /**
  87. * set template ane template-relate functions
  88. * it must have a container element with ng-non-bindable
  89. */
  90. var template = element.html().trim();
  91. if (angular.element(template).length != 1) {
  92. throw "info-window working as a template must have a container";
  93. }
  94. infoWindow.__template = template.replace(/\s?ng-non-bindable[='"]+/,"");
  95. infoWindow.__compile = function(scope) {
  96. var el = $compile(infoWindow.__template)(scope);
  97. scope.$apply();
  98. infoWindow.setContent(el[0]);
  99. };
  100. infoWindow.__eval = function(event) {
  101. var template = infoWindow.__template;
  102. var _this = this;
  103. template = template.replace(/{{(event|this)[^;\}]+}}/g, function(match) {
  104. var expression = match.replace(/[{}]/g, "").replace("this.", "_this.");
  105. return eval(expression);
  106. });
  107. return template;
  108. };
  109. return infoWindow;
  110. };
  111. return {
  112. restrict: 'E',
  113. require: '^map',
  114. link: function(scope, element, attrs, mapController) {
  115. element.css('display','none');
  116. var orgAttrs = parser.orgAttributes(element);
  117. var filtered = parser.filter(attrs);
  118. var options = parser.getOptions(filtered, scope);
  119. var events = parser.getEvents(scope, filtered);
  120. console.log('infoWindow', 'options', options, 'events', events);
  121. var infoWindow = getInfoWindow(options, events, element);
  122. mapController.addObject('infoWindows', infoWindow);
  123. parser.observeAttrSetObj(orgAttrs, attrs, infoWindow); /* observers */
  124. // show InfoWindow when initialized
  125. if (infoWindow.visible) {
  126. //if (!infoWindow.position) { throw "Invalid position"; }
  127. scope.$on('mapInitialized', function(evt, map) {
  128. $timeout(function() {
  129. infoWindow.__template = infoWindow.__eval.apply(this, [evt]);
  130. infoWindow.__compile(scope);
  131. infoWindow.map = map;
  132. infoWindow.position &amp;&amp; infoWindow.open(map);
  133. });
  134. });
  135. }
  136. // show InfoWindow on a marker when initialized
  137. if (infoWindow.visibleOnMarker) {
  138. scope.$on('mapInitialized', function(evt, map) {
  139. $timeout(function() {
  140. var markerId = infoWindow.visibleOnMarker;
  141. var marker = map.markers[markerId];
  142. if (!marker) throw "Invalid marker id";
  143. infoWindow.__template = infoWindow.__eval.apply(this, [evt]);
  144. infoWindow.__compile(scope);
  145. infoWindow.open(map, marker);
  146. });
  147. });
  148. }
  149. /**
  150. * provide showInfoWindow method to scope
  151. */
  152. scope.showInfoWindow = scope.showInfoWindow ||
  153. function(event, id, anchor) {
  154. var infoWindow = mapController.map.infoWindows[id],
  155. tempTemplate = infoWindow.__template; // set template in a temporary variable
  156. infoWindow.__template = infoWindow.__eval.apply(this, [event]);
  157. infoWindow.__compile(scope);
  158. if (anchor) {
  159. infoWindow.open(mapController.map, anchor);
  160. } else if (this.getPosition) {
  161. infoWindow.open(mapController.map, this);
  162. } else {
  163. infoWindow.open(mapController.map);
  164. }
  165. infoWindow.__template = tempTemplate; // reset template to the object
  166. };
  167. /**
  168. * provide hideInfoWindow method to scope
  169. */
  170. scope.hideInfoWindow = scope.hideInfoWindow ||
  171. function(event, id, anchor) {
  172. var infoWindow = mapController.map.infoWindows[id];
  173. infoWindow.__template = infoWindow.__eval.apply(this, [event]);
  174. infoWindow.__compile(scope);
  175. infoWindow.close();
  176. };
  177. } //link
  178. }; // return
  179. }]);// function
  180. </code></pre>
  181. </article>
  182. </section>
  183. </div>
  184. <nav>
  185. <h2><a href="index.html">Index</a></h2><h3>service</h3><ul><li><a href="Attr2Options.html">Attr2Options</a></li><li><a href="GeoCoder.html">GeoCoder</a></li><li><a href="NavigatorGeolocation.html">NavigatorGeolocation</a></li><li><a href="StreetView.html">StreetView</a></li></ul><h3>directive</h3><ul><li><a href="bicycling-layer.html">bicycling-layer</a></li><li><a href="cloud-layer.html">cloud-layer</a></li><li><a href="custom-control.html">custom-control</a></li><li><a href="drawing-manager.html">drawing-manager</a></li><li><a href="dynamic-maps-engine-layer.html">dynamic-maps-engine-layer</a></li><li><a href="fusion-tables-layer.html">fusion-tables-layer</a></li><li><a href="heatmap-layer.html">heatmap-layer</a></li><li><a href="info-window.html">info-window</a></li><li><a href="kml-layer.html">kml-layer</a></li><li><a href="lazy-load.html">lazy-load</a></li><li><a href="map.html">map</a></li><li><a href="map-data.html">map-data</a></li><li><a href="map-type.html">map-type</a></li><li><a href="MapController.html">MapController</a></li><li><a href="maps-engine-layer.html">maps-engine-layer</a></li><li><a href="marker.html">marker</a></li><li><a href="overlay-map-type.html">overlay-map-type</a></li><li><a href="shape.html">shape</a></li><li><a href="traffic-layer.html">traffic-layer</a></li><li><a href="transit-layer.html">transit-layer</a></li><li><a href="weather-layer.html">weather-layer</a></li></ul>
  186. </nav>
  187. <br clear="both">
  188. <footer>
  189. Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.3.0-alpha9</a>
  190. and <a href="https://github.com/allenhwkim/angular-jsdoc">angular-jsdoc</a>
  191. </footer>
  192. <script> prettyPrint(); </script>
  193. <script src="scripts/linenumber.js"> </script>
  194. <script>
  195. var href=window.location.href.match(/\/([^\/]+$)/)[1];
  196. document.querySelector("nav a[href='"+href+"']").scrollIntoView(true);
  197. if (window.location.hash == "")
  198. document.querySelector("body").scrollIntoView(true);
  199. </script>
  200. </body>
  201. </html>