source: extensions/rv_gmaps/trunk/template/data_handler.js @ 12701

Last change on this file since 12701 was 12701, checked in by rvelices, 12 years ago

rv_gmaps auto sync gps with exif + towards full maps api v3 migration

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