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 proceed(element, 'paste', handler);
13 return proceed(element, 'change', handler);
15 return proceed(element, eventName, handler);
19 // avoid openlayers alerts
20 OpenLayers.Console.userError = function(error) {
21 SYJView.messenger.setMessage(error, "error");
28 $("geom_title").observe('contentchange', this.enableSubmit.bindAsEventListener(this));
33 $("geom_submit").blur();
34 $("geom_title").blur();
45 if (this.status === "enabled") {
49 $("geom_title").disabled = false;
50 $("geom_title").activate();
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";
95 OpenLayers.Handler.SyjModifiablePath = OpenLayers.Class(OpenLayers.Handler.ModifiablePath, {
96 mouseup: function(evt) {
97 // do not add a point when navigating
98 var mapControls = this.control.map.controls, idx, ctrl;
100 for (idx = mapControls.length; idx-->0; ) {
101 ctrl = mapControls[idx];
102 if (this.isCtrlNavigationActive(ctrl, evt)) {
106 return OpenLayers.Handler.ModifiablePath.prototype.mouseup.apply(this, arguments);
109 addPoints: function(pixel) {
110 // redraw before last point. As we have a special style for last point, we
111 // need to redraw before last point after adding a new point (otherwise, it
112 // keeps special style forever)
113 var oldpoint = this.point;
114 OpenLayers.Handler.ModifiablePath.prototype.addPoints.apply(this, arguments);
115 this.layer.drawFeature(oldpoint);
118 isCtrlNavigationActive: function(ctrl, evt) {
119 var tolerance = 4, xDiff, yDiff;
121 if (!(ctrl instanceof OpenLayers.Control.Navigation)) {
126 ctrl.zoomBox.active &&
127 ctrl.zoomBox.handler &&
128 ctrl.zoomBox.handler.active &&
129 ctrl.zoomBox.handler.dragHandler &&
130 ctrl.zoomBox.handler.dragHandler.start) {
135 !ctrl.dragPan.active ||
136 !ctrl.dragPan.handler ||
137 !ctrl.dragPan.handler.started ||
138 !ctrl.dragPan.handler.start) {
142 // if mouse moved 4 or less pixels, consider it has not moved.
145 xDiff = evt.xy.x - ctrl.dragPan.handler.start.x;
146 yDiff = evt.xy.y - ctrl.dragPan.handler.start.y;
148 if (Math.sqrt(Math.pow(xDiff,2) + Math.pow(yDiff,2)) <= tolerance) {
154 finalize: function(cancel) {
155 // do nothing. We don't want to finalize path
160 edit: new OpenLayers.StyleMap({
161 "default": new OpenLayers.Style({
162 pointRadius: "${radius}", // sized according to type attribute
163 fillColor: "#ffcc66",
164 strokeColor: "#ff9933",
166 strokeOpacity: "${opacity}",
167 fillOpacity: "${opacity}"
171 radius: function(feature) {
174 if (!(feature.geometry instanceof OpenLayers.Geometry.Point)) {
177 if (feature.type === "middle") {
180 features = feature.layer.features;
181 if (OpenLayers.Util.indexOf(features, feature) === 0) {
183 } else if (OpenLayers.Util.indexOf(features, feature) === features.length - 1) {
188 opacity: function (feature) {
189 if (feature.type === "middle") {
198 "select": new OpenLayers.Style({
199 externalGraphic: "icons/delete.png",
203 "select_for_canvas": new OpenLayers.Style({
205 fillColor: "lightblue"
210 view: new OpenLayers.StyleMap({
211 "default": new OpenLayers.Style({
219 var WGS84 = new OpenLayers.Projection("EPSG:4326");
220 var Mercator = new OpenLayers.Projection("EPSG:900913");
226 wkt: new OpenLayers.Format.WKT({ internalProjection: Mercator, externalProjection: WGS84 }),
227 needsFormResubmit: false,
228 hasInitialGeom: false,
231 var externalGraphic, baseURL, baseLayer, layerOptions, extent, hidemessenger;
233 // is svg context, opera does not resolve links with base element is svg context
234 externalGraphic = styleMap.edit.styles.select.defaultStyle.externalGraphic;
235 baseURL = document.getElementsByTagName("base")[0].href;
236 styleMap.edit.styles.select.defaultStyle.externalGraphic = baseURL + externalGraphic;
238 this.map = new OpenLayers.Map('map', {
240 new OpenLayers.Control.Navigation(),
241 new OpenLayers.Control.PanZoom(),
242 new OpenLayers.Control.Attribution()
247 baseLayer = new OpenLayers.Layer.OSM("OSM", null, { wrapDateLine: true , attribution: SyjStrings.osmAttribution });
249 layerOptions = {format: OpenLayers.Format.WKT,
251 styleMap: styleMap.view};
252 if (gLoggedInfo.creatorname) {
253 layerOptions.attribution = SyjStrings.routeBy + ' ' + '<strong>' + gLoggedInfo.creatorname + '</strong>';
256 this.viewLayer = new OpenLayers.Layer.Vector("View Layer", layerOptions);
257 this.map.addLayers([baseLayer, this.viewLayer]);
259 $("edit-btn").observe('click', (function() {
260 this.messenger.hide();
264 $("geomform").ajaxize({
265 presubmit: this.prepareForm.bind(this),
266 onSuccess: this.saveSuccess.bind(this),
267 onFailure: this.saveFailure.bind(this)
269 SyjSaveUI.init().hide();
271 this.messenger = $('message');
272 hidemessenger = this.messenger.empty();
273 new CloseBtn(this.messenger, {
279 this.messenger.hide();
282 if ($("geom_data").value) {
283 this.viewLayer.addFeatures([this.wkt.read($("geom_data").value)]);
284 extent = this.viewLayer.getDataExtent();
285 // XXX: ie has not guessed height of map main div yet during map
286 // initialisation. Now, it will read it correctly.
287 this.map.updateSize();
288 this.hasInitialGeom = true;
290 extent = new OpenLayers.Bounds(gMaxExtent.minlon, gMaxExtent.minlat, gMaxExtent.maxlon, gMaxExtent.maxlat)
291 .transform(WGS84, Mercator);
293 this.map.zoomToExtent(extent);
294 document.observe('simplebox:shown', this.observer.bindAsEventListener(this));
297 observer: function(evt) {
298 if (evt.eventName === "simplebox:shown" && evt.memo.element !== $("termsofusearea")) {
299 this.messenger.hide();
303 prepareForm: function(form) {
304 if (!loginMgr.logged && !$("geom_accept").checked) {
305 this.messenger.setMessage(SyjStrings.acceptTermsofuseWarn, "warn");
306 $("geom_accept_container").highlight('#F08080');
307 $("geom_accept").activate();
311 var line, realPoints, idx, handler;
313 line = new OpenLayers.Geometry.LineString();
314 realPoints = this.editControl.handler.realPoints;
315 for (idx = 0; idx < realPoints.length; idx++) {
316 line.addComponent(realPoints[idx].geometry.clone());
318 this.viewLayer.addFeatures(new OpenLayers.Feature.Vector(line));
319 handler = this.editControl.handler;
320 OpenLayers.Handler.ModifiablePath.prototype.finalize.apply(handler, arguments);
321 // we need to recreate them on next createFeature; otherwise
322 // they'll reference destroyed features
323 delete(handler.handlers.drag);
324 delete(handler.handlers.feature);
325 this.editControl.deactivate();
327 $("geom_data").value = this.wkt.write(new OpenLayers.Feature.Vector(line));
328 this.needsFormResubmit = false;
329 SyjSaveUI.disable.bind(SyjSaveUI).defer();
330 this.messenger.hide();
334 editMode: function() {
335 var components, point0, point, pixels, pixel, idx;
337 this.initEditControl();
339 this.editControl.activate();
340 if (this.viewLayer.features.length) {
341 components = this.viewLayer.features[0].geometry.components;
342 point0 = components[0];
344 pixel = this.map.getPixelFromLonLat(new OpenLayers.LonLat(point0.x, point0.y));
345 this.editControl.handler.createFeature(pixel);
346 this.editControl.handler.lastUp = pixel;
348 for (idx = 1; idx < components.length; idx++) {
349 point = components[idx];
350 pixels.push(this.map.getPixelFromLonLat(new OpenLayers.LonLat(point.x, point.y)));
352 this.editControl.handler.addPoints(pixels);
356 this.viewLayer.destroyFeatures();
359 if (this.editControl.handler.realPoints && this.editControl.handler.realPoints.length >= 2) {
360 SyjSaveUI.show().disableSubmit();
362 SyjSaveUI.show().disable();
366 initEditControl: function() {
369 if (this.editControl) {
373 this.editControl = new OpenLayers.Control.DrawFeature(new OpenLayers.Layer.Vector(), OpenLayers.Handler.SyjModifiablePath, {
375 modify: function(f, line) {
376 if (this.handler.realPoints.length < 2) {
377 SyjSaveUI.show().disable();
379 SyjSaveUI.show().enable();
386 styleMap: styleMap.edit
390 this.map.addControl(this.editControl);
391 if (this.editControl.layer.renderer instanceof OpenLayers.Renderer.Canvas) {
392 // using externalGraphic with canvas renderer is definitively too buggy
393 styles = this.editControl.handler.layerOptions.styleMap.styles;
394 styles.select = styles.select_for_canvas;
398 saveSuccess: function(transport) {
399 if (!this.hasInitialGeom) { // we have created a new path, change location
400 location = "idx/" + transport.responseText;
404 this.messenger.setMessage(SyjStrings.saveSuccess, "success");
407 document.title = $('geom_title').value;
410 saveFailure: function(transport) {
411 var httpCode = 0, message = "";
414 httpCode = transport.getStatus();
418 message = SyjStrings.notReachedError;
422 message = SyjStrings.requestError;
423 if (transport.responseJSON) {
424 switch (transport.responseJSON.message) {
426 message = SyjStrings.uniquePathError;
435 SYJLogin.messenger.setMessage(SyjStrings.loginNeeded, "warn");
436 SYJLogin.modalbox.show();
437 this.needsFormResubmit = true;
440 message = SyjStrings.gonePathError;
443 message = SyjStrings.serverError;
444 this.needsFormResubmit = true;
447 message = SyjStrings.unknownError;
452 // is some cases, we let the user resubmit, in some other cases, he
453 // needs to modify the path before submitting again
454 if (this.needsFormResubmit) {
458 this.messenger.setMessage(message, "error");
462 var SYJModalClass = Class.create({
466 this.area = $(this.type + '_area');
467 this.messenger = $(this.type + "_message");
468 this.modalbox = new SimpleBox(this.area, {
469 closeMethods: ["onescapekey", "onouterclick", "onbutton"]
472 $(this.type + "_control_anchor").observe("click", function(evt) {
473 this.modalbox.show();
475 }.bindAsEventListener(this));
477 document.observe('simplebox:shown', this.observer.bindAsEventListener(this));
478 document.observe('simplebox:hidden', this.observer.bindAsEventListener(this));
480 $(this.type + "form").ajaxize({
481 presubmit: this.presubmit.bind(this),
482 onSuccess: this.success.bind(this),
483 onFailure: this.failure.bind(this)
487 checkNotEmpty: function(input, message) {
488 if ($(input).value.strip().empty()) {
489 this.messenger.setMessage(message, "warn");
490 $(input).highlight('#F08080').activate();
496 observer: function(evt) {
497 var simplebox, input;
499 if (evt.eventName === "simplebox:shown" && evt.memo.element !== $("termsofusearea")) {
500 simplebox = evt.memo;
501 if (simplebox === this.modalbox) {
502 input = this.area.select('input[type="text"]')[0];
507 this.modalbox.hide();
510 } else if (evt.eventName === "simplebox:hidden" && evt.memo.element !== $("termsofusearea")) {
511 simplebox = evt.memo;
512 if (simplebox === this.modalbox) {
518 failure: function(transport) {
519 var httpCode = 0, message = SyjStrings.unknownError, input; // default message error
522 httpCode = transport.getStatus();
527 message = SyjStrings.notReachedError;
532 message = SyjStrings.requestError;
535 message = SyjStrings.serverError;
539 this.messenger.setMessage(message, "error");
540 input = this.area.select('input[type="text"]')[0];
541 input.highlight('#F08080').activate();
545 this.messenger.hide();
546 this.area.select('.message').invoke('setMessageStatus', null);
550 var SYJUserClass = Class.create(SYJModalClass, {
554 init: function($super) {
556 $("termsofusearea").hide();
558 $$("#user_termsofuse_anchor, #geom_termsofuse_anchor").invoke('observe', "click", function(evt) {
560 $("termsofusearea").show();
561 $("termsofuseiframe").setAttribute("src", evt.target.href);
562 this.toubox = new SimpleBox($("termsofusearea"), {
563 closeMethods: ["onescapekey", "onouterclick", "onbutton"]
568 }.bindAsEventListener(this));
570 $$("#login_area_create > a").invoke('observe', 'click',
572 this.modalbox.show();
574 }.bindAsEventListener(this));
576 $("user_password").observe('contentchange', function(evt) {
577 if (evt.target.value.length < 6) {
578 $("user_password-desc").setMessageStatus("warn");
580 $("user_password-desc").setMessageStatus("success");
582 }.bindAsEventListener(this));
584 $("account-info").hide();
585 $("account-info-bullet").observe('click', function(evt) {
586 var elt = $("account-info");
588 evt.target.src = "icons/bullet_arrow_right.png";
591 evt.target.src = "icons/bullet_arrow_down.png";
598 presubmit: function() {
599 if (!(this.checkNotEmpty("user_pseudo", SyjStrings.userEmptyWarn))) {
603 if (!($("user_pseudo").value.match(/^[a-zA-Z0-9_.]+$/))) {
604 this.messenger.setMessage(SyjStrings.invalidPseudo, "warn");
605 $("user_pseudo").highlight('#F08080').activate();
609 if (!(this.checkNotEmpty("user_password", SyjStrings.passwordEmptyWarn))) {
613 if ($("user_password").value.length < 6) {
614 $("user_password-desc").setMessageStatus("warn");
615 $("user_password").highlight('#F08080').activate();
619 if ($("user_password").value !== $("user_password_confirm").value) {
620 this.messenger.setMessage(SyjStrings.passwordNoMatchWarn, "warn");
621 $("user_password").highlight('#F08080').activate();
625 if (!(this.checkNotEmpty("user_email", SyjStrings.emailEmptyWarn))) {
629 if (!$("user_accept").checked) {
630 this.messenger.setMessage(SyjStrings.acceptTermsofuseWarn, "warn");
631 $("user_accept_container").highlight('#F08080');
632 $("user_accept").activate();
640 success: function(transport) {
642 SYJView.messenger.setMessage(SyjStrings.userSuccess, "success");
643 this.modalbox.hide();
644 if (SYJView.needsFormResubmit) {
645 SYJView.messenger.addMessage(SyjStrings.canResubmit);
646 $("geom_submit").activate();
650 failure: function($super, transport) {
651 var httpCode = 0, focusInput = null, message = "";
654 httpCode = transport.getStatus();
662 if (transport.responseJSON) {
663 switch (transport.responseJSON.message) {
665 message = SyjStrings.emailInvalidWarn;
666 focusInput = $("user_email");
669 message = SyjStrings.uniqueUserError;
670 focusInput = $("user_pseudo");
673 message = SyjStrings.uniqueEmailError;
674 focusInput = $("user_email");
682 this.messenger.setMessage(message, "error");
684 focusInput.highlight('#F08080').activate();
692 var SYJUser = new SYJUserClass();
694 var SYJLoginClass = Class.create(SYJModalClass, {
697 init: function($super) {
701 presubmit: function() {
702 if (!(this.checkNotEmpty("login_user", SyjStrings.userEmptyWarn))) {
710 success: function(transport) {
711 if (transport.responseText === "1") {
712 loginMgr.login(true);
716 SYJView.messenger.setMessage(SyjStrings.loginSuccess, "success");
717 this.modalbox.hide();
718 if (SYJView.needsFormResubmit) {
719 SYJView.messenger.addMessage(SyjStrings.canResubmit);
720 $("geom_submit").activate();
724 failure: function($super, transport) {
725 var httpCode = 0, focusInput = null, message = "";
728 httpCode = transport.getStatus();
736 message = SyjStrings.loginFailure;
737 focusInput = $("login_user");
742 this.messenger.setMessage(message, "error");
744 focusInput.highlight('#F08080').activate();
752 var SYJLogin = new SYJLoginClass();
754 var SYJNewpwdClass = Class.create(SYJModalClass, {
757 presubmit: function() {
758 if (!(this.checkNotEmpty("newpwd_email", SyjStrings.emailEmptyWarn))) {
764 success: function(transport) {
765 SYJView.messenger.setMessage(SyjStrings.newpwdSuccess, "success");
766 this.modalbox.hide();
770 var SYJNewpwd = new SYJNewpwdClass();
772 var loginMgr = Object.extend(gLoggedInfo, {
775 updateUI: function() {
776 if (!this.controlsdeck) {
777 this.controlsdeck = new Deck("login_controls");
780 this.controlsdeck.setIndex(1);
781 $$(".logged-hide").invoke('hide');
782 $$(".logged-show").invoke('show');
784 this.controlsdeck.setIndex(0);
785 $$(".logged-hide").invoke('show');
786 $$(".logged-show").invoke('hide');
789 if (this.iscreator) {
790 $("data_controls").show();
792 $("data_controls").hide();
796 login: function(aIsCreator) {
797 if (typeof aIsCreator === "boolean") {
798 this.iscreator = aIsCreator;
805 document.observe("dom:loaded", function() {