]> dev.renevier.net Git - syp.git/blob - js/syp.js
forbird creation or deletion of item; forbids adding or deleting an image
[syp.git] / js / syp.js
1 /* Copyright (c) 2009 Arnaud Renevier, Inc, published under the modified BSD
2  * license. */
3
4 OpenLayers.Control.SypAttribution = OpenLayers.Class (OpenLayers.Control.Attribution, {
5     updateAttribution: function() {
6         var attributions = [SypStrings.propulsedByLink];
7         if (this.map && this.map.layers) {
8             for(var i=0, len=this.map.layers.length; i<len; i++) {
9                 var layer = this.map.layers[i];
10                 if (layer.attribution && layer.getVisibility()) {
11                     attributions.push( layer.attribution );
12                 }
13             }  
14             this.div.innerHTML = attributions.join(this.separator);
15         }
16     }
17 });
18
19 var SYP = {
20     Settings: {
21         MARKER_ICON: "media/marker-normal.png",
22         MARKER_SELECT_ICON: "media/marker-selected.png",
23         MARKER_HEIGHT: 25
24     },
25
26     map: null,
27     baseLayer: null,
28     dataLayer: null,
29     selectControl: null,
30
31     init: function() {
32         this.map = new OpenLayers.Map("map", {
33             controls:[
34                 new OpenLayers.Control.SypAttribution(),
35                 new OpenLayers.Control.Navigation(),
36                 new OpenLayers.Control.PanZoom(),
37                 new OpenLayers.Control.Permalink()
38             ],
39             projection: new OpenLayers.Projection("EPSG:900913"),
40             displayProjection: new OpenLayers.Projection("EPSG:4326")
41         } );
42
43         this.baseLayer = this.createBaseLayer();
44         this.dataLayer = this.createDataLayer();
45         this.map.addLayers([this.baseLayer, this.dataLayer]);
46
47         this.selectControl = this.createSelectControl();
48         this.map.addControl(this.selectControl);
49         this.selectControl.activate();
50
51         if (!this.map.getCenter()) {
52             var centerBounds = new OpenLayers.Bounds();
53
54             var mapProj = this.map.getProjectionObject();
55             var sypOrigProj = new OpenLayers.Projection("EPSG:4326");
56
57             var bottomLeft = new OpenLayers.LonLat(sypOrig[0],sypOrig[1]);
58             bottomLeft = bottomLeft.transform(sypOrigProj, mapProj);
59             var topRight = new OpenLayers.LonLat(sypOrig[2],sypOrig[3])
60             topRight = topRight.transform(sypOrigProj, mapProj);
61
62             centerBounds.extend(bottomLeft);
63             centerBounds.extend(topRight);
64             this.map.zoomToExtent(centerBounds);
65         }
66     },
67
68     createBaseLayer: function() {
69         return new OpenLayers.Layer.OSM("OSM");
70     },
71
72     createDataLayer: function(map) {
73         var defaultStyle = new OpenLayers.Style({
74             externalGraphic: this.Settings.MARKER_ICON,
75             graphicHeight: "${height}"
76         }, {
77             context: {
78                 height: function(feature) {
79                     var defaultHeight = SYP.Settings.MARKER_HEIGHT || 32;
80                     var increase = 4 * (feature.attributes.count - 1);
81                     return Math.min(defaultHeight + increase, 50);
82                 }
83             }
84         });
85         var selectStyle = new OpenLayers.Style({
86             externalGraphic: this.Settings.MARKER_SELECT_ICON,
87             graphicHeight: this.Settings.MARKER_HEIGHT || 32 
88         });
89         var styleMap = new OpenLayers.StyleMap (
90                         {"default": defaultStyle,
91                          "select": selectStyle});
92
93         var layer = new OpenLayers.Layer.GML("KML", "items.php", 
94            {
95                strategies: [
96                 new OpenLayers.Strategy.Cluster()
97                 ],
98             styleMap: styleMap,
99             format: OpenLayers.Format.KML, 
100             projection: this.map.displayProjection,
101             eventListeners: { scope: this,
102                               loadend: this.checkForFeatures
103                             }
104            });
105
106         return layer;
107     },
108
109     createSelectControl: function() {
110         var control = new OpenLayers.Control.SelectFeature(
111                                             this.dataLayer, {
112                                                onSelect: this.onFeatureSelect,
113                                                onUnselect: this.onFeatureUnselect,
114                                                toggle: true,
115                                                clickout: false
116                                                             });
117         return control;
118     },
119
120     checkForFeatures: function() {
121         var features = this.dataLayer.features;
122         if (features.length == 0) {
123             var message = SypStrings.noImageRegistered;
124             this.Utils.displayUserMessage(message, "warn");
125         }
126     },
127
128     createPopup: function(position, contentHTML) {
129         var popup = new OpenLayers.Popup.Anchored("popup",
130                                                   position,
131                                                   null,
132                                                   contentHTML,
133                                                   null,
134                                                   true);
135         popup.autoSize = true;
136         popup.backgroundColor = ""; // deal with it in css
137         popup.border = ""; // deal with it in css
138         popup.closeOnMove = true;
139         return popup;
140     },
141
142     onFeatureUnselect: function (feature) {
143         var map = feature.layer.map;
144         var permaControl = map.getControlsByClass("OpenLayers.Control.Permalink");
145         if (permaControl[0]) {
146             permaControl[0].div.style.display = "";
147         }
148         if (!feature.popup) {
149             this.map.events.unregister("movestart", this, this._unselect);
150             return;
151         }
152         var popup = feature.popup;
153         if (popup.visible()) {
154             popup.hide();
155         }
156     },
157
158     onFeatureSelect: function(feature) {
159         var map = feature.layer.map;
160         if (feature.attributes.count > 1) {
161             this.unselect(feature);
162             var lonlat = new OpenLayers.LonLat(feature.geometry.x, feature.geometry.y);
163             map.setCenter(lonlat, map.zoom + 1);
164             return;
165         }
166         var permaControl = map.getControlsByClass("OpenLayers.Control.Permalink");
167         if (permaControl[0]) {
168             permaControl[0].div.style.display = "none";
169         }
170         var popup = feature.popup;
171
172         var popupPos = null;
173         if (sypPopupNearFeature) {
174             popupPos = feature.geometry.getBounds().getCenterLonLat();
175         } else {
176             popupPos = SYP.Utils.brCorner(map, 8);
177         }
178
179         // we cannot reuse popup; we need to recreate it in order for IE
180         // expressions to work. Otherwise, we get a 0x0 image on second view.
181         if (popup) {
182             popup.destroy();
183         }
184         var contentHTML;
185         if (feature.cluster[0].attributes.name) {
186             contentHTML = "<h2>" +
187                           feature.cluster[0].attributes.name + 
188                           "</h2>" + 
189                           feature.cluster[0].attributes.description;
190         } else {
191             contentHTML = feature.cluster[0].attributes.description;
192         }
193         if (!contentHTML || !contentHTML.length) {
194             this.map.events.register("movestart", this, this._unselect = function () { this.unselect(feature)});
195             return;
196         }
197         popup = SYP.createPopup(popupPos, contentHTML);
198         var control = this;
199         popup.hide = function () {
200             OpenLayers.Element.hide(this.div);
201             control.unselectAll();
202         };
203         map.addPopup(popup);
204         feature.popup = popup;
205         var anchor = popup.div.getElementsByTagName("a")[0];
206         if (anchor) {
207             anchor.onclick = function() { 
208                 SYP.showBigImage(this.href);
209                 return false;
210             }
211         }
212     },
213
214     showBigImage: function (href) {
215         try {
216             document.getElementById('bigimg_container').style.display = "table";
217         } catch(e) {
218             document.getElementById('bigimg_container').style.display = "block";
219         }
220         var maxHeight = document.body.clientHeight * 0.9;
221         var maxWidth = document.body.clientWidth * 0.9;
222         document.getElementById('bigimg').style.height = "";
223         document.getElementById('bigimg').style.width = "";
224         document.getElementById('bigimg').style.maxHeight = maxHeight + "px";
225         document.getElementById('bigimg').style.maxWidth = maxWidth + "px";
226         document.getElementById('bigimg').onload = function () {
227             var icon = document.getElementById('bigimg_close');
228             icon.style.top = this.offsetTop;
229             icon.style.left = this.offsetLeft + this.clientWidth - icon.clientWidth;
230
231             var heightRatio = this.clientHeight / parseInt(this.style.maxHeight);
232             var widthRatio = this.clientWidth / parseInt(this.style.maxWidth);
233             if (heightRatio > 1 || widthRatio > 1) {
234                 if (heightRatio > widthRatio) {
235                     this.style.height = this.style.maxHeight;
236                 } else {
237                     this.style.width = this.style.maxWidth;
238                 }
239             }
240
241         };
242         document.getElementById('bigimg').src = href;
243     },
244
245     closeBigImage: function() {
246         document.getElementById('bigimg').src = "";
247         document.getElementById('bigimg').parentNode.innerHTML = document.getElementById('bigimg').parentNode.innerHTML;
248         document.getElementById('bigimg_container').style.display = "none";
249     },
250
251     Utils: {
252         brCorner: function(map, margin) {
253             var bounds = map.calculateBounds();
254             var corner = new OpenLayers.LonLat(bounds.right, bounds.bottom);
255             var cornerAsPx = map.getPixelFromLonLat(corner);
256             cornerAsPx = cornerAsPx.add( -margin, -margin);
257             corner = map.getLonLatFromPixel(cornerAsPx);
258             return corner;
259         },
260         displayUserMessage: function(message, status) {
261             var div = document.getElementById('message');
262             while (div.firstChild)
263                 div.removeChild(div.firstChild);
264             var textNode = document.createTextNode(message);
265             switch (status) {
266                 case "error":
267                     div.style.color = "red";
268                     break;
269                 case "warn":
270                     div.style.color = "#FF8C00";
271                     break;
272                 case "success":
273                     div.style.color = "green";
274                     break;
275                 default:
276                     div.style.color = "black";
277                     break;
278             }
279             div.style.display = "block";
280             div.appendChild(textNode);
281         }
282     }
283 };
284
285 // if possible, determine language with HTTP_ACCEPT_LANGUAGE instead of
286 // navigator.language
287 if (OpenLayers.Lang[SypStrings.language]) {
288     OpenLayers.Lang.setCode(SypStrings.language);
289 }
290
291 // avoid alerts
292 OpenLayers.Console.userError = function(error) { 
293     SYP.Utils.displayUserMessage(error, "error");
294 }
295
296 // sometimes, especially when cache is clear, firefox does not compute
297 // correctly popup size. That's because at the end of getRenderedDimensions,
298 // dimensions of image is not known. So, we work around that problem by setting
299 // image width and image height. That way, dimensions of image are set in
300 // innerHTML, and are therefore known in getRenderedDimensions
301 OpenLayers.Popup.Anchored.prototype.registerImageListeners = function() {
302     var onImgLoad = function() {
303         this.img.width =  this.img.width;
304         this.img.height =  this.img.height;
305         this.popup.updateSize();
306         OpenLayers.Event.stopObserving(
307             this.img, "load", this.img._onImageLoad
308         );
309     };
310
311     var images = this.contentDiv.getElementsByTagName("img");
312     for (var i = 0, len = images.length; i < len; i++) {
313         var img = images[i];
314         if (img.width == 0 || img.height == 0) {
315
316             var context = {
317                 'popup': this,
318                 'img': img
319             };
320
321             img._onImgLoad = OpenLayers.Function.bind(onImgLoad, context);
322
323             OpenLayers.Event.observe(img, 'load', img._onImgLoad);
324         }    
325     } 
326 }