Skip to content

Commit

Permalink
Added iron-selector to enable singleInfoWindow property
Browse files Browse the repository at this point in the history
Implemented the singleInfoWindow property by using iron-selector and
two new events: google-map-marker-open and google-map-marker-close.
  • Loading branch information
davidwittenbrink committed Oct 5, 2015
1 parent eb85e76 commit d125811
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 8 deletions.
1 change: 1 addition & 0 deletions bower.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
"polymer": "Polymer/polymer#^1.1.4",
"google-apis": "GoogleWebComponents/google-apis#^1.1.1",
"iron-resizable-behavior": "PolymerElements/iron-resizable-behavior#^1.0.0"
"iron-selector": "PolymerElements/iron-selector#^1.0.0"
},
"devDependencies": {
"web-component-tester": "*",
Expand Down
42 changes: 38 additions & 4 deletions google-map-marker.html
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,14 @@
* @event google-map-marker-rightclick
* @param {google.maps.MouseEvent} event The mouse event.
*/

/**
* Fired when an infowindow is opened.
* @event google-map-marker-open
*/
/**
* Fired when the close button of the infowindow is pressed.
* @event google-map-marker-close
*/
properties: {
/**
* A Google Maps marker object.
Expand Down Expand Up @@ -202,6 +209,15 @@
type: String,
value: null,
observer: '_animationChanged'
},

/**
* Specifies whether the InfoWindow is open or not
*/
open: {
type: Boolean,
value: false,
observer: '_openChanged'
}
},

Expand All @@ -222,6 +238,8 @@
if (this.marker) {
this.marker.setMap(this.map);
}
if (this.open)
this.fire('google-map-marker-open');
},

_updatePosition: function() {
Expand Down Expand Up @@ -310,20 +328,35 @@
if (!this.info) {
// Create a new infowindow
this.info = new google.maps.InfoWindow();
this.infoHandler_ = google.maps.event.addListener(this.marker, 'click', function() {
this.info.open(this.map, this.marker);
this.openInfoHandler_ = google.maps.event.addListener(this.marker, 'click', function() {
this.fire('google-map-marker-open');
}.bind(this));

this.closeInfoHandler_ = google.maps.event.addListener(this.info, 'closeclick', function() {
this.fire('google-map-marker-close');
}.bind(this));
}
this.info.setContent(content);
} else {
if (this.info) {
// Destroy the existing infowindow. It doesn't make sense to have an empty one.
google.maps.event.removeListener(this.infoHandler_);
google.maps.event.removeListener(this.openInfoHandler_);
google.maps.event.removeListener(this.closeInfoHandler_);
this.info = null;
}
}
},

_openChanged: function() {
if (this.info) {
if (this.open) {
this.info.open(this.map, this.marker);
} else {
this.info.close();
}
}
},

_mapReady: function() {
this._listeners = {};
this.marker = new google.maps.Marker({
Expand All @@ -343,6 +376,7 @@
this._clickEventsChanged();
this._contentChanged();
this._mouseEventsChanged();
this._openChanged();
setupDragHandler_.bind(this)();
},

Expand Down
30 changes: 26 additions & 4 deletions google-map.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
<link rel="import" href="../polymer/polymer.html">
<link rel="import" href="../google-apis/google-maps-api.html">
<link rel="import" href="../iron-resizable-behavior/iron-resizable-behavior.html">
<link rel="import" href="../iron-selector/iron-selector.html">
<link rel="import" href="google-map-marker.html">
<!--
The `google-map` element renders a Google Map.
Expand Down Expand Up @@ -74,8 +75,9 @@
on-api-load="_mapApiLoaded"></google-maps-api>

<div id="map"></div>

<content id="markers" select="google-map-marker"></content>
<iron-selector id="selector" multi="[[!singleInfoWindow]]" selected-attribute="open" activate-event="google-map-marker-open" on-google-map-marker-close="_deselectMarker">
<content id="markers" select="google-map-marker"></content>
</iron-selector>

</template>
</dom-module>
Expand Down Expand Up @@ -340,8 +342,15 @@
type: Array,
value: function() { return []; },
readOnly: true
}
},

/**
* If set, all other info windows on markers are closed when opening a new one.
*/
singleInfoWindow: {
type: Boolean,
value: false
}
},

behaviors: [
Expand Down Expand Up @@ -683,7 +692,20 @@
this._listeners[name] = google.maps.event.addListener(this.map, name, function(event) {
this.fire('google-map-' + name, event);
}.bind(this));
}
},

_deselectMarker: function(e) {
// If singleInfoWindow is set, update core-selector's selected attribute to be null.
// Else remvoe the marker from core-selector's selected array.
var markerSelector = this.$.selector;
var markerIndex = markerSelector.items.indexOf(e.target);

if (this.singleInfoWindow) {
markerSelector.selected = null;
} else {
markerSelector.selectedValues = markerSelector.selectedValues.filter(function(i) {return i != markerIndex});
}
}

});

Expand Down

0 comments on commit d125811

Please sign in to comment.