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,
230 var externalGraphic, baseURL, baseLayer, layerOptions, extent, hidemessenger;
232 // is svg context, opera does not resolve links with base element is svg context
233 externalGraphic = styleMap.edit.styles.select.defaultStyle.externalGraphic;
234 baseURL = document.getElementsByTagName("base")[0].href;
235 styleMap.edit.styles.select.defaultStyle.externalGraphic = baseURL + externalGraphic;
237 this.map = new OpenLayers.Map('map', {
239 new OpenLayers.Control.Navigation(),
240 new OpenLayers.Control.PanZoom(),
241 new OpenLayers.Control.Attribution()
246 baseLayer = new OpenLayers.Layer.OSM("OSM", null, { wrapDateLine: true , attribution: SyjStrings.osmAttribution });
248 layerOptions = {format: OpenLayers.Format.WKT,
250 styleMap: styleMap.view};
251 if (gLoggedInfo.creatorname) {
252 layerOptions.attribution = SyjStrings.routeBy + ' ' + '<strong>' + gLoggedInfo.creatorname + '</strong>';
255 this.viewLayer = new OpenLayers.Layer.Vector("View Layer", layerOptions);
256 this.map.addLayers([baseLayer, this.viewLayer]);
258 $("edit-btn").observe('click', (function() {
259 this.messenger.hide();
263 $("geomform").ajaxize({
264 presubmit: this.prepareForm.bind(this),
265 onSuccess: this.saveSuccess.bind(this),
266 onFailure: this.saveFailure.bind(this)
268 SyjSaveUI.init().hide();
270 this.messenger = $('message');
271 hidemessenger = this.messenger.empty();
272 new CloseBtn(this.messenger, {
278 this.messenger.hide();
281 if ($("geom_data").value) {
282 this.viewLayer.addFeatures([this.wkt.read($("geom_data").value)]);
283 extent = this.viewLayer.getDataExtent();
284 // XXX: ie has not guessed height of map main div yet during map
285 // initialisation. Now, it will read it correctly.
286 this.map.updateSize();
288 extent = new OpenLayers.Bounds(gMaxExtent.minlon, gMaxExtent.minlat, gMaxExtent.maxlon, gMaxExtent.maxlat)
289 .transform(WGS84, Mercator);
291 this.map.zoomToExtent(extent);
292 document.observe('simplebox:shown', this.observer.bindAsEventListener(this));
295 observer: function(evt) {
296 if (evt.eventName === "simplebox:shown" && evt.memo.element !== $("termsofusearea")) {
297 this.messenger.hide();
301 prepareForm: function(form) {
302 if (!loginMgr.logged && !$("geom_accept").checked) {
303 this.messenger.setMessage(SyjStrings.acceptTermsofuseWarn, "warn");
304 $("geom_accept_container").highlight('#F08080');
305 $("geom_accept").activate();
309 var line, realPoints, idx, handler;
311 line = new OpenLayers.Geometry.LineString();
312 realPoints = this.editControl.handler.realPoints;
313 for (idx = 0; idx < realPoints.length; idx++) {
314 line.addComponent(realPoints[idx].geometry.clone());
316 this.viewLayer.addFeatures(new OpenLayers.Feature.Vector(line));
317 handler = this.editControl.handler;
318 OpenLayers.Handler.ModifiablePath.prototype.finalize.apply(handler, arguments);
319 // we need to recreate them on next createFeature; otherwise
320 // they'll reference destroyed features
321 delete(handler.handlers.drag);
322 delete(handler.handlers.feature);
323 this.editControl.deactivate();
325 $("geom_data").value = this.wkt.write(new OpenLayers.Feature.Vector(line));
326 this.needsFormResubmit = false;
327 SyjSaveUI.disable.bind(SyjSaveUI).defer();
328 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 (transport.responseJSON && (typeof transport.responseJSON.redirect === "string")) {
398 location = transport.responseJSON.redirect;
402 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();
416 message = SyjStrings.notReachedError;
420 message = SyjStrings.requestError;
421 if (transport.responseJSON) {
422 switch (transport.responseJSON.message) {
424 message = SyjStrings.uniquePathError;
433 SYJLogin.messenger.setMessage(SyjStrings.loginNeeded, "warn");
434 SYJLogin.modalbox.show();
435 this.needsFormResubmit = true;
438 message = SyjStrings.gonePathError;
441 message = SyjStrings.serverError;
442 this.needsFormResubmit = true;
445 message = SyjStrings.unknownError;
450 // is some cases, we let the user resubmit, in some other cases, he
451 // needs to modify the path before submitting again
452 if (this.needsFormResubmit) {
456 this.messenger.setMessage(message, "error");
460 var SYJModalClass = Class.create({
464 this.area = $(this.type + '_area');
465 this.messenger = $(this.type + "_message");
466 this.modalbox = new SimpleBox(this.area, {
467 closeMethods: ["onescapekey", "onouterclick", "onbutton"]
470 $(this.type + "_control_anchor").observe("click", function(evt) {
471 this.modalbox.show();
473 }.bindAsEventListener(this));
475 document.observe('simplebox:shown', this.observer.bindAsEventListener(this));
476 document.observe('simplebox:hidden', this.observer.bindAsEventListener(this));
478 $(this.type + "form").ajaxize({
479 presubmit: this.presubmit.bind(this),
480 onSuccess: this.success.bind(this),
481 onFailure: this.failure.bind(this)
485 checkNotEmpty: function(input, message) {
486 if ($(input).value.strip().empty()) {
487 this.messenger.setMessage(message, "warn");
488 $(input).highlight('#F08080').activate();
494 observer: function(evt) {
495 var simplebox, input;
497 if (evt.eventName === "simplebox:shown" && evt.memo.element !== $("termsofusearea")) {
498 simplebox = evt.memo;
499 if (simplebox === this.modalbox) {
500 input = this.area.select('input[type="text"]')[0];
505 this.modalbox.hide();
508 } else if (evt.eventName === "simplebox:hidden" && evt.memo.element !== $("termsofusearea")) {
509 simplebox = evt.memo;
510 if (simplebox === this.modalbox) {
516 failure: function(transport) {
517 var httpCode = 0, message = SyjStrings.unknownError, input; // default message error
520 httpCode = transport.getStatus();
525 message = SyjStrings.notReachedError;
530 message = SyjStrings.requestError;
533 message = SyjStrings.serverError;
537 this.messenger.setMessage(message, "error");
538 input = this.area.select('input[type="text"]')[0];
539 input.highlight('#F08080').activate();
543 this.messenger.hide();
544 this.area.select('.message').invoke('setMessageStatus', null);
548 var SYJUserClass = Class.create(SYJModalClass, {
552 init: function($super) {
554 $("termsofusearea").hide();
556 $$("#user_termsofuse_anchor, #geom_termsofuse_anchor").invoke('observe', "click", function(evt) {
558 $("termsofusearea").show();
559 $("termsofuseiframe").setAttribute("src", evt.target.href);
560 this.toubox = new SimpleBox($("termsofusearea"), {
561 closeMethods: ["onescapekey", "onouterclick", "onbutton"]
566 }.bindAsEventListener(this));
568 $$("#login_area_create > a").invoke('observe', 'click',
570 this.modalbox.show();
572 }.bindAsEventListener(this));
574 $("user_password").observe('contentchange', function(evt) {
575 if (evt.target.value.length < 6) {
576 $("user_password-desc").setMessageStatus("warn");
578 $("user_password-desc").setMessageStatus("success");
580 }.bindAsEventListener(this));
582 $("account-info").hide();
583 $("account-info-bullet").observe('click', function(evt) {
584 var elt = $("account-info");
586 evt.target.src = "icons/bullet_arrow_right.png";
589 evt.target.src = "icons/bullet_arrow_down.png";
596 presubmit: function() {
597 if (!(this.checkNotEmpty("user_pseudo", SyjStrings.userEmptyWarn))) {
601 if (!($("user_pseudo").value.match(/^[a-zA-Z0-9_.]+$/))) {
602 this.messenger.setMessage(SyjStrings.invalidPseudo, "warn");
603 $("user_pseudo").highlight('#F08080').activate();
607 if (!(this.checkNotEmpty("user_password", SyjStrings.passwordEmptyWarn))) {
611 if ($("user_password").value.length < 6) {
612 $("user_password-desc").setMessageStatus("warn");
613 $("user_password").highlight('#F08080').activate();
617 if ($("user_password").value !== $("user_password_confirm").value) {
618 this.messenger.setMessage(SyjStrings.passwordNoMatchWarn, "warn");
619 $("user_password").highlight('#F08080').activate();
623 if (!(this.checkNotEmpty("user_email", SyjStrings.emailEmptyWarn))) {
627 if (!$("user_accept").checked) {
628 this.messenger.setMessage(SyjStrings.acceptTermsofuseWarn, "warn");
629 $("user_accept_container").highlight('#F08080');
630 $("user_accept").activate();
638 success: function(transport) {
640 SYJView.messenger.setMessage(SyjStrings.userSuccess, "success");
641 this.modalbox.hide();
642 if (SYJView.needsFormResubmit) {
643 SYJView.messenger.addMessage(SyjStrings.canResubmit);
644 $("geom_submit").activate();
648 failure: function($super, transport) {
649 var httpCode = 0, focusInput = null, message = "";
652 httpCode = transport.getStatus();
660 if (transport.responseJSON) {
661 switch (transport.responseJSON.message) {
663 message = SyjStrings.emailInvalidWarn;
664 focusInput = $("user_email");
667 message = SyjStrings.uniqueUserError;
668 focusInput = $("user_pseudo");
671 message = SyjStrings.uniqueEmailError;
672 focusInput = $("user_email");
680 this.messenger.setMessage(message, "error");
682 focusInput.highlight('#F08080').activate();
690 var SYJUser = new SYJUserClass();
692 var SYJLoginClass = Class.create(SYJModalClass, {
695 init: function($super) {
699 presubmit: function() {
700 if (!(this.checkNotEmpty("login_user", SyjStrings.userEmptyWarn))) {
708 success: function(transport) {
709 if (transport.responseText === "1") {
710 loginMgr.login(true);
714 SYJView.messenger.setMessage(SyjStrings.loginSuccess, "success");
715 this.modalbox.hide();
716 if (SYJView.needsFormResubmit) {
717 SYJView.messenger.addMessage(SyjStrings.canResubmit);
718 $("geom_submit").activate();
722 failure: function($super, transport) {
723 var httpCode = 0, focusInput = null, message = "";
726 httpCode = transport.getStatus();
734 message = SyjStrings.loginFailure;
735 focusInput = $("login_user");
740 this.messenger.setMessage(message, "error");
742 focusInput.highlight('#F08080').activate();
750 var SYJLogin = new SYJLoginClass();
752 var SYJNewpwdClass = Class.create(SYJModalClass, {
755 presubmit: function() {
756 if (!(this.checkNotEmpty("newpwd_email", SyjStrings.emailEmptyWarn))) {
762 success: function(transport) {
763 SYJView.messenger.setMessage(SyjStrings.newpwdSuccess, "success");
764 this.modalbox.hide();
768 var SYJNewpwd = new SYJNewpwdClass();
770 var loginMgr = Object.extend(gLoggedInfo, {
773 updateUI: function() {
774 if (!this.controlsdeck) {
775 this.controlsdeck = new Deck("login_controls");
778 this.controlsdeck.setIndex(1);
779 $$(".logged-hide").invoke('hide');
780 $$(".logged-show").invoke('show');
782 this.controlsdeck.setIndex(0);
783 $$(".logged-hide").invoke('show');
784 $$(".logged-show").invoke('hide');
787 if (this.iscreator) {
788 $("data_controls").show();
790 $("data_controls").hide();
794 login: function(aIsCreator) {
795 if (typeof aIsCreator === "boolean") {
796 this.iscreator = aIsCreator;
803 document.observe("dom:loaded", function() {