|
26 | 26 | */
|
27 | 27 | (function() {
|
28 | 28 | 'use strict';
|
29 |
| - var parser, $timeout; |
30 |
| - |
31 |
| - var cAbortEvent = function (e) { |
32 |
| - e.preventDefault && e.preventDefault(); |
33 |
| - e.cancelBubble = true; |
34 |
| - e.stopPropagation && e.stopPropagation(); |
35 |
| - }; |
| 29 | + var parser, $timeout, $compile; |
36 | 30 |
|
37 | 31 | var CustomMarker = function(options) {
|
38 | 32 | options = options || {};
|
39 | 33 |
|
40 | 34 | this.el = document.createElement('div');
|
41 | 35 | this.el.style.display = 'inline-block';
|
42 | 36 | this.visible = true;
|
43 |
| - for (var key in options) { |
| 37 | + for (var key in options) { /* jshint ignore:line */ |
44 | 38 | this[key] = options[key];
|
45 | 39 | }
|
46 | 40 | };
|
|
49 | 43 |
|
50 | 44 | CustomMarker.prototype = new google.maps.OverlayView();
|
51 | 45 |
|
52 |
| - CustomMarker.prototype.setContent = function(html) { |
53 |
| - this.el.innerHTML = this.content = html; |
| 46 | + CustomMarker.prototype.setContent = function(html, scope) { |
| 47 | + this.el.innerHTML = html; |
54 | 48 | this.el.style.position = 'absolute';
|
| 49 | + if (scope) { |
| 50 | + $compile(angular.element(this.el).contents())(scope); |
| 51 | + } |
55 | 52 | };
|
56 | 53 |
|
57 | 54 | CustomMarker.prototype.setPosition = function(position) {
|
58 |
| - position && (this.position = position); |
| 55 | + position && (this.position = position); /* jshint ignore:line */ |
59 | 56 | if (this.getProjection() && typeof this.position.lng == 'function') {
|
60 | 57 | var posPixel = this.getProjection().fromLatLngToDivPixel(this.position);
|
61 | 58 | var x = Math.round(posPixel.x - (this.el.offsetWidth/2));
|
|
66 | 63 | };
|
67 | 64 |
|
68 | 65 | CustomMarker.prototype.setZIndex = function(zIndex) {
|
69 |
| - zIndex && (this.zIndex = zIndex); |
| 66 | + zIndex && (this.zIndex = zIndex); /* jshint ignore:line */ |
70 | 67 | this.el.style.zIndex = this.zIndex;
|
71 | 68 | };
|
72 | 69 |
|
|
77 | 74 |
|
78 | 75 | CustomMarker.prototype.addClass = function(className) {
|
79 | 76 | var classNames = this.el.className.trim().split(' ');
|
80 |
| - (classNames.indexOf(className) == -1) && classNames.push(className); |
| 77 | + (classNames.indexOf(className) == -1) && classNames.push(className); /* jshint ignore:line */ |
81 | 78 | this.el.className = classNames.join(' ');
|
82 | 79 | };
|
83 | 80 |
|
84 | 81 | CustomMarker.prototype.removeClass = function(className) {
|
85 | 82 | var classNames = this.el.className.split(' ');
|
86 | 83 | var index = classNames.indexOf(className);
|
87 |
| - (index > -1) && classNames.splice(index, 1); |
| 84 | + (index > -1) && classNames.splice(index, 1); /* jshint ignore:line */ |
88 | 85 | this.el.className = classNames.join(' ');
|
89 | 86 | };
|
90 | 87 |
|
|
104 | 101 | };
|
105 | 102 | };
|
106 | 103 |
|
107 |
| - var customMarkerDirective = function(Attr2Options, _$timeout_) { |
| 104 | + var linkFunc = function(orgHtml, varsToWatch) { |
| 105 | + //console.log('orgHtml', orgHtml, 'varsToWatch', varsToWatch); |
| 106 | + |
| 107 | + return function(scope, element, attrs, mapController) { |
| 108 | + |
| 109 | + var orgAttrs = parser.orgAttributes(element); |
| 110 | + var filtered = parser.filter(attrs); |
| 111 | + var options = parser.getOptions(filtered, scope); |
| 112 | + var events = parser.getEvents(scope, filtered); |
| 113 | + |
| 114 | + /** |
| 115 | + * build a custom marker element |
| 116 | + */ |
| 117 | + var removedEl = element[0].parentElement.removeChild(element[0]); |
| 118 | + console.log("custom-marker options", options); |
| 119 | + var customMarker = new CustomMarker(options); |
| 120 | + |
| 121 | + $timeout(function() { //apply contents, class, and location after it is compiled |
| 122 | + scope.$watch('[' + varsToWatch.join(',') + ']', function(val) { |
| 123 | + customMarker.setContent(orgHtml, scope); |
| 124 | + }); |
| 125 | + |
| 126 | + customMarker.setContent(removedEl.innerHTML, scope); |
| 127 | + var classNames = removedEl.firstElementChild.className; |
| 128 | + customMarker.addClass('custom-marker'); |
| 129 | + customMarker.addClass(classNames); |
| 130 | + console.log('customMarker', customMarker); |
| 131 | + |
| 132 | + if (!(options.position instanceof google.maps.LatLng)) { |
| 133 | + mapController.getGeoLocation(options.position).then( |
| 134 | + function(latlng) { |
| 135 | + customMarker.setPosition(latlng); |
| 136 | + } |
| 137 | + ); |
| 138 | + } |
| 139 | + }); |
| 140 | + |
| 141 | + console.log("custom-marker events", "events"); |
| 142 | + for (var eventName in events) { /* jshint ignore:line */ |
| 143 | + google.maps.event.addDomListener( |
| 144 | + customMarker.el, eventName, events[eventName]); |
| 145 | + } |
| 146 | + mapController.addObject('customMarkers', customMarker); |
| 147 | + |
| 148 | + element.bind('$destroy', function() { |
| 149 | + //Is it required to remove event listeners when DOM is removed? |
| 150 | + mapController.deleteObject('customMarkers', customMarker); |
| 151 | + }); |
| 152 | + |
| 153 | + }; // linkFunc |
| 154 | + }; |
| 155 | + |
| 156 | + var customMarkerDirective = function(Attr2Options, _$timeout_, _$compile_) { |
108 | 157 | parser = Attr2Options;
|
109 | 158 | $timeout = _$timeout_;
|
| 159 | + $compile = _$compile_; |
110 | 160 | setCustomMarker();
|
111 | 161 |
|
112 | 162 | return {
|
113 | 163 | restrict: 'E',
|
114 | 164 | require: '^map',
|
115 |
| - link: function(scope, element, attrs, mapController) { |
116 |
| - |
117 |
| - var orgAttrs = parser.orgAttributes(element); |
118 |
| - var filtered = parser.filter(attrs); |
119 |
| - var options = parser.getOptions(filtered, scope); |
120 |
| - var events = parser.getEvents(scope, filtered); |
121 |
| - |
122 |
| - /** |
123 |
| - * build a custom marker element |
124 |
| - */ |
125 |
| - var removedEl = element[0].parentElement.removeChild(element[0]); |
126 |
| - console.log("custom-marker options", options); |
127 |
| - var customMarker = new CustomMarker(options); |
128 |
| - |
129 |
| - $timeout(function() { //apply contents, class, and location after it is compiled |
130 |
| - customMarker.setContent(removedEl.innerHTML); |
131 |
| - var classNames = removedEl.firstElementChild.className; |
132 |
| - customMarker.addClass('custom-marker'); |
133 |
| - customMarker.addClass(classNames); |
134 |
| - console.log('customMarker', customMarker); |
135 |
| - |
136 |
| - if (!(options.position instanceof google.maps.LatLng)) { |
137 |
| - mapController.getGeoLocation(options.position).then( |
138 |
| - function(latlng) { |
139 |
| - customMarker.setPosition(latlng); |
140 |
| - } |
141 |
| - ); |
| 165 | + compile: function(element) { |
| 166 | + var orgHtml = element.html(); |
| 167 | + var matches = orgHtml.match(/{{([^}]+)}}/g); |
| 168 | + var varsToWatch = []; |
| 169 | + (matches || []).forEach(function(match) { //filter out that contains '::', 'this.' |
| 170 | + var toWatch = match.replace('{{','').replace('}}',''); |
| 171 | + if (match.indexOf('::') == -1 && match.indexOf('this.') == -1 && varsToWatch.indexOf(toWatch) == -1) { |
| 172 | + varsToWatch.push(match.replace('{{','').replace('}}','')); |
142 | 173 | }
|
143 | 174 | });
|
144 | 175 |
|
145 |
| - console.log("custom-marker events", "events"); |
146 |
| - for (var eventName in events) { |
147 |
| - google.maps.event.addDomListener( |
148 |
| - customMarker.el, eventName, events[eventName]); |
149 |
| - } |
150 |
| - mapController.addObject('customMarkers', customMarker); |
151 |
| - |
152 |
| - element.bind('$destroy', function() { |
153 |
| - //Is it required to remove event listeners when DOM is removed? |
154 |
| - mapController.deleteObject('customMarkers', customMarker); |
155 |
| - }); |
156 |
| - |
157 |
| - } //link |
| 176 | + return linkFunc(orgHtml, varsToWatch); |
| 177 | + } |
158 | 178 | }; // return
|
159 | 179 | };// function
|
160 |
| - customMarkerDirective.$inject = ['Attr2Options', '$timeout']; |
| 180 | + customMarkerDirective.$inject = ['Attr2Options', '$timeout', '$compile']; |
161 | 181 |
|
162 | 182 | angular.module('ngMap').directive('customMarker', customMarkerDirective);
|
163 | 183 | })();
|
0 commit comments