|
50 | 50 | (function() {
|
51 | 51 | 'use strict';
|
52 | 52 |
|
53 |
| - var infoWindow = function(Attr2MapOptions, $compile, $timeout, $parse, NgMap) { |
| 53 | + var infoWindow = function(Attr2MapOptions, $compile, $q, $templateRequest, $timeout, $parse, NgMap) { |
54 | 54 | var parser = Attr2MapOptions;
|
55 | 55 |
|
56 | 56 | var getInfoWindow = function(options, events, element) {
|
|
74 | 74 | }
|
75 | 75 |
|
76 | 76 | /**
|
77 |
| - * set template ane template-relate functions |
| 77 | + * set template and template-related functions |
78 | 78 | * it must have a container element with ng-non-bindable
|
79 | 79 | */
|
80 |
| - var template = element.html().trim(); |
81 |
| - if (angular.element(template).length != 1) { |
82 |
| - throw "info-window working as a template must have a container"; |
83 |
| - } |
84 |
| - infoWindow.__template = template.replace(/\s?ng-non-bindable[='"]+/,""); |
| 80 | + var templatePromise = $q(function(resolve) { |
| 81 | + if (angular.isString(element)) { |
| 82 | + $templateRequest(element).then(function (requestedTemplate) { |
| 83 | + resolve(angular.element(requestedTemplate).wrap('<div>').parent()); |
| 84 | + }, function(message) { |
| 85 | + throw "info-window template request failed: " + message; |
| 86 | + }); |
| 87 | + } |
| 88 | + else { |
| 89 | + resolve(element); |
| 90 | + } |
| 91 | + }).then(function(resolvedTemplate) { |
| 92 | + var template = resolvedTemplate.html().trim(); |
| 93 | + if (angular.element(template).length != 1) { |
| 94 | + throw "info-window working as a template must have a container"; |
| 95 | + } |
| 96 | + infoWindow.__template = template.replace(/\s?ng-non-bindable[='"]+/,""); |
| 97 | + }); |
85 | 98 |
|
86 | 99 | infoWindow.__open = function(map, scope, anchor) {
|
87 |
| - $timeout(function() { |
88 |
| - anchor && (scope.anchor = anchor); |
89 |
| - var el = $compile(infoWindow.__template)(scope); |
90 |
| - infoWindow.setContent(el[0]); |
91 |
| - scope.$apply(); |
92 |
| - if (anchor && anchor.getPosition) { |
93 |
| - infoWindow.open(map, anchor); |
94 |
| - } else if (anchor && anchor instanceof google.maps.LatLng) { |
95 |
| - infoWindow.open(map); |
96 |
| - infoWindow.setPosition(anchor); |
97 |
| - } else { |
98 |
| - infoWindow.open(map); |
99 |
| - } |
100 |
| - var infoWindowContainerEl = infoWindow.content.parentElement.parentElement.parentElement; |
101 |
| - infoWindowContainerEl.className = "ng-map-info-window"; |
| 100 | + templatePromise.then(function() { |
| 101 | + $timeout(function() { |
| 102 | + anchor && (scope.anchor = anchor); |
| 103 | + var el = $compile(infoWindow.__template)(scope); |
| 104 | + infoWindow.setContent(el[0]); |
| 105 | + scope.$apply(); |
| 106 | + if (anchor && anchor.getPosition) { |
| 107 | + infoWindow.open(map, anchor); |
| 108 | + } else if (anchor && anchor instanceof google.maps.LatLng) { |
| 109 | + infoWindow.open(map); |
| 110 | + infoWindow.setPosition(anchor); |
| 111 | + } else { |
| 112 | + infoWindow.open(map); |
| 113 | + } |
| 114 | + var infoWindowContainerEl = infoWindow.content.parentElement.parentElement.parentElement; |
| 115 | + infoWindowContainerEl.className = "ng-map-info-window"; |
| 116 | + }); |
102 | 117 | });
|
103 | 118 | };
|
104 | 119 |
|
|
115 | 130 | var options = parser.getOptions(filtered, {scope: scope});
|
116 | 131 | var events = parser.getEvents(scope, filtered);
|
117 | 132 |
|
| 133 | + var infoWindow = getInfoWindow(options, events, options.template || element); |
118 | 134 | var address;
|
119 | 135 | if (options.position && !(options.position instanceof google.maps.LatLng)) {
|
120 | 136 | address = options.position;
|
121 | 137 | }
|
122 |
| - var infoWindow = getInfoWindow(options, events, element); |
123 | 138 | if (address) {
|
124 | 139 | NgMap.getGeoLocation(address).then(function(latlng) {
|
125 | 140 | infoWindow.setPosition(latlng);
|
|
132 | 147 | mapController.addObject('infoWindows', infoWindow);
|
133 | 148 | mapController.observeAttrSetObj(orgAttrs, attrs, infoWindow);
|
134 | 149 |
|
135 |
| - mapController.showInfoWindow = |
| 150 | + mapController.showInfoWindow = |
136 | 151 | mapController.map.showInfoWindow = mapController.showInfoWindow ||
|
137 | 152 | function(p1, p2, p3) { //event, id, marker
|
138 | 153 | var id = typeof p1 == 'string' ? p1 : p2;
|
|
194 | 209 |
|
195 | 210 | }; // infoWindow
|
196 | 211 | infoWindow.$inject =
|
197 |
| - ['Attr2MapOptions', '$compile', '$timeout', '$parse', 'NgMap']; |
| 212 | + ['Attr2MapOptions', '$compile', '$q', '$templateRequest', '$timeout', '$parse', 'NgMap']; |
198 | 213 |
|
199 | 214 | angular.module('ngMap').directive('infoWindow', infoWindow);
|
200 | 215 | })();
|
0 commit comments