]> dev.renevier.net Git - syj.git/blobdiff - public/js/syj.js
css improvements for nominatim-suggestions
[syj.git] / public / js / syj.js
index 81fbc7aa97326d88eac364cc04d337b8aac993e1..e33ea4e306cfef58018c21641c4a1b98e9595748 100644 (file)
@@ -40,6 +40,7 @@ var SyjSaveUI = {
 
     enableSubmit: function() {
         $("geom_submit").disabled = false;
+        $("geom_accept").disabled = false;
         this.status = "partial";
         return this;
     },
@@ -47,28 +48,87 @@ var SyjSaveUI = {
     disableSubmit: function() {
         $("geom_submit").blur();
         $("geom_submit").disabled = true;
+        $("geom_accept").blur();
+        $("geom_accept").disabled = true;
         this.status = "partial";
         return this;
     }
 };
 
-var SYJDataUi = function() {
-    var deck = null;
-    var getdeck = function() {
-        if (!deck) {
-            deck = new Deck("data_controls");
+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);
         }
-        return deck;
-    }
+    };
+}());
+
+var SYJDataUi = (function() {
+    var deck = null,
+        infotoggler = null,
+        getdeck = function() {
+            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();
+            }
         }
-    }
-}();
+    };
+}());
 
 OpenLayers.Handler.SyjModifiablePath = OpenLayers.Class(OpenLayers.Handler.ModifiablePath, {
     mouseup: function(evt) {
@@ -227,10 +287,8 @@ var SYJView = {
 
         layerOptions = {format:     OpenLayers.Format.WKT,
                         projection: WGS84,
-                        styleMap:   styleMap.view};
-        if (gLoggedInfo.creatorname) {
-            layerOptions.attribution = SyjStrings.routeBy + ' ' + '<strong>' + gLoggedInfo.creatorname + '</strong>';
-        }
+                        styleMap:   styleMap.view,
+                        attribution: SyjStrings.geomAttribution };
 
         this.viewLayer = new OpenLayers.Layer.Vector("View Layer", layerOptions);
         this.map.addLayers([baseLayer, this.viewLayer]);
@@ -260,7 +318,6 @@ var SYJView = {
                 this.messenger.hide();
                 this.editMode();
                 this.mode = 'create';
-                SyjSaveUI.enableSubmit();
             }.bind(this));
         }
 
@@ -294,6 +351,7 @@ var SYJView = {
         }
         this.map.zoomToExtent(extent);
         document.observe('simplebox:shown', this.observer.bindAsEventListener(this));
+        SYJPathLength.update();
     },
 
     observer: function(evt) {
@@ -389,6 +447,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 = {};
                     }
