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 (!$("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();
416 message = SyjStrings.notReachedError;
421 message = SyjStrings.requestError; // default message
422 if (transport.responseJSON) {
423 switch (transport.responseJSON.message) {
425 message = SyjStrings.unreferencedError;
428 message = SyjStrings.uniquePathError;
436 message = SyjStrings.serverError;
437 this.needsFormResubmit = true;
440 message = SyjStrings.unknownError;
445 // is some cases, we let the user resubmit, in some other cases, he
446 // needs to modify the path before submitting again
447 if (this.needsFormResubmit) {
451 this.messenger.setMessage(message, "error");
455 var SYJModalClass = Class.create({
459 this.area = $(this.type + '_area');
460 this.messenger = $(this.type + "_message");
461 this.modalbox = new SimpleBox(this.area, {
462 closeMethods: ["onescapekey", "onouterclick", "onbutton"]
465 $(this.type + "_control_anchor").observe("click", function(evt) {
466 this.modalbox.show();
468 }.bindAsEventListener(this));
470 document.observe('simplebox:shown', this.observer.bindAsEventListener(this));
471 document.observe('simplebox:hidden', this.observer.bindAsEventListener(this));
473 $(this.type + "form").ajaxize({
474 presubmit: this.presubmit.bind(this),
475 onSuccess: this.success.bind(this),
476 onFailure: this.failure.bind(this)
480 checkNotEmpty: function(input, message) {
481 if ($(input).value.strip().empty()) {
482 this.messenger.setMessage(message, "warn");
483 $(input).highlight('#F08080').activate();
489 observer: function(evt) {
490 var simplebox, input;
492 if (evt.eventName === "simplebox:shown" && evt.memo.element !== $("termsofusearea")) {
493 simplebox = evt.memo;
494 if (simplebox === this.modalbox) {
495 input = this.area.select('input[type="text"]')[0];
500 this.modalbox.hide();
503 } else if (evt.eventName === "simplebox:hidden" && evt.memo.element !== $("termsofusearea")) {
504 simplebox = evt.memo;
505 if (simplebox === this.modalbox) {
511 failure: function(transport) {
512 var httpCode = 0, message = SyjStrings.unknownError, input; // default message error
515 httpCode = transport.getStatus();
520 message = SyjStrings.notReachedError;
525 message = SyjStrings.requestError;
528 message = SyjStrings.serverError;
532 this.messenger.setMessage(message, "error");
533 input = this.area.select('input[type="text"]')[0];
534 input.highlight('#F08080').activate();
538 this.messenger.hide();
539 this.area.select('.message').invoke('setMessageStatus', null);
543 var SYJUserClass = Class.create(SYJModalClass, {
547 init: function($super) {
549 $("termsofusearea").hide();
551 $$("#user_termsofuse_anchor, #geom_termsofuse_anchor").invoke('observe', "click", function(evt) {
553 $("termsofusearea").show();
554 $("termsofuseiframe").setAttribute("src", evt.target.href);
555 this.toubox = new SimpleBox($("termsofusearea"), {
556 closeMethods: ["onescapekey", "onouterclick", "onbutton"]
561 }.bindAsEventListener(this));
563 $$("#login_area_create > a").invoke('observe', 'click',
565 this.modalbox.show();
567 }.bindAsEventListener(this));
569 $("user_password").observe('contentchange', function(evt) {
570 if (evt.target.value.length < 6) {
571 $("user_password-desc").setMessageStatus("warn");
573 $("user_password-desc").setMessageStatus("success");
575 }.bindAsEventListener(this));
577 $("account-info").hide();
578 $("account-info-bullet").observe('click', function(evt) {
579 var elt = $("account-info");
581 evt.target.src = "icons/bullet_arrow_right.png";
584 evt.target.src = "icons/bullet_arrow_down.png";
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').activate();
602 if (!(this.checkNotEmpty("user_password", SyjStrings.passwordEmptyWarn))) {
606 if ($("user_password").value.length < 6) {
607 $("user_password-desc").setMessageStatus("warn");
608 $("user_password").highlight('#F08080').activate();
612 if ($("user_password").value !== $("user_password_confirm").value) {
613 this.messenger.setMessage(SyjStrings.passwordNoMatchWarn, "warn");
614 $("user_password").highlight('#F08080').activate();
618 if (!(this.checkNotEmpty("user_email", SyjStrings.emailEmptyWarn))) {
622 if (!$("user_accept").checked) {
623 this.messenger.setMessage(SyjStrings.acceptTermsofuseWarn, "warn");
624 $("user_accept_container").highlight('#F08080');
625 $("user_accept").activate();
633 success: function(transport) {
635 SYJView.messenger.setMessage(SyjStrings.userSuccess, "success");
636 this.modalbox.hide();
637 if (SYJView.needsFormResubmit) {
638 SYJView.messenger.addMessage(SyjStrings.canResubmit);
639 $("geom_submit").activate();
643 failure: function($super, transport) {
644 var httpCode = 0, focusInput = null, message = "";
647 httpCode = transport.getStatus();
655 if (transport.responseJSON) {
656 switch (transport.responseJSON.message) {
658 message = SyjStrings.emailInvalidWarn;
659 focusInput = $("user_email");
662 message = SyjStrings.uniqueUserError;
663 focusInput = $("user_pseudo");
666 message = SyjStrings.uniqueEmailError;
667 focusInput = $("user_email");
675 this.messenger.setMessage(message, "error");
677 focusInput.highlight('#F08080').activate();
685 var SYJUser = new SYJUserClass();
687 var SYJLoginClass = Class.create(SYJModalClass, {
690 init: function($super) {
694 presubmit: function() {
695 if (!(this.checkNotEmpty("login_user", SyjStrings.userEmptyWarn))) {
703 success: function(transport) {
704 if (transport.responseText === "1") {
705 loginMgr.login(true);
709 SYJView.messenger.setMessage(SyjStrings.loginSuccess, "success");
710 this.modalbox.hide();
711 if (SYJView.needsFormResubmit) {
712 SYJView.messenger.addMessage(SyjStrings.canResubmit);
713 $("geom_submit").activate();
717 failure: function($super, transport) {
718 var httpCode = 0, focusInput = null, message = "";
721 httpCode = transport.getStatus();
729 message = SyjStrings.loginFailure;
730 focusInput = $("login_user");
735 this.messenger.setMessage(message, "error");
737 focusInput.highlight('#F08080').activate();
745 var SYJLogin = new SYJLoginClass();
747 var SYJNewpwdClass = Class.create(SYJModalClass, {
750 presubmit: function() {
751 if (!(this.checkNotEmpty("newpwd_email", SyjStrings.emailEmptyWarn))) {
757 success: function(transport) {
758 SYJView.messenger.setMessage(SyjStrings.newpwdSuccess, "success");
759 this.modalbox.hide();
763 var SYJNewpwd = new SYJNewpwdClass();
765 var loginMgr = Object.extend(gLoggedInfo, {
768 updateUI: function() {
769 if (!this.controlsdeck) {
770 this.controlsdeck = new Deck("login_controls");
773 this.controlsdeck.setIndex(1);
774 $$(".logged-hide").invoke('hide');
775 $$(".logged-show").invoke('show');
777 this.controlsdeck.setIndex(0);
778 $$(".logged-hide").invoke('show');
779 $$(".logged-show").invoke('hide');
782 if (this.iscreator) {
783 $("data_controls").show();
785 $("data_controls").hide();
789 login: function(aIsCreator) {
790 if (typeof aIsCreator === "boolean") {
791 this.iscreator = aIsCreator;
798 document.observe("dom:loaded", function() {