1 /* This file is part of Syj, Copyright (c) 2010-2011 Arnaud Renevier,
2 and is published under the AGPL license. */
6 // avoid openlayers alerts
7 OpenLayers.Console.userError = function(error) {
8 SYJView.messenger.setMessage(error, "error");
11 OpenLayers.Layer.Vector.prototype.renderers = ["SVG2", "VML", "Canvas"];
17 $("geom_title").observe('contentchange', this.enableSubmit.bindAsEventListener(this));
22 if (this.status === "enabled") {
26 $("geom_title").disabled = false;
27 $("geom_title").activate();
28 $$("#geom_accept_container, #geom_title_container").invoke('removeClassName', "disabled");
29 this.status = "enabled";
34 if (this.status === "disabled") {
38 $("geom_title").blur();
39 $("geom_title").disabled = true;
40 $$("#geom_accept_container, #geom_title_container").invoke('addClassName', "disabled");
41 this.status = "disabled";
45 enableSubmit: function() {
46 $("geom_submit").disabled = false;
47 $("geom_accept").disabled = false;
48 this.status = "partial";
52 disableSubmit: function() {
53 $("geom_submit").blur();
54 $("geom_submit").disabled = true;
55 $("geom_accept").blur();
56 $("geom_accept").disabled = true;
57 this.status = "partial";
62 var SYJPathLength = (function(){
65 var pathLength = 0, unit;
66 if (SYJView.mode === 'view') {
67 if (SYJView.viewLayer.features.length) {
68 pathLength = SYJView.viewLayer.features[0].geometry.getGeodesicLength(Mercator);
71 pathLength = SYJView.editControl.handler.line.geometry.getGeodesicLength(Mercator);
74 if (pathLength === 0) {
75 $("path-length").hide();
78 $("path-length").show();
80 if (pathLength < 1000) {
82 pathLength = Math.round(pathLength * 100) / 100;
86 pathLength = Math.round(pathLength) / 1000;
89 $("path-length-content").update(pathLength + ' ' + unit);
94 var SYJDataUi = (function() {
97 getdeck = function() {
99 deck = new Deck("data_controls");
103 getinfotoggler = function() {
105 infotoggler = new Toggler('path-infos-content');
106 $("path-infos-toggler").insert({bottom: infotoggler.element});
107 var anchor = $("path-infos-anchor");
108 var parent = anchor.up('.menu-item');
112 anchor.observe('click', function(evt) {
114 infotoggler.toggle(evt);
116 document.observe('toggler:open', function(evt) {
117 if (evt.memo === infotoggler) {
118 // XXX: update informations
125 viewmode: function() {
126 getdeck().setIndex(0);
127 if ($("path-infos")) {
129 getinfotoggler().close();
130 $("path-infos").show();
133 editmode: function() {
134 getdeck().setIndex(1);
135 if ($("path-infos")) {
136 $("path-infos").hide();
142 OpenLayers.Handler.SyjModifiablePath = OpenLayers.Class(OpenLayers.Handler.ModifiablePath, {
143 mouseup: function(evt) {
144 // do not add a point when navigating
145 var mapControls = this.control.map.controls, idx, ctrl;
147 for (idx = mapControls.length; idx-->0; ) {
148 ctrl = mapControls[idx];
149 if (this.isCtrlNavigationActive(ctrl, evt)) {
153 return OpenLayers.Handler.ModifiablePath.prototype.mouseup.apply(this, arguments);
156 addPoints: function(pixel) {
157 // redraw before last point. As we have a special style for last point, we
158 // need to redraw before last point after adding a new point (otherwise, it
159 // keeps special style forever)
160 var oldpoint = this.point;
161 OpenLayers.Handler.ModifiablePath.prototype.addPoints.apply(this, arguments);
162 this.layer.drawFeature(oldpoint);
165 isCtrlNavigationActive: function(ctrl, evt) {
166 var tolerance = 4, xDiff, yDiff;
168 if (!(ctrl instanceof OpenLayers.Control.Navigation)) {
173 ctrl.zoomBox.active &&
174 ctrl.zoomBox.handler &&
175 ctrl.zoomBox.handler.active &&
176 ctrl.zoomBox.handler.dragHandler &&
177 ctrl.zoomBox.handler.dragHandler.start) {
182 !ctrl.dragPan.active ||
183 !ctrl.dragPan.handler ||
184 !ctrl.dragPan.handler.started ||
185 !ctrl.dragPan.handler.start) {
189 // if mouse moved 4 or less pixels, consider it has not moved.
192 xDiff = evt.xy.x - ctrl.dragPan.handler.start.x;
193 yDiff = evt.xy.y - ctrl.dragPan.handler.start.y;
195 if (Math.sqrt(Math.pow(xDiff,2) + Math.pow(yDiff,2)) <= tolerance) {
201 finalize: function(cancel) {
202 // do nothing. We don't want to finalize path
207 edit: new OpenLayers.StyleMap({
208 "default": new OpenLayers.Style({
209 pointRadius: "${radius}", // sized according to type attribute
210 fillColor: "#ffcc66",
211 strokeColor: "#ff9933",
213 strokeOpacity: "${opacity}",
214 fillOpacity: "${opacity}"
218 radius: function(feature) {
221 if (!(feature.geometry instanceof OpenLayers.Geometry.Point)) {
224 if (feature.type === "middle") {
227 features = feature.layer.features;
228 if (OpenLayers.Util.indexOf(features, feature) === 0) {
230 } else if (OpenLayers.Util.indexOf(features, feature) === features.length - 1) {
235 opacity: function (feature) {
236 if (feature.type === "middle") {
245 "select": new OpenLayers.Style({
246 externalGraphic: "icons/delete.png",
250 "select_for_canvas": new OpenLayers.Style({
252 fillColor: "lightblue"
257 view: new OpenLayers.StyleMap({
258 "default": new OpenLayers.Style({
266 var WGS84 = new OpenLayers.Projection("EPSG:4326");
267 var Mercator = new OpenLayers.Projection("EPSG:900913");
273 wkt: new OpenLayers.Format.WKT({ internalProjection: Mercator, externalProjection: WGS84 }),
274 needsFormResubmit: false,
279 var externalGraphic, baseURL, baseLayer, layerOptions, hidemessenger;
281 // is svg context, opera does not resolve links with base element is svg context
282 externalGraphic = styleMap.edit.styles.select.defaultStyle.externalGraphic;
283 baseURL = document.getElementsByTagName("base")[0].href;
284 styleMap.edit.styles.select.defaultStyle.externalGraphic = baseURL + externalGraphic;
286 this.map = new OpenLayers.Map('map', {
288 new OpenLayers.Control.Navigation(),
289 new OpenLayers.Control.PanZoom(),
290 new OpenLayers.Control.Attribution()
295 baseLayer = new OpenLayers.Layer.OSM("OSM", [
296 'http://a.tile.openstreetmap.org/${z}/${x}/${y}.png',
297 'http://b.tile.openstreetmap.org/${z}/${x}/${y}.png',
298 'http://c.tile.openstreetmap.org/${z}/${x}/${y}.png'],
299 { attribution: SyjStrings.osmAttribution });
301 layerOptions = {format: OpenLayers.Format.WKT,
303 styleMap: styleMap.view,
304 attribution: SyjStrings.geomAttribution };
306 this.viewLayer = new OpenLayers.Layer.Vector("View Layer", layerOptions);
307 this.map.addLayers([baseLayer, this.viewLayer]);
310 $("edit-btn").observe('click', function() {
311 $("geom_submit").value = SyjStrings.editAction;
312 this.messenger.clearMessages();
318 if ($("create-btn")) {
319 $("create-btn").observe('click', function() {
320 $("geom_submit").value = SyjStrings.createAction;
321 this.messenger.clearMessages();
323 this.mode = 'create';
327 if ($("clone-btn")) {
328 $("clone-btn").observe('click', function() {
329 $("geom_submit").value = SyjStrings.cloneAction;
330 $("geom_title").value = "";
331 this.messenger.clearMessages();
333 this.mode = 'create';
337 $("geomform").ajaxize({
338 presubmit: this.prepareForm.bind(this),
339 onSuccess: this.saveSuccess.bind(this),
340 onFailure: this.saveFailure.bind(this)
344 this.messenger = $('message');
345 hidemessenger = this.messenger.empty();
346 new CloseBtn(this.messenger, {
352 this.messenger.hide();
355 if (typeof gInitialGeom !== "undefined" && typeof gInitialGeom.data !== "undefined") {
356 this.viewLayer.addFeatures([this.wkt.read(gInitialGeom.data)]);
357 // XXX: ie has not guessed height of map main div yet during map
358 // initialisation. Now, it will read it correctly.
359 this.map.updateSize();
360 this.map.zoomToExtent(this.viewLayer.getDataExtent());
362 this.initMaPos(gInitialPos);
365 $("map-overlay").hide();
366 $("geom_upload").observe('change', function(evt) {
367 var file = null, reader = null, readerror = null;
368 if (window.FileList && window.FileReader) {
369 file = evt.target.files[0];
370 reader = new FileReader();
371 readerror = function() {
372 this.messenger.setMessage(SyjStrings.uploadFileError, "warn");
374 reader.onload = function(evt) {
375 var data = null, results = null, engine = null, vector = null, i = 0, formats = ['KML', 'GPX', 'GeoJSON'];
377 $("geom_upload_container").removeClassName("disabled");
378 $("geom_upload").disabled = false;
383 data = evt.target.result;
385 for (i = 0; i < formats.length; i++) {
386 engine = new OpenLayers.Format[formats[i]]({ internalProjection: Mercator, externalProjection: WGS84 });
388 results = engine.read(data);
391 if (results && results.length) {
395 if (!results || !results.length) {
401 if (vector.geometry.CLASS_NAME !== "OpenLayers.Geometry.LineString") {
405 this.viewLayer.addFeatures([vector]);
406 this.map.zoomToExtent(this.viewLayer.getDataExtent());
409 $("edit-btn").click();
410 } else if ($("create-btn")) {
411 $("create-btn").click();
414 if (this.editControl.handler.realPoints.length < 2) {
420 if (vector.data && vector.data.name) {
421 $("geom_title").value = vector.data.name;
424 $("geom_upload_container").addClassName("disabled");
425 $("geom_upload").disabled = true;
426 reader.readAsText(file);
429 $("map-overlay").show();
431 this.editControl.deactivate();
434 document.observe('simplebox:shown', this.observer.bindAsEventListener(this));
435 SYJPathLength.update();
438 initMaPos: function (aPos) {
439 var extent = null, center = null, zoom = 0;
441 if (aPos.hasOwnProperty('lon') && aPos.hasOwnProperty('lat') && aPos.hasOwnProperty('zoom')) {
442 center = new OpenLayers.LonLat(parseFloat(aPos.lon), parseFloat(aPos.lat)).transform(WGS84, Mercator);
443 zoom = parseInt(aPos.zoom, 10);
444 } else if (aPos.hasOwnProperty('minlon') && aPos.hasOwnProperty('minlat')
445 && aPos.hasOwnProperty('maxlon') && aPos.hasOwnProperty('maxlat')) {
446 extent = new OpenLayers.Bounds(aPos.minlon, aPos.minlat, aPos.maxlon, aPos.maxlat)
447 .transform(WGS84, Mercator);
449 extent = new OpenLayers.Bounds(-160, -70, 160, 70).transform(WGS84, Mercator);
453 this.map.zoomToExtent(extent);
455 this.map.setCenter(center, zoom);
459 observer: function(evt) {
460 if (evt.eventName === "simplebox:shown" && evt.memo.element !== $("termsofusearea")) {
461 this.messenger.clearMessages();
465 prepareForm: function(form) {
466 if (!LoginMgr.logged && !$("geom_accept").checked) {
467 this.messenger.setMessage(SyjStrings.acceptTermsofuseWarn, "warn");
468 $("geom_accept_container").highlight('#F08080');
469 $("geom_accept").activate();
473 var line, realPoints, idx;
475 line = new OpenLayers.Geometry.LineString();
476 realPoints = this.editControl.handler.realPoints;
477 for (idx = 0; idx < realPoints.length; idx++) {
478 line.addComponent(realPoints[idx].geometry.clone());
480 this.viewLayer.addFeatures(new OpenLayers.Feature.Vector(line));
484 if (line.components.length) {
485 $("geom_data").value = this.wkt.write(new OpenLayers.Feature.Vector(line));
487 $("geom_data").value = "";
490 if (this.mode === "edit" && typeof gLoggedInfo.pathid !== "undefined") {
491 $("geomform").setAttribute("action", "path/" + gLoggedInfo.pathid.toString() + '/update');
493 $("geomform").setAttribute("action", "path");
495 this.needsFormResubmit = false;
496 SyjSaveUI.disable.bind(SyjSaveUI).defer();
497 this.messenger.clearMessages();
501 viewMode: function() {
502 var handler = this.editControl.handler;
503 OpenLayers.Handler.ModifiablePath.prototype.finalize.apply(handler, arguments);
504 // we need to recreate them on next createFeature; otherwise
505 // they'll reference destroyed features
506 delete(handler.handlers.drag);
507 delete(handler.handlers.feature);
508 this.editControl.deactivate();
511 editMode: function() {
512 var components, point0, point, pixels, pixel, idx;
514 this.initEditControl();
516 this.editControl.activate();
517 if (this.viewLayer.features.length) {
518 components = this.viewLayer.features[0].geometry.components;
519 point0 = components[0];
521 pixel = this.map.getPixelFromLonLat(new OpenLayers.LonLat(point0.x, point0.y));
522 this.editControl.handler.createFeature(pixel);
523 this.editControl.handler.lastUp = pixel;
525 for (idx = 1; idx < components.length; idx++) {
526 point = components[idx];
527 pixels.push(this.map.getPixelFromLonLat(new OpenLayers.LonLat(point.x, point.y)));
529 this.editControl.handler.addPoints(pixels);
531 this.unsavedRoute = {
532 features: this.viewLayer.features.invoke('clone'),
533 title: $("geom_title").value
537 this.viewLayer.destroyFeatures();
539 SYJDataUi.editmode();
540 if (this.editControl.handler.realPoints && this.editControl.handler.realPoints.length >= 2) {
541 SyjSaveUI.disableSubmit();
547 initEditControl: function() {
550 if (this.editControl) {
554 this.editControl = new OpenLayers.Control.DrawFeature(new OpenLayers.Layer.Vector(), OpenLayers.Handler.SyjModifiablePath, {
556 modify: function(f, line) {
557 SYJPathLength.update();
559 var npoints = this.handler.realPoints.length;
561 $("geom_upload_container").show();
562 SYJView.unsavedRoute = null;
564 if (!SYJView.unsavedRoute) {
565 SYJView.unsavedRoute = {};
575 create: function(f, line) {
576 this.messenger.clearMessages();
577 $("geom_upload_container").hide();
583 styleMap: styleMap.edit
587 this.map.addControl(this.editControl);
588 if (this.editControl.layer.renderer instanceof OpenLayers.Renderer.Canvas) {
589 // using externalGraphic with canvas renderer is definitively too buggy
590 styles = this.editControl.handler.layerOptions.styleMap.styles;
591 styles.select = styles.select_for_canvas;
595 saveSuccess: function(transport) {
596 // server sends and empty response on success. If we get a response, that
597 // probably means an error or warning has been printed by server.
598 if (!transport.responseJSON && transport.responseText.length) {
599 this.saveFailure(null, 500);
603 this.unsavedRoute = null;
604 if (transport.responseJSON && (typeof transport.responseJSON.redirect === "string")) {
605 location = transport.responseJSON.redirect;
609 this.messenger.setMessage(SyjStrings.saveSuccess, "success");
610 SYJDataUi.viewmode();
611 document.title = $('geom_title').value;
614 saveFailure: function(transport, httpCode) {
616 if (typeof httpCode === "undefined") {
617 httpCode = transport? transport.getStatus(): 0;
622 message = SyjStrings.notReachedError;
626 message = SyjStrings.requestError;
630 SYJLogin.messenger.setMessage(SyjStrings.loginNeeded, "warn");
631 SYJLogin.modalbox.show();
632 this.needsFormResubmit = true;
635 message = SyjStrings.gonePathError;
638 message = SyjStrings.serverError;
639 this.needsFormResubmit = true;
642 message = SyjStrings.unknownError;
647 // is some cases, we let the user resubmit, in some other cases, he
648 // needs to modify the path before submitting again
649 if (this.needsFormResubmit) {
653 this.messenger.setMessage(message, "error");
657 var SYJModalClass = Class.create({
661 this.area = $(this.type + '_area');
662 this.messenger = $(this.type + "_message");
663 this.modalbox = new SimpleBox(this.area, {
664 closeMethods: ["onescapekey", "onouterclick", "onbutton"]
667 var anchor = $(this.type + '_control_anchor');
668 var parent = anchor.up('.menu-item');
672 anchor.observe("click", function(evt) {
673 this.modalbox.show();
675 }.bindAsEventListener(this));
677 document.observe('simplebox:shown', this.observer.bindAsEventListener(this));
678 document.observe('simplebox:hidden', this.observer.bindAsEventListener(this));
680 $(this.type + "form").ajaxize({
681 presubmit: this.presubmit.bind(this),
682 onSuccess: this.success.bind(this),
683 onFailure: this.failure.bind(this)
687 checkNotEmpty: function(input, message) {
688 if ($(input).value.strip().empty()) {
689 this.messenger.setMessage(message, "warn");
690 $(input).highlight('#F08080').activate();
696 observer: function(evt) {
697 var simplebox, input;
699 if (evt.eventName === "simplebox:shown" && evt.memo.element !== $("termsofusearea")) {
700 simplebox = evt.memo;
701 if (simplebox === this.modalbox) {
702 input = this.area.select('input[type="text"]')[0];
707 this.modalbox.hide();
710 } else if (evt.eventName === "simplebox:hidden" && evt.memo.element !== $("termsofusearea")) {
711 simplebox = evt.memo;
712 if (simplebox === this.modalbox) {
718 failure: function(transport) {
719 var httpCode = 0, message = SyjStrings.unknownError, input; // default message error
722 httpCode = transport.getStatus();
727 message = SyjStrings.notReachedError;
732 message = SyjStrings.requestError;
735 message = SyjStrings.serverError;
739 this.messenger.setMessage(message, "error");
740 input = this.area.select('input[type="text"]')[0];
741 input.highlight('#F08080').activate();
745 this.messenger.clearMessages();
746 this.area.select('.message').invoke('setMessageStatus', null);
750 var SYJUserClass = Class.create(SYJModalClass, {
754 init: function($super) {
756 $("termsofusearea").hide();
758 $$("#user_termsofuse_anchor, #geom_termsofuse_anchor").invoke('observe', "click", function(evt) {
760 this.toubox = new SimpleBox($("termsofusearea"), {
761 closeMethods: ["onescapekey", "onouterclick", "onbutton"]
765 if (!$("termsofuseiframe").getAttribute("src")) {
766 $("termsofusearea").show();
767 $("termsofuseiframe").setAttribute("src", evt.target.href);
770 }.bindAsEventListener(this));
772 $$("#login_area_create > a").invoke('observe', 'click',
774 this.modalbox.show();
776 }.bindAsEventListener(this));
778 $("user_pseudo-desc").hide();
779 $("user_pseudo").observe('contentchange', function(evt) {
780 var value = evt.target.value;
781 PseudoChecker.reset();
782 if (value && !(value.match(/^[a-zA-Z0-9_.]+$/))) {
783 $("user_pseudo-desc").show().setMessageStatus("warn");
785 $("user_pseudo-desc").hide();
787 }).timedobserve(function() {
788 PseudoChecker.check();
791 $("user_password").observe('contentchange', function(evt) {
792 if (evt.target.value.length < 6) {
793 $("user_password-desc").setMessageStatus("warn");
795 $("user_password-desc").setMessageStatus("success");
797 }.bindAsEventListener(this));
799 $('account-create-anchor').insert({after: new Toggler('account-info').element});
802 presubmit: function() {
803 this.messenger.clearMessages();
804 PseudoChecker.reset();
805 if (!(this.checkNotEmpty("user_pseudo", SyjStrings.userEmptyWarn))) {
809 if (!($("user_pseudo").value.match(/^[a-zA-Z0-9_.]+$/))) {
810 $("user_pseudo-desc").show().setMessageStatus("warn");
811 $("user_pseudo").highlight('#F08080').activate();
815 if (PseudoChecker.exists[$("user_pseudo").value]) {
816 PseudoChecker.availableMessage(false);
817 $("user_pseudo").highlight('#F08080').activate();
821 if (!(this.checkNotEmpty("user_password", SyjStrings.passwordEmptyWarn))) {
825 if ($("user_password").value.length < 6) {
826 $("user_password-desc").setMessageStatus("warn");
827 $("user_password").highlight('#F08080').activate();
831 if ($("user_password").value !== $("user_password_confirm").value) {
832 this.messenger.setMessage(SyjStrings.passwordNoMatchWarn, "warn");
833 $("user_password").highlight('#F08080').activate();
837 if (!(this.checkNotEmpty("user_email", SyjStrings.emailEmptyWarn))) {
841 if (!$("user_accept").checked) {
842 this.messenger.setMessage(SyjStrings.acceptTermsofuseWarn, "warn");
843 $("user_accept_container").highlight('#F08080');
844 $("user_accept").activate();
852 success: function(transport) {
853 if (!transport.responseJSON ||
854 typeof transport.responseJSON.pseudo !== "string"
856 this.messenger.setMessage(SyjStrings.unknownError, "error");
860 LoginMgr.login(transport.responseJSON.pseudo);
861 SYJView.messenger.setMessage(SyjStrings.userSuccess, "success");
862 this.modalbox.hide();
863 if (SYJView.needsFormResubmit) {
864 SYJView.messenger.addMessage(SyjStrings.canResubmit);
865 $("geom_submit").activate();
869 failure: function($super, transport) {
870 var httpCode = 0, focusInput = null, message = "";
873 httpCode = transport.getStatus();
881 if (transport.responseJSON) {
882 switch (transport.responseJSON.message) {
884 message = SyjStrings.emailInvalidWarn;
885 focusInput = $("user_email");
888 PseudoChecker.availableMessage(false);
889 focusInput = $("user_pseudo");
892 message = SyjStrings.uniqueEmailError;
893 focusInput = $("user_email");
902 this.messenger.setMessage(message, "error");
904 focusInput.highlight('#F08080').activate();
911 var SYJUser = new SYJUserClass();
913 var SYJLoginClass = Class.create(SYJModalClass, {
916 init: function($super) {
920 presubmit: function() {
921 this.messenger.clearMessages();
922 if (!(this.checkNotEmpty("login_user", SyjStrings.userEmptyWarn))) {
930 success: function(transport) {
931 if (!transport.responseJSON ||
932 typeof transport.responseJSON.iscreator !== "boolean" ||
933 typeof transport.responseJSON.pseudo !== "string"
935 this.messenger.setMessage(SyjStrings.unknownError, "error");
938 LoginMgr.login(transport.responseJSON.pseudo, transport.responseJSON.iscreator);
940 SYJView.messenger.setMessage(SyjStrings.loginSuccess, "success");
941 this.modalbox.hide();
942 if (SYJView.needsFormResubmit) {
943 SYJView.messenger.addMessage(SyjStrings.canResubmit);
944 $("geom_submit").activate();
948 failure: function($super, transport) {
949 var httpCode = 0, focusInput = null, message = "";
952 httpCode = transport.getStatus();
960 message = SyjStrings.loginFailure;
961 focusInput = $("login_user");
966 this.messenger.setMessage(message, "error");
968 focusInput.highlight('#F08080').activate();
976 var SYJLogin = new SYJLoginClass();
978 var SYJNewpwdClass = Class.create(SYJModalClass, {
981 presubmit: function() {
982 if (!(this.checkNotEmpty("newpwd_email", SyjStrings.emailEmptyWarn))) {
988 success: function(transport) {
989 SYJView.messenger.setMessage(SyjStrings.newpwdSuccess, "success");
990 this.modalbox.hide();
994 var SYJNewpwd = new SYJNewpwdClass();
996 var LoginMgr = Object.extend(gLoggedInfo, {
999 updateUI: function() {
1000 if (!this.controlsdeck) {
1001 this.controlsdeck = new Deck("login_controls");
1004 this.controlsdeck.setIndex(1);
1005 $$(".logged-hide").invoke('hide');
1006 $$(".logged-show").invoke('show');
1008 this.controlsdeck.setIndex(0);
1009 $$(".logged-hide").invoke('show');
1010 $$(".logged-show").invoke('hide');
1013 if ($("edit-btn")) {
1014 if (this.iscreator && SYJView.mode === 'view') {
1015 $("edit-btn").show();
1017 $("edit-btn").hide();
1022 login: function(aPseudo, aIsCreator) {
1023 if (typeof aIsCreator === "boolean") {
1024 this.iscreator = aIsCreator;
1027 $$('.logged-pseudo').each(function(elt) {
1028 $A(elt.childNodes).filter(function(node) {
1029 return (node.nodeType === 3 || node.tagName.toLowerCase() === 'br');
1030 }).each(function(node) {
1031 node.nodeValue = node.nodeValue.replace('%s', aPseudo);
1038 var PseudoChecker = {
1045 message: function(str, status, throbber) {
1047 if (!this.messageelt) {
1048 row = new Element('tr');
1049 // we can't use row.update('<td></td><td><div></div></td>')
1050 // because gecko would mangle the <td>s
1051 row.insert(new Element('td'))
1052 .insert((new Element('td')).update(new Element('div')));
1054 $("user_pseudo").up('tr').insert({after: row});
1055 this.messageelt = new Element('span');
1056 this.throbber = new Element("img", { src: "icons/throbber.gif"});
1057 row.down('div').insert(this.throbber).insert(this.messageelt);
1060 this.throbber.show();
1062 this.throbber.hide();
1064 this.messageelt.up().setStyle({visibility: ''});
1065 this.messageelt.className = status;
1066 this.messageelt.update(str);
1069 availableMessage: function(available) {
1070 var message = available ? SyjStrings.availablePseudo: SyjStrings.unavailablePseudo,
1071 status = available ? "success": "warn";
1072 this.message(message, status, false);
1078 this.req = this.currentvalue = null;
1080 if (this.messageelt) {
1081 this.messageelt.up().setStyle({visibility: 'hidden'});
1086 var pseudo = $("user_pseudo").value;
1090 if (!pseudo || !(pseudo.match(/^[a-zA-Z0-9_.]+$/))) {
1094 if (typeof this.exists[pseudo] === "boolean") {
1096 this.availableMessage(!this.exists[pseudo]);
1100 this.message(SyjStrings.pseudoChecking, "", true);
1102 this.currentvalue = pseudo;
1103 this.req = new Ajax.TimedRequest('userexists/' + encodeURIComponent(pseudo), 20, {
1104 onFailure: this.failure.bind(this),
1105 onSuccess: this.success.bind(this)
1109 failure: function(transport) {
1110 var httpCode = 0, value = this.currentvalue;
1113 httpCode = transport.getStatus();
1116 if (httpCode === 404) {
1117 this.exists[value] = false;
1118 this.availableMessage(true);
1123 success: function(transport) {
1124 var httpCode = transport.getStatus(), value = this.currentvalue;
1126 this.exists[value] = true;
1127 this.availableMessage(false);
1131 var Nominatim = (function() {
1132 var presubmit = function() {
1133 var input = $("nominatim-search");
1134 if (input.value.strip().empty()) {
1135 $("nominatim-message").setMessage(SyjStrings.notEmptyField, "warn");
1139 $("nominatim-suggestions").hide();
1140 $("nominatim-message").hide();
1141 $("nominatim-throbber").show();
1145 var zoomToExtent = function(bounds) { // we must call map.setCenter with forceZoomChange to true. See ol#2798
1146 var center = bounds.getCenterLonLat();
1147 if (this.baseLayer.wrapDateLine) {
1148 var maxExtent = this.getMaxExtent();
1149 bounds = bounds.clone();
1150 while (bounds.right < bounds.left) {
1151 bounds.right += maxExtent.getWidth();
1153 center = bounds.getCenterLonLat().wrapDateLine(maxExtent);
1155 this.setCenter(center, this.getZoomForExtent(bounds), false, true);
1158 var success = function(transport) {
1159 $("nominatim-throbber").hide();
1161 if (!transport.responseJSON || !transport.responseJSON.length) {
1162 $("nominatim-message").setMessage(SyjStrings.noResult, 'error');
1163 $("nominatim-search").activate();
1167 var place = transport.responseJSON[0],
1168 bbox = place.boundingbox;
1170 if (!bbox || bbox.length !== 4) {
1171 $("nominatim-message").setMessage(SyjStrings.requestError, 'error');
1175 extent = new OpenLayers.Bounds(bbox[2], bbox[1], bbox[3], bbox[0]).transform(WGS84, Mercator);
1176 zoomToExtent.call(SYJView.map, extent);
1178 $("nominatim-suggestions-list").update();
1180 var clickhandler = function(bbox) {
1181 return function(evt) {
1183 var extent = new OpenLayers.Bounds(bbox[2], bbox[1], bbox[3], bbox[0]).transform(WGS84, Mercator);
1184 $("nominatim-suggestions-list").select("li").invoke('removeClassName', 'current');
1185 evt.target.up('li').addClassName('current');
1186 SYJView.map.zoomToExtent(extent);
1191 for (i = 0; i < transport.responseJSON.length; i++) {
1192 var item = transport.responseJSON[i];
1193 if (item.display_name && item.boundingbox && item.boundingbox.length === 4) {
1194 var li = new Element("li");
1195 var anchor = new Element("a", {
1197 className: "nominatim-suggestions-link"
1200 anchor.observe('click', clickhandler(item.boundingbox));
1201 Element.text(anchor, item.display_name);
1203 var icon = new Element("img", {
1204 className: "nominatim-suggestions-icon",
1205 src: item.icon || 'icons/world.png'
1207 li.insert(icon).insert(anchor);
1208 $("nominatim-suggestions-list").insert(li);
1209 if ($("nominatim-suggestions-list").childNodes.length >= 6) {
1215 if ($("nominatim-suggestions-list").childNodes.length > 1) {
1216 var bottomOffset = $('data_controls').measure('height') + 3;
1217 $("nominatim-suggestions").setStyle({
1218 bottom: (document.viewport.getHeight() - $('data_controls').cumulativeOffset().top + 3).toString() + 'px'
1220 $("nominatim-suggestions-list").select("li:first-child")[0].addClassName('current');
1222 $("nominatim-suggestions").hide();
1227 var failure = function(transport) {
1228 $("nominatim-throbber").hide();
1230 var httpCode = 0, message = SyjStrings.unknownError, input; // default message error
1233 httpCode = transport.getStatus();
1238 message = SyjStrings.notReachedError;
1242 message = SyjStrings.requestError;
1245 message = SyjStrings.serverError;
1249 $("nominatim-message").setMessage(message, 'error');
1254 if (!$("nominatim-form")) {
1257 $("nominatim-controls").hide();
1258 $("nominatim-label").observe('click', function(evt) {
1259 $("nominatim-controls").show();
1260 $("nominatim-search").activate();
1264 $("nominatim-form").ajaxize({
1265 presubmit: presubmit,
1269 new CloseBtn($("nominatim-suggestions"));
1271 $$("#nominatim-message, #nominatim-suggestions, #nominatim-throbber").invoke('hide');
1276 document.observe("dom:loaded", function() {
1279 SYJDataUi.viewmode();
1282 LoginMgr.updateUI();
1286 window.onbeforeunload = function() {
1287 if (SYJView.unsavedRoute) {
1288 return SyjStrings.unsavedConfirmExit;