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");
13 $("geom_title").observe('contentchange', this.enableSubmit.bindAsEventListener(this));
18 if (this.status === "enabled") {
22 $("geom_title").disabled = false;
23 $("geom_title").activate();
24 $$("#geom_accept_container, #geom_title_container").invoke('removeClassName', "disabled");
25 this.status = "enabled";
30 if (this.status === "disabled") {
34 $("geom_title").blur();
35 $("geom_title").disabled = true;
36 $$("#geom_accept_container, #geom_title_container").invoke('addClassName', "disabled");
37 this.status = "disabled";
41 enableSubmit: function() {
42 $("geom_submit").disabled = false;
43 $("geom_accept").disabled = false;
44 this.status = "partial";
48 disableSubmit: function() {
49 $("geom_submit").blur();
50 $("geom_submit").disabled = true;
51 $("geom_accept").blur();
52 $("geom_accept").disabled = true;
53 this.status = "partial";
58 var SYJPathLength = (function(){
61 var pathLength = 0, unit;
62 if (SYJView.mode === 'view') {
63 if (SYJView.viewLayer.features.length) {
64 pathLength = SYJView.viewLayer.features[0].geometry.getGeodesicLength(Mercator);
67 pathLength = SYJView.editControl.handler.line.geometry.getGeodesicLength(Mercator);
70 if (pathLength === 0) {
71 $("path-length").hide();
74 $("path-length").show();
76 if (pathLength < 1000) {
78 pathLength = Math.round(pathLength * 100) / 100;
82 pathLength = Math.round(pathLength) / 1000;
85 $("path-length-content").update(pathLength + ' ' + unit);
90 var SYJDataUi = (function() {
93 getdeck = function() {
95 deck = new Deck("data_controls");
99 getinfotoggler = function() {
101 infotoggler = new Toggler('path-infos-content');
102 $("path-infos-toggler").insert({bottom: infotoggler.element});
103 var anchor = $("path-infos-anchor");
104 var parent = anchor.up('.menu-item');
108 anchor.observe('click', function(evt) {
110 infotoggler.toggle(evt);
112 document.observe('toggler:open', function(evt) {
113 if (evt.memo === infotoggler) {
114 // XXX: update informations
121 viewmode: function() {
122 getdeck().setIndex(0);
123 if ($("path-infos")) {
125 getinfotoggler().close();
126 $("path-infos").show();
129 editmode: function() {
130 getdeck().setIndex(1);
131 if ($("path-infos")) {
132 $("path-infos").hide();
138 OpenLayers.Handler.SyjModifiablePath = OpenLayers.Class(OpenLayers.Handler.ModifiablePath, {
139 mouseup: function(evt) {
140 // do not add a point when navigating
141 var mapControls = this.control.map.controls, idx, ctrl;
143 for (idx = mapControls.length; idx-->0; ) {
144 ctrl = mapControls[idx];
145 if (this.isCtrlNavigationActive(ctrl, evt)) {
149 return OpenLayers.Handler.ModifiablePath.prototype.mouseup.apply(this, arguments);
152 addPoints: function(pixel) {
153 // redraw before last point. As we have a special style for last point, we
154 // need to redraw before last point after adding a new point (otherwise, it
155 // keeps special style forever)
156 var oldpoint = this.point;
157 OpenLayers.Handler.ModifiablePath.prototype.addPoints.apply(this, arguments);
158 this.layer.drawFeature(oldpoint);
161 isCtrlNavigationActive: function(ctrl, evt) {
162 var tolerance = 4, xDiff, yDiff;
164 if (!(ctrl instanceof OpenLayers.Control.Navigation)) {
169 ctrl.zoomBox.active &&
170 ctrl.zoomBox.handler &&
171 ctrl.zoomBox.handler.active &&
172 ctrl.zoomBox.handler.dragHandler &&
173 ctrl.zoomBox.handler.dragHandler.start) {
178 !ctrl.dragPan.active ||
179 !ctrl.dragPan.handler ||
180 !ctrl.dragPan.handler.started ||
181 !ctrl.dragPan.handler.start) {
185 // if mouse moved 4 or less pixels, consider it has not moved.
188 xDiff = evt.xy.x - ctrl.dragPan.handler.start.x;
189 yDiff = evt.xy.y - ctrl.dragPan.handler.start.y;
191 if (Math.sqrt(Math.pow(xDiff,2) + Math.pow(yDiff,2)) <= tolerance) {
197 finalize: function(cancel) {
198 // do nothing. We don't want to finalize path
203 edit: new OpenLayers.StyleMap({
204 "default": new OpenLayers.Style({
205 pointRadius: "${radius}", // sized according to type attribute
206 fillColor: "#ffcc66",
207 strokeColor: "#ff9933",
209 strokeOpacity: "${opacity}",
210 fillOpacity: "${opacity}"
214 radius: function(feature) {
217 if (!(feature.geometry instanceof OpenLayers.Geometry.Point)) {
220 if (feature.type === "middle") {
223 features = feature.layer.features;
224 if (OpenLayers.Util.indexOf(features, feature) === 0) {
226 } else if (OpenLayers.Util.indexOf(features, feature) === features.length - 1) {
231 opacity: function (feature) {
232 if (feature.type === "middle") {
241 "select": new OpenLayers.Style({
242 externalGraphic: "icons/delete.png",
246 "select_for_canvas": new OpenLayers.Style({
248 fillColor: "lightblue"
253 view: new OpenLayers.StyleMap({
254 "default": new OpenLayers.Style({
262 var WGS84 = new OpenLayers.Projection("EPSG:4326");
263 var Mercator = new OpenLayers.Projection("EPSG:900913");
269 wkt: new OpenLayers.Format.WKT({ internalProjection: Mercator, externalProjection: WGS84 }),
270 needsFormResubmit: false,
275 var externalGraphic, baseURL, baseLayer, layerOptions, hidemessenger;
277 // is svg context, opera does not resolve links with base element is svg context
278 externalGraphic = styleMap.edit.styles.select.defaultStyle.externalGraphic;
279 baseURL = document.getElementsByTagName("base")[0].href;
280 styleMap.edit.styles.select.defaultStyle.externalGraphic = baseURL + externalGraphic;
282 this.map = new OpenLayers.Map('map', {
284 new OpenLayers.Control.Navigation(),
285 new OpenLayers.Control.PanZoom(),
286 new OpenLayers.Control.Attribution()
291 baseLayer = new OpenLayers.Layer.OSM("OSM", [
292 'http://a.tile.openstreetmap.org/${z}/${x}/${y}.png',
293 'http://b.tile.openstreetmap.org/${z}/${x}/${y}.png',
294 'http://c.tile.openstreetmap.org/${z}/${x}/${y}.png'],
295 { attribution: SyjStrings.osmAttribution });
297 layerOptions = {format: OpenLayers.Format.WKT,
299 styleMap: styleMap.view,
300 attribution: SyjStrings.geomAttribution };
302 this.viewLayer = new OpenLayers.Layer.Vector("View Layer", layerOptions);
303 this.map.addLayers([baseLayer, this.viewLayer]);
306 $("edit-btn").observe('click', function() {
307 $("geom_submit").value = SyjStrings.editAction;
308 this.messenger.hide();
314 if ($("create-btn")) {
315 $("create-btn").observe('click', function() {
316 $("geom_submit").value = SyjStrings.createAction;
317 this.messenger.hide();
319 this.mode = 'create';
323 if ($("clone-btn")) {
324 $("clone-btn").observe('click', function() {
325 $("geom_submit").value = SyjStrings.cloneAction;
326 $("geom_title").value = "";
327 this.messenger.hide();
329 this.mode = 'create';
333 $("geomform").ajaxize({
334 presubmit: this.prepareForm.bind(this),
335 onSuccess: this.saveSuccess.bind(this),
336 onFailure: this.saveFailure.bind(this)
340 this.messenger = $('message');
341 hidemessenger = this.messenger.empty();
342 new CloseBtn(this.messenger, {
348 this.messenger.hide();
351 if (typeof gInitialGeom !== "undefined" && typeof gInitialGeom.data !== "undefined") {
352 this.viewLayer.addFeatures([this.wkt.read(gInitialGeom.data)]);
353 // XXX: ie has not guessed height of map main div yet during map
354 // initialisation. Now, it will read it correctly.
355 this.map.updateSize();
356 this.map.zoomToExtent(this.viewLayer.getDataExtent());
358 this.initMaPos(gInitialPos);
361 $("map-overlay").hide();
362 $("geom_upload").observe('change', function(evt) {
363 var file = null, reader = null, readerror = null;
364 if (window.FileList && window.FileReader) {
365 file = evt.target.files[0];
366 reader = new FileReader();
367 readerror = function() {
368 this.messenger.setMessage(SyjStrings.uploadFileError, "warn");
370 reader.onload = function(evt) {
371 var data = null, results = null, engine = null, vector = null, i = 0, formats = ['KML', 'GPX', 'GeoJSON'];
373 $("geom_upload_container").removeClassName("disabled");
374 $("geom_upload").disabled = false;
379 data = evt.target.result;
381 for (i = 0; i < formats.length; i++) {
382 engine = new OpenLayers.Format[formats[i]]({ internalProjection: Mercator, externalProjection: WGS84 });
384 results = engine.read(data);
387 if (results && results.length) {
391 if (!results || !results.length) {
397 if (vector.geometry.CLASS_NAME !== "OpenLayers.Geometry.LineString") {
401 this.viewLayer.addFeatures([vector]);
402 this.map.zoomToExtent(this.viewLayer.getDataExtent());
405 $("edit-btn").click();
406 } else if ($("create-btn")) {
407 $("create-btn").click();
410 if (this.editControl.handler.realPoints.length < 2) {
416 if (vector.data && vector.data.name) {
417 $("geom_title").value = vector.data.name;
420 $("geom_upload_container").addClassName("disabled");
421 $("geom_upload").disabled = true;
422 reader.readAsText(file);
425 $("map-overlay").show();
427 this.editControl.deactivate();
430 document.observe('simplebox:shown', this.observer.bindAsEventListener(this));
431 SYJPathLength.update();
434 initMaPos: function (aPos) {
435 var extent = null, center = null, zoom = 0;
437 if (aPos.hasOwnProperty('lon') && aPos.hasOwnProperty('lat') && aPos.hasOwnProperty('zoom')) {
438 center = new OpenLayers.LonLat(parseFloat(aPos.lon), parseFloat(aPos.lat)).transform(WGS84, Mercator);
439 zoom = parseInt(aPos.zoom, 10);
440 } else if (aPos.hasOwnProperty('minlon') && aPos.hasOwnProperty('minlat')
441 && aPos.hasOwnProperty('maxlon') && aPos.hasOwnProperty('maxlat')) {
442 extent = new OpenLayers.Bounds(aPos.minlon, aPos.minlat, aPos.maxlon, aPos.maxlat)
443 .transform(WGS84, Mercator);
445 extent = new OpenLayers.Bounds(-160, -70, 160, 70).transform(WGS84, Mercator);
449 this.map.zoomToExtent(extent);
451 this.map.setCenter(center, zoom);
455 observer: function(evt) {
456 if (evt.eventName === "simplebox:shown" && evt.memo.element !== $("termsofusearea")) {
457 this.messenger.hide();
461 prepareForm: function(form) {
462 if (!LoginMgr.logged && !$("geom_accept").checked) {
463 this.messenger.setMessage(SyjStrings.acceptTermsofuseWarn, "warn");
464 $("geom_accept_container").highlight('#F08080');
465 $("geom_accept").activate();
469 var line, realPoints, idx;
471 line = new OpenLayers.Geometry.LineString();
472 realPoints = this.editControl.handler.realPoints;
473 for (idx = 0; idx < realPoints.length; idx++) {
474 line.addComponent(realPoints[idx].geometry.clone());
476 this.viewLayer.addFeatures(new OpenLayers.Feature.Vector(line));
480 if (line.components.length) {
481 $("geom_data").value = this.wkt.write(new OpenLayers.Feature.Vector(line));
483 $("geom_data").value = "";
486 if (this.mode === "edit" && typeof gLoggedInfo.pathid !== "undefined") {
487 $("geomform").setAttribute("action", "path/" + gLoggedInfo.pathid.toString() + '/update');
489 $("geomform").setAttribute("action", "path");
491 this.needsFormResubmit = false;
492 SyjSaveUI.disable.bind(SyjSaveUI).defer();
493 this.messenger.hide();
497 viewMode: function() {
498 var handler = this.editControl.handler;
499 OpenLayers.Handler.ModifiablePath.prototype.finalize.apply(handler, arguments);
500 // we need to recreate them on next createFeature; otherwise
501 // they'll reference destroyed features
502 delete(handler.handlers.drag);
503 delete(handler.handlers.feature);
504 this.editControl.deactivate();
507 editMode: function() {
508 var components, point0, point, pixels, pixel, idx;
510 this.initEditControl();
512 this.editControl.activate();
513 if (this.viewLayer.features.length) {
514 components = this.viewLayer.features[0].geometry.components;
515 point0 = components[0];
517 pixel = this.map.getPixelFromLonLat(new OpenLayers.LonLat(point0.x, point0.y));
518 this.editControl.handler.createFeature(pixel);
519 this.editControl.handler.lastUp = pixel;
521 for (idx = 1; idx < components.length; idx++) {
522 point = components[idx];
523 pixels.push(this.map.getPixelFromLonLat(new OpenLayers.LonLat(point.x, point.y)));
525 this.editControl.handler.addPoints(pixels);
527 this.unsavedRoute = {
528 features: this.viewLayer.features.invoke('clone'),
529 title: $("geom_title").value
533 this.viewLayer.destroyFeatures();
535 SYJDataUi.editmode();
536 if (this.editControl.handler.realPoints && this.editControl.handler.realPoints.length >= 2) {
537 SyjSaveUI.disableSubmit();
543 initEditControl: function() {
546 if (this.editControl) {
550 this.editControl = new OpenLayers.Control.DrawFeature(new OpenLayers.Layer.Vector(), OpenLayers.Handler.SyjModifiablePath, {
552 modify: function(f, line) {
553 SYJPathLength.update();
555 var npoints = this.handler.realPoints.length;
557 $("geom_upload_container").show();
558 SYJView.unsavedRoute = null;
560 if (!SYJView.unsavedRoute) {
561 SYJView.unsavedRoute = {};
571 create: function(f, line) {
572 this.messenger.hide();
573 $("geom_upload_container").hide();
579 styleMap: styleMap.edit
583 this.map.addControl(this.editControl);
584 if (this.editControl.layer.renderer instanceof OpenLayers.Renderer.Canvas) {
585 // using externalGraphic with canvas renderer is definitively too buggy
586 styles = this.editControl.handler.layerOptions.styleMap.styles;
587 styles.select = styles.select_for_canvas;
591 saveSuccess: function(transport) {
592 // server sends and empty response on success. If we get a response, that
593 // probably means an error or warning has been printed by server.
594 if (!transport.responseJSON && transport.responseText.length) {
595 this.saveFailure(null, 500);
599 this.unsavedRoute = null;
600 if (transport.responseJSON && (typeof transport.responseJSON.redirect === "string")) {
601 location = transport.responseJSON.redirect;
605 this.messenger.setMessage(SyjStrings.saveSuccess, "success");
606 SYJDataUi.viewmode();
607 document.title = $('geom_title').value;
610 saveFailure: function(transport, httpCode) {
612 if (typeof httpCode === "undefined") {
613 httpCode = transport? transport.getStatus(): 0;
618 message = SyjStrings.notReachedError;
622 message = SyjStrings.requestError;
626 SYJLogin.messenger.setMessage(SyjStrings.loginNeeded, "warn");
627 SYJLogin.modalbox.show();
628 this.needsFormResubmit = true;
631 message = SyjStrings.gonePathError;
634 message = SyjStrings.serverError;
635 this.needsFormResubmit = true;
638 message = SyjStrings.unknownError;
643 // is some cases, we let the user resubmit, in some other cases, he
644 // needs to modify the path before submitting again
645 if (this.needsFormResubmit) {
649 this.messenger.setMessage(message, "error");
653 var SYJModalClass = Class.create({
657 this.area = $(this.type + '_area');
658 this.messenger = $(this.type + "_message");
659 this.modalbox = new SimpleBox(this.area, {
660 closeMethods: ["onescapekey", "onouterclick", "onbutton"]
663 var anchor = $(this.type + '_control_anchor');
664 var parent = anchor.up('.menu-item');
668 anchor.observe("click", function(evt) {
669 this.modalbox.show();
671 }.bindAsEventListener(this));
673 document.observe('simplebox:shown', this.observer.bindAsEventListener(this));
674 document.observe('simplebox:hidden', this.observer.bindAsEventListener(this));
676 $(this.type + "form").ajaxize({
677 presubmit: this.presubmit.bind(this),
678 onSuccess: this.success.bind(this),
679 onFailure: this.failure.bind(this)
683 checkNotEmpty: function(input, message) {
684 if ($(input).value.strip().empty()) {
685 this.messenger.setMessage(message, "warn");
686 $(input).highlight('#F08080').activate();
692 observer: function(evt) {
693 var simplebox, input;
695 if (evt.eventName === "simplebox:shown" && evt.memo.element !== $("termsofusearea")) {
696 simplebox = evt.memo;
697 if (simplebox === this.modalbox) {
698 input = this.area.select('input[type="text"]')[0];
703 this.modalbox.hide();
706 } else if (evt.eventName === "simplebox:hidden" && evt.memo.element !== $("termsofusearea")) {
707 simplebox = evt.memo;
708 if (simplebox === this.modalbox) {
714 failure: function(transport) {
715 var httpCode = 0, message = SyjStrings.unknownError, input; // default message error
718 httpCode = transport.getStatus();
723 message = SyjStrings.notReachedError;
728 message = SyjStrings.requestError;
731 message = SyjStrings.serverError;
735 this.messenger.setMessage(message, "error");
736 input = this.area.select('input[type="text"]')[0];
737 input.highlight('#F08080').activate();
741 this.messenger.hide();
742 this.area.select('.message').invoke('setMessageStatus', null);
746 var SYJUserClass = Class.create(SYJModalClass, {
750 init: function($super) {
752 $("termsofusearea").hide();
754 $$("#user_termsofuse_anchor, #geom_termsofuse_anchor").invoke('observe', "click", function(evt) {
756 this.toubox = new SimpleBox($("termsofusearea"), {
757 closeMethods: ["onescapekey", "onouterclick", "onbutton"]
761 if (!$("termsofuseiframe").getAttribute("src")) {
762 $("termsofusearea").show();
763 $("termsofuseiframe").setAttribute("src", evt.target.href);
766 }.bindAsEventListener(this));
768 $$("#login_area_create > a").invoke('observe', 'click',
770 this.modalbox.show();
772 }.bindAsEventListener(this));
774 $("user_pseudo-desc").hide();
775 $("user_pseudo").observe('contentchange', function(evt) {
776 var value = evt.target.value;
777 PseudoChecker.reset();
778 if (value && !(value.match(/^[a-zA-Z0-9_.]+$/))) {
779 $("user_pseudo-desc").show().setMessageStatus("warn");
781 $("user_pseudo-desc").hide();
783 }).timedobserve(function() {
784 PseudoChecker.check();
787 $("user_password").observe('contentchange', function(evt) {
788 if (evt.target.value.length < 6) {
789 $("user_password-desc").setMessageStatus("warn");
791 $("user_password-desc").setMessageStatus("success");
793 }.bindAsEventListener(this));
795 $('account-create-anchor').insert({after: new Toggler('account-info').element});
798 presubmit: function() {
799 this.messenger.hide();
800 PseudoChecker.reset();
801 if (!(this.checkNotEmpty("user_pseudo", SyjStrings.userEmptyWarn))) {
805 if (!($("user_pseudo").value.match(/^[a-zA-Z0-9_.]+$/))) {
806 $("user_pseudo-desc").show().setMessageStatus("warn");
807 $("user_pseudo").highlight('#F08080').activate();
811 if (PseudoChecker.exists[$("user_pseudo").value]) {
812 PseudoChecker.availableMessage(false);
813 $("user_pseudo").highlight('#F08080').activate();
817 if (!(this.checkNotEmpty("user_password", SyjStrings.passwordEmptyWarn))) {
821 if ($("user_password").value.length < 6) {
822 $("user_password-desc").setMessageStatus("warn");
823 $("user_password").highlight('#F08080').activate();
827 if ($("user_password").value !== $("user_password_confirm").value) {
828 this.messenger.setMessage(SyjStrings.passwordNoMatchWarn, "warn");
829 $("user_password").highlight('#F08080').activate();
833 if (!(this.checkNotEmpty("user_email", SyjStrings.emailEmptyWarn))) {
837 if (!$("user_accept").checked) {
838 this.messenger.setMessage(SyjStrings.acceptTermsofuseWarn, "warn");
839 $("user_accept_container").highlight('#F08080');
840 $("user_accept").activate();
848 success: function(transport) {
849 if (!transport.responseJSON ||
850 typeof transport.responseJSON.pseudo !== "string"
852 this.messenger.setMessage(SyjStrings.unknownError, "error");
856 LoginMgr.login(transport.responseJSON.pseudo);
857 SYJView.messenger.setMessage(SyjStrings.userSuccess, "success");
858 this.modalbox.hide();
859 if (SYJView.needsFormResubmit) {
860 SYJView.messenger.addMessage(SyjStrings.canResubmit);
861 $("geom_submit").activate();
865 failure: function($super, transport) {
866 var httpCode = 0, focusInput = null, message = "";
869 httpCode = transport.getStatus();
877 if (transport.responseJSON) {
878 switch (transport.responseJSON.message) {
880 message = SyjStrings.emailInvalidWarn;
881 focusInput = $("user_email");
884 PseudoChecker.availableMessage(false);
885 focusInput = $("user_pseudo");
888 message = SyjStrings.uniqueEmailError;
889 focusInput = $("user_email");
898 this.messenger.setMessage(message, "error");
900 focusInput.highlight('#F08080').activate();
907 var SYJUser = new SYJUserClass();
909 var SYJLoginClass = Class.create(SYJModalClass, {
912 init: function($super) {
916 presubmit: function() {
917 this.messenger.hide();
918 if (!(this.checkNotEmpty("login_user", SyjStrings.userEmptyWarn))) {
926 success: function(transport) {
927 if (!transport.responseJSON ||
928 typeof transport.responseJSON.iscreator !== "boolean" ||
929 typeof transport.responseJSON.pseudo !== "string"
931 this.messenger.setMessage(SyjStrings.unknownError, "error");
934 LoginMgr.login(transport.responseJSON.pseudo, transport.responseJSON.iscreator);
936 SYJView.messenger.setMessage(SyjStrings.loginSuccess, "success");
937 this.modalbox.hide();
938 if (SYJView.needsFormResubmit) {
939 SYJView.messenger.addMessage(SyjStrings.canResubmit);
940 $("geom_submit").activate();
944 failure: function($super, transport) {
945 var httpCode = 0, focusInput = null, message = "";
948 httpCode = transport.getStatus();
956 message = SyjStrings.loginFailure;
957 focusInput = $("login_user");
962 this.messenger.setMessage(message, "error");
964 focusInput.highlight('#F08080').activate();
972 var SYJLogin = new SYJLoginClass();
974 var SYJNewpwdClass = Class.create(SYJModalClass, {
977 presubmit: function() {
978 if (!(this.checkNotEmpty("newpwd_email", SyjStrings.emailEmptyWarn))) {
984 success: function(transport) {
985 SYJView.messenger.setMessage(SyjStrings.newpwdSuccess, "success");
986 this.modalbox.hide();
990 var SYJNewpwd = new SYJNewpwdClass();
992 var LoginMgr = Object.extend(gLoggedInfo, {
995 updateUI: function() {
996 if (!this.controlsdeck) {
997 this.controlsdeck = new Deck("login_controls");
1000 this.controlsdeck.setIndex(1);
1001 $$(".logged-hide").invoke('hide');
1002 $$(".logged-show").invoke('show');
1004 this.controlsdeck.setIndex(0);
1005 $$(".logged-hide").invoke('show');
1006 $$(".logged-show").invoke('hide');
1009 if ($("edit-btn")) {
1010 if (this.iscreator && SYJView.mode === 'view') {
1011 $("edit-btn").show();
1013 $("edit-btn").hide();
1018 login: function(aPseudo, aIsCreator) {
1019 if (typeof aIsCreator === "boolean") {
1020 this.iscreator = aIsCreator;
1023 $$('.logged-pseudo').each(function(elt) {
1024 $A(elt.childNodes).filter(function(node) {
1025 return (node.nodeType === 3 || node.tagName.toLowerCase() === 'br');
1026 }).each(function(node) {
1027 node.nodeValue = node.nodeValue.replace('%s', aPseudo);
1034 var PseudoChecker = {
1041 message: function(str, status, throbber) {
1043 if (!this.messageelt) {
1044 row = new Element('tr');
1045 // we can't use row.update('<td></td><td><div></div></td>')
1046 // because gecko would mangle the <td>s
1047 row.insert(new Element('td'))
1048 .insert((new Element('td')).update(new Element('div')));
1050 $("user_pseudo").up('tr').insert({after: row});
1051 this.messageelt = new Element('span');
1052 this.throbber = new Element("img", { src: "icons/throbber.gif"});
1053 row.down('div').insert(this.throbber).insert(this.messageelt);
1056 this.throbber.show();
1058 this.throbber.hide();
1060 this.messageelt.up().setStyle({visibility: ''});
1061 this.messageelt.className = status;
1062 this.messageelt.update(str);
1065 availableMessage: function(available) {
1066 var message = available ? SyjStrings.availablePseudo: SyjStrings.unavailablePseudo,
1067 status = available ? "success": "warn";
1068 this.message(message, status, false);
1074 this.req = this.currentvalue = null;
1076 if (this.messageelt) {
1077 this.messageelt.up().setStyle({visibility: 'hidden'});
1082 var pseudo = $("user_pseudo").value;
1086 if (!pseudo || !(pseudo.match(/^[a-zA-Z0-9_.]+$/))) {
1090 if (typeof this.exists[pseudo] === "boolean") {
1092 this.availableMessage(!this.exists[pseudo]);
1096 this.message(SyjStrings.pseudoChecking, "", true);
1098 this.currentvalue = pseudo;
1099 this.req = new Ajax.TimedRequest('userexists/' + encodeURIComponent(pseudo), 20, {
1100 onFailure: this.failure.bind(this),
1101 onSuccess: this.success.bind(this)
1105 failure: function(transport) {
1106 var httpCode = 0, value = this.currentvalue;
1109 httpCode = transport.getStatus();
1112 if (httpCode === 404) {
1113 this.exists[value] = false;
1114 this.availableMessage(true);
1119 success: function(transport) {
1120 var httpCode = transport.getStatus(), value = this.currentvalue;
1122 this.exists[value] = true;
1123 this.availableMessage(false);
1127 var Nominatim = (function() {
1128 var presubmit = function() {
1129 var input = $("nominatim-search");
1130 if (input.value.strip().empty()) {
1131 $("nominatim-message").setMessage(SyjStrings.notEmptyField, "warn");
1135 $("nominatim-suggestions").hide();
1136 $("nominatim-message").hide();
1137 $("nominatim-throbber").show();
1141 var zoomToExtent = function(bounds) { // we must call map.setCenter with forceZoomChange to true. See ol#2798
1142 var center = bounds.getCenterLonLat();
1143 if (this.baseLayer.wrapDateLine) {
1144 var maxExtent = this.getMaxExtent();
1145 bounds = bounds.clone();
1146 while (bounds.right < bounds.left) {
1147 bounds.right += maxExtent.getWidth();
1149 center = bounds.getCenterLonLat().wrapDateLine(maxExtent);
1151 this.setCenter(center, this.getZoomForExtent(bounds), false, true);
1154 var success = function(transport) {
1155 $("nominatim-throbber").hide();
1157 if (!transport.responseJSON || !transport.responseJSON.length) {
1158 $("nominatim-message").setMessage(SyjStrings.noResult, 'error');
1159 $("nominatim-search").activate();
1163 var place = transport.responseJSON[0],
1164 bbox = place.boundingbox;
1166 if (!bbox || bbox.length !== 4) {
1167 $("nominatim-message").setMessage(SyjStrings.requestError, 'error');
1171 extent = new OpenLayers.Bounds(bbox[2], bbox[1], bbox[3], bbox[0]).transform(WGS84, Mercator);
1172 zoomToExtent.call(SYJView.map, extent);
1174 $("nominatim-suggestions-list").update();
1176 var clickhandler = function(bbox) {
1177 return function(evt) {
1179 var extent = new OpenLayers.Bounds(bbox[2], bbox[1], bbox[3], bbox[0]).transform(WGS84, Mercator);
1180 $("nominatim-suggestions-list").select("li").invoke('removeClassName', 'current');
1181 evt.target.up('li').addClassName('current');
1182 SYJView.map.zoomToExtent(extent);
1187 for (i = 0; i < transport.responseJSON.length; i++) {
1188 var item = transport.responseJSON[i];
1189 if (item.display_name && item.boundingbox && item.boundingbox.length === 4) {
1190 var li = new Element("li");
1191 var anchor = new Element("a", {
1193 className: "nominatim-suggestions-link"
1196 anchor.observe('click', clickhandler(item.boundingbox));
1197 Element.text(anchor, item.display_name);
1199 var icon = new Element("img", {
1200 className: "nominatim-suggestions-icon",
1201 src: item.icon || 'icons/world.png'
1203 li.insert(icon).insert(anchor);
1204 $("nominatim-suggestions-list").insert(li);
1205 if ($("nominatim-suggestions-list").childNodes.length >= 6) {
1211 if ($("nominatim-suggestions-list").childNodes.length > 1) {
1212 var bottomOffset = $('data_controls').measure('height') + 3;
1213 $("nominatim-suggestions").setStyle({
1214 bottom: (document.viewport.getHeight() - $('data_controls').cumulativeOffset().top + 3).toString() + 'px'
1216 $("nominatim-suggestions-list").select("li:first-child")[0].addClassName('current');
1218 $("nominatim-suggestions").hide();
1223 var failure = function(transport) {
1224 $("nominatim-throbber").hide();
1226 var httpCode = 0, message = SyjStrings.unknownError, input; // default message error
1229 httpCode = transport.getStatus();
1234 message = SyjStrings.notReachedError;
1238 message = SyjStrings.requestError;
1241 message = SyjStrings.serverError;
1245 $("nominatim-message").setMessage(message, 'error');
1250 if (!$("nominatim-form")) {
1253 $("nominatim-controls").hide();
1254 $("nominatim-label").observe('click', function(evt) {
1255 $("nominatim-controls").show();
1256 $("nominatim-search").activate();
1260 $("nominatim-form").ajaxize({
1261 presubmit: presubmit,
1265 new CloseBtn($("nominatim-suggestions"));
1267 $$("#nominatim-message, #nominatim-suggestions, #nominatim-throbber").invoke('hide');
1272 document.observe("dom:loaded", function() {
1275 SYJDataUi.viewmode();
1278 LoginMgr.updateUI();
1282 window.onbeforeunload = function() {
1283 if (SYJView.unsavedRoute) {
1284 return SyjStrings.unsavedConfirmExit;