1 /* This file is part of Syj, Copyright (c) 2010 Arnaud Renevier,
2 and is published under the AGPL license. */
3 Element.addMethods('input', {
4 observe : Element.Methods.observe.wrap(function(proceed, element, eventName, handler) {
5 if (eventName === "contentchange") {
6 proceed(element, 'keyup', function(evt) {
7 if (evt.keyCode == 13) {
10 handler.apply(null, arguments);
12 return proceed(element, 'change', handler);
14 return proceed(element, eventName, handler);
18 // avoid openlayers alerts
19 OpenLayers.Console.userError = function(error) {
20 SYJView.messenger.setMessage(error, "error");
27 $("geom_title").observe('contentchange', this.enableSubmit.bindAsEventListener(this));
32 $("geom_submit").blur();
33 $("geom_title").blur();
44 if (this.status === "enabled") {
48 $("geom_title").disabled = false;
49 $("geom_title").focus();
50 $("geom_title").select();
51 $("geomform").removeClassName("disabled");
52 this.status = "enabled";
57 if (this.status === "disabled") {
61 $("geom_title").blur();
62 $("geom_title").disabled = true;
63 $("geomform").addClassName("disabled");
64 this.status = "disabled";
68 enableSubmit: function() {
69 $("geom_submit").disabled = false;
70 this.status = "partial";
74 disableSubmit: function() {
75 $("geom_submit").blur();
76 $("geom_submit").disabled = true;
77 this.status = "partial";
99 OpenLayers.Handler.SyjModifiablePath = OpenLayers.Class(OpenLayers.Handler.ModifiablePath, {
100 mouseup: function(evt) {
101 // do not add a point when navigating
102 var mapControls = this.control.map.controls, idx, ctrl;
104 for (idx = mapControls.length; idx-->0; ) {
105 ctrl = mapControls[idx];
106 if (this.isCtrlNavigationActive(ctrl, evt)) {
110 return OpenLayers.Handler.ModifiablePath.prototype.mouseup.apply(this, arguments);
113 addPoints: function(pixel) {
114 // redraw before last point. As we have a special style for last point, we
115 // need to redraw before last point after adding a new point (otherwise, it
116 // keeps special style forever)
117 var oldpoint = this.point;
118 OpenLayers.Handler.ModifiablePath.prototype.addPoints.apply(this, arguments);
119 this.layer.drawFeature(oldpoint);
122 isCtrlNavigationActive: function(ctrl, evt) {
123 var tolerance = 4, xDiff, yDiff;
125 if (!(ctrl instanceof OpenLayers.Control.Navigation)) {
130 ctrl.zoomBox.active &&
131 ctrl.zoomBox.handler &&
132 ctrl.zoomBox.handler.active &&
133 ctrl.zoomBox.handler.dragHandler &&
134 ctrl.zoomBox.handler.dragHandler.start) {
139 !ctrl.dragPan.active ||
140 !ctrl.dragPan.handler ||
141 !ctrl.dragPan.handler.started ||
142 !ctrl.dragPan.handler.start) {
146 // if mouse moved 4 or less pixels, consider it has not moved.
149 xDiff = evt.xy.x - ctrl.dragPan.handler.start.x;
150 yDiff = evt.xy.y - ctrl.dragPan.handler.start.y;
152 if (Math.sqrt(Math.pow(xDiff,2) + Math.pow(yDiff,2)) <= tolerance) {
158 finalize: function(cancel) {
159 // do nothing. We don't want to finalize path
164 edit: new OpenLayers.StyleMap({
165 "default": new OpenLayers.Style({
166 pointRadius: "${radius}", // sized according to type attribute
167 fillColor: "#ffcc66",
168 strokeColor: "#ff9933",
170 strokeOpacity: "${opacity}",
171 fillOpacity: "${opacity}"
175 radius: function(feature) {
178 if (!(feature.geometry instanceof OpenLayers.Geometry.Point)) {
181 if (feature.type === "middle") {
184 features = feature.layer.features;
185 if (OpenLayers.Util.indexOf(features, feature) === 0) {
187 } else if (OpenLayers.Util.indexOf(features, feature) === features.length - 1) {
192 opacity: function (feature) {
193 if (feature.type === "middle") {
202 "select": new OpenLayers.Style({
203 externalGraphic: "icons/delete.png",
207 "select_for_canvas": new OpenLayers.Style({
209 fillColor: "lightblue"
214 view: new OpenLayers.StyleMap({
215 "default": new OpenLayers.Style({
223 var WGS84 = new OpenLayers.Projection("EPSG:4326");
224 var Mercator = new OpenLayers.Projection("EPSG:900913");
230 wkt: new OpenLayers.Format.WKT({ internalProjection: Mercator, externalProjection: WGS84 }),
231 needsFormResubmit: false,
234 var externalGraphic, baseURL, baseLayer, layerOptions, extent = null, hidemessenger;
236 // is svg context, opera does not resolve links with base element is svg context
237 externalGraphic = styleMap.edit.styles.select.defaultStyle.externalGraphic;
238 baseURL = document.getElementsByTagName("base")[0].href;
239 styleMap.edit.styles.select.defaultStyle.externalGraphic = baseURL + externalGraphic;
241 this.map = new OpenLayers.Map('map', {
243 new OpenLayers.Control.Navigation(),
244 new OpenLayers.Control.PanZoom(),
245 new OpenLayers.Control.Attribution()
250 baseLayer = new OpenLayers.Layer.OSM("OSM", null, { wrapDateLine: true , attribution: SyjStrings.osmAttribution });
252 layerOptions = {format: OpenLayers.Format.WKT,
254 styleMap: styleMap.view};
255 if (gLoggedInfo.ownername) {
256 layerOptions.attribution = SyjStrings.routeBy + ' ' + '<strong>' + gLoggedInfo.ownername + '</strong>';
259 this.viewLayer = new OpenLayers.Layer.Vector("View Layer", layerOptions);
260 this.map.addLayers([baseLayer, this.viewLayer]);
262 $("edit-btn").observe('click', (function() {
263 this.messenger.hide();
266 SyjEditUI.init().hide();
268 $("geomform").ajaxize({
269 presubmit: this.prepareForm.bind(this),
270 onSuccess: this.saveSuccess.bind(this),
271 onFailure: this.saveFailure.bind(this)
273 SyjSaveUI.init().hide();
275 this.messenger = $('message');
276 hidemessenger = this.messenger.empty();
277 new CloseBtn(this.messenger, {
283 this.messenger.hide();
287 if ($("geom_data").value) {
288 this.viewLayer.addFeatures([this.wkt.read($("geom_data").value)]);
289 extent = this.viewLayer.getDataExtent();
290 // XXX: ie has not guessed height of map main div yet during map
291 // initialisation. Now, it will read it correctly.
292 this.map.updateSize();
295 extent = new OpenLayers.Bounds(gMaxExtent.minlon, gMaxExtent.minlat, gMaxExtent.maxlon, gMaxExtent.maxlat)
296 .transform(WGS84, Mercator);
299 this.map.zoomToExtent(extent);
300 document.observe('simplebox:shown', this.observer.bindAsEventListener(this));
303 observer: function(evt) {
304 if (evt.eventName === "simplebox:shown" && evt.memo.element !== $("termsofusearea")) {
305 this.messenger.hide();
309 prepareForm: function(form) {
310 var line, realPoints, idx, handler;
312 line = new OpenLayers.Geometry.LineString();
313 realPoints = this.editControl.handler.realPoints;
314 for (idx = 0; idx < realPoints.length; idx++) {
315 line.addComponent(realPoints[idx].geometry.clone());
317 this.viewLayer.addFeatures(new OpenLayers.Feature.Vector(line));
318 handler = this.editControl.handler;
319 OpenLayers.Handler.ModifiablePath.prototype.finalize.apply(handler, arguments);
320 // we need to recreate them on next createFeature; otherwise
321 // they'll reference destroyed features
322 delete(handler.handlers.drag);
323 delete(handler.handlers.feature);
324 this.editControl.deactivate();
326 $("geom_data").value = this.wkt.write(new OpenLayers.Feature.Vector(line));
327 this.needsFormResubmit = false;
328 SyjSaveUI.disable.bind(SyjSaveUI).defer();
329 this.messenger.hide();
332 editMode: function() {
333 var components, point0, point, pixels, pixel, idx;
335 this.initEditControl();
337 this.editControl.activate();
338 if (this.viewLayer.features.length) {
339 components = this.viewLayer.features[0].geometry.components;
340 point0 = components[0];
342 pixel = this.map.getPixelFromLonLat(new OpenLayers.LonLat(point0.x, point0.y));
343 this.editControl.handler.createFeature(pixel);
344 this.editControl.handler.lastUp = pixel;
346 for (idx = 1; idx < components.length; idx++) {
347 point = components[idx];
348 pixels.push(this.map.getPixelFromLonLat(new OpenLayers.LonLat(point.x, point.y)));
350 this.editControl.handler.addPoints(pixels);
354 this.viewLayer.destroyFeatures();
357 if (this.editControl.handler.realPoints && this.editControl.handler.realPoints.length >= 2) {
358 SyjSaveUI.show().disableSubmit();
360 SyjSaveUI.show().disable();
364 initEditControl: function() {
367 if (this.editControl) {
371 this.editControl = new OpenLayers.Control.DrawFeature(new OpenLayers.Layer.Vector(), OpenLayers.Handler.SyjModifiablePath, {
373 modify: function(f, line) {
374 if (this.handler.realPoints.length < 2) {
375 SyjSaveUI.show().disable();
377 SyjSaveUI.show().enable();
384 styleMap: styleMap.edit
388 this.map.addControl(this.editControl);
389 if (this.editControl.layer.renderer instanceof OpenLayers.Renderer.Canvas) {
390 // using externalGraphic with canvas renderer is definitively too buggy
391 styles = this.editControl.handler.layerOptions.styleMap.styles;
392 styles.select = styles.select_for_canvas;
396 saveSuccess: function(transport) {
397 if (!$("geom_id").value) {
398 location = "idx/" + transport.responseText;
401 this.messenger.setMessage(SyjStrings.saveSuccess, "success");
405 document.title = $('geom_title').value;
408 saveFailure: function(transport) {
409 var httpCode = 0, message = "";
412 httpCode = transport.getStatus();
417 message = SyjStrings.notReachedError;
422 message = SyjStrings.requestError; // default message
423 if (transport.responseJSON) {
424 switch (transport.responseJSON.message) {
426 message = SyjStrings.unreferencedError;
429 message = SyjStrings.uniquePathError;
438 this.needsFormResubmit = true;
439 if (loginMgr.hasAlreadyConnected()) {
440 SYJLogin.messenger.setMessage(SyjStrings.cookiesNeeded, "warn");
442 SYJLogin.messenger.setMessage(SyjStrings.loginNeeded, "warn");
444 SYJLogin.modalbox.show();
447 message = SyjStrings.serverError;
448 this.needsFormResubmit = true;
451 message = SyjStrings.unknownError;
456 // is some cases, we let the user resubmit, in some other cases, he
457 // needs to modify the path before submitting again
458 if (this.needsFormResubmit) {
462 this.messenger.setMessage(message, "error");
466 var SYJModalClass = Class.create({
470 this.area = $(this.type + '_area');
471 this.messenger = $(this.type + "_message");
472 this.modalbox = new SimpleBox(this.area, {
473 closeMethods: ["onescapekey", "onouterclick", "onbutton"]
476 $(this.type + "_control_anchor").observe("click", function(evt) {
477 this.modalbox.show();
479 }.bindAsEventListener(this));
481 document.observe('simplebox:shown', this.observer.bindAsEventListener(this));
482 document.observe('simplebox:hidden', this.observer.bindAsEventListener(this));
484 $(this.type + "form").ajaxize({
485 presubmit: this.presubmit.bind(this),
486 onSuccess: this.success.bind(this),
487 onFailure: this.failure.bind(this)
491 checkNotEmpty: function(input, message) {
492 if ($(input).value.strip().empty()) {
493 this.messenger.setMessage(message, "warn");
494 $(input).highlight('#F08080').focus();
500 observer: function(evt) {
501 var simplebox, input;
503 if (evt.eventName === "simplebox:shown" && evt.memo.element !== $("termsofusearea")) {
504 simplebox = evt.memo;
505 if (simplebox === this.modalbox) {
506 input = this.area.select('input[type="text"]')[0];
512 this.modalbox.hide();
515 } else if (evt.eventName === "simplebox:hidden" && evt.memo.element !== $("termsofusearea")) {
516 simplebox = evt.memo;
517 if (simplebox === this.modalbox) {
523 failure: function(transport) {
524 var httpCode = 0, message = SyjStrings.unknownError, input; // default message error
527 httpCode = transport.getStatus();
532 message = SyjStrings.notReachedError;
537 message = SyjStrings.requestError;
540 message = SyjStrings.serverError;
544 this.messenger.setMessage(message, "error");
545 input = this.area.select('input[type="text"]')[0];
546 input.highlight('#F08080').focus();
551 this.messenger.hide();
552 this.area.select('.message').invoke('setMessageStatus', null);
556 var SYJUserClass = Class.create(SYJModalClass, {
560 init: function($super) {
562 $("termsofusearea").hide();
564 $("user_termsofuse_anchor").observe("click", function(evt) {
566 $("termsofusearea").show();
567 $("termsofuseiframe").setAttribute("src", evt.target.href);
568 this.toubox = new SimpleBox($("termsofusearea"), {
569 closeMethods: ["onescapekey", "onouterclick", "onbutton"]
574 }.bindAsEventListener(this));
576 $$("#login_area_create > a").invoke('observe', 'click',
578 this.modalbox.show();
580 }.bindAsEventListener(this));
582 $("user_password").observe('contentchange', function(evt) {
583 if (evt.target.value.length < 6) {
584 $("user_password-desc").setMessageStatus("warn");
586 $("user_password-desc").setMessageStatus("success");
588 }.bindAsEventListener(this));
591 presubmit: function() {
592 if (!(this.checkNotEmpty("user_pseudo", SyjStrings.userEmptyWarn))) {
596 if (!($("user_pseudo").value.match(/[a-zA-Z0-9_.]+$/))) {
597 this.messenger.setMessage(SyjStrings.invalidPseudo, "warn");
598 $("user_pseudo").highlight('#F08080').focus();
599 $("user_pseudo").select();
603 if (!(this.checkNotEmpty("user_password", SyjStrings.passwordEmptyWarn))) {
607 if ($("user_password").value.length < 6) {
608 $("user_password-desc").setMessageStatus("warn");
609 $("user_password").highlight('#F08080').focus();
610 $("user_password").select();
614 if ($("user_password").value !== $("user_password_confirm").value) {
615 this.messenger.setMessage(SyjStrings.passwordNoMatchWarn, "warn");
616 $("user_password").highlight('#F08080').focus();
617 $("user_password").select();
621 if (!(this.checkNotEmpty("user_email", SyjStrings.emailEmptyWarn))) {
625 if (!$("user_accept").checked) {
626 this.messenger.setMessage(SyjStrings.acceptTermsofuseWarn, "warn");
627 $("user_accept").highlight('#F08080').focus();
635 success: function(transport) {
637 SYJView.messenger.setMessage(SyjStrings.userSuccess, "success");
638 this.modalbox.hide();
639 if (SYJView.needsFormResubmit) {
640 SYJView.messenger.addMessage(SyjStrings.canResubmit);
641 $("geom_submit").focus();
645 failure: function($super, transport) {
646 var httpCode = 0, focusInput = null, message = "";
649 httpCode = transport.getStatus();
657 if (transport.responseJSON) {
658 switch (transport.responseJSON.message) {
660 message = SyjStrings.emailInvalidWarn;
661 focusInput = $("user_email");
664 message = SyjStrings.uniqueUserError;
665 focusInput = $("user_pseudo");
668 message = SyjStrings.uniqueEmailError;
669 focusInput = $("user_email");
677 this.messenger.setMessage(message, "error");
679 focusInput.highlight('#F08080').focus();
688 var SYJUser = new SYJUserClass();
690 var SYJLoginClass = Class.create(SYJModalClass, {
693 init: function($super) {
697 presubmit: function() {
698 if (!(this.checkNotEmpty("login_user", SyjStrings.userEmptyWarn))) {
706 success: function(transport) {
707 if (transport.responseText === "1") {
708 loginMgr.login(true);
712 SYJView.messenger.setMessage(SyjStrings.loginSuccess, "success");
713 this.modalbox.hide();
714 if (SYJView.needsFormResubmit) {
715 SYJView.messenger.addMessage(SyjStrings.canResubmit);
716 $("geom_submit").focus();
720 failure: function($super, transport) {
721 var httpCode = 0, focusInput = null, message = "";
724 httpCode = transport.getStatus();
732 message = SyjStrings.loginFailure;
733 focusInput = $("login_user");
738 this.messenger.setMessage(message, "error");
740 focusInput.highlight('#F08080').focus();
749 var SYJLogin = new SYJLoginClass();
751 var SYJNewpwdClass = Class.create(SYJModalClass, {
754 presubmit: function() {
755 if (!(this.checkNotEmpty("newpwd_email", SyjStrings.emailEmptyWarn))) {
761 success: function(transport) {
762 SYJView.messenger.setMessage(SyjStrings.newpwdSuccess, "success");
763 this.modalbox.hide();
767 var SYJNewpwd = new SYJNewpwdClass();
769 var loginMgr = Object.extend(gLoggedInfo, {
772 updateUI: function() {
773 if (!this.controlsdeck) {
774 this.controlsdeck = new Deck("login_controls");
777 this.controlsdeck.setIndex(1);
779 this.controlsdeck.setIndex(0);
783 $("data_controls").show();
785 $("data_controls").hide();
789 login: function(aIsOwner) {
790 if (typeof aIsOwner === "boolean") {
791 this.isowner = aIsOwner;
798 // needed in case of 403 errors: if user has already connected successfully
799 // before, it probably means he has cookies disabled
800 hasAlreadyConnected: function() {
801 return (this.connections >= 1);
805 document.observe("dom:loaded", function() {