@@ -599,13 +658,15 @@ var SYJUserClass = Class.create(SYJModalClass, {
 
         $$("#user_termsofuse_anchor, #geom_termsofuse_anchor").invoke('observe', "click", function(evt) {
             if (!this.toubox) {
-                $("termsofusearea").show();
-                $("termsofuseiframe").setAttribute("src", evt.target.href);
                 this.toubox = new SimpleBox($("termsofusearea"), {
                     closeMethods: ["onescapekey", "onouterclick", "onbutton"]
                 });
             }
             this.toubox.show();
+            if (!$("termsofuseiframe").getAttribute("src")) {
+                $("termsofusearea").show();
+                $("termsofuseiframe").setAttribute("src", evt.target.href);
+            }
             evt.stop();
         }.bindAsEventListener(this));
 
@@ -636,18 +697,7 @@ var SYJUserClass = Class.create(SYJModalClass, {
             }
         }.bindAsEventListener(this));
 
-        $("account-info").hide();
-        $("account-info-bullet").observe('click', function(evt) {
-            var elt = $("account-info");
-            if (elt.visible()) {
-                evt.target.src = "icons/bullet_arrow_right.png";
-                elt.hide();
-            } else {
-                evt.target.src = "icons/bullet_arrow_down.png";
-                elt.show();
-            }
-            evt.stop();
-        });
+        $('account-create-anchor').insert({after: new Toggler('account-info').element});
     },
 
     presubmit: function() {
@@ -772,11 +822,21 @@ var SYJLoginClass = Class.create(SYJModalClass, {
     },
 
     success: function(transport) {
-        if (transport.responseText === "1") {
-            LoginMgr.login(true);
-        } else {
-            LoginMgr.login();
+        if (!transport.responseJSON ||
+            typeof transport.responseJSON.iscreator !== "boolean" ||
+            typeof transport.responseJSON.pseudo !== "string"
+            ) {
+            this.messenger.setMessage(SyjStrings.unknownError, "error");
+            return;
         }
+        LoginMgr.login(transport.responseJSON.iscreator);
+        $$('.logged-pseudo').each(function(elt) {
+            $A(elt.childNodes).filter(function(node) {
+                return (node.nodeType === 3 || node.tagName.toLowerCase() === 'br');
+            }).each(function(node) {
+                node.nodeValue = node.nodeValue.replace('%s', transport.responseJSON.pseudo);
+            });
+        });
         SYJView.messenger.setMessage(SyjStrings.loginSuccess, "success");
         this.modalbox.hide();
         if (SYJView.needsFormResubmit) {
@@ -886,7 +946,7 @@ var PseudoChecker = {
 
             $("user_pseudo").up('tr').insert({after: row});
             this.messageelt = new Element('span');
-            this.throbber = new Element("img", { src: "icons/pseudo-throbber.gif"});
+            this.throbber = new Element("img", { src: "icons/throbber.gif"});
             row.down('div').insert(this.throbber).insert(this.messageelt);
         }
         if (throbber) {
@@ -961,6 +1021,149 @@ var PseudoChecker = {
     }
 };
 
+var Nominatim = (function() {
+    var presubmit = function() {
+        var input = $("nominatim-search");
+        if (input.value.strip().empty()) {
+            $("nominatim-message").setMessage(SyjStrings.notEmptyField, "warn");
+            input.activate();
+            return false;
+        }
+        $("nominatim-suggestions").hide();
+        $("nominatim-message").hide();
+        $("nominatim-throbber").show();
+        return true;
+    };
+
+    var zoomToExtent = function(bounds) { // we must call map.setCenter with forceZoomChange to true. See ol#2798
+        var center = bounds.getCenterLonLat();
+        if (this.baseLayer.wrapDateLine) {
+            var maxExtent = this.getMaxExtent();
+            bounds = bounds.clone();
+            while (bounds.right < bounds.left) {
+                bounds.right += maxExtent.getWidth();
+            }
+            center = bounds.getCenterLonLat().wrapDateLine(maxExtent);
+        }
+        this.setCenter(center, this.getZoomForExtent(bounds), false, true);
+    }
+
+    var success = function(transport) {
+        $("nominatim-throbber").hide();
+
+        if (!transport.responseJSON || !transport.responseJSON.length) {
+            $("nominatim-message").setMessage(SyjStrings.noResult, 'error');
+            $("nominatim-search").activate();
+            return;
+        }
+
+        var place = transport.responseJSON[0],
+            bbox = place.boundingbox;
+
+        if (!bbox || bbox.length !== 4) {
+            $("nominatim-message").setMessage(SyjStrings.requestError, 'error');
+            return;
+        }
+
+        extent = new OpenLayers.Bounds(bbox[2], bbox[1], bbox[3], bbox[0]).transform(WGS84, Mercator);
+        zoomToExtent.call(SYJView.map, extent);
+
+        $("nominatim-suggestions-list").update();
+
+        var clickhandler = function(bbox) {
+            return function(evt) {
+                evt.stop();
+                var extent = new OpenLayers.Bounds(bbox[2], bbox[1], bbox[3], bbox[0]).transform(WGS84, Mercator);
+                $("nominatim-suggestions-list").select("li").invoke('removeClassName', 'current');
+                evt.target.up('li').addClassName('current');
+                SYJView.map.zoomToExtent(extent);
+            };
+        };
+
+        for (var i = 0; i < transport.responseJSON.length; i++) {
+            var item = transport.responseJSON[i];
+            if (item.display_name && item.boundingbox && item.boundingbox.length === 4) {
+                var li = new Element("li");
+                var anchor = new Element("a", {
+                    href: "",
+                    className: "nominatim-suggestions-link"
+                });
+
+                anchor.observe('click', clickhandler(item.boundingbox));
+                Element.text(anchor, item.display_name);
+
+                var icon = new Element("img", {
+                    className: "nominatim-suggestions-icon",
+                    src: item.icon || 'icons/world.png'
+                });
+                li.insert(icon).insert(anchor);
+                $("nominatim-suggestions-list").insert(li);
+                if ($("nominatim-suggestions-list").childNodes.length >= 6) {
+                    break;
+                }
+            }
+        }
+
+        if ($("nominatim-suggestions-list").childNodes.length > 1) {
+            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();
+        }
+
+    };
+
+    var failure = function(transport) {
+        $("nominatim-throbber").hide();
+
+        var httpCode = 0, message = SyjStrings.unknownError, input; // default message error
+
+        if (transport) {
+            httpCode = transport.getStatus();
+        }
+
+        switch (httpCode) {
+            case 0:
+                message = SyjStrings.notReachedError;
+            break;
+            case 400:
+            case 404:
+                message = SyjStrings.requestError;
+            break;
+            case 500:
+                message = SyjStrings.serverError;
+            break;
+        }
+
+        $("nominatim-message").setMessage(message, 'error');
+    };
+
+    return {
+        init: function() {
+            if (!$("nominatim-form")) {
+               return;
+            }
+            $("nominatim-controls").hide();
+            $("nominatim-label").observe('click', function(evt) {
+                $("nominatim-controls").show();
+                $("nominatim-search").activate();
+                evt.stop();
+            });
+
+            $("nominatim-form").ajaxize({
+                presubmit: presubmit,
+                onSuccess: success,
+                onFailure: failure
+              });
+            new CloseBtn($("nominatim-suggestions"));
+
+            $$("#nominatim-message, #nominatim-suggestions, #nominatim-throbber").invoke('hide');
+        }
+    };
+}());
 
 document.observe("dom:loaded", function() {
     SYJLogin.init();
@@ -969,7 +1172,9 @@ document.observe("dom:loaded", function() {
     SYJView.init();
     SYJNewpwd.init();
     LoginMgr.updateUI();
+    Nominatim.init();
 });
+
 window.onbeforeunload = function() {
     if (SYJView.unsavedRoute) {
         return SyjStrings.unsavedConfirmExit;