]> dev.renevier.net Git - syj.git/blobdiff - public/js/syj.js
css improvements for nominatim-suggestions
[syj.git] / public / js / syj.js
index 112c6f762530d03ba2c67a0144dc98ab075911e3..e33ea4e306cfef58018c21641c4a1b98e9595748 100644 (file)
@@ -14,18 +14,6 @@ var SyjSaveUI = {
         return this;
     },
 
-    hide: function() {
-        $("geom_submit").blur();
-        $("geom_title").blur();
-        $("geomform").hide();
-        return this;
-    },
-
-    show: function() {
-        $("geomform").show();
-        return this;
-    },
-
     enable: function() {
         if (this.status === "enabled") {
             return this;
@@ -52,6 +40,7 @@ var SyjSaveUI = {
 
     enableSubmit: function() {
         $("geom_submit").disabled = false;
+        $("geom_accept").disabled = false;
         this.status = "partial";
         return this;
     },
@@ -59,23 +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 SyjEditUI = {
-    hide: function() {
-        $("data_controls_btns").blur();
-        $("data_controls_btns").hide();
-        return this;
-    },
+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);
+            }
 
-    show: function() {
-        $("data_controls_btns").show();
-        return this;
-    }
-};
+            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;
+        },
+        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) {
@@ -234,41 +287,38 @@ 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]);
 
         if ($("edit-btn")) {
-            $("edit-btn").observe('click', (function() {
+            $("edit-btn").observe('click', function() {
                 $("geom_submit").value = SyjStrings.editAction;
                 this.messenger.hide();
                 this.editMode();
                 this.mode = 'edit';
-            }).bind(this));
+            }.bind(this));
         }
 
         if ($("create-btn")) {
-            $("create-btn").observe('click', (function() {
+            $("create-btn").observe('click', function() {
                 $("geom_submit").value = SyjStrings.createAction;
                 this.messenger.hide();
                 this.editMode();
                 this.mode = 'create';
-            }).bind(this));
+            }.bind(this));
         }
 
         if ($("clone-btn")) {
-            $("clone-btn").observe('click', (function() {
+            $("clone-btn").observe('click', function() {
                 $("geom_submit").value = SyjStrings.cloneAction;
                 $("geom_title").value = "";
                 this.messenger.hide();
                 this.editMode();
                 this.mode = 'create';
-                SyjSaveUI.enableSubmit();
-            }).bind(this));
+            }.bind(this));
         }
 
         $("geomform").ajaxize({
@@ -276,7 +326,7 @@ var SYJView = {
                 onSuccess: this.saveSuccess.bind(this),
                 onFailure: this.saveFailure.bind(this)
                 });
-        SyjSaveUI.init().hide();
+        SyjSaveUI.init();
 
         this.messenger = $('message');
         hidemessenger = this.messenger.empty();
@@ -289,8 +339,8 @@ var SYJView = {
             this.messenger.hide();
         }
 
-        if ($("geom_data").value) {
-            this.viewLayer.addFeatures([this.wkt.read($("geom_data").value)]);
+        if (typeof gInitialGeom !== "undefined" && typeof gInitialGeom.data !== "undefined") {
+            this.viewLayer.addFeatures([this.wkt.read(gInitialGeom.data)]);
             extent = this.viewLayer.getDataExtent();
             // XXX: ie has not guessed height of map main div yet during map
             // initialisation. Now, it will read it correctly.
@@ -301,6 +351,7 @@ var SYJView = {
         }
         this.map.zoomToExtent(extent);
         document.observe('simplebox:shown', this.observer.bindAsEventListener(this));
+        SYJPathLength.update();
     },
 
     observer: function(evt) {
@@ -378,12 +429,11 @@ var SYJView = {
 
         this.viewLayer.destroyFeatures();
 
-        SyjEditUI.hide();
+        SYJDataUi.editmode();
         if (this.editControl.handler.realPoints && this.editControl.handler.realPoints.length >= 2) {
-            SyjSaveUI.show();
             SyjSaveUI.disableSubmit();
         } else {
-            SyjSaveUI.show().disable();
+            SyjSaveUI.disable();
         }
     },
 
@@ -397,10 +447,14 @@ 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 = {};
+                    }
                     if (this.handler.realPoints.length < 2) {
-                        SyjSaveUI.show().disable();
+                        SyjSaveUI.disable();
                     } else {
-                        SyjSaveUI.show().enable();
+                        SyjSaveUI.enable();
                     }
                 }
             },
@@ -425,14 +479,13 @@ var SYJView = {
             callback: function(form) {
                 this.viewMode();
                 this.mode = 'view';
-                SyjSaveUI.hide();
-                SyjEditUI.show();
+                SYJDataUi.viewmode();
                 this.messenger.hide();
 
-                if (typeof this.unsavedRoute.features !== "undefined") {
+                if (this.unsavedRoute && typeof this.unsavedRoute.features !== "undefined") {
                     this.viewLayer.addFeatures(this.unsavedRoute.features);
                 }
-                if (typeof this.unsavedRoute.title !== "undefined") {
+                if (this.unsavedRoute && typeof this.unsavedRoute.title !== "undefined") {
                     $("geom_title").value = this.unsavedRoute.title;
                 } else {
                     $("geom_title").value = "";
@@ -443,15 +496,15 @@ var SYJView = {
     },
 
     saveSuccess: function(transport) {
+      this.unsavedRoute = null;
+
       if (transport.responseJSON && (typeof transport.responseJSON.redirect === "string")) {
           location = transport.responseJSON.redirect;
           return;
       }
 
       this.messenger.setMessage(SyjStrings.saveSuccess, "success");
-      SyjSaveUI.hide();
-      SyjEditUI.show();
-      this.unsavedRoute = null;
+      SYJDataUi.viewmode();
       document.title = $('geom_title').value;
     },
 
@@ -605,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));
 
@@ -642,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() {
@@ -778,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) {
@@ -892,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) {
@@ -967,11 +1021,164 @@ 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();
     SYJUser.init();
+    SYJDataUi.viewmode();
     SYJView.init();
     SYJNewpwd.init();
     LoginMgr.updateUI();
+    Nominatim.init();
 });
+
+window.onbeforeunload = function() {
+    if (SYJView.unsavedRoute) {
+        return SyjStrings.unsavedConfirmExit;
+    } else {
+        return undefined;
+    }
+};