Skip to content

Commit

Permalink
Fixes issue #6
Browse files Browse the repository at this point in the history
- Added the closeWhenOthersOpen option.
- Modified the way google map listeners are tracked.
- Updated the readme.
- Modified the test scripts slightly.
- Added a git ignore for vscode settings.
  • Loading branch information
joelkravets committed Mar 9, 2017
1 parent cef6dc6 commit 4c49830
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 21 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@
/test/scripts/snazzy-info-window.min.js
/test/css/snazzy-info-window.min.css.map
/test/css/snazzy-info-window.min.css
/.vscode
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,13 @@ panning the map.
- Type: _boolean_
- Default: `true`

#### closeWhenOthersOpen

Determines if the info window will close when any other Snazzy Info Window is opened.

- Type: _boolean_
- Default: `false`

#### showCloseButton

Determines if the info window will show a close button.
Expand Down
55 changes: 36 additions & 19 deletions src/snazzy-info-window.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
const _classPrefix = 'si-';
const _root2 = 1.41421356237;
const _inverseRoot2 = 0.7071067811865474;
const _eventPrefix = 'snazzy-info-window-';
const _defaultShadow = {
h: '0px',
v: '3px',
Expand All @@ -14,6 +15,7 @@ const _defaultOptions = {
pointer: true,
openOnMarkerClick: true,
closeOnMapClick: true,
closeWhenOthersOpen: false,
showCloseButton: true,
panOnOpen: true,
edgeOffset: {
Expand Down Expand Up @@ -132,11 +134,11 @@ export default class SnazzyInfoWindow extends google.maps.OverlayView {

// This listener remains active when the info window is closed.
if (google && this._marker && this._opts.openOnMarkerClick) {
this._openOnMarkerClickListener = google.maps.event.addListener(this._marker, 'click', () => {
this.trackListener(google.maps.event.addListener(this._marker, 'click', () => {
if (!this.getMap()) {
this.open();
}
});
}), true);
}

// When using a position the default option for the offset is 0
Expand Down Expand Up @@ -185,15 +187,26 @@ export default class SnazzyInfoWindow extends google.maps.OverlayView {
return lambda ? lambda.apply(this) : undefined;
}

// Track the provided listener. A persistent listener means it remains
// tracked even if the info window is closed.
trackListener(listener, persistent) {
this._listeners.push({ listener, persistent });
}

// Will clear all listeners that are used during the open state.
clearListeners() {
clearListeners(clearPersistent) {
if (google) {
if (this._listeners) {
this._listeners.forEach((listener) => {
google.maps.event.removeListener(listener);
this._listeners.forEach((e) => {
if (clearPersistent || !e.persistent) {
google.maps.event.removeListener(e.listener);
e.listener = null;
}
});
this._listeners = this._listeners.filter((e) => {
return e.listener != null;
});
}
this._listeners = [];
}
}

Expand Down Expand Up @@ -229,14 +242,8 @@ export default class SnazzyInfoWindow extends google.maps.OverlayView {
if (this.getMap()) {
this.setMap(null);
}

if (google) {
if (this._openOnMarkerClickListener) {
google.maps.event.removeListener(this._openOnMarkerClickListener);
this._openOnMarkerClickListener = null;
}
}
this.clearListeners();
// Make sure to clear all persistent listeners
this.clearListeners(true);
}

setContent(content) {
Expand Down Expand Up @@ -405,6 +412,9 @@ export default class SnazzyInfoWindow extends google.maps.OverlayView {
this.resize();
this.reposition();
this.activateCallback('afterOpen');
if (google) {
google.maps.event.trigger(this.getMap(), `${_eventPrefix}opened`, this);
}
}
}

Expand Down Expand Up @@ -529,15 +539,22 @@ export default class SnazzyInfoWindow extends google.maps.OverlayView {
const map = this.getMap();
this.clearListeners();
if (this._opts.closeOnMapClick) {
this._listeners.push(google.maps.event.addListener(map, 'click', () => {
this.trackListener(google.maps.event.addListener(map, 'click', () => {
this.close();
}));
}
if (this._opts.closeWhenOthersOpen) {
this.trackListener(google.maps.event.addListener(map, `${_eventPrefix}opened`, (other) => {
if (this !== other) {
this.close();
}
}));
}
if (google) {
// Clear out the previous map bounds
this._previousWidth = null;
this._previousHeight = null;
this._listeners.push(google.maps.event.addListener(map, 'bounds_changed', () => {
this.trackListener(google.maps.event.addListener(map, 'bounds_changed', () => {
const d = map.getDiv();
const ow = d.offsetWidth;
const oh = d.offsetHeight;
Expand All @@ -552,15 +569,15 @@ export default class SnazzyInfoWindow extends google.maps.OverlayView {

// Marker moves
if (this._marker) {
this._listeners.push(google.maps.event.addListener(this._marker,
this.trackListener(google.maps.event.addListener(this._marker,
'position_changed', () => {
this.draw();
}));
}

// Close button
if (this._opts.showCloseButton && !this._opts.closeButtonMarkup) {
this._listeners.push(google.maps.event.addDomListener(this._html.closeButton,
this.trackListener(google.maps.event.addDomListener(this._html.closeButton,
'click', (e) => {
e.cancelBubble = true;
if (e.stopPropagation) {
Expand All @@ -577,7 +594,7 @@ export default class SnazzyInfoWindow extends google.maps.OverlayView {
'touchstart', 'touchend', 'touchmove',
'wheel', 'mousewheel', 'DOMMouseScroll', 'MozMousePixelScroll'];
mouseEvents.forEach((event) => {
this._listeners.push(google.maps.event.addDomListener(this._html.wrapper,
this.trackListener(google.maps.event.addDomListener(this._html.wrapper,
event, (e) => {
e.cancelBubble = true;
if (e.stopPropagation) {
Expand Down
3 changes: 2 additions & 1 deletion test/scripts/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ $(function() {
var map = new google.maps.Map($(".map-canvas")[0], {
zoom: 14,
center: new google.maps.LatLng(40.72, -74),
mapTypeId: google.maps.MapTypeId.ROADMAP
mapTypeId: google.maps.MapTypeId.ROADMAP,
clickableIcons: false
});

var marker = new google.maps.Marker({
Expand Down
5 changes: 4 additions & 1 deletion test/scripts/multi.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ $(function(){
var map = new google.maps.Map($('.map-canvas')[0], {
zoom: 14,
center: center,
mapTypeId: google.maps.MapTypeId.ROADMAP
mapTypeId: google.maps.MapTypeId.ROADMAP,
clickableIcons: false
});
var dx = 0.003;
var placements = [
Expand All @@ -15,6 +16,7 @@ $(function(){
{ type: 'bottom', LatLng: offsetCenter(-dx, 0) },
{ type: 'left', LatLng: offsetCenter(0, -dx) }
];
window.infos = [];
$.each(placements, function(i, e){
var marker = new google.maps.Marker({
map: map,
Expand All @@ -31,5 +33,6 @@ $(function(){
panOnOpen: false
}));
info.open();
window.infos.push(info);
});
});

0 comments on commit 4c49830

Please sign in to comment.