]> dev.renevier.net Git - syj.git/blobdiff - public/js/syj.js
remove close button for geom form
[syj.git] / public / js / syj.js
index b3c8d800cbe3a5a74f2eb9ab812958a99d6d345a..50184b2a6974fe0c97dc8f43db21a78d609166d0 100644 (file)
@@ -55,20 +55,77 @@ var SyjSaveUI = {
     }
 };
 
+var SYJPathLength = (function(){
+    return {
+        update: function() {
+            var pathLength = 0, unit;
+            if (SYJView.mode === 'view') {
+                if (SYJView.viewLayer.features.length) {
+                    pathLength = SYJView.viewLayer.features[0].geometry.getGeodesicLength(Mercator);
+                }
+            } else {
+                pathLength = SYJView.editControl.handler.line.geometry.getGeodesicLength(Mercator);
+            }
+
+            if (pathLength === 0) {
+                $("path-length").hide();
+                return;
+            }
+            $("path-length").show();
+
+            if (pathLength < 1000) {
+                // precision: 1 cm
+                pathLength = Math.round(pathLength * 100) / 100;
+                unit = 'm';
+            } else {
+                // precision: 1 m
+                pathLength = Math.round(pathLength) / 1000;
+                unit = 'km';
+            }
+            $("path-length-content").update(pathLength + ' ' + unit);
+        }
+    };
+}());
+
 var SYJDataUi = (function() {
     var deck = null,
+        infotoggler = null,
         getdeck = function() {
-        if (!deck) {
-            deck = new Deck("data_controls");
-        }
-        return deck;
-    };
+            if (!deck) {
+                deck = new Deck("data_controls");
+            }
+            return deck;
+        },
+        getinfotoggler = function() {
+            if (!infotoggler) {
+                infotoggler = new Toggler('path-infos-content');
+                $("path-infos-toggler").insert({bottom: infotoggler.element});
+                $("path-infos-anchor").observe('click', function(evt) {
+                    evt.stop();
+                    infotoggler.toggle(evt);
+                });
+                document.observe('toggler:open', function(evt) {
+                    if (evt.memo === infotoggler) {
+                        // XXX: update informations
+                    }
+                });
+            }
+            return infotoggler;
+        };
     return {
         viewmode: function() {
             getdeck().setIndex(0);
+            if ($("path-infos")) {
+                getinfotoggler();
+                getinfotoggler().close();
+                $("path-infos").show();
+            }
         },
         editmode: function() {
             getdeck().setIndex(1);
+            if ($("path-infos")) {
+                $("path-infos").hide();
+            }
         }
     };
 }());
@@ -294,6 +351,67 @@ var SYJView = {
         }
         this.map.zoomToExtent(extent);
         document.observe('simplebox:shown', this.observer.bindAsEventListener(this));
