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, osmLayer, layerOptions, hidemessenger, layerCode, parameters;
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 this.createLayerSwitcher(),
287 new OpenLayers.Control.Attribution()
292 osmLayer = new OpenLayers.Layer.OSM("OSM", [
293 'http://a.tile.openstreetmap.org/${z}/${x}/${y}.png',
294 'http://b.tile.openstreetmap.org/${z}/${x}/${y}.png',
295 'http://c.tile.openstreetmap.org/${z}/${x}/${y}.png'],
296 { wrapDateLine: true , attribution: SyjStrings.osmAttribution, layerCode: 'O'});
298 mapquestLayer = new OpenLayers.Layer.OSM("Mapquest", [
299 'http://otile1.mqcdn.com/tiles/1.0.0/osm/${z}/${x}/${y}.png',
300 'http://otile2.mqcdn.com/tiles/1.0.0/osm/${z}/${x}/${y}.png',
301 'http://otile3.mqcdn.com/tiles/1.0.0/osm/${z}/${x}/${y}.png',
302 'http://otile4.mqcdn.com/tiles/1.0.0/osm/${z}/${x}/${y}.png'],
303 { wrapDateLine: true , attribution: SyjStrings.mapquestAttribution, layerCode: 'M'});
305 layerOptions = {format: OpenLayers.Format.WKT,
307 styleMap: styleMap.view,
308 attribution: SyjStrings.geomAttribution };
310 this.viewLayer = new OpenLayers.Layer.Vector("View Layer", layerOptions);
311 this.map.addLayers([osmLayer, mapquestLayer, this.viewLayer]);
313 this.map.setBaseLayer(mapquestLayer);
315 parameters = OpenLayers.Util.getParameters(window.location.href);
316 if (parameters.layer) {
317 layerCode = parameters.layer;
319 store.remove('baselayer');
323 layerCode = store.get('baselayer');
328 layerCode = layerCode.toUpperCase();
330 $([osmLayer, mapquestLayer]).each(function(layer) {
331 if (layer.layerCode === layerCode) {
332 self.map.setBaseLayer(layer);
338 this.map.events.register("changebaselayer", this, this.saveBaseLayer);
341 $("edit-btn").observe('click', function() {
342 $("geom_submit").value = SyjStrings.editAction;
343 this.messenger.hide();
349 if ($("create-btn")) {
350 $("create-btn").observe('click', function() {
351 $("geom_submit").value = SyjStrings.createAction;
352 this.messenger.hide();
354 this.mode = 'create';
358 if ($("clone-btn")) {
359 $("clone-btn").observe('click', function() {
360 $("geom_submit").value = SyjStrings.cloneAction;
361 $("geom_title").value = "";
362 this.messenger.hide();
364 this.mode = 'create';
368 $("geomform").ajaxize({
369 presubmit: this.prepareForm.bind(this),
370 onSuccess: this.saveSuccess.bind(this),
371 onFailure: this.saveFailure.bind(this)
375 this.messenger = $('message');
376 hidemessenger = this.messenger.empty();
377 new CloseBtn(this.messenger, {
383 this.messenger.hide();
386 if (typeof gInitialGeom !== "undefined" && typeof gInitialGeom.data !== "undefined") {
387 this.viewLayer.addFeatures([this.wkt.read(gInitialGeom.data)]);
388 // XXX: ie has not guessed height of map main div yet during map
389 // initialisation. Now, it will read it correctly.
390 this.map.updateSize();
391 this.map.zoomToExtent(this.viewLayer.getDataExtent());
393 this.initMaPos(gInitialPos);
396 $("map-overlay").hide();
397 $("geom_upload").observe('change', function(evt) {
398 var file = null, reader = null, readerror = null;
399 if (window.FileList && window.FileReader) {
400 file = evt.target.files[0];
401 reader = new FileReader();
402 readerror = function() {
403 this.messenger.setMessage(SyjStrings.uploadFileError, "warn");
405 reader.onload = function(evt) {
406 var data = null, results = null, engine = null, vector = null, i = 0, formats = ['KML', 'GPX', 'GeoJSON'];
408 $("geom_upload_container").removeClassName("disabled");
409 $("geom_upload").disabled = false;
414 data = evt.target.result;
416 for (i = 0; i < formats.length; i++) {
417 engine = new OpenLayers.Format[formats[i]]({ internalProjection: Mercator, externalProjection: WGS84 });
419 results = engine.read(data);
422 if (results && results.length) {
426 if (!results || !results.length) {
432 if (vector.geometry.CLASS_NAME !== "OpenLayers.Geometry.LineString") {
436 this.viewLayer.addFeatures([vector]);
437 this.map.zoomToExtent(this.viewLayer.getDataExtent());
440 $("edit-btn").click();
441 } else if ($("create-btn")) {
442 $("create-btn").click();
445 if (this.editControl.handler.realPoints.length < 2) {
451 if (vector.data && vector.data.name) {
452 $("geom_title").value = vector.data.name;
455 $("geom_upload_container").addClassName("disabled");
456 $("geom_upload").disabled = true;
457 reader.readAsText(file);
460 $("map-overlay").show();
462 this.editControl.deactivate();
465 document.observe('simplebox:shown', this.observer.bindAsEventListener(this));
466 SYJPathLength.update();
469 saveBaseLayer: function(data) {
471 store.set('baselayer', data.layer.layerCode);
475 createLayerSwitcher: function() {
476 var control = new OpenLayers.Control.LayerSwitcher({roundedCorner: false});
477 // XXX: we need to "live-patch" LayerSwitcher to use our icons. We use
478 // a regexp instead of a string in case OpenLayers is modified and in
479 // case browsers modify the function representation
480 control.loadContents = eval('(function() { return (' + control.loadContents.toString().replace(
481 /\s*=\s*imgLocation\s*\+\s*['"]layer-switcher-maximize\.png['"]\s*;/,
482 " = 'icons/layer-switcher-maximize-flipped.png';"
484 var oldMaximizeControl = control.maximizeControl;
486 control.maximizeControl = (function(oldfunc) {
488 oldfunc.apply(control, arguments);
489 self.messenger.hide();
491 }(control.maximizeControl));
496 initMaPos: function (aPos) {
497 var extent = null, center = null, zoom = 0;
499 if (aPos.hasOwnProperty('lon') && aPos.hasOwnProperty('lat') && aPos.hasOwnProperty('zoom')) {
500 center = new OpenLayers.LonLat(parseFloat(aPos.lon), parseFloat(aPos.lat)).transform(WGS84, Mercator);
501 zoom = parseInt(aPos.zoom, 10);
502 } else if (aPos.hasOwnProperty('minlon') && aPos.hasOwnProperty('minlat')
503 && aPos.hasOwnProperty('maxlon') && aPos.hasOwnProperty('maxlat')) {
504 extent = new OpenLayers.Bounds(aPos.minlon, aPos.minlat, aPos.maxlon, aPos.maxlat)
505 .transform(WGS84, Mercator);
507 extent = new OpenLayers.Bounds(-160, -70, 160, 70).transform(WGS84, Mercator);
511 this.map.zoomToExtent(extent);
513 this.map.setCenter(center, zoom);
518 resizeMap: function() {
520 map.style.width = map.offsetWidth.toString() + 'px';
521 map.style.height = map.offsetHeight.toString() + 'px';
524 observer: function(evt) {
525 if (evt.eventName === "simplebox:shown" && evt.memo.element !== $("termsofusearea")) {
526 this.messenger.hide();
530 prepareForm: function(form) {
531 if (!LoginMgr.logged && !$("geom_accept").checked) {
532 this.messenger.setMessage(SyjStrings.acceptTermsofuseWarn, "warn");
533 $("geom_accept_container").highlight('#F08080');
534 $("geom_accept").activate();
538 var line, realPoints, idx;
540 line = new OpenLayers.Geometry.LineString();
541 realPoints = this.editControl.handler.realPoints;
542 for (idx = 0; idx < realPoints.length; idx++) {
543 line.addComponent(realPoints[idx].geometry.clone());
545 this.viewLayer.addFeatures(new OpenLayers.Feature.Vector(line));
549 if (line.components.length) {
550 $("geom_data").value = this.wkt.write(new OpenLayers.Feature.Vector(line));
552 $("geom_data").value = "";
555 if (this.mode === "edit" && typeof gLoggedInfo.pathid !== "undefined") {
556 $("geomform").setAttribute("action", "path/" + gLoggedInfo.pathid.toString() + '/update');
558 $("geomform").setAttribute("action", "path");
560 this.needsFormResubmit = false;
561 SyjSaveUI.disable.bind(SyjSaveUI).defer();
562 this.messenger.hide();
566 viewMode: function() {
567 var handler = this.editControl.handler;
568 OpenLayers.Handler.ModifiablePath.prototype.finalize.apply(handler, arguments);
569 // we need to recreate them on next createFeature; otherwise
570 // they'll reference destroyed features
571 delete(handler.handlers.drag);
572 delete(handler.handlers.feature);
573 this.editControl.deactivate();
576 editMode: function() {
577 var components, point0, point, pixels, pixel, idx;
579 this.initEditControl();
581 this.editControl.activate();
582 if (this.viewLayer.features.length) {
583 components = this.viewLayer.features[0].geometry.components;
584 point0 = components[0];
586 pixel = this.map.getPixelFromLonLat(new OpenLayers.LonLat(point0.x, point0.y));
587 this.editControl.handler.createFeature(pixel);
588 this.editControl.handler.lastUp = pixel;
590 for (idx = 1; idx < components.length; idx++) {
591 point = components[idx];
592 pixels.push(this.map.getPixelFromLonLat(new OpenLayers.LonLat(point.x, point.y)));
594 this.editControl.handler.addPoints(pixels);
596 this.unsavedRoute = {
597 features: this.viewLayer.features.invoke('clone'),
598 title: $("geom_title").value
602 this.viewLayer.destroyFeatures();
604 SYJDataUi.editmode();
605 if (this.editControl.handler.realPoints && this.editControl.handler.realPoints.length >= 2) {
606 SyjSaveUI.disableSubmit();
612 initEditControl: function() {
615 if (this.editControl) {
619 this.editControl = new OpenLayers.Control.DrawFeature(new OpenLayers.Layer.Vector(), OpenLayers.Handler.SyjModifiablePath, {
621 modify: function(f, line) {
622 SYJPathLength.update();
624 var npoints = this.handler.realPoints.length;
626 $("geom_upload_container").show();
627 SYJView.unsavedRoute = null;
629 if (!SYJView.unsavedRoute) {
630 SYJView.unsavedRoute = {};
640 create: function(f, line) {
641 this.messenger.hide();
642 $("geom_upload_container").hide();
648 styleMap: styleMap.edit
652 this.map.addControl(this.editControl);
653 if (this.editControl.layer.renderer instanceof OpenLayers.Renderer.Canvas) {
654 // using externalGraphic with canvas renderer is definitively too buggy
655 styles = this.editControl.handler.layerOptions.styleMap.styles;
656 styles.select = styles.select_for_canvas;
660 saveSuccess: function(transport) {
661 // server sends and empty response on success. If we get a response, that
662 // probably means an error or warning has been printed by server.
663 if (!transport.responseJSON && transport.responseText.length) {
664 this.saveFailure(null, 500);
668 this.unsavedRoute = null;
669 if (transport.responseJSON && (typeof transport.responseJSON.redirect === "string")) {
670 location = transport.responseJSON.redirect;
674 this.messenger.setMessage(SyjStrings.saveSuccess, "success");
675 SYJDataUi.viewmode();
676 document.title = $('geom_title').value;
679 saveFailure: function(transport, httpCode) {
681 if (typeof httpCode === "undefined") {
682 httpCode = transport? transport.getStatus(): 0;
687 message = SyjStrings.notReachedError;
691 message = SyjStrings.requestError;
695 SYJLogin.messenger.setMessage(SyjStrings.loginNeeded, "warn");
696 SYJLogin.modalbox.show();
697 this.needsFormResubmit = true;
700 message = SyjStrings.gonePathError;
703 message = SyjStrings.serverError;
704 this.needsFormResubmit = true;
707 message = SyjStrings.unknownError;
712 // is some cases, we let the user resubmit, in some other cases, he
713 // needs to modify the path before submitting again
714 if (this.needsFormResubmit) {
718 this.messenger.setMessage(message, "error");
722 var SYJModalClass = Class.create({
726 this.area = $(this.type + '_area');
727 this.messenger = $(this.type + "_message");
728 this.modalbox = new SimpleBox(this.area, {
729 closeMethods: ["onescapekey", "onouterclick", "onbutton"]
732 var anchor = $(this.type + '_control_anchor');
733 var parent = anchor.up('.menu-item');
737 anchor.observe("click", function(evt) {
738 this.modalbox.show();
740 }.bindAsEventListener(this));
742 document.observe('simplebox:shown', this.observer.bindAsEventListener(this));
743 document.observe('simplebox:hidden', this.observer.bindAsEventListener(this));
745 $(this.type + "form").ajaxize({
746 presubmit: this.presubmit.bind(this),
747 onSuccess: this.success.bind(this),
748 onFailure: this.failure.bind(this)
752 checkNotEmpty: function(input, message) {
753 if ($(input).value.strip().empty()) {
754 this.messenger.setMessage(message, "warn");
755 $(input).highlight('#F08080').activate();
761 observer: function(evt) {
762 var simplebox, input;
764 if (evt.eventName === "simplebox:shown" && evt.memo.element !== $("termsofusearea")) {
765 simplebox = evt.memo;
766 if (simplebox === this.modalbox) {
767 input = this.area.select('input[type="text"]')[0];
772 this.modalbox.hide();
775 } else if (evt.eventName === "simplebox:hidden" && evt.memo.element !== $("termsofusearea")) {
776 simplebox = evt.memo;
777 if (simplebox === this.modalbox) {
783 failure: function(transport) {
784 var httpCode = 0, message = SyjStrings.unknownError, input; // default message error
787 httpCode = transport.getStatus();
792 message = SyjStrings.notReachedError;
797 message = SyjStrings.requestError;
800 message = SyjStrings.serverError;
804 this.messenger.setMessage(message, "error");
805 input = this.area.select('input[type="text"]')[0];
806 input.highlight('#F08080').activate();
810 this.messenger.hide();
811 this.area.select('.message').invoke('setMessageStatus', null);
815 var SYJUserClass = Class.create(SYJModalClass, {
819 init: function($super) {
821 $("termsofusearea").hide();
823 $$("#user_termsofuse_anchor, #geom_termsofuse_anchor").invoke('observe', "click", function(evt) {
825 this.toubox = new SimpleBox($("termsofusearea"), {
826 closeMethods: ["onescapekey", "onouterclick", "onbutton"]
830 if (!$("termsofuseiframe").getAttribute("src")) {
831 $("termsofusearea").show();
832 $("termsofuseiframe").setAttribute("src", evt.target.href);
835 }.bindAsEventListener(this));
837 $$("#login_area_create > a").invoke('observe', 'click',
839 this.modalbox.show();
841 }.bindAsEventListener(this));
843 $("user_pseudo-desc").hide();
844 $("user_pseudo").observe('contentchange', function(evt) {
845 var value = evt.target.value;
846 PseudoChecker.reset();
847 if (value && !(value.match(/^[a-zA-Z0-9_.]+$/))) {
848 $("user_pseudo-desc").show().setMessageStatus("warn");
850 $("user_pseudo-desc").hide();
852 }).timedobserve(function() {
853 PseudoChecker.check();
856 $("user_password").observe('contentchange', function(evt) {
857 if (evt.target.value.length < 6) {
858 $("user_password-desc").setMessageStatus("warn");
860 $("user_password-desc").setMessageStatus("success");
862 }.bindAsEventListener(this));
864 $('account-create-anchor').insert({after: new Toggler('account-info').element});
867 presubmit: function() {
868 this.messenger.hide();
869 PseudoChecker.reset();
870 if (!(this.checkNotEmpty("user_pseudo", SyjStrings.userEmptyWarn))) {
874 if (!($("user_pseudo").value.match(/^[a-zA-Z0-9_.]+$/))) {
875 $("user_pseudo-desc").show().setMessageStatus("warn");
876 $("user_pseudo").highlight('#F08080').activate();
880 if (PseudoChecker.exists[$("user_pseudo").value]) {
881 PseudoChecker.availableMessage(false);
882 $("user_pseudo").highlight('#F08080').activate();
886 if (!(this.checkNotEmpty("user_password", SyjStrings.passwordEmptyWarn))) {
890 if ($("user_password").value.length < 6) {
891 $("user_password-desc").setMessageStatus("warn");
892 $("user_password").highlight('#F08080').activate();
896 if ($("user_password").value !== $("user_password_confirm").value) {
897 this.messenger.setMessage(SyjStrings.passwordNoMatchWarn, "warn");
898 $("user_password").highlight('#F08080').activate();
902 if (!(this.checkNotEmpty("user_email", SyjStrings.emailEmptyWarn))) {
906 if (!$("user_accept").checked) {
907 this.messenger.setMessage(SyjStrings.acceptTermsofuseWarn, "warn");
908 $("user_accept_container").highlight('#F08080');
909 $("user_accept").activate();
917 success: function(transport) {
918 if (!transport.responseJSON ||
919 typeof transport.responseJSON.pseudo !== "string"
921 this.messenger.setMessage(SyjStrings.unknownError, "error");
925 LoginMgr.login(transport.responseJSON.pseudo);
926 SYJView.messenger.setMessage(SyjStrings.userSuccess, "success");
927 this.modalbox.hide();
928 if (SYJView.needsFormResubmit) {
929 SYJView.messenger.addMessage(SyjStrings.canResubmit);
930 $("geom_submit").activate();
934 failure: function($super, transport) {
935 var httpCode = 0, focusInput = null, message = "";
938 httpCode = transport.getStatus();
946 if (transport.responseJSON) {
947 switch (transport.responseJSON.message) {
949 message = SyjStrings.emailInvalidWarn;
950 focusInput = $("user_email");
953 PseudoChecker.availableMessage(false);
954 focusInput = $("user_pseudo");
957 message = SyjStrings.uniqueEmailError;
958 focusInput = $("user_email");
967 this.messenger.setMessage(message, "error");
969 focusInput.highlight('#F08080').activate();
976 var SYJUser = new SYJUserClass();
978 var SYJLoginClass = Class.create(SYJModalClass, {
981 init: function($super) {
985 presubmit: function() {
986 this.messenger.hide();
987 if (!(this.checkNotEmpty("login_user", SyjStrings.userEmptyWarn))) {
995 success: function(transport) {
996 if (!transport.responseJSON ||
997 typeof transport.responseJSON.iscreator !== "boolean" ||
998 typeof transport.responseJSON.pseudo !== "string"
1000 this.messenger.setMessage(SyjStrings.unknownError, "error");
1003 LoginMgr.login(transport.responseJSON.pseudo, transport.responseJSON.iscreator);
1005 SYJView.messenger.setMessage(SyjStrings.loginSuccess, "success");
1006 this.modalbox.hide();
1007 if (SYJView.needsFormResubmit) {
1008 SYJView.messenger.addMessage(SyjStrings.canResubmit);
1009 $("geom_submit").activate();
1013 failure: function($super, transport) {
1014 var httpCode = 0, focusInput = null, message = "";
1017 httpCode = transport.getStatus();
1025 message = SyjStrings.loginFailure;
1026 focusInput = $("login_user");
1031 this.messenger.setMessage(message, "error");
1033 focusInput.highlight('#F08080').activate();
1041 var SYJLogin = new SYJLoginClass();
1043 var SYJNewpwdClass = Class.create(SYJModalClass, {
1046 presubmit: function() {
1047 if (!(this.checkNotEmpty("newpwd_email", SyjStrings.emailEmptyWarn))) {
1053 success: function(transport) {
1054 SYJView.messenger.setMessage(SyjStrings.newpwdSuccess, "success");
1055 this.modalbox.hide();
1059 var SYJNewpwd = new SYJNewpwdClass();
1061 var LoginMgr = Object.extend(gLoggedInfo, {
1064 updateUI: function() {
1065 if (!this.controlsdeck) {
1066 this.controlsdeck = new Deck("login_controls");
1069 this.controlsdeck.setIndex(1);
1070 $$(".logged-hide").invoke('hide');
1071 $$(".logged-show").invoke('show');
1073 this.controlsdeck.setIndex(0);
1074 $$(".logged-hide").invoke('show');
1075 $$(".logged-show").invoke('hide');
1078 if ($("edit-btn")) {
1079 if (this.iscreator && SYJView.mode === 'view') {
1080 $("edit-btn").show();
1082 $("edit-btn").hide();
1087 login: function(aPseudo, aIsCreator) {
1088 if (typeof aIsCreator === "boolean") {
1089 this.iscreator = aIsCreator;
1092 $$('.logged-pseudo').each(function(elt) {
1093 $A(elt.childNodes).filter(function(node) {
1094 return (node.nodeType === 3 || node.tagName.toLowerCase() === 'br');
1095 }).each(function(node) {
1096 node.nodeValue = node.nodeValue.replace('%s', aPseudo);
1103 var PseudoChecker = {
1110 message: function(str, status, throbber) {
1112 if (!this.messageelt) {
1113 row = new Element('tr');
1114 // we can't use row.update('<td></td><td><div></div></td>')
1115 // because gecko would mangle the <td>s
1116 row.insert(new Element('td'))
1117 .insert((new Element('td')).update(new Element('div')));
1119 $("user_pseudo").up('tr').insert({after: row});
1120 this.messageelt = new Element('span');
1121 this.throbber = new Element("img", { src: "icons/throbber.gif"});
1122 row.down('div').insert(this.throbber).insert(this.messageelt);
1125 this.throbber.show();
1127 this.throbber.hide();
1129 this.messageelt.up().setStyle({visibility: ''});
1130 this.messageelt.className = status;
1131 this.messageelt.update(str);
1134 availableMessage: function(available) {
1135 var message = available ? SyjStrings.availablePseudo: SyjStrings.unavailablePseudo,
1136 status = available ? "success": "warn";
1137 this.message(message, status, false);
1143 this.req = this.currentvalue = null;
1145 if (this.messageelt) {
1146 this.messageelt.up().setStyle({visibility: 'hidden'});
1151 var pseudo = $("user_pseudo").value;
1155 if (!pseudo || !(pseudo.match(/^[a-zA-Z0-9_.]+$/))) {
1159 if (typeof this.exists[pseudo] === "boolean") {
1161 this.availableMessage(!this.exists[pseudo]);
1165 this.message(SyjStrings.pseudoChecking, "", true);
1167 this.currentvalue = pseudo;
1168 this.req = new Ajax.TimedRequest('userexists/' + encodeURIComponent(pseudo), 20, {
1169 onFailure: this.failure.bind(this),
1170 onSuccess: this.success.bind(this)
1174 failure: function(transport) {
1175 var httpCode = 0, value = this.currentvalue;
1178 httpCode = transport.getStatus();
1181 if (httpCode === 404) {
1182 this.exists[value] = false;
1183 this.availableMessage(true);
1188 success: function(transport) {
1189 var httpCode = transport.getStatus(), value = this.currentvalue;
1191 this.exists[value] = true;
1192 this.availableMessage(false);
1196 var Nominatim = (function() {
1197 var presubmit = function() {
1198 var input = $("nominatim-search");
1199 if (input.value.strip().empty()) {
1200 $("nominatim-message").setMessage(SyjStrings.notEmptyField, "warn");
1204 $("nominatim-suggestions").hide();
1205 $("nominatim-message").hide();
1206 $("nominatim-throbber").show();
1210 var zoomToExtent = function(bounds) { // we must call map.setCenter with forceZoomChange to true. See ol#2798
1211 var center = bounds.getCenterLonLat();
1212 if (this.baseLayer.wrapDateLine) {
1213 var maxExtent = this.getMaxExtent();
1214 bounds = bounds.clone();
1215 while (bounds.right < bounds.left) {
1216 bounds.right += maxExtent.getWidth();
1218 center = bounds.getCenterLonLat().wrapDateLine(maxExtent);
1220 this.setCenter(center, this.getZoomForExtent(bounds), false, true);
1223 var success = function(transport) {
1224 $("nominatim-throbber").hide();
1226 if (!transport.responseJSON || !transport.responseJSON.length) {
1227 $("nominatim-message").setMessage(SyjStrings.noResult, 'error');
1228 $("nominatim-search").activate();
1232 var place = transport.responseJSON[0],
1233 bbox = place.boundingbox;
1235 if (!bbox || bbox.length !== 4) {
1236 $("nominatim-message").setMessage(SyjStrings.requestError, 'error');
1240 extent = new OpenLayers.Bounds(bbox[2], bbox[1], bbox[3], bbox[0]).transform(WGS84, Mercator);
1241 zoomToExtent.call(SYJView.map, extent);
1243 $("nominatim-suggestions-list").update();
1245 var clickhandler = function(bbox) {
1246 return function(evt) {
1248 var extent = new OpenLayers.Bounds(bbox[2], bbox[1], bbox[3], bbox[0]).transform(WGS84, Mercator);
1249 $("nominatim-suggestions-list").select("li").invoke('removeClassName', 'current');
1250 evt.target.up('li').addClassName('current');
1251 SYJView.map.zoomToExtent(extent);
1256 for (i = 0; i < transport.responseJSON.length; i++) {
1257 var item = transport.responseJSON[i];
1258 if (item.display_name && item.boundingbox && item.boundingbox.length === 4) {
1259 var li = new Element("li");
1260 var anchor = new Element("a", {
1262 className: "nominatim-suggestions-link"
1265 anchor.observe('click', clickhandler(item.boundingbox));
1266 Element.text(anchor, item.display_name);
1268 var icon = new Element("img", {
1269 className: "nominatim-suggestions-icon",
1270 src: item.icon || 'icons/world.png'
1272 li.insert(icon).insert(anchor);
1273 $("nominatim-suggestions-list").insert(li);
1274 if ($("nominatim-suggestions-list").childNodes.length >= 6) {
1280 if ($("nominatim-suggestions-list").childNodes.length > 1) {
1281 var bottomOffset = $('data_controls').measure('height') + 3;
1282 $("nominatim-suggestions").setStyle({
1283 bottom: (document.viewport.getHeight() - $('data_controls').cumulativeOffset().top + 3).toString() + 'px'
1285 $("nominatim-suggestions-list").select("li:first-child")[0].addClassName('current');
1287 $("nominatim-suggestions").hide();
1292 var failure = function(transport) {
1293 $("nominatim-throbber").hide();
1295 var httpCode = 0, message = SyjStrings.unknownError, input; // default message error
1298 httpCode = transport.getStatus();
1303 message = SyjStrings.notReachedError;
1307 message = SyjStrings.requestError;
1310 message = SyjStrings.serverError;
1314 $("nominatim-message").setMessage(message, 'error');
1319 if (!$("nominatim-form")) {
1322 $("nominatim-controls").hide();
1323 $("nominatim-label").observe('click', function(evt) {
1324 $("nominatim-controls").show();
1325 $("nominatim-search").activate();
1329 $("nominatim-form").ajaxize({
1330 presubmit: presubmit,
1334 new CloseBtn($("nominatim-suggestions"));
1336 $$("#nominatim-message, #nominatim-suggestions, #nominatim-throbber").invoke('hide');
1341 document.observe("dom:loaded", function() {
1344 SYJDataUi.viewmode();
1347 LoginMgr.updateUI();
1351 window.onbeforeunload = function() {
1352 if (SYJView.unsavedRoute) {
1353 return SyjStrings.unsavedConfirmExit;
1359 window.onresize = function() {
1360 SYJView.resizeMap();