1 | |
---|
2 | function PwgDataHandler( map, opts ) |
---|
3 | { |
---|
4 | this._map = map; |
---|
5 | this._infoWindow = new google.maps.InfoWindow(); |
---|
6 | this.options = jQuery.fn.extend( |
---|
7 | { |
---|
8 | markerOptions: {}, |
---|
9 | show_all_img_src: null |
---|
10 | } |
---|
11 | , opts || {} ); |
---|
12 | |
---|
13 | google.maps.event.addListener( map, "infowindowclose", function() {map.getInfoWindow().pwgMarker=null;} ); |
---|
14 | } |
---|
15 | |
---|
16 | PwgDataHandler.prototype = { |
---|
17 | |
---|
18 | _map: null, |
---|
19 | _infoWindow: null, |
---|
20 | options: {}, |
---|
21 | _markers: [], |
---|
22 | _navHtmlIds: ["gotoPrevImage", "gotoNextImage"], |
---|
23 | _timerBindPictureNavigation: null, |
---|
24 | _prevResult: { nb_items:0 }, |
---|
25 | |
---|
26 | |
---|
27 | terminate: function() |
---|
28 | { |
---|
29 | this._map = null; |
---|
30 | try { |
---|
31 | for (i=0; i<this._markers.length; i++) |
---|
32 | this._markers[i].pwg = null; |
---|
33 | this._markers.length = 0; |
---|
34 | } |
---|
35 | catch(e){} |
---|
36 | }, |
---|
37 | |
---|
38 | handle: function( data ) |
---|
39 | { |
---|
40 | var i; |
---|
41 | if (document.title != data.title ) |
---|
42 | document.title = data.title; |
---|
43 | var elt = document.getElementById("aPageUrl"); |
---|
44 | if (elt && elt.href!=data.page_url.urlNoHtm() ) |
---|
45 | { |
---|
46 | elt.href = data.page_url.urlNoHtm(); |
---|
47 | elt.innerHTML = data.title; |
---|
48 | elt.title = Localization.fmt1("go to %s", data.title); |
---|
49 | } |
---|
50 | var elt = document.getElementById("aBlowup"); |
---|
51 | if (elt && elt.href!=data.blowup_url.urlNoHtm()) |
---|
52 | elt.href = data.blowup_url.urlNoHtm(); |
---|
53 | |
---|
54 | var elt = document.getElementById("aKml"); |
---|
55 | if (elt && elt.href!=data.kml_url.urlNoHtm()) |
---|
56 | { |
---|
57 | elt.href = data.kml_url.urlNoHtm(); |
---|
58 | elt.title = Localization.fmt1("opens %s in Google Earth", data.title); |
---|
59 | } |
---|
60 | var changed = true; |
---|
61 | if (this._prevResult.nb_items==data.nb_items && this._markers.length==data.image_clusters.length ) |
---|
62 | { |
---|
63 | changed = false; |
---|
64 | if (this._markers.length>0 && !this._markers[0].getPosition().equals( data.image_clusters[0].position ) ) |
---|
65 | changed=true; |
---|
66 | } |
---|
67 | |
---|
68 | if (changed) |
---|
69 | { |
---|
70 | var newMarkers = []; |
---|
71 | var infoWindowMarker = this._infoWindow.pwgMarker; |
---|
72 | |
---|
73 | for (i=0; i<data.image_clusters.length; i++) |
---|
74 | { |
---|
75 | var cluster = data.image_clusters[i]; |
---|
76 | var theTitle = ""; |
---|
77 | if (cluster.nb_items>1) |
---|
78 | theTitle = Localization.fmt1("%d photos", cluster.nb_items); |
---|
79 | else |
---|
80 | theTitle = cluster.items[0].name; |
---|
81 | |
---|
82 | var marker; |
---|
83 | marker = this._markers.pop(); |
---|
84 | if (marker && marker==infoWindowMarker) |
---|
85 | { |
---|
86 | marker.setMap(null); |
---|
87 | google.maps.event.clearListeners(marker, "click" ); |
---|
88 | google.maps.event.clearListeners(marker, "dblclick" ); |
---|
89 | this._infoWindow.pwgMarker = infoWindowMarker = null; |
---|
90 | if (document.is_debug) glog('removed marker with infoWindow'); |
---|
91 | marker = this._markers.pop(); |
---|
92 | } |
---|
93 | |
---|
94 | if (!marker) |
---|
95 | { |
---|
96 | marker = new google.maps.Marker(this.options.markerOptions); |
---|
97 | marker.setPosition(cluster.position); |
---|
98 | marker.setTitle(theTitle); |
---|
99 | google.maps.event.addListener( marker, "click", pwgBind(this, this._onMarkerClick, marker) ); |
---|
100 | google.maps.event.addListener( marker, "dblclick", pwgBind(this, this._onMarkerDblClick, marker) ); |
---|
101 | marker.setMap(this._map); |
---|
102 | } |
---|
103 | else |
---|
104 | { |
---|
105 | marker.currentImageIndex=0; |
---|
106 | marker.setPosition( cluster.position ); |
---|
107 | marker.setTitle(theTitle); |
---|
108 | marker.setMap(this._map); |
---|
109 | } |
---|
110 | |
---|
111 | newMarkers.push(marker); |
---|
112 | |
---|
113 | marker.pwg = { |
---|
114 | nb_items: cluster.nb_items, |
---|
115 | images: cluster.items, |
---|
116 | bounds: cluster.bounds, |
---|
117 | blowup_url: cluster.blowup_url |
---|
118 | }; |
---|
119 | } |
---|
120 | |
---|
121 | for (i=0; i<this._markers.length; i++) |
---|
122 | { |
---|
123 | this._markers[i].setMap(null); |
---|
124 | google.maps.event.clearListeners(this._markers[i], "click" ); |
---|
125 | google.maps.event.clearListeners(this._markers[i], "dblclick" ); |
---|
126 | } |
---|
127 | |
---|
128 | this._markers = newMarkers; |
---|
129 | |
---|
130 | this._prevResult.nb_items=data.nb_items; |
---|
131 | } |
---|
132 | |
---|
133 | document.getElementById("dataLoadStatus").innerHTML = Localization.fmt1("%d photos", data.nb_items); |
---|
134 | }, |
---|
135 | |
---|
136 | _onMarkerClick: function( marker ) |
---|
137 | { |
---|
138 | if (this._infoWindow.pwgMarker == marker ) |
---|
139 | return; // already open |
---|
140 | var content = ""; |
---|
141 | if ( !marker.currentImageIndex ) |
---|
142 | marker.currentImageIndex = 0; |
---|
143 | content += '<div class="gmiw_header" style="' + (marker.pwg.images.length>1 ? '': 'display:none')+ '">'; |
---|
144 | content += '<span id="pwgImageCounters">'+this.buildCurrentPictureCounterHtml(marker)+'</span> '; |
---|
145 | content += '<a href="javascript:void(0);" id="'+this._navHtmlIds[0]+'">'; |
---|
146 | content += '<span>'+ "« " + Localization.get('Prev') + '</span>'; |
---|
147 | content += '</a>'; |
---|
148 | content += " "; |
---|
149 | content += '<a href="javascript:void(0);" id="'+this._navHtmlIds[1]+'">'; |
---|
150 | content += '<span>'+Localization.get('Next') + " »"+'</span>'; |
---|
151 | content += '</a>'; |
---|
152 | content += " "; |
---|
153 | var imgShowAll = ''; |
---|
154 | if (this.options.show_all_img_src) |
---|
155 | imgShowAll = '<img src="'+this.options.show_all_img_src+'" alt="" style="border:0" /> '; |
---|
156 | content += '<a id="pwgImageBlowup" href="'+marker.pwg.blowup_url+'" onclick="return PwgDataHandler.blowupUrl(this.href);" title='+Localization.getQ('show all photos around this location')+'>'+ |
---|
157 | imgShowAll+ '<span>'+Localization.get('Show all')+'</span>'+ |
---|
158 | '</a>'; |
---|
159 | content += '</div>'; |
---|
160 | content += '<div id="pwgImageDetail">' + this.buildCurrentPictureHtml( marker ) + '</div>'; |
---|
161 | |
---|
162 | this._infoWindow.setContent( content ); |
---|
163 | this._infoWindow.setPosition( marker.getPosition() ); |
---|
164 | this._infoWindow.open( this._map ); |
---|
165 | |
---|
166 | // bind to next / prev a little later because sometimes the nodes are not immediately created |
---|
167 | if (this._timerBindPictureNavigation) clearTimeout(this._timerBindPictureNavigation); |
---|
168 | this._timerBindPictureNavigation = null; |
---|
169 | if (marker.pwg.images.length>1) |
---|
170 | this._timerBindPictureNavigation = setTimeout( pwgBind(this, this._onTimeoutBindPictureNavigation, marker), 250 ); |
---|
171 | }, |
---|
172 | |
---|
173 | _onMarkerDblClick: function( marker ) |
---|
174 | { |
---|
175 | this._map.fitBounds( marker.pwg.bounds ); |
---|
176 | }, |
---|
177 | |
---|
178 | buildCurrentPictureHtml: function( marker ) |
---|
179 | { |
---|
180 | var imageDetail = marker.pwg.images[marker.currentImageIndex]; |
---|
181 | var res = ""; |
---|
182 | res += '<div class="gmiw_imageTitle">' + imageDetail.name + "</div>"; |
---|
183 | res += |
---|
184 | '<div class="gmiw_imageContent">' + |
---|
185 | '<div class="gmiw_imageWrap">'+ |
---|
186 | '<a href="'+imageDetail.page_url+'">' + |
---|
187 | '<img src="' + imageDetail.tn + '" alt="thumb" />'+ |
---|
188 | '</a></div>' + |
---|
189 | '<div class="gmiw_imageComment">' + imageDetail.comment + '</div>' + |
---|
190 | '</div>'; |
---|
191 | return res; |
---|
192 | }, |
---|
193 | |
---|
194 | buildCurrentPictureCounterHtml: function( marker ) |
---|
195 | { |
---|
196 | var res = |
---|
197 | '<b>'+(marker.currentImageIndex+1)+'</b>'+ |
---|
198 | '/' + |
---|
199 | '<b>'+marker.pwg.images.length+'</b>'; |
---|
200 | if (marker.pwg.nb_items>marker.pwg.images.length) |
---|
201 | res+= " "+Localization.fmt1("out of %d", marker.pwg.nb_items ); |
---|
202 | return res; |
---|
203 | }, |
---|
204 | |
---|
205 | |
---|
206 | _onTimeoutBindPictureNavigation: function( marker ) |
---|
207 | { |
---|
208 | clearTimeout(this._timerBindPictureNavigation); |
---|
209 | this._timerBindPictureNavigation = null; |
---|
210 | this._infoWindow.pwgMarker = marker; |
---|
211 | for (var i=0; i< this._navHtmlIds.length; i++) |
---|
212 | { |
---|
213 | var elt = document.getElementById( this._navHtmlIds[i] ); |
---|
214 | if (elt) |
---|
215 | google.maps.event.addDomListener(elt, "click", pwgBind(this, this._onPictureNavigate, marker, i) ); |
---|
216 | } |
---|
217 | }, |
---|
218 | |
---|
219 | _onPictureNavigate: function(marker, dir ) |
---|
220 | { |
---|
221 | if (dir==0) dir=-1; |
---|
222 | marker.currentImageIndex += dir; |
---|
223 | if (marker.currentImageIndex<0) |
---|
224 | marker.currentImageIndex = marker.pwg.images.length-1; |
---|
225 | else if (marker.currentImageIndex >= marker.pwg.images.length) |
---|
226 | marker.currentImageIndex = 0; |
---|
227 | |
---|
228 | try { |
---|
229 | var elt = document.getElementById( "pwgImageDetail" ); |
---|
230 | elt.innerHTML = this.buildCurrentPictureHtml( marker ); |
---|
231 | } |
---|
232 | catch (e) |
---|
233 | { |
---|
234 | alert (e.message); |
---|
235 | } |
---|
236 | |
---|
237 | try { |
---|
238 | var elt = document.getElementById( "pwgImageCounters" ); |
---|
239 | elt.innerHTML = this.buildCurrentPictureCounterHtml( marker ); |
---|
240 | } |
---|
241 | catch (e) |
---|
242 | { |
---|
243 | alert (e.message); |
---|
244 | } |
---|
245 | return false; |
---|
246 | } |
---|
247 | |
---|
248 | } |
---|
249 | |
---|
250 | PwgDataHandler.blowupUrl = function(theUrl) |
---|
251 | { |
---|
252 | jQuery.fx.off = true; |
---|
253 | jQuery.nyroModalManual( { |
---|
254 | url: theUrl, |
---|
255 | forceType: 'iframe', |
---|
256 | width: 1920 |
---|
257 | }); |
---|
258 | return false; |
---|
259 | } |
---|
260 | |
---|
261 | |
---|
262 | String.prototype.urlNoHtm = function() |
---|
263 | { |
---|
264 | return this.replace( "&", "&" ); |
---|
265 | } |
---|