1 /* This file is part of Syj, Copyright (c) 2010-2011 Arnaud Renevier,
2 and is published under the AGPL license. */
4 // avoid openlayers alerts
5 OpenLayers.Console.userError = function(error) {
6 SYJView.messenger.setMessage(error, "error");
9 OpenLayers.Layer.Vector.prototype.renderers = ["SVG2", "VML", "Canvas"];
15 $("geom_title").observe('contentchange', this.enableSubmit.bindAsEventListener(this));
20 if (this.status === "enabled") {
24 $("geom_title").disabled = false;
25 $("geom_title").activate();
26 $$("#geom_accept_container, #geom_title_container").invoke('removeClassName', "disabled");
27 this.status = "enabled";
32 if (this.status === "disabled") {
36 $("geom_title").blur();
37 $("geom_title").disabled = true;
38 $$("#geom_accept_container, #geom_title_container").invoke('addClassName', "disabled");
39 this.status = "disabled";
43 enableSubmit: function() {
44 $("geom_submit").disabled = false;
45 $("geom_accept").disabled = false;
46 this.status = "partial";
50 disableSubmit: function() {
51 $("geom_submit").blur();
52 $("geom_submit").disabled = true;
53 $("geom_accept").blur();
54 $("geom_accept").disabled = true;
55 this.status = "partial";
60 var SYJPathLength = (function(){
63 var pathLength = 0, unit;
64 if (SYJView.mode === 'view') {
65 if (SYJView.viewLayer.features.length) {
66 pathLength = SYJView.viewLayer.features[0].geometry.getGeodesicLength(Mercator);
69 pathLength = SYJView.editControl.handler.line.geometry.getGeodesicLength(Mercator);
72 if (pathLength === 0) {
73 $("path-length").hide();
76 $("path-length").show();
78 if (pathLength < 1000) {
80 pathLength = Math.round(pathLength * 100) / 100;
84 pathLength = Math.round(pathLength) / 1000;
87 $("path-length-content").update(pathLength + ' ' + unit);
92 var SYJDataUi = (function() {
95 getdeck = function() {
97 deck = new Deck("data_controls");
101 getinfotoggler = function() {
103 infotoggler = new Toggler('path-infos-content');
104 $("path-infos-toggler").insert({bottom: infotoggler.element});
105 var anchor = $("path-infos-anchor");
106 var parent = anchor.up('.menu-item');
110 anchor.observe('click', function(evt) {
112 infotoggler.toggle(evt);
114 document.observe('toggler:open', function(evt) {
115 if (evt.memo === infotoggler) {
116 // XXX: update informations
123 viewmode: function() {
124 getdeck().setIndex(0);
125 if ($("path-infos")) {
127 getinfotoggler().close();
128 $("path-infos").show();
131 editmode: function() {
132 getdeck().setIndex(1);
133 if ($("path-infos")) {
134 $("path-infos").hide();
140 OpenLayers.Handler.SyjModifiablePath = OpenLayers.Class(OpenLayers.Handler.ModifiablePath, {
141 mouseup: function(evt) {
142 // do not add a point when navigating
143 var mapControls = this.control.map.controls, idx, ctrl;
145 for (idx = mapControls.length; idx-->0; ) {
146 ctrl = mapControls[idx];
147 if (this.isCtrlNavigationActive(ctrl, evt)) {
151 return OpenLayers.Handler.ModifiablePath.prototype.mouseup.apply(this, arguments);
154 addPoints: function(pixel) {
155 // redraw before last point. As we have a special style for last point, we
156 // need to redraw before last point after adding a new point (otherwise, it
157 // keeps special style forever)
158 var oldpoint = this.point;
159 OpenLayers.Handler.ModifiablePath.prototype.addPoints.apply(this, arguments);
160 this.layer.drawFeature(oldpoint);
163 isCtrlNavigationActive: function(ctrl, evt) {
164 var tolerance = 4, xDiff, yDiff;
166 if (!(ctrl instanceof OpenLayers.Control.Navigation)) {
171 ctrl.zoomBox.active &&
172 ctrl.zoomBox.handler &&
173 ctrl.zoomBox.handler.active &&
174 ctrl.zoomBox.handler.dragHandler &&
175 ctrl.zoomBox.handler.dragHandler.start) {
180 !ctrl.dragPan.active ||
181 !ctrl.dragPan.handler ||
182 !ctrl.dragPan.handler.started ||
183 !ctrl.dragPan.handler.start) {
187 // if mouse moved 4 or less pixels, consider it has not moved.
190 xDiff = evt.xy.x - ctrl.dragPan.handler.start.x;
191 yDiff = evt.xy.y - ctrl.dragPan.handler.start.y;
193 if (Math.sqrt(Math.pow(xDiff,2) + Math.pow(yDiff,2)) <= tolerance) {
199 finalize: function(cancel) {
200 // do nothing. We don't want to finalize path
205 edit: new OpenLayers.StyleMap({
206 "default": new OpenLayers.Style({
207 pointRadius: "${radius}", // sized according to type attribute
208 fillColor: "#ffcc66",
209 strokeColor: "#ff9933",
211 strokeOpacity: "${opacity}",
212 fillOpacity: "${opacity}"
216 radius: function(feature) {
219 if (!(feature.geometry instanceof OpenLayers.Geometry.Point)) {
222 if (feature.type === "middle") {
225 features = feature.layer.features;
226 if (OpenLayers.Util.indexOf(features, feature) === 0) {
228 } else if (OpenLayers.Util.indexOf(features, feature) === features.length - 1) {
233 opacity: function (feature) {
234 if (feature.type === "middle") {
243 "select": new OpenLayers.Style({
244 externalGraphic: "icons/delete.png",
248 "select_for_canvas": new OpenLayers.Style({
250 fillColor: "lightblue"
255 view: new OpenLayers.StyleMap({
256 "default": new OpenLayers.Style({
264 var WGS84 = new OpenLayers.Projection("EPSG:4326");
265 var Mercator = new OpenLayers.Projection("EPSG:900913");
271 wkt: new OpenLayers.Format.WKT({ internalProjection: Mercator, externalProjection: WGS84 }),
272 needsFormResubmit: false,
277 var externalGraphic, baseURL, baseLayer, layerOptions, hidemessenger;
279 // is svg context, opera does not resolve links with base element is svg context
280 externalGraphic = styleMap.edit.styles.select.defaultStyle.externalGraphic;
281 baseURL = document.getElementsByTagName("base")[0].href;
282 styleMap.edit.styles.select.defaultStyle.externalGraphic = baseURL + externalGraphic;
284 this.map = new OpenLayers.Map('map', {
286 new OpenLayers.Control.Navigation(),
287 new OpenLayers.Control.PanZoom(),
288 new OpenLayers.Control.Attribution()
293 baseLayer = new OpenLayers.Layer.OSM("OSM", [
294 'http://a.tile.openstreetmap.org/${z}/${x}/${y}.png',
295 'http://b.tile.openstreetmap.org/${z}/${x}/${y}.png',
296 'http://c.tile.openstreetmap.org/${z}/${x}/${y}.png'],
297 { attribution: SyjStrings.osmAttribution });
299 layerOptions = {format: OpenLayers.Format.WKT,
301 styleMap: styleMap.view,
302 attribution: SyjStrings.geomAttribution };
304 this.viewLayer = new OpenLayers.Layer.Vector("View Layer", layerOptions);
305 this.map.addLayers([baseLayer, this.viewLayer]);
308 $("edit-btn").observe('click', function() {
309 $("geom_submit").value = SyjStrings.editAction;
310 this.messenger.clearMessages();
316 if ($("create-btn")) {
317 $("create-btn").observe('click', function() {
318 $("geom_submit").value = SyjStrings.createAction;
319 this.messenger.clearMessages();
321 this.mode = 'create';
325 if ($("clone-btn")) {
326 $("clone-btn").observe('click', function() {
327 $("geom_submit").value = SyjStrings.cloneAction;
328 $("geom_title").value = "";
329 this.messenger.clearMessages();
331 this.mode = 'create';
335 $("geomform").ajaxize({
336 presubmit: this.prepareForm.bind(this),
337 onSuccess: this.saveSuccess.bind(this),
338 onFailure: this.saveFailure.bind(this)
342 this.messenger = $('message');
343 hidemessenger = this.messenger.empty();
344 new CloseBtn(this.messenger, {
350 this.messenger.hide();
353 if (typeof gInitialGeom !== "undefined" && typeof gInitialGeom.data !== "undefined") {
354 this.viewLayer.addFeatures([this.wkt.read(gInitialGeom.data)]);
355 // XXX: ie has not guessed height of map main div yet during map
356 // initialisation. Now, it will read it correctly.
357 this.map.updateSize();
358 this.map.zoomToExtent(this.viewLayer.getDataExtent());
360 this.initMaPos(gInitialPos);
363 $("map-overlay").hide();
364 $("geom_upload").observe('change', function(evt) {
365 var file = null, reader = null, readerror = null;
366 if (window.FileList && window.FileReader) {
367 file = evt.target.files[0];
368 reader = new FileReader();
369 readerror = function() {
370 this.messenger.setMessage(SyjStrings.uploadFileError, "warn");
372 reader.onload = function(evt) {
373 var data = null, results = null, engine = null, vector = null, i = 0, formats = ['KML', 'GPX', 'GeoJSON'];
375 $("geom_upload_container").removeClassName("disabled");
376 $("geom_upload").disabled = false;
381 data = evt.target.result;
383 for (i = 0; i < formats.length; i++) {
384 engine = new OpenLayers.Format[formats[i]]({ internalProjection: Mercator, externalProjection: WGS84 });
386 results = engine.read(data);
389 if (results && results.length) {
393 if (!results || !results.length) {
399 if (vector.geometry.CLASS_NAME !== "OpenLayers.Geometry.LineString") {
403 this.viewLayer.addFeatures([vector]);
404 this.map.zoomToExtent(this.viewLayer.getDataExtent());
407 $("edit-btn").click();
408 } else if ($("create-btn")) {
409 $("create-btn").click();
412 if (this.editControl.handler.realPoints.length < 2) {
418 if (vector.data && vector.data.name) {
419 $("geom_title").value = vector.data.name;
422 $("geom_upload_container").addClassName("disabled");
423 $("geom_upload").disabled = true;
424 reader.readAsText(file);
427 $("map-overlay").show();
429 this.editControl.deactivate();
432 document.observe('simplebox:shown', this.observer.bindAsEventListener(this));
433 SYJPathLength.update();
436 initMaPos: function (aPos) {
437 var extent = null, center = null, zoom = 0;
439 if (aPos.hasOwnProperty('lon') && aPos.hasOwnProperty('lat') && aPos.hasOwnProperty('zoom')) {
440 center = new OpenLayers.LonLat(parseFloat(aPos.lon), parseFloat(aPos.lat)).transform(WGS84, Mercator);
441 zoom = parseInt(aPos.zoom, 10);
442 } else if (aPos.hasOwnProperty('minlon') && aPos.hasOwnProperty('minlat')
443 && aPos.hasOwnProperty('maxlon') && aPos.hasOwnProperty('maxlat')) {
444 extent = new OpenLayers.Bounds(aPos.minlon, aPos.minlat, aPos.maxlon, aPos.maxlat)
445 .transform(WGS84, Mercator);
447 extent = new OpenLayers.Bounds(-160, -70, 160, 70).transform(WGS84, Mercator);
451 this.map.zoomToExtent(extent);
453 this.map.setCenter(center, zoom);
457 observer: function(evt) {
458 if (evt.eventName === "simplebox:shown" && evt.memo.element !== $("termsofusearea")) {
459 this.messenger.clearMessages();
463 prepareForm: function(form) {
464 if (!LoginMgr.logged && !$("geom_accept").checked) {
465 this.messenger.setMessage(SyjStrings.acceptTermsofuseWarn, "warn");
466 $("geom_accept_container").highlight('#F08080');
467 $("geom_accept").activate();
471 var line, realPoints, idx;
473 line = new OpenLayers.Geometry.LineString();
474 realPoints = this.editControl.handler.realPoints;
475 for (idx = 0; idx < realPoints.length; idx++) {
476 line.addComponent(realPoints[idx].geometry.clone());
478 this.viewLayer.addFeatures(new OpenLayers.Feature.Vector(line));
482 if (line.components.length) {
483 $("geom_data").value = this.wkt.write(new OpenLayers.Feature.Vector(line));
485 $("geom_data").value = "";
488 if (this.mode === "edit" && typeof gLoggedInfo.pathid !== "undefined") {
489 $("geomform").setAttribute("action", "path/" + gLoggedInfo.pathid.toString() + '/update');
491 $("geomform").setAttribute("action", "path");
493 this.needsFormResubmit = false;
494 SyjSaveUI.disable.bind(SyjSaveUI).defer();
495 this.messenger.clearMessages();
499 viewMode: function() {
500 var handler = this.editControl.handler;
501 OpenLayers.Handler.ModifiablePath.prototype.finalize.apply(handler, arguments);
502 // we need to recreate them on next createFeature; otherwise
503 // they'll reference destroyed features
504 delete(handler.handlers.drag);
505 delete(handler.handlers.feature);
506 this.editControl.deactivate();
509 editMode: function() {
510 var components, point0, point, pixels, pixel, idx;
512 this.initEditControl();
514 this.editControl.activate();
515 if (this.viewLayer.features.length) {
516 components = this.viewLayer.features[0].geometry.components;
517 point0 = components[0];
519 pixel = this.map.getPixelFromLonLat(new OpenLayers.LonLat(point0.x, point0.y));
520 this.editControl.handler.createFeature(pixel);
521 this.editControl.handler.lastUp = pixel;
523 for (idx = 1; idx < components.length; idx++) {
524 point = components[idx];
525 pixels.push(this.map.getPixelFromLonLat(new OpenLayers.LonLat(point.x, point.y)));
527 this.editControl.handler.addPoints(pixels);
529 this.unsavedRoute = {
530 features: this.viewLayer.features.invoke('clone'),
531 title: $("geom_title").value
535 this.viewLayer.destroyFeatures();
537 SYJDataUi.editmode();
538 if (this.editControl.handler.realPoints && this.editControl.handler.realPoints.length >= 2) {
539 SyjSaveUI.disableSubmit();
545 initEditControl: function() {
548 if (this.editControl) {
552 this.editControl = new OpenLayers.Control.DrawFeature(new OpenLayers.Layer.Vector(), OpenLayers.Handler.SyjModifiablePath, {
554 modify: function(f, line) {
555 SYJPathLength.update();
557 var npoints = this.handler.realPoints.length;
559 $("geom_upload_container").show();
560 SYJView.unsavedRoute = null;
562 if (!SYJView.unsavedRoute) {
563 SYJView.unsavedRoute = {};
573 create: function(f, line) {
574 this.messenger.clearMessages();
575 $("geom_upload_container").hide();
581 styleMap: styleMap.edit
585 this.map.addControl(this.editControl);
586 if (this.editControl.layer.renderer instanceof OpenLayers.Renderer.Canvas) {
587 // using externalGraphic with canvas renderer is definitively too buggy
588 styles = this.editControl.handler.layerOptions.styleMap.styles;
589 styles.select = styles.select_for_canvas;
593 saveSuccess: function(transport) {
594 // server sends and empty response on success. If we get a response, that
595 // probably means an error or warning has been printed by server.
596 if (!transport.responseJSON && transport.responseText.length) {
597 this.saveFailure(null, 500);
601 this.unsavedRoute = null;
602 if (transport.responseJSON && (typeof transport.responseJSON.redirect === "string")) {
603 location = transport.responseJSON.redirect;
607 this.messenger.setMessage(SyjStrings.saveSuccess, "success");
608 SYJDataUi.viewmode();
609 document.title = $('geom_title').value;
612 saveFailure: function(transport, httpCode) {
614 if (typeof httpCode === "undefined") {
615 httpCode = transport? transport.getStatus(): 0;
620 message = SyjStrings.notReachedError;
624 message = SyjStrings.requestError;
628 SYJLogin.messenger.setMessage(SyjStrings.loginNeeded, "warn");
629 SYJLogin.modalbox.show();
630 this.needsFormResubmit = true;
633 message = SyjStrings.gonePathError;
636 message = SyjStrings.serverError;
637 this.needsFormResubmit = true;
640 message = SyjStrings.unknownError;
645 // is some cases, we let the user resubmit, in some other cases, he
646 // needs to modify the path before submitting again
647 if (this.needsFormResubmit) {
651 this.messenger.setMessage(message, "error");
655 var SYJModalClass = Class.create({
659 this.area = $(this.type + '_area');
660 this.messenger = $(this.type + "_message");
661 this.modalbox = new SimpleBox(this.area, {
662 closeMethods: ["onescapekey", "onouterclick", "onbutton"]
665 var anchor = $(this.type + '_control_anchor');
666 var parent = anchor.up('.menu-item');
670 anchor.observe("click", function(evt) {
671 this.modalbox.show();
673 }.bindAsEventListener(this));
675 document.observe('simplebox:shown', this.observer.bindAsEventListener(this));
676 document.observe('simplebox:hidden', this.observer.bindAsEventListener(this));
678 $(this.type + "form").ajaxize({
679 presubmit: this.presubmit.bind(this),
680 onSuccess: this.success.bind(this),
681 onFailure: this.failure.bind(this)
685 checkNotEmpty: function(input, message) {
686 if ($(input).value.strip().empty()) {
687 this.messenger.setMessage(message, "warn");
688 $(input).highlight('#F08080').activate();
694 observer: function(evt) {
695 var simplebox, input;
697 if (evt.eventName === "simplebox:shown" && evt.memo.element !== $("termsofusearea")) {
698 simplebox = evt.memo;
699 if (simplebox === this.modalbox) {
700 input = this.area.select('input[type="text"]')[0];
705 this.modalbox.hide();
708 } else if (evt.eventName === "simplebox:hidden" && evt.memo.element !== $("termsofusearea")) {
709 simplebox = evt.memo;
710 if (simplebox === this.modalbox) {
716 failure: function(transport) {
717 var httpCode = 0, message = SyjStrings.unknownError, input; // default message error
720 httpCode = transport.getStatus();
725 message = SyjStrings.notReachedError;
730 message = SyjStrings.requestError;
733 message = SyjStrings.serverError;
737 this.messenger.setMessage(message, "error");
738 input = this.area.select('input[type="text"]')[0];
739 input.highlight('#F08080').activate();
743 this.messenger.clearMessages();
744 this.area.select('.message').invoke('setMessageStatus', null);
748 var SYJUserClass = Class.create(SYJModalClass, {
752 init: function($super) {
754 $("termsofusearea").hide();
756 $$("#user_termsofuse_anchor, #geom_termsofuse_anchor").invoke('observe', "click", function(evt) {
758 this.toubox = new SimpleBox($("termsofusearea"), {
759 closeMethods: ["onescapekey", "onouterclick", "onbutton"]
763 if (!$("termsofuseiframe").getAttribute("src")) {
764 $("termsofusearea").show();
765 $("termsofuseiframe").setAttribute("src", evt.target.href);
768 }.bindAsEventListener(this));
770 $$("#login_area_create > a").invoke('observe', 'click',
772 this.modalbox.show();
774 }.bindAsEventListener(this));
776 $("user_pseudo-desc").hide();
777 $("user_pseudo").observe('contentchange', function(evt) {
778 var value = evt.target.value;
779 PseudoChecker.reset();
780 if (value && !(value.match(/^[a-zA-Z0-9_.]+$/))) {
781 $("user_pseudo-desc").show().setMessageStatus("warn");
783 $("user_pseudo-desc").hide();
785 }).timedobserve(function() {
786 PseudoChecker.check();
789 $("user_password").observe('contentchange', function(evt) {
790 if (evt.target.value.length < 6) {
791 $("user_password-desc").setMessageStatus("warn");
793 $("user_password-desc").setMessageStatus("success");
795 }.bindAsEventListener(this));
797 $('account-create-anchor').insert({after: new Toggler('account-info').element});
800 presubmit: function() {
801 this.messenger.clearMessages();
802 PseudoChecker.reset();
803 if (!(this.checkNotEmpty("user_pseudo", SyjStrings.userEmptyWarn))) {
807 if (!($("user_pseudo").value.match(/^[a-zA-Z0-9_.]+$/))) {
808 $("user_pseudo-desc").show().setMessageStatus("warn");
809 $("user_pseudo").highlight('#F08080').activate();
813 if (PseudoChecker.exists[$("user_pseudo").value]) {
814 PseudoChecker.availableMessage(false);
815 $("user_pseudo").highlight('#F08080').activate();
819 if (!(this.checkNotEmpty("user_password", SyjStrings.passwordEmptyWarn))) {
823 if ($("user_password").value.length < 6) {
824 $("user_password-desc").setMessageStatus("warn");
825 $("user_password").highlight('#F08080').activate();
829 if ($("user_password").value !== $("user_password_confirm").value) {
830 this.messenger.setMessage(SyjStrings.passwordNoMatchWarn, "warn");
831 $("user_password").highlight('#F08080').activate();
835 if (!(this.checkNotEmpty("user_email", SyjStrings.emailEmptyWarn))) {
839 if (!$("user_accept").checked) {
840 this.messenger.setMessage(SyjStrings.acceptTermsofuseWarn, "warn");
841 $("user_accept_container").highlight('#F08080');
842 $("user_accept").activate();
850 success: function(transport) {
851 if (!transport.responseJSON ||
852 typeof transport.responseJSON.pseudo !== "string"
854 this.messenger.setMessage(SyjStrings.unknownError, "error");
858 LoginMgr.login(transport.responseJSON.pseudo);
859 SYJView.messenger.setMessage(SyjStrings.userSuccess, "success");
860 this.modalbox.hide();
861 if (SYJView.needsFormResubmit) {
862 SYJView.messenger.addMessage(SyjStrings.canResubmit);
863 $("geom_submit").activate();
867 failure: function($super, transport) {
868 var httpCode = 0, focusInput = null, message = "";
871 httpCode = transport.getStatus();
879 if (transport.responseJSON) {
880 switch (transport.responseJSON.message) {
882 message = SyjStrings.emailInvalidWarn;
883 focusInput = $("user_email");
886 PseudoChecker.availableMessage(false);
887 focusInput = $("user_pseudo");
890 message = SyjStrings.uniqueEmailError;
891 focusInput = $("user_email");
900 this.messenger.setMessage(message, "error");
902 focusInput.highlight('#F08080').activate();
909 var SYJUser = new SYJUserClass();
911 var SYJLoginClass = Class.create(SYJModalClass, {
914 init: function($super) {
918 presubmit: function() {
919 this.messenger.clearMessages();
920 if (!(this.checkNotEmpty("login_user", SyjStrings.userEmptyWarn))) {
928 success: function(transport) {
929 if (!transport.responseJSON ||
930 typeof transport.responseJSON.iscreator !== "boolean" ||
931 typeof transport.responseJSON.pseudo !== "string"
933 this.messenger.setMessage(SyjStrings.unknownError, "error");
936 LoginMgr.login(transport.responseJSON.pseudo, transport.responseJSON.iscreator);
938 SYJView.messenger.setMessage(SyjStrings.loginSuccess, "success");
939 this.modalbox.hide();
940 if (SYJView.needsFormResubmit) {
941 SYJView.messenger.addMessage(SyjStrings.canResubmit);
942 $("geom_submit").activate();
946 failure: function($super, transport) {
947 var httpCode = 0, focusInput = null, message = "";
950 httpCode = transport.getStatus();
958 message = SyjStrings.loginFailure;
959 focusInput = $("login_user");
964 this.messenger.setMessage(message, "error");
966 focusInput.highlight('#F08080').activate();
974 var SYJLogin = new SYJLoginClass();
976 var SYJNewpwdClass = Class.create(SYJModalClass, {
979 presubmit: function() {
980 if (!(this.checkNotEmpty("newpwd_email", SyjStrings.emailEmptyWarn))) {
986 success: function(transport) {
987 SYJView.messenger.setMessage(SyjStrings.newpwdSuccess, "success");
988 this.modalbox.hide();
992 var SYJNewpwd = new SYJNewpwdClass();
994 var LoginMgr = Object.extend(gLoggedInfo, {
997 updateUI: function() {
998 if (!this.controlsdeck) {
999 this.controlsdeck = new Deck("login_controls");
1002 this.controlsdeck.setIndex(1);
1003 $$(".logged-hide").invoke('hide');
1004 $$(".logged-show").invoke('show');
1006 this.controlsdeck.setIndex(0);
1007 $$(".logged-hide").invoke('show');
1008 $$(".logged-show").invoke('hide');
1011 if ($("edit-btn")) {
1012 if (this.iscreator && SYJView.mode === 'view') {
1013 $("edit-btn").show();
1015 $("edit-btn").hide();
1020 login: function(aPseudo, aIsCreator) {
1021 if (typeof aIsCreator === "boolean") {
1022 this.iscreator = aIsCreator;
1025 $$('.logged-pseudo').each(function(elt) {
1026 $A(elt.childNodes).filter(function(node) {
1027 return (node.nodeType === 3 || node.tagName.toLowerCase() === 'br');
1028 }).each(function(node) {
1029 node.nodeValue = node.nodeValue.replace('%s', aPseudo);
1036 var PseudoChecker = {
1043 message: function(str, status, throbber) {
1045 if (!this.messageelt) {
1046 row = new Element('tr');
1047 // we can't use row.update('<td></td><td><div></div></td>')
1048 // because gecko would mangle the <td>s
1049 row.insert(new Element('td'))
1050 .insert((new Element('td')).update(new Element('div')));
1052 $("user_pseudo").up('tr').insert({after: row});
1053 this.messageelt = new Element('span');
1054 this.throbber = new Element("img", { src: "icons/throbber.gif"});
1055 row.down('div').insert(this.throbber).insert(this.messageelt);
1058 this.throbber.show();
1060 this.throbber.hide();
1062 this.messageelt.up().setStyle({visibility: ''});
1063 this.messageelt.className = status;
1064 this.messageelt.update(str);
1067 availableMessage: function(available) {
1068 var message = available ? SyjStrings.availablePseudo: SyjStrings.unavailablePseudo,
1069 status = available ? "success": "warn";
1070 this.message(message, status, false);
1076 this.req = this.currentvalue = null;
1078 if (this.messageelt) {
1079 this.messageelt.up().setStyle({visibility: 'hidden'});
1084 var pseudo = $("user_pseudo").value;
1088 if (!pseudo || !(pseudo.match(/^[a-zA-Z0-9_.]+$/))) {
1092 if (typeof this.exists[pseudo] === "boolean") {
1094 this.availableMessage(!this.exists[pseudo]);
1098 this.message(SyjStrings.pseudoChecking, "", true);
1100 this.currentvalue = pseudo;
1101 this.req = new Ajax.TimedRequest('userexists/' + encodeURIComponent(pseudo), 20, {
1102 onFailure: this.failure.bind(this),
1103 onSuccess: this.success.bind(this)
1107 failure: function(transport) {
1108 var httpCode = 0, value = this.currentvalue;
1111 httpCode = transport.getStatus();
1114 if (httpCode === 404) {
1115 this.exists[value] = false;
1116 this.availableMessage(true);
1121 success: function(transport) {
1122 var httpCode = transport.getStatus(), value = this.currentvalue;
1124 this.exists[value] = true;
1125 this.availableMessage(false);
1129 var Nominatim = (function() {
1130 var presubmit = function() {
1131 var input = $("nominatim-search");
1132 if (input.value.strip().empty()) {
1133 $("nominatim-message").setMessage(SyjStrings.notEmptyField, "warn");
1137 $("nominatim-suggestions").hide();
1138 $("nominatim-message").hide();
1139 $("nominatim-throbber").show();
1143 var zoomToExtent = function(bounds) { // we must call map.setCenter with forceZoomChange to true. See ol#2798
1144 var center = bounds.getCenterLonLat();
1145 if (this.baseLayer.wrapDateLine) {
1146 var maxExtent = this.getMaxExtent();
1147 bounds = bounds.clone();
1148 while (bounds.right < bounds.left) {
1149 bounds.right += maxExtent.getWidth();
1151 center = bounds.getCenterLonLat().wrapDateLine(maxExtent);
1153 this.setCenter(center, this.getZoomForExtent(bounds), false, true);
1156 var success = function(transport) {
1157 $("nominatim-throbber").hide();
1159 if (!transport.responseJSON || !transport.responseJSON.length) {
1160 $("nominatim-message").setMessage(SyjStrings.noResult, 'error');
1161 $("nominatim-search").activate();
1165 var place = transport.responseJSON[0],
1166 bbox = place.boundingbox;
1168 if (!bbox || bbox.length !== 4) {
1169 $("nominatim-message").setMessage(SyjStrings.requestError, 'error');
1173 extent = new OpenLayers.Bounds(bbox[2], bbox[1], bbox[3], bbox[0]).transform(WGS84, Mercator);
1174 zoomToExtent.call(SYJView.map, extent);
1176 $("nominatim-suggestions-list").update();
1178 var clickhandler = function(bbox) {
1179 return function(evt) {
1181 var extent = new OpenLayers.Bounds(bbox[2], bbox[1], bbox[3], bbox[0]).transform(WGS84, Mercator);
1182 $("nominatim-suggestions-list").select("li").invoke('removeClassName', 'current');
1183 evt.target.up('li').addClassName('current');
1184 SYJView.map.zoomToExtent(extent);
1189 for (i = 0; i < transport.responseJSON.length; i++) {
1190 var item = transport.responseJSON[i];
1191 if (item.display_name && item.boundingbox && item.boundingbox.length === 4) {
1192 var li = new Element("li");
1193 var anchor = new Element("a", {
1195 className: "nominatim-suggestions-link"
1198 anchor.observe('click', clickhandler(item.boundingbox));
1199 Element.text(anchor, item.display_name);
1201 var icon = new Element("img", {
1202 className: "nominatim-suggestions-icon",
1203 src: item.icon || 'icons/world.png'
1205 li.insert(icon).insert(anchor);
1206 $("nominatim-suggestions-list").insert(li);
1207 if ($("nominatim-suggestions-list").childNodes.length >= 6) {
1213 if ($("nominatim-suggestions-list").childNodes.length > 1) {
1214 var bottomOffset = $('data_controls').measure('height') + 3;
1215 $("nominatim-suggestions").setStyle({
1216 bottom: (document.viewport.getHeight() - $('data_controls').cumulativeOffset().top + 3).toString() + 'px'
1218 $("nominatim-suggestions-list").select("li:first-child")[0].addClassName('current');
1220 $("nominatim-suggestions").hide();
1225 var failure = function(transport) {
1226 $("nominatim-throbber").hide();
1228 var httpCode = 0, message = SyjStrings.unknownError, input; // default message error
1231 httpCode = transport.getStatus();
1236 message = SyjStrings.notReachedError;
1240 message = SyjStrings.requestError;
1243 message = SyjStrings.serverError;
1247 $("nominatim-message").setMessage(message, 'error');
1252 if (!$("nominatim-form")) {
1255 $("nominatim-controls").hide();
1256 $("nominatim-label").observe('click', function(evt) {
1257 $("nominatim-controls").show();
1258 $("nominatim-search").activate();
1262 $("nominatim-form").ajaxize({
1263 presubmit: presubmit,
1267 new CloseBtn($("nominatim-suggestions"));
1269 $$("#nominatim-message, #nominatim-suggestions, #nominatim-throbber").invoke('hide');
1274 document.observe("dom:loaded", function() {
1277 SYJDataUi.viewmode();
1280 LoginMgr.updateUI();
1284 window.onbeforeunload = function() {
1285 if (SYJView.unsavedRoute) {
1286 return SyjStrings.unsavedConfirmExit;