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; // default message
423 if (transport.responseJSON) {
424 switch (transport.responseJSON.message) {
426 message = SyjStrings.uniquePathError;
434 message = SyjStrings.gonePathError;
437 message = SyjStrings.serverError;
438 this.needsFormResubmit = true;
441 message = SyjStrings.unknownError;
446 // is some cases, we let the user resubmit, in some other cases, he
447 // needs to modify the path before submitting again
448 if (this.needsFormResubmit) {
452 this.messenger.setMessage(message, "error");
456 var SYJModalClass = Class.create({
460 this.area = $(this.type + '_area');
461 this.messenger = $(this.type + "_message");
462 this.modalbox = new SimpleBox(this.area, {
463 closeMethods: ["onescapekey", "onouterclick", "onbutton"]
466 $(this.type + "_control_anchor").observe("click", function(evt) {
467 this.modalbox.show();
469 }.bindAsEventListener(this));
471 document.observe('simplebox:shown', this.observer.bindAsEventListener(this));
472 document.observe('simplebox:hidden', this.observer.bindAsEventListener(this));
474 $(this.type + "form").ajaxize({
475 presubmit: this.presubmit.bind(this),
476 onSuccess: this.success.bind(this),
477 onFailure: this.failure.bind(this)
481 checkNotEmpty: function(input, message) {
482 if ($(input).value.strip().empty()) {
483 this.messenger.setMessage(message, "warn");
484 $(input).highlight('#F08080').activate();
490 observer: function(evt) {
491 var simplebox, input;
493 if (evt.eventName === "simplebox:shown" && evt.memo.element !== $("termsofusearea")) {
494 simplebox = evt.memo;
495 if (simplebox === this.modalbox) {
496 input = this.area.select('input[type="text"]')[0];
501 this.modalbox.hide();
504 } else if (evt.eventName === "simplebox:hidden" && evt.memo.element !== $("termsofusearea")) {
505 simplebox = evt.memo;
506 if (simplebox === this.modalbox) {
512 failure: function(transport) {
513 var httpCode = 0, message = SyjStrings.unknownError, input; // default message error
516 httpCode = transport.getStatus();
521 message = SyjStrings.notReachedError;
526 message = SyjStrings.requestError;
529 message = SyjStrings.serverError;
533 this.messenger.setMessage(message, "error");
534 input = this.area.select('input[type="text"]')[0];
535 input.highlight('#F08080').activate();
539 this.messenger.hide();
540 this.area.select('.message').invoke('setMessageStatus', null);
544 var SYJUserClass = Class.create(SYJModalClass, {
548 init: function($super) {
550 $("termsofusearea").hide();
552 $$("#user_termsofuse_anchor, #geom_termsofuse_anchor").invoke('observe', "click", function(evt) {
554 $("termsofusearea").show();
555 $("termsofuseiframe").setAttribute("src", evt.target.href);
556 this.toubox = new SimpleBox($("termsofusearea"), {
557 closeMethods: ["onescapekey", "onouterclick", "onbutton"]
562 }.bindAsEventListener(this));
564 $$("#login_area_create > a").invoke('observe', 'click',
566 this.modalbox.show();
568 }.bindAsEventListener(this));
570 $("user_password").observe('contentchange', function(evt) {
571 if (evt.target.value.length < 6) {
572 $("user_password-desc").setMessageStatus("warn");
574 $("user_password-desc").setMessageStatus("success");
576 }.bindAsEventListener(this));
578 $("account-info").hide();
579 $("account-info-bullet").observe('click', function(evt) {
580 var elt = $("account-info");
582 evt.target.src = "icons/bullet_arrow_right.png";
585 evt.target.src = "icons/bullet_arrow_down.png";
592 presubmit: function() {
593 if (!(this.checkNotEmpty("user_pseudo", SyjStrings.userEmptyWarn))) {
597 if (!($("user_pseudo").value.match(/^[a-zA-Z0-9_.]+$/))) {
598 this.messenger.setMessage(SyjStrings.invalidPseudo, "warn");
599 $("user_pseudo").highlight('#F08080').activate();
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').activate();
613 if ($("user_password").value !== $("user_password_confirm").value) {
614 this.messenger.setMessage(SyjStrings.passwordNoMatchWarn, "warn");
615 $("user_password").highlight('#F08080').activate();
619 if (!(this.checkNotEmpty("user_email", SyjStrings.emailEmptyWarn))) {
623 if (!$("user_accept").checked) {
624 this.messenger.setMessage(SyjStrings.acceptTermsofuseWarn, "warn");
625 $("user_accept_container").highlight('#F08080');
626 $("user_accept").activate();
634 success: function(transport) {
636 SYJView.messenger.setMessage(SyjStrings.userSuccess, "success");
637 this.modalbox.hide();
638 if (SYJView.needsFormResubmit) {
639 SYJView.messenger.addMessage(SyjStrings.canResubmit);
640 $("geom_submit").activate();
644 failure: function($super, transport) {
645 var httpCode = 0, focusInput = null, message = "";
648 httpCode = transport.getStatus();
656 if (transport.responseJSON) {
657 switch (transport.responseJSON.message) {
659 message = SyjStrings.emailInvalidWarn;
660 focusInput = $("user_email");
663 message = SyjStrings.uniqueUserError;
664 focusInput = $("user_pseudo");
667 message = SyjStrings.uniqueEmailError;
668 focusInput = $("user_email");
676 this.messenger.setMessage(message, "error");
678 focusInput.highlight('#F08080').activate();
686 var SYJUser = new SYJUserClass();
688 var SYJLoginClass = Class.create(SYJModalClass, {
691 init: function($super) {
695 presubmit: function() {
696 if (!(this.checkNotEmpty("login_user", SyjStrings.userEmptyWarn))) {
704 success: function(transport) {
705 if (transport.responseText === "1") {
706 loginMgr.login(true);
710 SYJView.messenger.setMessage(SyjStrings.loginSuccess, "success");
711 this.modalbox.hide();
712 if (SYJView.needsFormResubmit) {
713 SYJView.messenger.addMessage(SyjStrings.canResubmit);
714 $("geom_submit").activate();
718 failure: function($super, transport) {
719 var httpCode = 0, focusInput = null, message = "";
722 httpCode = transport.getStatus();
730 message = SyjStrings.loginFailure;
731 focusInput = $("login_user");
736 this.messenger.setMessage(message, "error");
738 focusInput.highlight('#F08080').activate();
746 var SYJLogin = new SYJLoginClass();
748 var SYJNewpwdClass = Class.create(SYJModalClass, {
751 presubmit: function() {
752 if (!(this.checkNotEmpty("newpwd_email", SyjStrings.emailEmptyWarn))) {
758 success: function(transport) {
759 SYJView.messenger.setMessage(SyjStrings.newpwdSuccess, "success");
760 this.modalbox.hide();
764 var SYJNewpwd = new SYJNewpwdClass();
766 var loginMgr = Object.extend(gLoggedInfo, {
769 updateUI: function() {
770 if (!this.controlsdeck) {
771 this.controlsdeck = new Deck("login_controls");
774 this.controlsdeck.setIndex(1);
775 $$(".logged-hide").invoke('hide');
776 $$(".logged-show").invoke('show');
778 this.controlsdeck.setIndex(0);
779 $$(".logged-hide").invoke('show');
780 $$(".logged-show").invoke('hide');
783 if (this.iscreator) {
784 $("data_controls").show();
786 $("data_controls").hide();
790 login: function(aIsCreator) {
791 if (typeof aIsCreator === "boolean") {
792 this.iscreator = aIsCreator;
799 document.observe("dom:loaded", function() {