]> dev.renevier.net Git - syj.git/blobdiff - public/js/syj.js
separate route to create a new path, and to update an existing one
[syj.git] / public / js / syj.js
index efff5ea89325c840aed04f7f4888e88453e572a9..f2ae9e19125230221488fe2c8eb6654a50550533 100644 (file)
@@ -4,11 +4,12 @@ Element.addMethods('input', {
     observe : Element.Methods.observe.wrap(function(proceed, element, eventName, handler) {
         if (eventName === "contentchange") {
             proceed(element, 'keyup', function(evt) {
-                if (evt.keyCode == 13) {
+                if (evt.keyCode === 13) {
                     return;
                 }
                 handler.apply(null, arguments);
             });
+            proceed(element, 'paste', handler);
             return proceed(element, 'change', handler);
         }
         return proceed(element, eventName, handler);
@@ -46,8 +47,7 @@ var SyjSaveUI = {
         }
         this.enableSubmit();
         $("geom_title").disabled = false;
-        $("geom_title").focus();
-        $("geom_title").select();
+        $("geom_title").activate();
         $("geomform").removeClassName("disabled");
         this.status = "enabled";
         return this;
@@ -80,10 +80,6 @@ var SyjSaveUI = {
 };
 
 var SyjEditUI = {
-    init: function() {
-        return this;
-    },
-
     hide: function() {
         $("edit-btn").blur();
         $("edit-btn").hide();
@@ -229,9 +225,10 @@ var SYJView = {
     map: null,
     wkt: new OpenLayers.Format.WKT({ internalProjection: Mercator, externalProjection: WGS84 }),
     needsFormResubmit: false,
+    hasInitialGeom: false,
 
     init: function() {
-        var externalGraphic, baseURL, baseLayer, layerOptions, extent = null, hidemessenger;
+        var externalGraphic, baseURL, baseLayer, layerOptions, extent, hidemessenger;
 
         // is svg context, opera does not resolve links with base element is svg context
         externalGraphic = styleMap.edit.styles.select.defaultStyle.externalGraphic;
@@ -252,8 +249,8 @@ var SYJView = {
         layerOptions = {format:     OpenLayers.Format.WKT,
                         projection: WGS84,
                         styleMap:   styleMap.view};
-        if (gLoggedInfo.ownername) {
-            layerOptions.attribution = SyjStrings.routeBy + ' ' + '<strong>' + gLoggedInfo.ownername + '</strong>';
+        if (gLoggedInfo.creatorname) {
+            layerOptions.attribution = SyjStrings.routeBy + ' ' + '<strong>' + gLoggedInfo.creatorname + '</strong>';
         }
 
         this.viewLayer = new OpenLayers.Layer.Vector("View Layer", layerOptions);
@@ -263,7 +260,6 @@ var SYJView = {
             this.messenger.hide();
             this.editMode();
         }).bind(this));
-        SyjEditUI.init().hide();
 
         $("geomform").ajaxize({
                 presubmit: this.prepareForm.bind(this),
@@ -283,18 +279,16 @@ var SYJView = {
             this.messenger.hide();
         }
 
-        extent = null;
         if ($("geom_data").value) {
             this.viewLayer.addFeatures([this.wkt.read($("geom_data").value)]);
             extent = this.viewLayer.getDataExtent();
             // XXX: ie has not guessed height of map main div yet during map
             // initialisation. Now, it will read it correctly.
             this.map.updateSize();
-            SyjEditUI.show();
+            this.hasInitialGeom = true;
         } else {
             extent = new OpenLayers.Bounds(gMaxExtent.minlon, gMaxExtent.minlat, gMaxExtent.maxlon, gMaxExtent.maxlat)
                                          .transform(WGS84, Mercator);
-            this.editMode();
         }
         this.map.zoomToExtent(extent);
         document.observe('simplebox:shown', this.observer.bindAsEventListener(this));
@@ -307,6 +301,13 @@ var SYJView = {
     },
 
     prepareForm: function(form) {
+        if (!loginMgr.logged && !$("geom_accept").checked) {
+            this.messenger.setMessage(SyjStrings.acceptTermsofuseWarn, "warn");
+            $("geom_accept_container").highlight('#F08080');
+            $("geom_accept").activate();
+            return false;
+        }
+
         var line, realPoints, idx, handler;
 
         line = new OpenLayers.Geometry.LineString();
@@ -327,6 +328,7 @@ var SYJView = {
         this.needsFormResubmit = false;
         SyjSaveUI.disable.bind(SyjSaveUI).defer();
         this.messenger.hide();
+        return true;
     },
 
     editMode: function() {
@@ -394,12 +396,12 @@ var SYJView = {
     },
 
     saveSuccess: function(transport) {
-      if (!$("geom_id").value) {
+      if (!this.hasInitialGeom) { // we have created a new path, change location
           location = "idx/" + transport.responseText;
           return;
       }
-      this.messenger.setMessage(SyjStrings.saveSuccess, "success");
 
+      this.messenger.setMessage(SyjStrings.saveSuccess, "success");
       SyjSaveUI.hide();
       SyjEditUI.show();
       document.title = $('geom_title').value;
@@ -411,20 +413,15 @@ var SYJView = {
         if (transport) {
             httpCode = transport.getStatus();
         }
-        message = "";
         switch (httpCode) {
             case 0:
                 message = SyjStrings.notReachedError;
             break;
             case 400:
             case 404:
-            case 410:
                 message = SyjStrings.requestError; // default message
                 if (transport.responseJSON) {
                     switch (transport.responseJSON.message) {
-                        case "unreferenced":
-                            message = SyjStrings.unreferencedError;
-                        break;
                         case "uniquepath":
                             message = SyjStrings.uniquePathError;
                         break;
@@ -433,15 +430,8 @@ var SYJView = {
                     }
                 }
             break;
-            case 403:
-                message = "";
-                this.needsFormResubmit = true;
-                if (loginMgr.hasAlreadyConnected()) {
-                    SYJLogin.messenger.setMessage(SyjStrings.cookiesNeeded, "warn");
-                } else {
-                    SYJLogin.messenger.setMessage(SyjStrings.loginNeeded, "warn");
-                }
-                SYJLogin.modalbox.show();
+            case 410:
+                message = SyjStrings.gonePathError;
             break;
             case 500:
                 message = SyjStrings.serverError;
@@ -491,7 +481,7 @@ var SYJModalClass = Class.create({
     checkNotEmpty: function(input, message) {
         if ($(input).value.strip().empty()) {
             this.messenger.setMessage(message, "warn");
-            $(input).highlight('#F08080').focus();
+            $(input).highlight('#F08080').activate();
             return false;
         }
         return true;
@@ -505,8 +495,7 @@ var SYJModalClass = Class.create({
             if (simplebox === this.modalbox) {
                 input = this.area.select('input[type="text"]')[0];
                 (function () {
-                    input.focus();
-                    input.select();
+                    input.activate();
                 }).defer();
             } else {
                 this.modalbox.hide();
@@ -543,8 +532,7 @@ var SYJModalClass = Class.create({
 
         this.messenger.setMessage(message, "error");
         input = this.area.select('input[type="text"]')[0];
-        input.highlight('#F08080').focus();
-        input.select();
+        input.highlight('#F08080').activate();
     },
 
     reset: function() {
@@ -561,7 +549,7 @@ var SYJUserClass = Class.create(SYJModalClass, {
         $super();
         $("termsofusearea").hide();
 
-        $("user_termsofuse_anchor").observe("click", function(evt) {
+        $$("#user_termsofuse_anchor, #geom_termsofuse_anchor").invoke('observe', "click", function(evt) {
             if (!this.toubox) {
                 $("termsofusearea").show();
                 $("termsofuseiframe").setAttribute("src", evt.target.href);
@@ -586,6 +574,19 @@ var SYJUserClass = Class.create(SYJModalClass, {
                 $("user_password-desc").setMessageStatus("success");
             }
         }.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();
+        });
     },
 
     presubmit: function() {
@@ -593,10 +594,9 @@ var SYJUserClass = Class.create(SYJModalClass, {
             return false;
         }
 
-        if (!($("user_pseudo").value.match(/[a-zA-Z0-9_.]+$/))) {
+        if (!($("user_pseudo").value.match(/^[a-zA-Z0-9_.]+$/))) {
             this.messenger.setMessage(SyjStrings.invalidPseudo, "warn");
-            $("user_pseudo").highlight('#F08080').focus();
-            $("user_pseudo").select();
+            $("user_pseudo").highlight('#F08080').activate();
             return false;
         }
 
@@ -606,15 +606,13 @@ var SYJUserClass = Class.create(SYJModalClass, {
 
         if ($("user_password").value.length < 6) {
             $("user_password-desc").setMessageStatus("warn");
-            $("user_password").highlight('#F08080').focus();
-            $("user_password").select();
+            $("user_password").highlight('#F08080').activate();
             return false;
         }
 
         if ($("user_password").value !== $("user_password_confirm").value) {
             this.messenger.setMessage(SyjStrings.passwordNoMatchWarn, "warn");
-            $("user_password").highlight('#F08080').focus();
-            $("user_password").select();
+            $("user_password").highlight('#F08080').activate();
             return false;
         }
 
@@ -624,7 +622,8 @@ var SYJUserClass = Class.create(SYJModalClass, {
 
         if (!$("user_accept").checked) {
             this.messenger.setMessage(SyjStrings.acceptTermsofuseWarn, "warn");
-            $("user_accept").highlight('#F08080').focus();
+            $("user_accept_container").highlight('#F08080');
+            $("user_accept").activate();
             return false;
         }
 
@@ -638,7 +637,7 @@ var SYJUserClass = Class.create(SYJModalClass, {
         this.modalbox.hide();
         if (SYJView.needsFormResubmit) {
             SYJView.messenger.addMessage(SyjStrings.canResubmit);
-            $("geom_submit").focus();
+            $("geom_submit").activate();
         }
     },
 
@@ -676,8 +675,7 @@ var SYJUserClass = Class.create(SYJModalClass, {
         if (message) {
             this.messenger.setMessage(message, "error");
             if (focusInput) {
-                focusInput.highlight('#F08080').focus();
-                focusInput.select();
+                focusInput.highlight('#F08080').activate();
             }
             return;
         }
@@ -713,7 +711,7 @@ var SYJLoginClass = Class.create(SYJModalClass, {
         this.modalbox.hide();
         if (SYJView.needsFormResubmit) {
             SYJView.messenger.addMessage(SyjStrings.canResubmit);
-            $("geom_submit").focus();
+            $("geom_submit").activate();
         }
     },
 
@@ -737,8 +735,7 @@ var SYJLoginClass = Class.create(SYJModalClass, {
         if (message) {
             this.messenger.setMessage(message, "error");
             if (focusInput) {
-                focusInput.highlight('#F08080').focus();
-                focusInput.select();
+                focusInput.highlight('#F08080').activate();
             }
             return;
         }
@@ -775,30 +772,27 @@ var loginMgr = Object.extend(gLoggedInfo, {
         }
         if (this.logged) {
             this.controlsdeck.setIndex(1);
+            $$(".logged-hide").invoke('hide');
+            $$(".logged-show").invoke('show');
         } else {
             this.controlsdeck.setIndex(0);
+            $$(".logged-hide").invoke('show');
+            $$(".logged-show").invoke('hide');
         }
 
-        if (this.isowner) {
+        if (this.iscreator) {
             $("data_controls").show();
         } else {
             $("data_controls").hide();
         }
     },
 
-    login: function(aIsOwner) {
-        if (typeof aIsOwner === "boolean") {
-            this.isowner = aIsOwner;
+    login: function(aIsCreator) {
+        if (typeof aIsCreator === "boolean") {
+            this.iscreator = aIsCreator;
         }
         this.logged = true;
-        this.connections++;
         this.updateUI();
-    },
-
-    // needed in case of 403 errors: if user has already connected successfully
-    // before, it probably means he has cookies disabled
-    hasAlreadyConnected: function() {
-        return (this.connections >= 1);
     }
 });