X-Git-Url: https://dev.renevier.net/?a=blobdiff_plain;f=public%2Fjs%2Fsyj.js;h=50184b2a6974fe0c97dc8f43db21a78d609166d0;hb=c57706da408cacae1008851edb650ac33865bd22;hp=3c4bece6a7767b2a093c145ef80a6b9e42ecc53a;hpb=ed6c499940e9867ce98902d25ffbf4d0b1c88598;p=syj.git diff --git a/public/js/syj.js b/public/js/syj.js index 3c4bece..50184b2 100644 --- a/public/js/syj.js +++ b/public/js/syj.js @@ -40,6 +40,7 @@ var SyjSaveUI = { enableSubmit: function() { $("geom_submit").disabled = false; + $("geom_accept").disabled = false; this.status = "partial"; return this; }, @@ -47,25 +48,84 @@ var SyjSaveUI = { disableSubmit: function() { $("geom_submit").blur(); $("geom_submit").disabled = true; + $("geom_accept").blur(); + $("geom_accept").disabled = true; this.status = "partial"; 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); + } + + 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(); + } } }; }()); @@ -258,7 +318,6 @@ var SYJView = { this.messenger.hide(); this.editMode(); this.mode = 'create'; - SyjSaveUI.enableSubmit(); }.bind(this)); } @@ -292,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) { @@ -387,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 = {}; } @@ -410,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) { @@ -636,18 +735,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() { @@ -896,7 +984,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) { @@ -971,6 +1059,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(); @@ -979,7 +1210,9 @@ document.observe("dom:loaded", function() { SYJView.init(); SYJNewpwd.init(); LoginMgr.updateUI(); + Nominatim.init(); }); + window.onbeforeunload = function() { if (SYJView.unsavedRoute) { return SyjStrings.unsavedConfirmExit;