Add Google Map of voters
[selectricity-live] / vendor / plugins / ym4r_gm / javascript / geoRssOverlay.js
1 // GeoRssOverlay: GMaps API extension to display a group of markers from
2 // a RSS feed
3 //
4 // Copyright 2006 Mikel Maron (email: mikel_maron yahoo com)
5 //
6 // The original version of this code is called MGeoRSS and can be found
7 // at the following address:
8 // http://brainoff.com/gmaps/mgeorss.html
9 //
10 // Modified by Andrew Turner to add support for the GeoRss Simple vocabulary
11 //
12 // Modified and bundled with YM4R in accordance with the following
13 // license:
14 //
15 // This work is public domain
16
17 function GeoRssOverlay(rssurl,icon,proxyurl,options){
18     this.rssurl = rssurl;
19     this.icon = icon;
20     this.proxyurl = proxyurl;
21     if(options['visible'] == undefined)
22         this.visible = true;
23     else
24         this.visible = options['visible'];
25     this.listDiv = options['listDiv']; //ID of the item list DIV
26     this.contentDiv = options['contentDiv']; //ID of the content DIV
27     this.listItemClass = options['listItemClass']; //Class of the list item DIV
28     this.limitItems = options['limit']; //Maximum number of displayed entries
29     this.request = false;
30     this.markers = [];
31 }
32
33 GeoRssOverlay.prototype = new GOverlay();
34
35 GeoRssOverlay.prototype.initialize=function(map) {
36     this.map = map;
37     this.load();
38 }
39
40 GeoRssOverlay.prototype.redraw = function(force){
41     //nothing to do : the markers are already taken care of
42 }
43
44 GeoRssOverlay.prototype.remove = function(){
45     for(var i= 0, len = this.markers.length ; i< len; i++){
46         this.map.removeOverlay(this.markers[i]);
47     }
48 }
49
50 GeoRssOverlay.prototype.showHide=function() {
51     if (this.visible) {
52         for (var i=0;i<this.markers.length;i++) {
53             this.map.removeOverlay(this.markers[i]);
54         }
55         this.visible = false;
56     } else {
57         for (var i=0;i<this.markers.length;i++) {
58             this.map.addOverlay(this.markers[i]);
59         }
60         this.visible = true;
61     }
62 }
63
64 GeoRssOverlay.prototype.showMarker = function(id){
65     var marker = this.markers[id];
66     if(marker != undefined){
67         GEvent.trigger(marker,"click");
68     }
69 }
70
71 GeoRssOverlay.prototype.copy = function(){
72     var oCopy = new GeoRssOVerlay(this.rssurl,this.icon,this.proxyurl);
73     oCopy.markers = [];
74     for(var i = 0 , len = this.markers.length ;i < len ; i++){
75         oCopy.markers.push(this.markers[i].copy());
76     }
77     return oCopy;
78 }
79
80 GeoRssOverlay.prototype.load=function() {
81     if (this.request != false) { 
82         return; 
83     }
84     this.request = GXmlHttp.create();
85     if (this.proxyurl != undefined) {
86         this.request.open("GET",this.proxyurl + '?q=' + encodeURIComponent(this.rssurl),true);
87     } else {
88         this.request.open("GET",this.rssurl, true);
89     }
90     var m = this;
91     this.request.onreadystatechange = function() {
92         m.callback();
93     }
94     this.request.send(null);
95 }
96
97 GeoRssOverlay.prototype.callback = function() {
98     if (this.request.readyState == 4) {
99         if (this.request.status == "200") {
100             var xmlDoc = this.request.responseXML;
101             if(xmlDoc.documentElement.getElementsByTagName("item").length != 0){
102                 //RSS
103                 var items = xmlDoc.documentElement.getElementsByTagName("item");
104             }else if(xmlDoc.documentElement.getElementsByTagName("entry").length != 0){
105                 //Atom
106                 var items = xmlDoc.documentElement.getElementsByTagName("entry");
107             }
108             for (var i = 0, len = this.limitItems?Math.min(this.limitItems,items.length):items.length; i < len; i++) {
109                 try {
110                     var marker = this.createMarker(items[i],i);
111                     this.markers.push(marker);
112                     if(this.visible){
113                         this.map.addOverlay(marker);
114                     }
115                 } catch (e) {
116                 }
117             }
118         }
119         this.request = false;
120     }
121 }
122
123 GeoRssOverlay.prototype.createMarker = function(item,index) {
124     
125     var title = item.getElementsByTagName("title")[0].childNodes[0].nodeValue;
126     if(item.getElementsByTagName("description").length != 0){
127         //Rss
128         var description = item.getElementsByTagName("description")[0].childNodes[0].nodeValue;
129         var link = item.getElementsByTagName("link")[0].childNodes[0].nodeValue;    }else if(item.getElementsByTagName("summary").length != 0){
130         //Atom
131         var description = item.getElementsByTagName("summary")[0].childNodes[0].nodeValue;
132         var link = item.getElementsByTagName("link")[0].attributes[0].nodeValue;
133         }
134     /* namespaces are handled by spec in moz, not in ie */
135     if (navigator.userAgent.toLowerCase().indexOf("msie") < 0) {
136         if(item.getElementsByTagNameNS("http://www.w3.org/2003/01/geo/wgs84_pos#","lat").length != 0){
137             //W3C Geo Vocabulary
138             var lat = item.getElementsByTagNameNS("http://www.w3.org/2003/01/geo/wgs84_pos#","lat")[0].childNodes[0].nodeValue;
139             var lng = item.getElementsByTagNameNS("http://www.w3.org/2003/01/geo/wgs84_pos#","long")[0].childNodes[0].nodeValue;
140         }else if(item.getElementsByTagNameNS("http://www.georss.org/georss","point").length != 0){
141             
142             //Simple
143             var latlng = item.getElementsByTagNameNS("http://www.georss.org/georss","point")[0].childNodes[0].nodeValue.split(" ");
144             var lat = latlng[0];
145             var lng = latlng[1];
146         }
147     } else {
148         
149         if(item.getElementsByTagName("geo:lat").length != 0){
150             //W3C Geo Vocabulary
151             var lat = item.getElementsByTagName("geo:lat")[0].childNodes[0].nodeValue;
152             var lng = item.getElementsByTagName("geo:long")[0].childNodes[0].nodeValue;
153         }else if(item.getElementsByTagName("georss:point").length != 0){
154             //Simple
155             var latlng = item.getElementsByTagName("georss:point")[0].childNodes[0].nodeValue.split(" ");
156             var lat = latlng[0];
157             var lng = latlng[1];
158         }
159     }
160     
161     var point = new GLatLng(parseFloat(lat), parseFloat(lng));
162     var marker = new GMarker(point,{'title': title});
163     var html = "<a href=\"" + link + "\">" + title + "</a><p/>" + description;
164     
165     if(this.contentDiv == undefined){
166         GEvent.addListener(marker, "click", function() {
167             marker.openInfoWindowHtml(html);
168         });
169     }else{
170         var contentDiv = this.contentDiv;
171         GEvent.addListener(marker, "click", function() {
172             document.getElementById(contentDiv).innerHTML = html;
173         });
174     }
175     
176     if(this.listDiv != undefined){
177         var a = document.createElement('a'); 
178         a.innerHTML = title;
179         a.setAttribute("href","#");
180         var georss = this;
181         a.onclick = function(){
182             georss.showMarker(index);
183             return false;
184         };
185         var div = document.createElement('div');
186         if(this.listItemClass != undefined){
187             div.setAttribute("class",this.listItemClass);
188         }
189         div.appendChild(a);
190         document.getElementById(this.listDiv).appendChild(div);
191     }
192     
193     return marker;
194 }

Benjamin Mako Hill || Want to submit a patch?