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";
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,
215 var externalGraphic, baseURL, baseLayer, layerOptions, extent, hidemessenger;
217 // is svg context, opera does not resolve links with base element is svg context
218 externalGraphic = styleMap.edit.styles.select.defaultStyle.externalGraphic;
219 baseURL = document.getElementsByTagName("base")[0].href;
220 styleMap.edit.styles.select.defaultStyle.externalGraphic = baseURL + externalGraphic;
222 this.map = new OpenLayers.Map('map', {
224 new OpenLayers.Control.Navigation(),
225 new OpenLayers.Control.PanZoom(),
226 new OpenLayers.Control.Attribution()
231 baseLayer = new OpenLayers.Layer.OSM("OSM", null, { wrapDateLine: true , attribution: SyjStrings.osmAttribution });
233 layerOptions = {format: OpenLayers.Format.WKT,
235 styleMap: styleMap.view};
236 if (gLoggedInfo.creatorname) {
237 layerOptions.attribution = SyjStrings.routeBy + ' ' + '<strong>' + gLoggedInfo.creatorname + '</strong>';
240 this.viewLayer = new OpenLayers.Layer.Vector("View Layer", layerOptions);
241 this.map.addLayers([baseLayer, this.viewLayer]);
243 $("edit-btn").observe('click', (function() {
244 this.messenger.hide();
248 $("geomform").ajaxize({
249 presubmit: this.prepareForm.bind(this),
250 onSuccess: this.saveSuccess.bind(this),
251 onFailure: this.saveFailure.bind(this)
253 SyjSaveUI.init().hide();
255 this.messenger = $('message');
256 hidemessenger = this.messenger.empty();
257 new CloseBtn(this.messenger, {
263 this.messenger.hide();
266 if ($("geom_data").value) {
267 this.viewLayer.addFeatures([this.wkt.read($("geom_data").value)]);
268 extent = this.viewLayer.getDataExtent();
269 // XXX: ie has not guessed height of map main div yet during map
270 // initialisation. Now, it will read it correctly.
271 this.map.updateSize();
273 extent = new OpenLayers.Bounds(gMaxExtent.minlon, gMaxExtent.minlat, gMaxExtent.maxlon, gMaxExtent.maxlat)
274 .transform(WGS84, Mercator);
276 this.map.zoomToExtent(extent);
277 document.observe('simplebox:shown', this.observer.bindAsEventListener(this));
280 observer: function(evt) {
281 if (evt.eventName === "simplebox:shown" && evt.memo.element !== $("termsofusearea")) {
282 this.messenger.hide();
286 prepareForm: function(form) {
287 if (!LoginMgr.logged && !$("geom_accept").checked) {
288 this.messenger.setMessage(SyjStrings.acceptTermsofuseWarn, "warn");
289 $("geom_accept_container").highlight('#F08080');
290 $("geom_accept").activate();
294 var line, realPoints, idx, handler;
296 line = new OpenLayers.Geometry.LineString();
297 realPoints = this.editControl.handler.realPoints;
298 for (idx = 0; idx < realPoints.length; idx++) {
299 line.addComponent(realPoints[idx].geometry.clone());
301 this.viewLayer.addFeatures(new OpenLayers.Feature.Vector(line));
302 handler = this.editControl.handler;
303 OpenLayers.Handler.ModifiablePath.prototype.finalize.apply(handler, arguments);
304 // we need to recreate them on next createFeature; otherwise
305 // they'll reference destroyed features
306 delete(handler.handlers.drag);
307 delete(handler.handlers.feature);
308 this.editControl.deactivate();
310 $("geom_data").value = this.wkt.write(new OpenLayers.Feature.Vector(line));
311 this.needsFormResubmit = false;
312 SyjSaveUI.disable.bind(SyjSaveUI).defer();
313 this.messenger.hide();
317 editMode: function() {
318 var components, point0, point, pixels, pixel, idx;
320 this.initEditControl();
322 this.editControl.activate();
323 if (this.viewLayer.features.length) {
324 components = this.viewLayer.features[0].geometry.components;
325 point0 = components[0];
327 pixel = this.map.getPixelFromLonLat(new OpenLayers.LonLat(point0.x, point0.y));
328 this.editControl.handler.createFeature(pixel);
329 this.editControl.handler.lastUp = pixel;
331 for (idx = 1; idx < components.length; idx++) {
332 point = components[idx];
333 pixels.push(this.map.getPixelFromLonLat(new OpenLayers.LonLat(point.x, point.y)));
335 this.editControl.handler.addPoints(pixels);
339 this.viewLayer.destroyFeatures();
342 if (this.editControl.handler.realPoints && this.editControl.handler.realPoints.length >= 2) {
343 SyjSaveUI.show().disableSubmit();
345 SyjSaveUI.show().disable();
349 initEditControl: function() {
352 if (this.editControl) {
356 this.editControl = new OpenLayers.Control.DrawFeature(new OpenLayers.Layer.Vector(), OpenLayers.Handler.SyjModifiablePath, {
358 modify: function(f, line) {
359 if (this.handler.realPoints.length < 2) {
360 SyjSaveUI.show().disable();
362 SyjSaveUI.show().enable();
369 styleMap: styleMap.edit
373 this.map.addControl(this.editControl);
374 if (this.editControl.layer.renderer instanceof OpenLayers.Renderer.Canvas) {
375 // using externalGraphic with canvas renderer is definitively too buggy
376 styles = this.editControl.handler.layerOptions.styleMap.styles;
377 styles.select = styles.select_for_canvas;
381 saveSuccess: function(transport) {
382 if (transport.responseJSON && (typeof transport.responseJSON.redirect === "string")) {
383 location = transport.responseJSON.redirect;
387 this.messenger.setMessage(SyjStrings.saveSuccess, "success");
390 document.title = $('geom_title').value;
393 saveFailure: function(transport) {
394 var httpCode = 0, message = "";
397 httpCode = transport.getStatus();
401 message = SyjStrings.notReachedError;
405 message = SyjStrings.requestError;
406 if (transport.responseJSON) {
407 switch (transport.responseJSON.message) {
409 message = SyjStrings.uniquePathError;
418 SYJLogin.messenger.setMessage(SyjStrings.loginNeeded, "warn");
419 SYJLogin.modalbox.show();
420 this.needsFormResubmit = true;
423 message = SyjStrings.gonePathError;
426 message = SyjStrings.serverError;
427 this.needsFormResubmit = true;
430 message = SyjStrings.unknownError;
435 // is some cases, we let the user resubmit, in some other cases, he
436 // needs to modify the path before submitting again
437 if (this.needsFormResubmit) {
441 this.messenger.setMessage(message, "error");
445 var SYJModalClass = Class.create({
449 this.area = $(this.type + '_area');
450 this.messenger = $(this.type + "_message");
451 this.modalbox = new SimpleBox(this.area, {
452 closeMethods: ["onescapekey", "onouterclick", "onbutton"]
455 $(this.type + "_control_anchor").observe("click", function(evt) {
456 this.modalbox.show();
458 }.bindAsEventListener(this));
460 document.observe('simplebox:shown', this.observer.bindAsEventListener(this));
461 document.observe('simplebox:hidden', this.observer.bindAsEventListener(this));
463 $(this.type + "form").ajaxize({
464 presubmit: this.presubmit.bind(this),
465 onSuccess: this.success.bind(this),
466 onFailure: this.failure.bind(this)
470 checkNotEmpty: function(input, message) {
471 if ($(input).value.strip().empty()) {
472 this.messenger.setMessage(message, "warn");
473 $(input).highlight('#F08080').activate();
479 observer: function(evt) {
480 var simplebox, input;
482 if (evt.eventName === "simplebox:shown" && evt.memo.element !== $("termsofusearea")) {
483 simplebox = evt.memo;
484 if (simplebox === this.modalbox) {
485 input = this.area.select('input[type="text"]')[0];
490 this.modalbox.hide();
493 } else if (evt.eventName === "simplebox:hidden" && evt.memo.element !== $("termsofusearea")) {
494 simplebox = evt.memo;
495 if (simplebox === this.modalbox) {
501 failure: function(transport) {
502 var httpCode = 0, message = SyjStrings.unknownError, input; // default message error
505 httpCode = transport.getStatus();
510 message = SyjStrings.notReachedError;
515 message = SyjStrings.requestError;
518 message = SyjStrings.serverError;
522 this.messenger.setMessage(message, "error");
523 input = this.area.select('input[type="text"]')[0];
524 input.highlight('#F08080').activate();
528 this.messenger.hide();
529 this.area.select('.message').invoke('setMessageStatus', null);
533 var SYJUserClass = Class.create(SYJModalClass, {
537 init: function($super) {
539 $("termsofusearea").hide();
541 $$("#user_termsofuse_anchor, #geom_termsofuse_anchor").invoke('observe', "click", function(evt) {
543 $("termsofusearea").show();
544 $("termsofuseiframe").setAttribute("src", evt.target.href);
545 this.toubox = new SimpleBox($("termsofusearea"), {
546 closeMethods: ["onescapekey", "onouterclick", "onbutton"]
551 }.bindAsEventListener(this));
553 $$("#login_area_create > a").invoke('observe', 'click',
555 this.modalbox.show();
557 }.bindAsEventListener(this));
559 $("user_pseudo-desc").hide();
560 $("user_pseudo").observe('contentchange', function(evt) {
561 var value = evt.target.value;
562 PseudoChecker.reset();
563 if (value && !(value.match(/^[a-zA-Z0-9_.]+$/))) {
564 $("user_pseudo-desc").show().setMessageStatus("warn");
566 $("user_pseudo-desc").hide();
568 }).timedobserve(function() {
569 PseudoChecker.check();
572 $("user_password").observe('contentchange', function(evt) {
573 if (evt.target.value.length < 6) {
574 $("user_password-desc").setMessageStatus("warn");
576 $("user_password-desc").setMessageStatus("success");
578 }.bindAsEventListener(this));
580 $("account-info").hide();
581 $("account-info-bullet").observe('click', function(evt) {
582 var elt = $("account-info");
584 evt.target.src = "icons/bullet_arrow_right.png";
587 evt.target.src = "icons/bullet_arrow_down.png";
594 presubmit: function() {
595 this.messenger.hide();
596 PseudoChecker.reset();
597 if (!(this.checkNotEmpty("user_pseudo", SyjStrings.userEmptyWarn))) {
601 if (!($("user_pseudo").value.match(/^[a-zA-Z0-9_.]+$/))) {
602 $("user_pseudo-desc").show().setMessageStatus("warn");
603 $("user_pseudo").highlight('#F08080').activate();
607 if (PseudoChecker.exists[$("user_pseudo").value]) {
608 PseudoChecker.availableMessage(false);
609 $("user_pseudo").highlight('#F08080').activate();
613 if (!(this.checkNotEmpty("user_password", SyjStrings.passwordEmptyWarn))) {
617 if ($("user_password").value.length < 6) {
618 $("user_password-desc").setMessageStatus("warn");
619 $("user_password").highlight('#F08080').activate();
623 if ($("user_password").value !== $("user_password_confirm").value) {
624 this.messenger.setMessage(SyjStrings.passwordNoMatchWarn, "warn");
625 $("user_password").highlight('#F08080').activate();
629 if (!(this.checkNotEmpty("user_email", SyjStrings.emailEmptyWarn))) {
633 if (!$("user_accept").checked) {
634 this.messenger.setMessage(SyjStrings.acceptTermsofuseWarn, "warn");
635 $("user_accept_container").highlight('#F08080');
636 $("user_accept").activate();
644 success: function(transport) {
646 SYJView.messenger.setMessage(SyjStrings.userSuccess, "success");
647 this.modalbox.hide();
648 if (SYJView.needsFormResubmit) {
649 SYJView.messenger.addMessage(SyjStrings.canResubmit);
650 $("geom_submit").activate();
654 failure: function($super, transport) {
655 var httpCode = 0, focusInput = null, message = "";
658 httpCode = transport.getStatus();
666 if (transport.responseJSON) {
667 switch (transport.responseJSON.message) {
669 message = SyjStrings.emailInvalidWarn;
670 focusInput = $("user_email");
673 PseudoChecker.availableMessage(false);
674 focusInput = $("user_pseudo");
677 message = SyjStrings.uniqueEmailError;
678 focusInput = $("user_email");
687 this.messenger.setMessage(message, "error");
689 focusInput.highlight('#F08080').activate();
696 var SYJUser = new SYJUserClass();
698 var SYJLoginClass = Class.create(SYJModalClass, {
701 init: function($super) {
705 presubmit: function() {
706 this.messenger.hide();
707 if (!(this.checkNotEmpty("login_user", SyjStrings.userEmptyWarn))) {
715 success: function(transport) {
716 if (transport.responseText === "1") {
717 LoginMgr.login(true);
721 SYJView.messenger.setMessage(SyjStrings.loginSuccess, "success");
722 this.modalbox.hide();
723 if (SYJView.needsFormResubmit) {
724 SYJView.messenger.addMessage(SyjStrings.canResubmit);
725 $("geom_submit").activate();
729 failure: function($super, transport) {
730 var httpCode = 0, focusInput = null, message = "";
733 httpCode = transport.getStatus();
741 message = SyjStrings.loginFailure;
742 focusInput = $("login_user");
747 this.messenger.setMessage(message, "error");
749 focusInput.highlight('#F08080').activate();
757 var SYJLogin = new SYJLoginClass();
759 var SYJNewpwdClass = Class.create(SYJModalClass, {
762 presubmit: function() {
763 if (!(this.checkNotEmpty("newpwd_email", SyjStrings.emailEmptyWarn))) {
769 success: function(transport) {
770 SYJView.messenger.setMessage(SyjStrings.newpwdSuccess, "success");
771 this.modalbox.hide();
775 var SYJNewpwd = new SYJNewpwdClass();
777 var LoginMgr = Object.extend(gLoggedInfo, {
780 updateUI: function() {
781 if (!this.controlsdeck) {
782 this.controlsdeck = new Deck("login_controls");
785 this.controlsdeck.setIndex(1);
786 $$(".logged-hide").invoke('hide');
787 $$(".logged-show").invoke('show');
789 this.controlsdeck.setIndex(0);
790 $$(".logged-hide").invoke('show');
791 $$(".logged-show").invoke('hide');
794 if (this.iscreator) {
795 $("data_controls").show();
797 $("data_controls").hide();
801 login: function(aIsCreator) {
802 if (typeof aIsCreator === "boolean") {
803 this.iscreator = aIsCreator;
810 var PseudoChecker = {
817 message: function(str, status, throbber) {
819 if (!this.messageelt) {
820 row = new Element('tr');
821 // we can't use row.update('<td></td><td><div></div></td>')
822 // because gecko would mangle the <td>s
823 row.insert(new Element('td'))
824 .insert((new Element('td')).update(new Element('div')));
826 $("user_pseudo").up('tr').insert({after: row});
827 this.messageelt = new Element('span');
828 this.throbber = new Element("img", { src: "icons/pseudo-throbber.gif"});
829 row.down('div').insert(this.throbber).insert(this.messageelt);
832 this.throbber.show();
834 this.throbber.hide();
836 this.messageelt.up().setStyle({visibility: ''});
837 this.messageelt.className = status;
838 this.messageelt.update(str);
841 availableMessage: function(available) {
842 var message = available ? SyjStrings.availablePseudo: SyjStrings.unavailablePseudo,
843 status = available ? "success": "warn";
844 this.message(message, status, false);
850 this.req = this.currentvalue = null;
852 if (this.messageelt) {
853 this.messageelt.up().setStyle({visibility: 'hidden'});
858 var pseudo = $("user_pseudo").value;
862 if (!pseudo || !(pseudo.match(/^[a-zA-Z0-9_.]+$/))) {
866 if (typeof this.exists[pseudo] === "boolean") {
868 this.availableMessage(!this.exists[pseudo]);
872 this.message(SyjStrings.pseudoChecking, "", true);
874 this.currentvalue = pseudo;
875 this.req = new Ajax.TimedRequest('userexists/' + encodeURIComponent(pseudo), 20, {
876 onFailure: this.failure.bind(this),
877 onSuccess: this.success.bind(this)
881 failure: function(transport) {
882 var httpCode = 0, value = this.currentvalue;
885 httpCode = transport.getStatus();
888 if (httpCode === 404) {
889 this.exists[value] = false;
890 this.availableMessage(true);
895 success: function(transport) {
896 var httpCode = transport.getStatus(), value = this.currentvalue;
898 this.exists[value] = true;
899 this.availableMessage(false);
904 document.observe("dom:loaded", function() {