+        SYJPathLength.update();
+
+        if (window.FileList && window.FileReader) {
+            $("map").observe("dragenter", function(evt) { evt.stop();});
+            $("map").observe("dragover", function(evt) { evt.stop();});
+            $("map").observe("drop", function(evt) {
+                evt.stop();
+                if (this.mode !== "view" || this.viewLayer.features.length) {
+                    return;
+                }
+                if (!evt.dataTransfer.files.length) {
+                    return;
+                }
+                var file = evt.dataTransfer.files[0];
+                var reader = new FileReader();
+                var readerror = function() {
+                    this.messenger.setMessage(SyjStrings.dragFileError, "warn");
+                }.bind(this);
+                reader.onload = function(evt) {
+                    if (evt.error) {
+                        readerror();
+                        return;
+                    }
+
+                    var results = null;
+                    var content = evt.target.result;
+
+                    var engine;
+                    var formats = ['KML', 'GPX'];
+
+                    for (var i = 0; i < formats.length; i++) {
+                        engine = new OpenLayers.Format[formats[i]]({ internalProjection: Mercator, externalProjection: WGS84 });
+                        try {
+                            results = engine.read(content);
+                        } catch(e) {
+                        }
+                        if (results || results.length) {
+                            break;
+                        }
+                    }
+                    if (!results || !results.length) {
+                        readerror();
+                        return;
+                    }
+
+
+                    var vector = results[0];
+                    if (vector.geometry.CLASS_NAME !== "OpenLayers.Geometry.LineString") {
+                        readerror();
+                        return;
+                    }
+                    this.viewLayer.addFeatures([vector]);
+                    this.map.zoomToExtent(this.viewLayer.getDataExtent());
+                    this.editMode();
+                    if (vector.data && vector.data.name) {
+                        $("geom_title").value = vector.data.name;
+                    }
+                 }.bind(this);
+                reader.readAsText(file);
+           }.bind(this));
+        }
     },
 
     observer: function(evt) {
@@ -389,6 +507,7 @@ var SYJView = {
         this.editControl = new OpenLayers.Control.DrawFeature(new OpenLayers.Layer.Vector(), OpenLayers.Handler.SyjModifiablePath, {
             callbacks: {
                 modify: function(f, line) {
+                    SYJPathLength.update();
                     if (!SYJView.unsavedRoute) {
                         SYJView.unsavedRoute = {};
                     }
@@ -412,28 +531,6 @@ var SYJView = {
             styles = this.editControl.handler.layerOptions.styleMap.styles;
             styles.select = styles.select_for_canvas;
         }
-        new CloseBtn($("geomform"), {
-            style : {
-                marginRight: "-40px",
-                marginTop: "-20px"
-            },
-            callback: function(form) {
-                this.viewMode();
-                this.mode = 'view';
-                SYJDataUi.viewmode();
-                this.messenger.hide();
-
-                if (this.unsavedRoute && typeof this.unsavedRoute.features !== "undefined") {
-                    this.viewLayer.addFeatures(this.unsavedRoute.features);
-                }
-                if (this.unsavedRoute && typeof this.unsavedRoute.title !== "undefined") {
-                    $("geom_title").value = this.unsavedRoute.title;
-                } else {
-                    $("geom_title").value = "";
-                }
-                this.unsavedRoute = null;
-            }.bind(this)
-        });
     },
 
     saveSuccess: function(transport) {
@@ -987,7 +1084,7 @@ var Nominatim = (function() {
             center = bounds.getCenterLonLat().wrapDateLine(maxExtent);
         }
         this.setCenter(center, this.getZoomForExtent(bounds), false, true);
-    }
+    };
 
     var success = function(transport) {
         $("nominatim-throbber").hide();
@@ -1031,13 +1128,12 @@ var Nominatim = (function() {
                 });
 
                 anchor.observe('click', clickhandler(item.boundingbox));
+                Element.text(anchor, item.display_name);
 
-                var text = document.createTextNode(item.display_name);
                 var icon = new Element("img", {
                     className: "nominatim-suggestions-icon",
                     src: item.icon || 'icons/world.png'
                 });
-                anchor.appendChild(text); // insert does not work; see prototype #1125
                 li.insert(icon).insert(anchor);
                 $("nominatim-suggestions-list").insert(li);
                 if ($("nominatim-suggestions-list").childNodes.length >= 6) {
@@ -1047,7 +1143,10 @@ var Nominatim = (function() {
         }
 
         if ($("nominatim-suggestions-list").childNodes.length > 1) {
-            $("nominatim-suggestions").show();
+            var bottomOffset = $('data_controls').measure('height') + 3;
+            $("nominatim-suggestions").setStyle({
+                bottom: (document.viewport.getHeight() - $('data_controls').cumulativeOffset().top + 3).toString() + 'px'
+            }).show();
             $("nominatim-suggestions-list").select("li:first-child")[0].addClassName('current');
         } else {
             $("nominatim-suggestions").hide();