1 /* This file is part of Syj, Copyright (c) 2010 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 $("geom_submit").blur();
19 $("geom_title").blur();
30 if (this.status === "enabled") {
34 $("geom_title").disabled = false;
35 $("geom_title").activate();
36 $("geomform").removeClassName("disabled");
37 this.status = "enabled";
42 if (this.status === "disabled") {
46 $("geom_title").blur();
47 $("geom_title").disabled = true;
48 $("geomform").addClassName("disabled");
49 this.status = "disabled";
53 enableSubmit: function() {
54 $("geom_submit").disabled = false;
55 this.status = "partial";
59 disableSubmit: function() {
60 $("geom_submit").blur();
61 $("geom_submit").disabled = true;
62 this.status = "partial";
69 $("data_controls_btns").blur();
70 $("data_controls_btns").hide();
75 $("data_controls_btns").show();
80 OpenLayers.Handler.SyjModifiablePath = OpenLayers.Class(OpenLayers.Handler.ModifiablePath, {
81 mouseup: function(evt) {
82 // do not add a point when navigating
83 var mapControls = this.control.map.controls, idx, ctrl;
85 for (idx = mapControls.length; idx-->0; ) {
86 ctrl = mapControls[idx];
87 if (this.isCtrlNavigationActive(ctrl, evt)) {
91 return OpenLayers.Handler.ModifiablePath.prototype.mouseup.apply(this, arguments);
94 addPoints: function(pixel) {
95 // redraw before last point. As we have a special style for last point, we
96 // need to redraw before last point after adding a new point (otherwise, it
97 // keeps special style forever)
98 var oldpoint = this.point;
99 OpenLayers.Handler.ModifiablePath.prototype.addPoints.apply(this, arguments);
100 this.layer.drawFeature(oldpoint);
103 isCtrlNavigationActive: function(ctrl, evt) {
104 var tolerance = 4, xDiff, yDiff;
106 if (!(ctrl instanceof OpenLayers.Control.Navigation)) {
111 ctrl.zoomBox.active &&
112 ctrl.zoomBox.handler &&
113 ctrl.zoomBox.handler.active &&
114 ctrl.zoomBox.handler.dragHandler &&
115 ctrl.zoomBox.handler.dragHandler.start) {
120 !ctrl.dragPan.active ||
121 !ctrl.dragPan.handler ||
122 !ctrl.dragPan.handler.started ||
123 !ctrl.dragPan.handler.start) {
127 // if mouse moved 4 or less pixels, consider it has not moved.
130 xDiff = evt.xy.x - ctrl.dragPan.handler.start.x;
131 yDiff = evt.xy.y - ctrl.dragPan.handler.start.y;
133 if (Math.sqrt(Math.pow(xDiff,2) + Math.pow(yDiff,2)) <= tolerance) {
139 finalize: function(cancel) {
140 // do nothing. We don't want to finalize path
145 edit: new OpenLayers.StyleMap({
146 "default": new OpenLayers.Style({
147 pointRadius: "${radius}", // sized according to type attribute
148 fillColor: "#ffcc66",
149 strokeColor: "#ff9933",
151 strokeOpacity: "${opacity}",
152 fillOpacity: "${opacity}"
156 radius: function(feature) {
159 if (!(feature.geometry instanceof OpenLayers.Geometry.Point)) {
162 if (feature.type === "middle") {
165 features = feature.layer.features;
166 if (OpenLayers.Util.indexOf(features, feature) === 0) {
168 } else if (OpenLayers.Util.indexOf(features, feature) === features.length - 1) {
173 opacity: function (feature) {
174 if (feature.type === "middle") {
183 "select": new OpenLayers.Style({
184 externalGraphic: "icons/delete.png",
188 "select_for_canvas": new OpenLayers.Style({
190 fillColor: "lightblue"
195 view: new OpenLayers.StyleMap({
196 "default": new OpenLayers.Style({
204 var WGS84 = new OpenLayers.Projection("EPSG:4326");
205 var Mercator = new OpenLayers.Projection("EPSG:900913");
211 wkt: new OpenLayers.Format.WKT({ internalProjection: Mercator, externalProjection: WGS84 }),
212 needsFormResubmit: false,
217 var externalGraphic, baseURL, baseLayer, layerOptions, extent, hidemessenger;
219 // is svg context, opera does not resolve links with base element is svg context
220 externalGraphic = styleMap.edit.styles.select.defaultStyle.externalGraphic;
221 baseURL = document.getElementsByTagName("base")[0].href;
222 styleMap.edit.styles.select.defaultStyle.externalGraphic = baseURL + externalGraphic;
224 this.map = new OpenLayers.Map('map', {
226 new OpenLayers.Control.Navigation(),
227 new OpenLayers.Control.PanZoom(),
228 new OpenLayers.Control.Attribution()
233 baseLayer = new OpenLayers.Layer.OSM("OSM", null, { wrapDateLine: true , attribution: SyjStrings.osmAttribution });
235 layerOptions = {format: OpenLayers.Format.WKT,
237 styleMap: styleMap.view};
238 if (gLoggedInfo.creatorname) {
239 layerOptions.attribution = SyjStrings.routeBy + ' ' + '<strong>' + gLoggedInfo.creatorname + '</strong>';
242 this.viewLayer = new OpenLayers.Layer.Vector("View Layer", layerOptions);
243 this.map.addLayers([baseLayer, this.viewLayer]);
246 $("edit-btn").observe('click', function() {
247 $("geom_submit").value = SyjStrings.editAction;
248 this.messenger.hide();
254 if ($("create-btn")) {
255 $("create-btn").observe('click', function() {
256 $("geom_submit").value = SyjStrings.createAction;
257 this.messenger.hide();
259 this.mode = 'create';
263 if ($("clone-btn")) {
264 $("clone-btn").observe('click', function() {
265 $("geom_submit").value = SyjStrings.cloneAction;
266 $("geom_title").value = "";
267 this.messenger.hide();
269 this.mode = 'create';
270 SyjSaveUI.enableSubmit();
274 $("geomform").ajaxize({
275 presubmit: this.prepareForm.bind(this),
276 onSuccess: this.saveSuccess.bind(this),
277 onFailure: this.saveFailure.bind(this)
279 SyjSaveUI.init().hide();
281 this.messenger = $('message');
282 hidemessenger = this.messenger.empty();
283 new CloseBtn(this.messenger, {
289 this.messenger.hide();
292 if (typeof gInitialGeom !== "undefined" && typeof gInitialGeom.data !== "undefined") {
293 this.viewLayer.addFeatures([this.wkt.read(gInitialGeom.data)]);
294 extent = this.viewLayer.getDataExtent();
295 // XXX: ie has not guessed height of map main div yet during map
296 // initialisation. Now, it will read it correctly.
297 this.map.updateSize();
299 extent = new OpenLayers.Bounds(gMaxExtent.minlon, gMaxExtent.minlat, gMaxExtent.maxlon, gMaxExtent.maxlat)
300 .transform(WGS84, Mercator);
302 this.map.zoomToExtent(extent);
303 document.observe('simplebox:shown', this.observer.bindAsEventListener(this));
306 observer: function(evt) {
307 if (evt.eventName === "simplebox:shown" && evt.memo.element !== $("termsofusearea")) {
308 this.messenger.hide();
312 prepareForm: function(form) {
313 if (!LoginMgr.logged && !$("geom_accept").checked) {
314 this.messenger.setMessage(SyjStrings.acceptTermsofuseWarn, "warn");
315 $("geom_accept_container").highlight('#F08080');
316 $("geom_accept").activate();
320 var line, realPoints, idx;
322 line = new OpenLayers.Geometry.LineString();
323 realPoints = this.editControl.handler.realPoints;
324 for (idx = 0; idx < realPoints.length; idx++) {
325 line.addComponent(realPoints[idx].geometry.clone());
327 this.viewLayer.addFeatures(new OpenLayers.Feature.Vector(line));
331 $("geom_data").value = this.wkt.write(new OpenLayers.Feature.Vector(line));
332 if (this.mode === "edit" && typeof gLoggedInfo.pathid !== "undefined") {
333 $("geomform").setAttribute("action", "path/" + gLoggedInfo.pathid.toString() + '/update');
335 $("geomform").setAttribute("action", "path");
337 this.needsFormResubmit = false;
338 SyjSaveUI.disable.bind(SyjSaveUI).defer();
339 this.messenger.hide();
343 viewMode: function() {
344 var handler = this.editControl.handler;
345 OpenLayers.Handler.ModifiablePath.prototype.finalize.apply(handler, arguments);
346 // we need to recreate them on next createFeature; otherwise
347 // they'll reference destroyed features
348 delete(handler.handlers.drag);
349 delete(handler.handlers.feature);
350 this.editControl.deactivate();
353 editMode: function() {
354 var components, point0, point, pixels, pixel, idx;
356 this.initEditControl();
358 this.editControl.activate();
359 if (this.viewLayer.features.length) {
360 components = this.viewLayer.features[0].geometry.components;
361 point0 = components[0];
363 pixel = this.map.getPixelFromLonLat(new OpenLayers.LonLat(point0.x, point0.y));
364 this.editControl.handler.createFeature(pixel);
365 this.editControl.handler.lastUp = pixel;
367 for (idx = 1; idx < components.length; idx++) {
368 point = components[idx];
369 pixels.push(this.map.getPixelFromLonLat(new OpenLayers.LonLat(point.x, point.y)));
371 this.editControl.handler.addPoints(pixels);
373 this.unsavedRoute = {
374 features: this.viewLayer.features.invoke('clone'),
375 title: $("geom_title").value
379 this.viewLayer.destroyFeatures();
382 if (this.editControl.handler.realPoints && this.editControl.handler.realPoints.length >= 2) {
384 SyjSaveUI.disableSubmit();
386 SyjSaveUI.show().disable();
390 initEditControl: function() {
393 if (this.editControl) {
397 this.editControl = new OpenLayers.Control.DrawFeature(new OpenLayers.Layer.Vector(), OpenLayers.Handler.SyjModifiablePath, {
399 modify: function(f, line) {
400 if (!SYJView.unsavedRoute) {
401 SYJView.unsavedRoute = {};
403 if (this.handler.realPoints.length < 2) {
404 SyjSaveUI.show().disable();
406 SyjSaveUI.show().enable();
413 styleMap: styleMap.edit
417 this.map.addControl(this.editControl);
418 if (this.editControl.layer.renderer instanceof OpenLayers.Renderer.Canvas) {
419 // using externalGraphic with canvas renderer is definitively too buggy
420 styles = this.editControl.handler.layerOptions.styleMap.styles;
421 styles.select = styles.select_for_canvas;
423 new CloseBtn($("geomform"), {
425 marginRight: "-40px",
428 callback: function(form) {
433 this.messenger.hide();
435 if (this.unsavedRoute && typeof this.unsavedRoute.features !== "undefined") {
436 this.viewLayer.addFeatures(this.unsavedRoute.features);
438 if (this.unsavedRoute && typeof this.unsavedRoute.title !== "undefined") {
439 $("geom_title").value = this.unsavedRoute.title;
441 $("geom_title").value = "";
443 this.unsavedRoute = null;
448 saveSuccess: function(transport) {
449 this.unsavedRoute = null;
451 if (transport.responseJSON && (typeof transport.responseJSON.redirect === "string")) {
452 location = transport.responseJSON.redirect;
456 this.messenger.setMessage(SyjStrings.saveSuccess, "success");
459 document.title = $('geom_title').value;
462 saveFailure: function(transport) {
463 var httpCode = 0, message = "";
466 httpCode = transport.getStatus();
470 message = SyjStrings.notReachedError;
474 message = SyjStrings.requestError;
475 if (transport.responseJSON) {
476 switch (transport.responseJSON.message) {
478 message = SyjStrings.uniquePathError;
487 SYJLogin.messenger.setMessage(SyjStrings.loginNeeded, "warn");
488 SYJLogin.modalbox.show();
489 this.needsFormResubmit = true;
492 message = SyjStrings.gonePathError;
495 message = SyjStrings.serverError;
496 this.needsFormResubmit = true;
499 message = SyjStrings.unknownError;
504 // is some cases, we let the user resubmit, in some other cases, he
505 // needs to modify the path before submitting again
506 if (this.needsFormResubmit) {
510 this.messenger.setMessage(message, "error");
514 var SYJModalClass = Class.create({
518 this.area = $(this.type + '_area');
519 this.messenger = $(this.type + "_message");
520 this.modalbox = new SimpleBox(this.area, {
521 closeMethods: ["onescapekey", "onouterclick", "onbutton"]
524 $(this.type + "_control_anchor").observe("click", function(evt) {
525 this.modalbox.show();
527 }.bindAsEventListener(this));
529 document.observe('simplebox:shown', this.observer.bindAsEventListener(this));
530 document.observe('simplebox:hidden', this.observer.bindAsEventListener(this));
532 $(this.type + "form").ajaxize({
533 presubmit: this.presubmit.bind(this),
534 onSuccess: this.success.bind(this),
535 onFailure: this.failure.bind(this)
539 checkNotEmpty: function(input, message) {
540 if ($(input).value.strip().empty()) {
541 this.messenger.setMessage(message, "warn");
542 $(input).highlight('#F08080').activate();
548 observer: function(evt) {
549 var simplebox, input;
551 if (evt.eventName === "simplebox:shown" && evt.memo.element !== $("termsofusearea")) {
552 simplebox = evt.memo;
553 if (simplebox === this.modalbox) {
554 input = this.area.select('input[type="text"]')[0];
559 this.modalbox.hide();
562 } else if (evt.eventName === "simplebox:hidden" && evt.memo.element !== $("termsofusearea")) {
563 simplebox = evt.memo;
564 if (simplebox === this.modalbox) {
570 failure: function(transport) {
571 var httpCode = 0, message = SyjStrings.unknownError, input; // default message error
574 httpCode = transport.getStatus();
579 message = SyjStrings.notReachedError;
584 message = SyjStrings.requestError;
587 message = SyjStrings.serverError;
591 this.messenger.setMessage(message, "error");
592 input = this.area.select('input[type="text"]')[0];
593 input.highlight('#F08080').activate();
597 this.messenger.hide();
598 this.area.select('.message').invoke('setMessageStatus', null);
602 var SYJUserClass = Class.create(SYJModalClass, {
606 init: function($super) {
608 $("termsofusearea").hide();
610 $$("#user_termsofuse_anchor, #geom_termsofuse_anchor").invoke('observe', "click", function(evt) {
612 $("termsofusearea").show();
613 $("termsofuseiframe").setAttribute("src", evt.target.href);
614 this.toubox = new SimpleBox($("termsofusearea"), {
615 closeMethods: ["onescapekey", "onouterclick", "onbutton"]
620 }.bindAsEventListener(this));
622 $$("#login_area_create > a").invoke('observe', 'click',
624 this.modalbox.show();
626 }.bindAsEventListener(this));
628 $("user_pseudo-desc").hide();
629 $("user_pseudo").observe('contentchange', function(evt) {
630 var value = evt.target.value;
631 PseudoChecker.reset();
632 if (value && !(value.match(/^[a-zA-Z0-9_.]+$/))) {
633 $("user_pseudo-desc").show().setMessageStatus("warn");
635 $("user_pseudo-desc").hide();
637 }).timedobserve(function() {
638 PseudoChecker.check();
641 $("user_password").observe('contentchange', function(evt) {
642 if (evt.target.value.length < 6) {
643 $("user_password-desc").setMessageStatus("warn");
645 $("user_password-desc").setMessageStatus("success");
647 }.bindAsEventListener(this));
649 $("account-info").hide();
650 $("account-info-bullet").observe('click', function(evt) {
651 var elt = $("account-info");
653 evt.target.src = "icons/bullet_arrow_right.png";
656 evt.target.src = "icons/bullet_arrow_down.png";
663 presubmit: function() {
664 this.messenger.hide();
665 PseudoChecker.reset();
666 if (!(this.checkNotEmpty("user_pseudo", SyjStrings.userEmptyWarn))) {
670 if (!($("user_pseudo").value.match(/^[a-zA-Z0-9_.]+$/))) {
671 $("user_pseudo-desc").show().setMessageStatus("warn");
672 $("user_pseudo").highlight('#F08080').activate();
676 if (PseudoChecker.exists[$("user_pseudo").value]) {
677 PseudoChecker.availableMessage(false);
678 $("user_pseudo").highlight('#F08080').activate();
682 if (!(this.checkNotEmpty("user_password", SyjStrings.passwordEmptyWarn))) {
686 if ($("user_password").value.length < 6) {
687 $("user_password-desc").setMessageStatus("warn");
688 $("user_password").highlight('#F08080').activate();
692 if ($("user_password").value !== $("user_password_confirm").value) {
693 this.messenger.setMessage(SyjStrings.passwordNoMatchWarn, "warn");
694 $("user_password").highlight('#F08080').activate();
698 if (!(this.checkNotEmpty("user_email", SyjStrings.emailEmptyWarn))) {
702 if (!$("user_accept").checked) {
703 this.messenger.setMessage(SyjStrings.acceptTermsofuseWarn, "warn");
704 $("user_accept_container").highlight('#F08080');
705 $("user_accept").activate();
713 success: function(transport) {
715 SYJView.messenger.setMessage(SyjStrings.userSuccess, "success");
716 this.modalbox.hide();
717 if (SYJView.needsFormResubmit) {
718 SYJView.messenger.addMessage(SyjStrings.canResubmit);
719 $("geom_submit").activate();
723 failure: function($super, transport) {
724 var httpCode = 0, focusInput = null, message = "";
727 httpCode = transport.getStatus();
735 if (transport.responseJSON) {
736 switch (transport.responseJSON.message) {
738 message = SyjStrings.emailInvalidWarn;
739 focusInput = $("user_email");
742 PseudoChecker.availableMessage(false);
743 focusInput = $("user_pseudo");
746 message = SyjStrings.uniqueEmailError;
747 focusInput = $("user_email");
756 this.messenger.setMessage(message, "error");
758 focusInput.highlight('#F08080').activate();
765 var SYJUser = new SYJUserClass();
767 var SYJLoginClass = Class.create(SYJModalClass, {
770 init: function($super) {
774 presubmit: function() {
775 this.messenger.hide();
776 if (!(this.checkNotEmpty("login_user", SyjStrings.userEmptyWarn))) {
784 success: function(transport) {
785 if (transport.responseText === "1") {
786 LoginMgr.login(true);
790 SYJView.messenger.setMessage(SyjStrings.loginSuccess, "success");
791 this.modalbox.hide();
792 if (SYJView.needsFormResubmit) {
793 SYJView.messenger.addMessage(SyjStrings.canResubmit);
794 $("geom_submit").activate();
798 failure: function($super, transport) {
799 var httpCode = 0, focusInput = null, message = "";
802 httpCode = transport.getStatus();
810 message = SyjStrings.loginFailure;
811 focusInput = $("login_user");
816 this.messenger.setMessage(message, "error");
818 focusInput.highlight('#F08080').activate();
826 var SYJLogin = new SYJLoginClass();
828 var SYJNewpwdClass = Class.create(SYJModalClass, {
831 presubmit: function() {
832 if (!(this.checkNotEmpty("newpwd_email", SyjStrings.emailEmptyWarn))) {
838 success: function(transport) {
839 SYJView.messenger.setMessage(SyjStrings.newpwdSuccess, "success");
840 this.modalbox.hide();
844 var SYJNewpwd = new SYJNewpwdClass();
846 var LoginMgr = Object.extend(gLoggedInfo, {
849 updateUI: function() {
850 if (!this.controlsdeck) {
851 this.controlsdeck = new Deck("login_controls");
854 this.controlsdeck.setIndex(1);
855 $$(".logged-hide").invoke('hide');
856 $$(".logged-show").invoke('show');
858 this.controlsdeck.setIndex(0);
859 $$(".logged-hide").invoke('show');
860 $$(".logged-show").invoke('hide');
864 if (this.iscreator && SYJView.mode === 'view') {
865 $("edit-btn").show();
867 $("edit-btn").hide();
872 login: function(aIsCreator) {
873 if (typeof aIsCreator === "boolean") {
874 this.iscreator = aIsCreator;
881 var PseudoChecker = {
888 message: function(str, status, throbber) {
890 if (!this.messageelt) {
891 row = new Element('tr');
892 // we can't use row.update('<td></td><td><div></div></td>')
893 // because gecko would mangle the <td>s
894 row.insert(new Element('td'))
895 .insert((new Element('td')).update(new Element('div')));
897 $("user_pseudo").up('tr').insert({after: row});
898 this.messageelt = new Element('span');
899 this.throbber = new Element("img", { src: "icons/pseudo-throbber.gif"});
900 row.down('div').insert(this.throbber).insert(this.messageelt);
903 this.throbber.show();
905 this.throbber.hide();
907 this.messageelt.up().setStyle({visibility: ''});
908 this.messageelt.className = status;
909 this.messageelt.update(str);
912 availableMessage: function(available) {
913 var message = available ? SyjStrings.availablePseudo: SyjStrings.unavailablePseudo,
914 status = available ? "success": "warn";
915 this.message(message, status, false);
921 this.req = this.currentvalue = null;
923 if (this.messageelt) {
924 this.messageelt.up().setStyle({visibility: 'hidden'});
929 var pseudo = $("user_pseudo").value;
933 if (!pseudo || !(pseudo.match(/^[a-zA-Z0-9_.]+$/))) {
937 if (typeof this.exists[pseudo] === "boolean") {
939 this.availableMessage(!this.exists[pseudo]);
943 this.message(SyjStrings.pseudoChecking, "", true);
945 this.currentvalue = pseudo;
946 this.req = new Ajax.TimedRequest('userexists/' + encodeURIComponent(pseudo), 20, {
947 onFailure: this.failure.bind(this),
948 onSuccess: this.success.bind(this)
952 failure: function(transport) {
953 var httpCode = 0, value = this.currentvalue;
956 httpCode = transport.getStatus();
959 if (httpCode === 404) {
960 this.exists[value] = false;
961 this.availableMessage(true);
966 success: function(transport) {
967 var httpCode = transport.getStatus(), value = this.currentvalue;
969 this.exists[value] = true;
970 this.availableMessage(false);
975 document.observe("dom:loaded", function() {
982 window.onbeforeunload = function() {
983 if (SYJView.unsavedRoute) {
984 return SyjStrings.unsavedConfirmExit;