X-Git-Url: https://dev.renevier.net/?a=blobdiff_plain;f=public%2Fjs%2Fsyj.js;h=9e5b60202fef19c33cd334f54f3af3ff6ff1ae4b;hb=bd3de07b5ee90369741ef197230b4744f33eeda1;hp=b3c8d800cbe3a5a74f2eb9ab812958a99d6d345a;hpb=db98e1f62272cff0b425133aa3b909c5cfa6183d;p=syj.git diff --git a/public/js/syj.js b/public/js/syj.js index b3c8d80..9e5b602 100644 --- a/public/js/syj.js +++ b/public/js/syj.js @@ -21,7 +21,7 @@ var SyjSaveUI = { this.enableSubmit(); $("geom_title").disabled = false; $("geom_title").activate(); - $("geomform").removeClassName("disabled"); + $$("#geom_accept_container, #geom_title_container").invoke('removeClassName', "disabled"); this.status = "enabled"; return this; }, @@ -33,7 +33,7 @@ var SyjSaveUI = { this.disableSubmit(); $("geom_title").blur(); $("geom_title").disabled = true; - $("geomform").addClassName("disabled"); + $$("#geom_accept_container, #geom_title_container").invoke('addClassName', "disabled"); this.status = "disabled"; return this; }, @@ -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(); + } } }; }()); @@ -210,7 +267,7 @@ var SYJView = { mode: 'view', init: function() { - var externalGraphic, baseURL, baseLayer, layerOptions, extent, hidemessenger; + var externalGraphic, baseURL, baseLayer, layerOptions, hidemessenger; // is svg context, opera does not resolve links with base element is svg context externalGraphic = styleMap.edit.styles.select.defaultStyle.externalGraphic; @@ -284,16 +341,44 @@ var SYJView = { 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. this.map.updateSize(); + this.map.zoomToExtent(this.viewLayer.getDataExtent()); } else { - extent = new OpenLayers.Bounds(gMaxExtent.minlon, gMaxExtent.minlat, gMaxExtent.maxlon, gMaxExtent.maxlat) - .transform(WGS84, Mercator); + this.initMaPos(gInitialPos); } - this.map.zoomToExtent(extent); + + $("map-overlay").hide(); + $("geom_upload").observe('change', function() { + $("map-overlay").show(); + SyjSaveUI.enable(); + this.editControl.deactivate(); + }.bind(this)); + document.observe('simplebox:shown', this.observer.bindAsEventListener(this)); + SYJPathLength.update(); + }, + + initMaPos: function (aPos) { + var extent = null, center = null, zoom = 0; + + if (aPos.hasOwnProperty('lon') && aPos.hasOwnProperty('lat') && aPos.hasOwnProperty('zoom')) { + center = new OpenLayers.LonLat(parseFloat(aPos.lon), parseFloat(aPos.lat)).transform(WGS84, Mercator); + zoom = parseInt(aPos.zoom); + } else if (aPos.hasOwnProperty('minlon') && aPos.hasOwnProperty('minlat') + && aPos.hasOwnProperty('maxlon') && aPos.hasOwnProperty('maxlat')) { + extent = new OpenLayers.Bounds(aPos.minlon, aPos.minlat, aPos.maxlon, aPos.maxlat) + .transform(WGS84, Mercator); + } else { + extent = new OpenLayers.Bounds(-160, -70, 160, 70).transform(WGS84, Mercator); + } + + if (extent) { + this.map.zoomToExtent(extent); + } else { + this.map.setCenter(center, zoom); + } }, observer: function(evt) { @@ -321,7 +406,12 @@ var SYJView = { this.viewMode(); - $("geom_data").value = this.wkt.write(new OpenLayers.Feature.Vector(line)); + if (line.components.length) { + $("geom_data").value = this.wkt.write(new OpenLayers.Feature.Vector(line)); + } else { + $("geom_data").value = ""; + } + if (this.mode === "edit" && typeof gLoggedInfo.pathid !== "undefined") { $("geomform").setAttribute("action", "path/" + gLoggedInfo.pathid.toString() + '/update'); } else { @@ -389,14 +479,26 @@ var SYJView = { this.editControl = new OpenLayers.Control.DrawFeature(new OpenLayers.Layer.Vector(), OpenLayers.Handler.SyjModifiablePath, { callbacks: { modify: function(f, line) { - if (!SYJView.unsavedRoute) { - SYJView.unsavedRoute = {}; + SYJPathLength.update(); + + var npoints = this.handler.realPoints.length; + if (npoints === 0) { + $("geom_upload_container").show(); + SYJView.unsavedRoute = null; + } else { + if (!SYJView.unsavedRoute) { + SYJView.unsavedRoute = {}; + } } - if (this.handler.realPoints.length < 2) { + + if (npoints < 2) { SyjSaveUI.disable(); } else { SyjSaveUI.enable(); } + }, + create: function(f, line) { + $("geom_upload_container").hide(); } }, @@ -412,33 +514,17 @@ 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) { - this.unsavedRoute = null; + // server sends and empty response on success. If we get a response, that + // probably means an error or warning has been printed by server. + if (!transport.responseJSON && transport.responseText.length) { + this.saveFailure(null, 500); + return; + } + this.unsavedRoute = null; if (transport.responseJSON && (typeof transport.responseJSON.redirect === "string")) { location = transport.responseJSON.redirect; return; @@ -449,12 +535,12 @@ var SYJView = { document.title = $('geom_title').value; }, - saveFailure: function(transport) { - var httpCode = 0, message = ""; - - if (transport) { - httpCode = transport.getStatus(); + saveFailure: function(transport, httpCode) { + var message = ""; + if (typeof httpCode === "undefined") { + httpCode = transport? transport.getStatus(): 0; } + switch (httpCode) { case 0: message = SyjStrings.notReachedError; @@ -692,7 +778,14 @@ var SYJUserClass = Class.create(SYJModalClass, { }, success: function(transport) { - LoginMgr.login(); + if (!transport.responseJSON || + typeof transport.responseJSON.pseudo !== "string" + ) { + this.messenger.setMessage(SyjStrings.unknownError, "error"); + return; + } + + LoginMgr.login(transport.responseJSON.pseudo); SYJView.messenger.setMessage(SyjStrings.userSuccess, "success"); this.modalbox.hide(); if (SYJView.needsFormResubmit) { @@ -770,14 +863,8 @@ var SYJLoginClass = Class.create(SYJModalClass, { 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); - }); - }); + LoginMgr.login(transport.responseJSON.pseudo, transport.responseJSON.iscreator); + SYJView.messenger.setMessage(SyjStrings.loginSuccess, "success"); this.modalbox.hide(); if (SYJView.needsFormResubmit) { @@ -860,11 +947,18 @@ var LoginMgr = Object.extend(gLoggedInfo, { } }, - login: function(aIsCreator) { + login: function(aPseudo, aIsCreator) { if (typeof aIsCreator === "boolean") { this.iscreator = aIsCreator; } this.logged = true; + $$('.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', aPseudo); + }); + }); this.updateUI(); } }); @@ -987,7 +1081,7 @@ var Nominatim = (function() { center = bounds.getCenterLonLat().wrapDateLine(maxExtent); } this.setCenter(center, this.getZoomForExtent(bounds), false, true); - } + }; var success = function(transport) { $("nominatim-throbber").hide(); @@ -1021,7 +1115,8 @@ var Nominatim = (function() { }; }; - for (var i = 0; i < transport.responseJSON.length; i++) { + var i; + for (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"); @@ -1031,13 +1126,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 +1141,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();