Skip to content
This repository was archived by the owner on Nov 30, 2018. It is now read-only.

Commit e18b22f

Browse files
committed
Window directive fixed for creating a single window on demand with a
Marker that has not been created initially. Therefore the onclick marker window example is now working again.
1 parent 1da149b commit e18b22f

File tree

9 files changed

+173
-133
lines changed

9 files changed

+173
-133
lines changed

dist/angular-google-maps.js

+73-43
Original file line numberDiff line numberDiff line change
@@ -385,10 +385,12 @@
385385
return opts;
386386
},
387387
createWindowOptions: function(gMarker, scope, content, defaults) {
388-
return angular.extend({}, defaults, {
389-
content: content,
390-
position: angular.isObject(gMarker) ? gMarker.getPosition() : new google.maps.LatLng(scope.coords.latitude, scope.coords.longitude)
391-
});
388+
if ((content != null) && (defaults != null)) {
389+
return angular.extend({}, defaults, {
390+
content: content,
391+
position: angular.isObject(gMarker) ? gMarker.getPosition() : new google.maps.LatLng(scope.coords.latitude, scope.coords.longitude)
392+
});
393+
}
392394
},
393395
defaultDelay: 50
394396
};
@@ -761,7 +763,10 @@
761763
return this.WindowChildModel = (function(_super) {
762764
__extends(WindowChildModel, _super);
763765

764-
function WindowChildModel(scope, opts, isIconVisibleOnClick, mapCtrl, markerCtrl, $http, $templateCache, $compile, needToManualDestroy) {
766+
WindowChildModel.include(directives.api.utils.GmapUtil);
767+
768+
function WindowChildModel(scope, opts, isIconVisibleOnClick, mapCtrl, markerCtrl, $http, $templateCache, $compile, element, needToManualDestroy) {
769+
this.element = element;
765770
if (needToManualDestroy == null) {
766771
needToManualDestroy = false;
767772
}
@@ -771,6 +776,7 @@
771776
this.handleClick = __bind(this.handleClick, this);
772777
this.watchCoords = __bind(this.watchCoords, this);
773778
this.watchShow = __bind(this.watchShow, this);
779+
this.createGWin = __bind(this.createGWin, this);
774780
this.scope = scope;
775781
this.opts = opts;
776782
this.mapCtrl = mapCtrl;
@@ -781,7 +787,7 @@
781787
this.$http = $http;
782788
this.$templateCache = $templateCache;
783789
this.$compile = $compile;
784-
this.gWin = new google.maps.InfoWindow(opts);
790+
this.createGWin();
785791
if (this.markerCtrl != null) {
786792
this.markerCtrl.setClickable(true);
787793
}
@@ -792,6 +798,27 @@
792798
this.$log.info(this);
793799
}
794800

801+
WindowChildModel.prototype.createGWin = function(createOpts) {
802+
var _this = this;
803+
if (createOpts == null) {
804+
createOpts = false;
805+
}
806+
if ((this.gWin == null) && createOpts) {
807+
this.opts = this.markerCtrl != null ? this.createWindowOptions(this.markerCtrl, this.scope, this.element.html(), {}) : {};
808+
}
809+
if ((this.opts != null) && this.gWin === void 0) {
810+
this.gWin = new google.maps.InfoWindow(this.opts);
811+
return google.maps.event.addListener(this.gWin, 'closeclick', function() {
812+
if (_this.markerCtrl != null) {
813+
_this.markerCtrl.setVisible(_this.initialMarkerVisibility);
814+
}
815+
if (_this.scope.closeClick != null) {
816+
return _this.scope.closeClick();
817+
}
818+
});
819+
}
820+
};
821+
795822
WindowChildModel.prototype.watchShow = function() {
796823
var _this = this;
797824
return this.scope.$watch('show', function(newValue, oldValue) {
@@ -802,8 +829,10 @@
802829
return _this.hideWindow();
803830
}
804831
} else {
805-
if (newValue && !_this.gWin.getMap()) {
806-
return _this.showWindow();
832+
if (_this.gWin != null) {
833+
if (newValue && !_this.gWin.getMap()) {
834+
return _this.showWindow();
835+
}
807836
}
808837
}
809838
}, true);
@@ -821,46 +850,47 @@
821850
WindowChildModel.prototype.handleClick = function() {
822851
var _this = this;
823852
if (this.markerCtrl != null) {
824-
google.maps.event.addListener(this.markerCtrl, 'click', function() {
853+
return google.maps.event.addListener(this.markerCtrl, 'click', function() {
825854
var pos;
855+
_this.createGWin(true);
826856
pos = _this.markerCtrl.getPosition();
827-
_this.gWin.setPosition(pos);
828-
_this.gWin.open(_this.mapCtrl);
857+
if (_this.gWin != null) {
858+
_this.gWin.setPosition(pos);
859+
_this.gWin.open(_this.mapCtrl);
860+
}
829861
return _this.markerCtrl.setVisible(_this.isIconVisibleOnClick);
830862
});
831863
}
832-
return google.maps.event.addListener(this.gWin, 'closeclick', function() {
833-
if (_this.markerCtrl != null) {
834-
_this.markerCtrl.setVisible(_this.initialMarkerVisibility);
835-
}
836-
if (_this.scope.closeClick != null) {
837-
return _this.scope.closeClick();
838-
}
839-
});
840864
};
841865

842866
WindowChildModel.prototype.showWindow = function() {
843867
var _this = this;
844868
if (this.scope.templateUrl) {
845-
return this.$http.get(this.scope.templateUrl, {
846-
cache: this.$templateCache
847-
}).then(function(content) {
848-
var compiled, templateScope;
849-
templateScope = _this.scope.$new();
850-
if (angular.isDefined(_this.scope.templateParameter)) {
851-
templateScope.parameter = _this.scope.templateParameter;
852-
}
853-
compiled = _this.$compile(content.data)(templateScope);
854-
_this.gWin.setContent(compiled.get(0));
855-
return _this.gWin.open(_this.mapCtrl);
856-
});
869+
if (this.gWin) {
870+
return this.$http.get(this.scope.templateUrl, {
871+
cache: this.$templateCache
872+
}).then(function(content) {
873+
var compiled, templateScope;
874+
templateScope = _this.scope.$new();
875+
if (angular.isDefined(_this.scope.templateParameter)) {
876+
templateScope.parameter = _this.scope.templateParameter;
877+
}
878+
compiled = _this.$compile(content.data)(templateScope);
879+
_this.gWin.setContent(compiled.get(0));
880+
return _this.gWin.open(_this.mapCtrl);
881+
});
882+
}
857883
} else {
858-
return this.gWin.open(this.mapCtrl);
884+
if (this.gWin != null) {
885+
return this.gWin.open(this.mapCtrl);
886+
}
859887
}
860888
};
861889

862890
WindowChildModel.prototype.hideWindow = function() {
863-
return this.gWin.close();
891+
if (this.gWin != null) {
892+
return this.gWin.close();
893+
}
864894
};
865895

866896
WindowChildModel.prototype.destroy = function() {
@@ -906,25 +936,25 @@
906936
};
907937

908938
function IMarkerParentModel(scope, element, attrs, mapCtrl, $timeout) {
939+
var self,
940+
_this = this;
941+
this.scope = scope;
942+
this.element = element;
943+
this.attrs = attrs;
944+
this.mapCtrl = mapCtrl;
945+
this.$timeout = $timeout;
909946
this.linkInit = __bind(this.linkInit, this);
910947
this.onDestroy = __bind(this.onDestroy, this);
911948
this.onWatch = __bind(this.onWatch, this);
912949
this.watch = __bind(this.watch, this);
913950
this.validateScope = __bind(this.validateScope, this);
914951
this.onTimeOut = __bind(this.onTimeOut, this);
915-
var self,
916-
_this = this;
917952
self = this;
918-
this.scope = scope;
919953
this.$log = directives.api.utils.Logger;
920-
this.element = element;
921-
this.attrs = attrs;
922954
if (!this.validateScope(scope)) {
923955
return;
924956
}
925957
this.doClick = angular.isDefined(attrs.click);
926-
this.mapCtrl = mapCtrl;
927-
this.$timeout = $timeout;
928958
if (scope.options != null) {
929959
this.DEFAULTS = scope.options;
930960
}
@@ -1123,7 +1153,7 @@
11231153
this.element.data('instance', this.gMarker);
11241154
google.maps.event.addListener(this.gMarker, 'click', function() {
11251155
if (_this.doClick && (scope.click != null)) {
1126-
return $timeout(function() {
1156+
return _this.$timeout(function() {
11271157
return _this.scope.click();
11281158
});
11291159
}
@@ -1948,9 +1978,9 @@ not 1:1 in this setting.
19481978
markerCtrl = ctrls.length > 1 && (ctrls[1] != null) ? ctrls[1].getMarker() : void 0;
19491979
defaults = scope.options != null ? scope.options : {};
19501980
hasScopeCoords = (scope != null) && (scope.coords != null) && (scope.coords.latitude != null) && (scope.coords.longitude != null);
1951-
opts = (markerCtrl != null) && hasScopeCoords ? _this.createWindowOptions(markerCtrl, scope, element.html(), defaults) : {};
1981+
opts = (markerCtrl != null) && hasScopeCoords ? _this.createWindowOptions(markerCtrl, scope, element.html(), defaults) : void 0;
19521982
if (mapCtrl != null) {
1953-
window = new directives.api.models.child.WindowChildModel(scope, opts, isIconVisibleOnClick, mapCtrl, markerCtrl, _this.$http, _this.$templateCache, _this.$compile);
1983+
window = new directives.api.models.child.WindowChildModel(scope, opts, isIconVisibleOnClick, mapCtrl, markerCtrl, _this.$http, _this.$templateCache, _this.$compile, element);
19541984
}
19551985
return scope.$on("$destroy", function() {
19561986
return window.destroy();

dist/angular-google-maps.min.js

+4-4
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

example/example.html

+29-29
Original file line numberDiff line numberDiff line change
@@ -47,44 +47,44 @@ <h1>angular-google-maps example</h1>
4747
<layer type="BicyclingLayer" show="map.showBicycling"></layer>
4848
<layer namespace="weather" type="WeatherLayer" show="map.showWeather"></layer>
4949

50-
<!--<marker coords="searchLocation">-->
51-
<!--</marker>-->
52-
<!--<markers models="map.randomMarkers" coords="'self'" icon="'icon'" click="'onClicked'"-->
53-
<!--doCluster="map.doClusterRandomMarkers" clusterOptions="map.clusterOptions">-->
54-
<!--</markers>-->
50+
<marker coords="searchLocation">
51+
</marker>
52+
<markers models="map.randomMarkers" coords="'self'" icon="'icon'" click="'onClicked'"
53+
doCluster="map.doClusterRandomMarkers" clusterOptions="map.clusterOptions">
54+
</markers>
5555

56-
<!--<markers models="map.markers2" coords="'self'" icon="'icon'" click="'onClicked'">-->
57-
<!--<windows show="'showWindow'" closeClick="'closeClick'">-->
58-
<!--<p ng-non-bindable>This is an info window at {{ latitude | number:4 }}, {{ longitude | number:4 }}!</p>-->
56+
<markers models="map.markers2" coords="'self'" icon="'icon'" click="'onClicked'">
57+
<windows show="'showWindow'" closeClick="'closeClick'">
58+
<p ng-non-bindable>This is an info window at {{ latitude | number:4 }}, {{ longitude | number:4 }}!</p>
5959

60-
<!--<p class="muted">My marker will stay open when the window is popped up!</p>-->
61-
<!--</windows>-->
62-
<!--</markers>-->
60+
<p class="muted">My marker will stay open when the window is popped up!</p>
61+
</windows>
62+
</markers>
6363

6464

65-
<!--<markers models="map.dynamicMarkers" coords="'self'" icon="'icon'" click="'onClicked'" fit='true'>-->
66-
<!--<windows show="'showWindow'" closeClick="'closeClick'">-->
67-
<!--<p>Dynamic Marker created via a delay!</p>-->
65+
<markers models="map.dynamicMarkers" coords="'self'" icon="'icon'" click="'onClicked'" fit='true'>
66+
<windows show="'showWindow'" closeClick="'closeClick'">
67+
<p>Dynamic Marker created via a delay!</p>
6868

69-
<!--<p ng-non-bindable>This is an info window at {{ latitude | number:4 }}, {{ longitude | number:4 }}!</p>-->
69+
<p ng-non-bindable>This is an info window at {{ latitude | number:4 }}, {{ longitude | number:4 }}!</p>
7070

71-
<!--<p class="muted">My marker will stay open when the window is popped up!</p>-->
72-
<!--</windows>-->
73-
<!--</markers>-->
71+
<p class="muted">My marker will stay open when the window is popped up!</p>
72+
</windows>
73+
</markers>
7474

75-
<!--&lt;!&ndash; prefedined markers &ndash;&gt;-->
76-
<!--&lt;!&ndash; rendering via ng-repear, HIGH OVERHEAD via DOM Manipulation &ndash;&gt;-->
77-
<!--<marker ng-repeat="m in map.markers" coords="m" icon="m.icon" click="onMarkerClicked(m)">-->
78-
<!--<marker-label content="m.title" anchor="22 0" class="marker-labels"/>-->
79-
<!--<window show="m.showWindow" closeClick="m.closeClick()">-->
80-
<!--<p>This is an info window at {{ m.latitude | number:4 }}, {{ m.longitude | number:4 }}!</p>-->
75+
<!-- prefedined markers -->
76+
<!-- rendering via ng-repear, HIGH OVERHEAD via DOM Manipulation -->
77+
<marker ng-repeat="m in map.markers" coords="m" icon="m.icon" click="onMarkerClicked(m)">
78+
<marker-label content="m.title" anchor="22 0" class="marker-labels"/>
79+
<window show="m.showWindow" closeClick="m.closeClick()">
80+
<p>This is an info window at {{ m.latitude | number:4 }}, {{ m.longitude | number:4 }}!</p>
8181

82-
<!--<p class="muted">My marker will stay open when the window is popped up!</p>-->
83-
<!--</window>-->
84-
<!--</marker>-->
82+
<p class="muted">My marker will stay open when the window is popped up!</p>
83+
</window>
84+
</marker>
8585

86-
<!--&lt;!&ndash; marker for clicked position &ndash;&gt;-->
87-
<!--&lt;!&ndash; can not put google.map.animations.BOUNCE , since it is not initiaized yet &ndash;&gt;-->
86+
<!-- marker for clicked position -->
87+
<!-- can not put google.map.animations.BOUNCE , since it is not initiaized yet -->
8888
<div ng-init="opts = {animation:1}">
8989
<marker coords="map.clickedMarker" options="opts">
9090
<!--<marker-label content="map.clickedMarker.title" anchor="24 40" class="marker-labels"/>-->

spec/coffee/directives/api/models/child/window-child-model.spec.coffee

+1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ describe("WindowChildModel", ->
1717
.value('mapCtrl',document.gMap)
1818
.value('markerCtrl',@gMarker)
1919
.value('opts',@windowOpts)
20+
.value('element','<span>hi</span>')
2021
.value('needToManualDestroy',false)
2122
.controller('childModel', directives.api.models.child.WindowChildModel)
2223

src/coffee/directives/api/models/child/window-child-model.coffee

+36-23
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
@ngGmapModule "directives.api.models.child", ->
22
class @WindowChildModel extends oo.BaseObject
3-
constructor: (scope, opts, isIconVisibleOnClick, mapCtrl, markerCtrl, $http, $templateCache, $compile, needToManualDestroy = false)->
3+
@include directives.api.utils.GmapUtil
4+
constructor: (scope, opts, isIconVisibleOnClick, mapCtrl, markerCtrl, $http, $templateCache, $compile,@element, needToManualDestroy = false)->
45
@scope = scope
56
@opts = opts
67
@mapCtrl = mapCtrl
@@ -11,7 +12,7 @@
1112
@$http = $http
1213
@$templateCache = $templateCache
1314
@$compile = $compile
14-
@gWin = new google.maps.InfoWindow(opts)
15+
@createGWin()
1516
# Open window on click
1617
@markerCtrl.setClickable(true) if @markerCtrl?
1718

@@ -21,6 +22,20 @@
2122
@needToManualDestroy = needToManualDestroy
2223
@$log.info(@)
2324

25+
createGWin:(createOpts=false) =>
26+
if !@gWin? and createOpts
27+
@opts = if @markerCtrl? then @createWindowOptions(@markerCtrl, @scope, @element.html(), {}) else {}
28+
29+
if @opts? and @gWin == undefined
30+
@gWin = new google.maps.InfoWindow(@opts)
31+
32+
# Set visibility of marker back to what it was before opening the window
33+
google.maps.event.addListener(@gWin, 'closeclick', =>
34+
if @markerCtrl?
35+
@markerCtrl.setVisible(@initialMarkerVisibility)
36+
@scope.closeClick() if @scope.closeClick?
37+
)
38+
2439
watchShow: () =>
2540
@scope.$watch('show', (newValue, oldValue) =>
2641
if (newValue != oldValue)
@@ -29,9 +44,10 @@
2944
else
3045
@hideWindow()
3146
else
32-
if (newValue and !@gWin.getMap())
33-
# If we're initially showing the marker and it's not yet visible, show it.
34-
@showWindow()
47+
if @gWin?
48+
if (newValue and !@gWin.getMap())
49+
# If we're initially showing the marker and it's not yet visible, show it.
50+
@showWindow()
3551
, true)
3652

3753
watchCoords: ()=>
@@ -44,33 +60,30 @@
4460
# Show the window and hide the marker on click
4561
if @markerCtrl?
4662
google.maps.event.addListener(@markerCtrl, 'click', =>
63+
@createGWin(true)
4764
pos = @markerCtrl.getPosition()
48-
@gWin.setPosition(pos)
49-
@gWin.open(@mapCtrl)
65+
if @gWin?
66+
@gWin.setPosition(pos)
67+
@gWin.open(@mapCtrl)
5068
@markerCtrl.setVisible(@isIconVisibleOnClick)
5169
)
52-
# Set visibility of marker back to what it was before opening the window
53-
google.maps.event.addListener(@gWin, 'closeclick', =>
54-
if @markerCtrl?
55-
@markerCtrl.setVisible(@initialMarkerVisibility)
56-
@scope.closeClick() if @scope.closeClick?
57-
)
5870

5971
showWindow: () =>
6072
if @scope.templateUrl
61-
@$http.get(@scope.templateUrl, { cache: @$templateCache }).then((content) =>
62-
templateScope = @scope.$new()
63-
if angular.isDefined(@scope.templateParameter)
64-
templateScope.parameter = @scope.templateParameter
65-
compiled = @$compile(content.data)(templateScope)
66-
@gWin.setContent(compiled.get(0))
67-
@gWin.open(@mapCtrl)
68-
)
73+
if @gWin
74+
@$http.get(@scope.templateUrl, { cache: @$templateCache }).then((content) =>
75+
templateScope = @scope.$new()
76+
if angular.isDefined(@scope.templateParameter)
77+
templateScope.parameter = @scope.templateParameter
78+
compiled = @$compile(content.data)(templateScope)
79+
@gWin.setContent(compiled.get(0))
80+
@gWin.open(@mapCtrl)
81+
)
6982
else
70-
@gWin.open(@mapCtrl)
83+
@gWin.open(@mapCtrl) if @gWin?
7184

7285
hideWindow: () =>
73-
@gWin.close()
86+
@gWin.close() if @gWin?
7487

7588
destroy: ()=>
7689
@hideWindow(@gWin)

0 commit comments

Comments
 (0)