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", null, { wrapDateLine: true , attribution: SyjStrings.osmAttribution });
293 layerOptions = {format: OpenLayers.Format.WKT,
295 styleMap: styleMap.view,
296 attribution: SyjStrings.geomAttribution };
298 this.viewLayer = new OpenLayers.Layer.Vector("View Layer", layerOptions);
299 this.map.addLayers([baseLayer, this.viewLayer]);
302 $("edit-btn").observe('click', function() {
303 $("geom_submit").value = SyjStrings.editAction;
304 this.messenger.hide();
310 if ($("create-btn")) {
311 $("create-btn").observe('click', function() {
312 $("geom_submit").value = SyjStrings.createAction;
313 this.messenger.hide();
315 this.mode = 'create';
319 if ($("clone-btn")) {
320 $("clone-btn").observe('click', function() {
321 $("geom_submit").value = SyjStrings.cloneAction;
322 $("geom_title").value = "";
323 this.messenger.hide();
325 this.mode = 'create';
329 $("geomform").ajaxize({
330 presubmit: this.prepareForm.bind(this),
331 onSuccess: this.saveSuccess.bind(this),
332 onFailure: this.saveFailure.bind(this)
336 this.messenger = $('message');
337 hidemessenger = this.messenger.empty();
338 new CloseBtn(this.messenger, {
344 this.messenger.hide();
347 if (typeof gInitialGeom !== "undefined" && typeof gInitialGeom.data !== "undefined") {
348 this.viewLayer.addFeatures([this.wkt.read(gInitialGeom.data)]);
349 // XXX: ie has not guessed height of map main div yet during map
350 // initialisation. Now, it will read it correctly.
351 this.map.updateSize();
352 this.map.zoomToExtent(this.viewLayer.getDataExtent());
354 this.initMaPos(gInitialPos);
357 $("map-overlay").hide();
358 $("geom_upload").observe('change', function(evt) {
359 var file = null, reader = null, readerror = null;
360 if (window.FileList && window.FileReader) {
361 file = evt.target.files[0];
362 reader = new FileReader();
363 readerror = function() {
364 this.messenger.setMessage(SyjStrings.uploadFileError, "warn");
366 reader.onload = function(evt) {
367 var data = null, results = null, engine = null, vector = null, i = 0, formats = ['KML', 'GPX', 'GeoJSON'];
369 $("geom_upload_container").removeClassName("disabled");
370 $("geom_upload").disabled = false;
375 data = evt.target.result;
377 for (i = 0; i < formats.length; i++) {
378 engine = new OpenLayers.Format[formats[i]]({ internalProjection: Mercator, externalProjection: WGS84 });
380 results = engine.read(data);
383 if (results && results.length) {
387 if (!results || !results.length) {
393 if (vector.geometry.CLASS_NAME !== "OpenLayers.Geometry.LineString") {
397 this.viewLayer.addFeatures([vector]);
398 this.map.zoomToExtent(this.viewLayer.getDataExtent());
401 $("edit-btn").click();
402 } else if ($("create-btn")) {
403 $("create-btn").click();
406 if (this.editControl.handler.realPoints.length < 2) {
412 if (vector.data && vector.data.name) {
413 $("geom_title").value = vector.data.name;
416 $("geom_upload_container").addClassName("disabled");
417 $("geom_upload").disabled = true;
418 reader.readAsText(file);
421 $("map-overlay").show();
423 this.editControl.deactivate();
426 document.observe('simplebox:shown', this.observer.bindAsEventListener(this));
427 SYJPathLength.update();
430 initMaPos: function (aPos) {
431 var extent = null, center = null, zoom = 0;
433 if (aPos.hasOwnProperty('lon') && aPos.hasOwnProperty('lat') && aPos.hasOwnProperty('zoom')) {
434 center = new OpenLayers.LonLat(parseFloat(aPos.lon), parseFloat(aPos.lat)).transform(WGS84, Mercator);
435 zoom = parseInt(aPos.zoom, 10);
436 } else if (aPos.hasOwnProperty('minlon') && aPos.hasOwnProperty('minlat')
437 && aPos.hasOwnProperty('maxlon') && aPos.hasOwnProperty('maxlat')) {
438 extent = new OpenLayers.Bounds(aPos.minlon, aPos.minlat, aPos.maxlon, aPos.maxlat)
439 .transform(WGS84, Mercator);
441 extent = new OpenLayers.Bounds(-160, -70, 160, 70).transform(WGS84, Mercator);
445 this.map.zoomToExtent(extent);
447 this.map.setCenter(center, zoom);
452 resizeMap: function() {
454 map.style.width = map.offsetWidth.toString() + 'px';
455 map.style.height = map.offsetHeight.toString() + 'px';
458 observer: function(evt) {
459 if (evt.eventName === "simplebox:shown" && evt.memo.element !== $("termsofusearea")) {
460 this.messenger.hide();
464 prepareForm: function(form) {
465 if (!LoginMgr.logged && !$("geom_accept").checked) {
466 this.messenger.setMessage(SyjStrings.acceptTermsofuseWarn, "warn");
467 $("geom_accept_container").highlight('#F08080');
468 $("geom_accept").activate();
472 var line, realPoints, idx;
474 line = new OpenLayers.Geometry.LineString();
475 realPoints = this.editControl.handler.realPoints;
476 for (idx = 0; idx < realPoints.length; idx++) {
477 line.addComponent(realPoints[idx].geometry.clone());
479 this.viewLayer.addFeatures(new OpenLayers.Feature.Vector(line));
483 if (line.components.length) {
484 $("geom_data").value = this.wkt.write(new OpenLayers.Feature.Vector(line));
486 $("geom_data").value = "";
489 if (this.mode === "edit" && typeof gLoggedInfo.pathid !== "undefined") {
490 $("geomform").setAttribute("action", "path/" + gLoggedInfo.pathid.toString() + '/update');
492 $("geomform").setAttribute("action", "path");
494 this.needsFormResubmit = false;
495 SyjSaveUI.disable.bind(SyjSaveUI).defer();
496 this.messenger.hide();
500 viewMode: function() {
501 var handler = this.editControl.handler;
502 OpenLayers.Handler.ModifiablePath.prototype.finalize.apply(handler, arguments);
503 // we need to recreate them on next createFeature; otherwise
504 // they'll reference destroyed features
505 delete(handler.handlers.drag);
506 delete(handler.handlers.feature);
507 this.editControl.deactivate();
510 editMode: function() {
511 var components, point0, point, pixels, pixel, idx;
513 this.initEditControl();
515 this.editControl.activate();
516 if (this.viewLayer.features.length) {
517 components = this.viewLayer.features[0].geometry.components;
518 point0 = components[0];
520 pixel = this.map.getPixelFromLonLat(new OpenLayers.LonLat(point0.x, point0.y));
521 this.editControl.handler.createFeature(pixel);
522 this.editControl.handler.lastUp = pixel;
524 for (idx = 1; idx < components.length; idx++) {
525 point = components[idx];
526 pixels.push(this.map.getPixelFromLonLat(new OpenLayers.LonLat(point.x, point.y)));
528 this.editControl.handler.addPoints(pixels);
530 this.unsavedRoute = {
531 features: this.viewLayer.features.invoke('clone'),
532 title: $("geom_title").value
536 this.viewLayer.destroyFeatures();
538 SYJDataUi.editmode();
539 if (this.editControl.handler.realPoints && this.editControl.handler.realPoints.length >= 2) {
540 SyjSaveUI.disableSubmit();
546 initEditControl: function() {
549 if (this.editControl) {
553 this.editControl = new OpenLayers.Control.DrawFeature(new OpenLayers.Layer.Vector(), OpenLayers.Handler.SyjModifiablePath, {
555 modify: function(f, line) {
556 SYJPathLength.update();
558 var npoints = this.handler.realPoints.length;
560 $("geom_upload_container").show();
561 SYJView.unsavedRoute = null;
563 if (!SYJView.unsavedRoute) {
564 SYJView.unsavedRoute = {};
574 create: function(f, line) {
575 this.messenger.hide();
576 $("geom_upload_container").hide();
582 styleMap: styleMap.edit
586 this.map.addControl(this.editControl);
587 if (this.editControl.layer.renderer instanceof OpenLayers.Renderer.Canvas) {
588 // using externalGraphic with canvas renderer is definitively too buggy
589 styles = this.editControl.handler.layerOptions.styleMap.styles;
590 styles.select = styles.select_for_canvas;
594 saveSuccess: function(transport) {
595 // server sends and empty response on success. If we get a response, that
596 // probably means an error or warning has been printed by server.
597 if (!transport.responseJSON && transport.responseText.length) {
598 this.saveFailure(null, 500);
602 this.unsavedRoute = null;
603 if (transport.responseJSON && (typeof transport.responseJSON.redirect === "string")) {
604 location = transport.responseJSON.redirect;
608 this.messenger.setMessage(SyjStrings.saveSuccess, "success");
609 SYJDataUi.viewmode();
610 document.title = $('geom_title').value;
613 saveFailure: function(transport, httpCode) {
615 if (typeof httpCode === "undefined") {
616 httpCode = transport? transport.getStatus(): 0;
621 message = SyjStrings.notReachedError;
625 message = SyjStrings.requestError;
629 SYJLogin.messenger.setMessage(SyjStrings.loginNeeded, "warn");
630 SYJLogin.modalbox.show();
631 this.needsFormResubmit = true;
634 message = SyjStrings.gonePathError;
637 message = SyjStrings.serverError;
638 this.needsFormResubmit = true;
641 message = SyjStrings.unknownError;
646 // is some cases, we let the user resubmit, in some other cases, he
647 // needs to modify the path before submitting again
648 if (this.needsFormResubmit) {
652 this.messenger.setMessage(message, "error");
656 var SYJModalClass = Class.create({
660 this.area = $(this.type + '_area');
661 this.messenger = $(this.type + "_message");
662 this.modalbox = new SimpleBox(this.area, {
663 closeMethods: ["onescapekey", "onouterclick", "onbutton"]
666 var anchor = $(this.type + '_control_anchor');
667 var parent = anchor.up('.menu-item');
671 anchor.observe("click", function(evt) {
672 this.modalbox.show();
674 }.bindAsEventListener(this));
676 document.observe('simplebox:shown', this.observer.bindAsEventListener(this));
677 document.observe('simplebox:hidden', this.observer.bindAsEventListener(this));
679 $(this.type + "form").ajaxize({
680 presubmit: this.presubmit.bind(this),
681 onSuccess: this.success.bind(this),
682 onFailure: this.failure.bind(this)
686 checkNotEmpty: function(input, message) {
687 if ($(input).value.strip().empty()) {
688 this.messenger.setMessage(message, "warn");
689 $(input).highlight('#F08080').activate();
695 observer: function(evt) {
696 var simplebox, input;
698 if (evt.eventName === "simplebox:shown" && evt.memo.element !== $("termsofusearea")) {
699 simplebox = evt.memo;
700 if (simplebox === this.modalbox) {
701 input = this.area.select('input[type="text"]')[0];
706 this.modalbox.hide();
709 } else if (evt.eventName === "simplebox:hidden" && evt.memo.element !== $("termsofusearea")) {
710 simplebox = evt.memo;
711 if (simplebox === this.modalbox) {
717 failure: function(transport) {
718 var httpCode = 0, message = SyjStrings.unknownError, input; // default message error
721 httpCode = transport.getStatus();
726 message = SyjStrings.notReachedError;
731 message = SyjStrings.requestError;
734 message = SyjStrings.serverError;
738 this.messenger.setMessage(message, "error");
739 input = this.area.select('input[type="text"]')[0];
740 input.highlight('#F08080').activate();
744 this.messenger.hide();
745 this.area.select('.message').invoke('setMessageStatus', null);
749 var SYJUserClass = Class.create(SYJModalClass, {
753 init: function($super) {
755 $("termsofusearea").hide();
757 $$("#user_termsofuse_anchor, #geom_termsofuse_anchor").invoke('observe', "click", function(evt) {
759 this.toubox = new SimpleBox($("termsofusearea"), {
760 closeMethods: ["onescapekey", "onouterclick", "onbutton"]
764 if (!$("termsofuseiframe").getAttribute("src")) {
765 $("termsofusearea").show();
766 $("termsofuseiframe").setAttribute("src", evt.target.href);
769 }.bindAsEventListener(this));
771 $$("#login_area_create > a").invoke('observe', 'click',
773 this.modalbox.show();
775 }.bindAsEventListener(this));
777 $("user_pseudo-desc").hide();
778 $("user_pseudo").observe('contentchange', function(evt) {
779 var value = evt.target.value;
780 PseudoChecker.reset();
781 if (value && !(value.match(/^[a-zA-Z0-9_.]+$/))) {
782 $("user_pseudo-desc").show().setMessageStatus("warn");
784 $("user_pseudo-desc").hide();
786 }).timedobserve(function() {
787 PseudoChecker.check();
790 $("user_password").observe('contentchange', function(evt) {
791 if (evt.target.value.length < 6) {
792 $("user_password-desc").setMessageStatus("warn");
794 $("user_password-desc").setMessageStatus("success");
796 }.bindAsEventListener(this));
798 $('account-create-anchor').insert({after: new Toggler('account-info').element});
801 presubmit: function() {
802 this.messenger.hide();
803 PseudoChecker.reset();
804 if (!(this.checkNotEmpty("user_pseudo", SyjStrings.userEmptyWarn))) {
808 if (!($("user_pseudo").value.match(/^[a-zA-Z0-9_.]+$/))) {
809 $("user_pseudo-desc").show().setMessageStatus("warn");
810 $("user_pseudo").highlight('#F08080').activate();
814 if (PseudoChecker.exists[$("user_pseudo").value]) {
815 PseudoChecker.availableMessage(false);
816 $("user_pseudo").highlight('#F08080').activate();
820 if (!(this.checkNotEmpty("user_password", SyjStrings.passwordEmptyWarn))) {
824 if ($("user_password").value.length < 6) {
825 $("user_password-desc").setMessageStatus("warn");
826 $("user_password").highlight('#F08080').activate();
830 if ($("user_password").value !== $("user_password_confirm").value) {
831 this.messenger.setMessage(SyjStrings.passwordNoMatchWarn, "warn");
832 $("user_password").highlight('#F08080').activate();
836 if (!(this.checkNotEmpty("user_email", SyjStrings.emailEmptyWarn))) {
840 if (!$("user_accept").checked) {
841 this.messenger.setMessage(SyjStrings.acceptTermsofuseWarn, "warn");
842 $("user_accept_container").highlight('#F08080');
843 $("user_accept").activate();
851 success: function(transport) {
852 if (!transport.responseJSON ||
853 typeof transport.responseJSON.pseudo !== "string"
855 this.messenger.setMessage(SyjStrings.unknownError, "error");
859 LoginMgr.login(transport.responseJSON.pseudo);
860 SYJView.messenger.setMessage(SyjStrings.userSuccess, "success");
861 this.modalbox.hide();
862 if (SYJView.needsFormResubmit) {
863 SYJView.messenger.addMessage(SyjStrings.canResubmit);
864 $("geom_submit").activate();
868 failure: function($super, transport) {
869 var httpCode = 0, focusInput = null, message = "";
872 httpCode = transport.getStatus();
880 if (transport.responseJSON) {
881 switch (transport.responseJSON.message) {
883 message = SyjStrings.emailInvalidWarn;
884 focusInput = $("user_email");
887 PseudoChecker.availableMessage(false);
888 focusInput = $("user_pseudo");
891 message = SyjStrings.uniqueEmailError;
892 focusInput = $("user_email");
901 this.messenger.setMessage(message, "error");
903 focusInput.highlight('#F08080').activate();
910 var SYJUser = new SYJUserClass();
912 var SYJLoginClass = Class.create(SYJModalClass, {
915 init: function($super) {
919 presubmit: function() {
920 this.messenger.hide();
921 if (!(this.checkNotEmpty("login_user", SyjStrings.userEmptyWarn))) {
929 success: function(transport) {
930 if (!transport.responseJSON ||
931 typeof transport.responseJSON.iscreator !== "boolean" ||
932 typeof transport.responseJSON.pseudo !== "string"
934 this.messenger.setMessage(SyjStrings.unknownError, "error");
937 LoginMgr.login(transport.responseJSON.pseudo, transport.responseJSON.iscreator);
939 SYJView.messenger.setMessage(SyjStrings.loginSuccess, "success");
940 this.modalbox.hide();
941 if (SYJView.needsFormResubmit) {
942 SYJView.messenger.addMessage(SyjStrings.canResubmit);
943 $("geom_submit").activate();
947 failure: function($super, transport) {
948 var httpCode = 0, focusInput = null, message = "";
951 httpCode = transport.getStatus();
959 message = SyjStrings.loginFailure;
960 focusInput = $("login_user");
965 this.messenger.setMessage(message, "error");
967 focusInput.highlight('#F08080').activate();
975 var SYJLogin = new SYJLoginClass();
977 var SYJNewpwdClass = Class.create(SYJModalClass, {
980 presubmit: function() {
981 if (!(this.checkNotEmpty("newpwd_email", SyjStrings.emailEmptyWarn))) {
987 success: function(transport) {
988 SYJView.messenger.setMessage(SyjStrings.newpwdSuccess, "success");
989 this.modalbox.hide();
993 var SYJNewpwd = new SYJNewpwdClass();
995 var LoginMgr = Object.extend(gLoggedInfo, {
998 updateUI: function() {
999 if (!this.controlsdeck) {
1000 this.controlsdeck = new Deck("login_controls");
1003 this.controlsdeck.setIndex(1);
1004 $$(".logged-hide").invoke('hide');
1005 $$(".logged-show").invoke('show');
1007 this.controlsdeck.setIndex(0);
1008 $$(".logged-hide").invoke('show');
1009 $$(".logged-show").invoke('hide');
1012 if ($("edit-btn")) {
1013 if (this.iscreator && SYJView.mode === 'view') {
1014 $("edit-btn").show();
1016 $("edit-btn").hide();
1021 login: function(aPseudo, aIsCreator) {
1022 if (typeof aIsCreator === "boolean") {
1023 this.iscreator = aIsCreator;
1026 $$('.logged-pseudo').each(function(elt) {
1027 $A(elt.childNodes).filter(function(node) {
1028 return (node.nodeType === 3 || node.tagName.toLowerCase() === 'br');
1029 }).each(function(node) {
1030 node.nodeValue = node.nodeValue.replace('%s', aPseudo);
1037 var PseudoChecker = {
1044 message: function(str, status, throbber) {
1046 if (!this.messageelt) {
1047 row = new Element('tr');
1048 // we can't use row.update('<td></td><td><div></div></td>')
1049 // because gecko would mangle the <td>s
1050 row.insert(new Element('td'))
1051 .insert((new Element('td')).update(new Element('div')));
1053 $("user_pseudo").up('tr').insert({after: row});
1054 this.messageelt = new Element('span');
1055 this.throbber = new Element("img", { src: "icons/throbber.gif"});
1056 row.down('div').insert(this.throbber).insert(this.messageelt);
1059 this.throbber.show();
1061 this.throbber.hide();
1063 this.messageelt.up().setStyle({visibility: ''});
1064 this.messageelt.className = status;
1065 this.messageelt.update(str);
1068 availableMessage: function(available) {
1069 var message = available ? SyjStrings.availablePseudo: SyjStrings.unavailablePseudo,
1070 status = available ? "success": "warn";
1071 this.message(message, status, false);
1077 this.req = this.currentvalue = null;
1079 if (this.messageelt) {
1080 this.messageelt.up().setStyle({visibility: 'hidden'});
1085 var pseudo = $("user_pseudo").value;
1089 if (!pseudo || !(pseudo.match(/^[a-zA-Z0-9_.]+$/))) {
1093 if (typeof this.exists[pseudo] === "boolean") {
1095 this.availableMessage(!this.exists[pseudo]);
1099 this.message(SyjStrings.pseudoChecking, "", true);
1101 this.currentvalue = pseudo;
1102 this.req = new Ajax.TimedRequest('userexists/' + encodeURIComponent(pseudo), 20, {
1103 onFailure: this.failure.bind(this),
1104 onSuccess: this.success.bind(this)
1108 failure: function(transport) {
1109 var httpCode = 0, value = this.currentvalue;
1112 httpCode = transport.getStatus();
1115 if (httpCode === 404) {
1116 this.exists[value] = false;
1117 this.availableMessage(true);
1122 success: function(transport) {
1123 var httpCode = transport.getStatus(), value = this.currentvalue;
1125 this.exists[value] = true;
1126 this.availableMessage(false);
1130 var Nominatim = (function() {
1131 var presubmit = function() {
1132 var input = $("nominatim-search");
1133 if (input.value.strip().empty()) {
1134 $("nominatim-message").setMessage(SyjStrings.notEmptyField, "warn");
1138 $("nominatim-suggestions").hide();
1139 $("nominatim-message").hide();
1140 $("nominatim-throbber").show();
1144 var zoomToExtent = function(bounds) { // we must call map.setCenter with forceZoomChange to true. See ol#2798
1145 var center = bounds.getCenterLonLat();
1146 if (this.baseLayer.wrapDateLine) {
1147 var maxExtent = this.getMaxExtent();
1148 bounds = bounds.clone();
1149 while (bounds.right < bounds.left) {
1150 bounds.right += maxExtent.getWidth();
1152 center = bounds.getCenterLonLat().wrapDateLine(maxExtent);
1154 this.setCenter(center, this.getZoomForExtent(bounds), false, true);
1157 var success = function(transport) {
1158 $("nominatim-throbber").hide();
1160 if (!transport.responseJSON || !transport.responseJSON.length) {
1161 $("nominatim-message").setMessage(SyjStrings.noResult, 'error');
1162 $("nominatim-search").activate();
1166 var place = transport.responseJSON[0],
1167 bbox = place.boundingbox;
1169 if (!bbox || bbox.length !== 4) {
1170 $("nominatim-message").setMessage(SyjStrings.requestError, 'error');
1174 extent = new OpenLayers.Bounds(bbox[2], bbox[1], bbox[3], bbox[0]).transform(WGS84, Mercator);
1175 zoomToExtent.call(SYJView.map, extent);
1177 $("nominatim-suggestions-list").update();
1179 var clickhandler = function(bbox) {
1180 return function(evt) {
1182 var extent = new OpenLayers.Bounds(bbox[2], bbox[1], bbox[3], bbox[0]).transform(WGS84, Mercator);
1183 $("nominatim-suggestions-list").select("li").invoke('removeClassName', 'current');
1184 evt.target.up('li').addClassName('current');
1185 SYJView.map.zoomToExtent(extent);
1190 for (i = 0; i < transport.responseJSON.length; i++) {
1191 var item = transport.responseJSON[i];
1192 if (item.display_name && item.boundingbox && item.boundingbox.length === 4) {
1193 var li = new Element("li");
1194 var anchor = new Element("a", {
1196 className: "nominatim-suggestions-link"
1199 anchor.observe('click', clickhandler(item.boundingbox));
1200 Element.text(anchor, item.display_name);
1202 var icon = new Element("img", {
1203 className: "nominatim-suggestions-icon",
1204 src: item.icon || 'icons/world.png'
1206 li.insert(icon).insert(anchor);
1207 $("nominatim-suggestions-list").insert(li);
1208 if ($("nominatim-suggestions-list").childNodes.length >= 6) {
1214 if ($("nominatim-suggestions-list").childNodes.length > 1) {
1215 var bottomOffset = $('data_controls').measure('height') + 3;
1216 $("nominatim-suggestions").setStyle({
1217 bottom: (document.viewport.getHeight() - $('data_controls').cumulativeOffset().top + 3).toString() + 'px'
1219 $("nominatim-suggestions-list").select("li:first-child")[0].addClassName('current');
1221 $("nominatim-suggestions").hide();
1226 var failure = function(transport) {
1227 $("nominatim-throbber").hide();
1229 var httpCode = 0, message = SyjStrings.unknownError, input; // default message error
1232 httpCode = transport.getStatus();
1237 message = SyjStrings.notReachedError;
1241 message = SyjStrings.requestError;
1244 message = SyjStrings.serverError;
1248 $("nominatim-message").setMessage(message, 'error');
1253 if (!$("nominatim-form")) {
1256 $("nominatim-controls").hide();
1257 $("nominatim-label").observe('click', function(evt) {
1258 $("nominatim-controls").show();
1259 $("nominatim-search").activate();
1263 $("nominatim-form").ajaxize({
1264 presubmit: presubmit,
1268 new CloseBtn($("nominatim-suggestions"));
1270 $$("#nominatim-message, #nominatim-suggestions, #nominatim-throbber").invoke('hide');
1275 document.observe("dom:loaded", function() {
1278 SYJDataUi.viewmode();
1281 LoginMgr.updateUI();
1285 window.onbeforeunload = function() {
1286 if (SYJView.unsavedRoute) {
1287 return SyjStrings.unsavedConfirmExit;
1293 window.onresize = function() {
1294 SYJView.resizeMap();