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";
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.creatorname) {
256 layerOptions.attribution = SyjStrings.routeBy + ' ' + '<strong>' + gLoggedInfo.creatorname + '</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 if (!loginMgr.logged && !$("geom_accept").checked) {
311 this.messenger.setMessage(SyjStrings.acceptTermsofuseWarn, "warn");
312 $("geom_accept_container").highlight('#F08080');
313 $("geom_accept").activate();
317 var line, realPoints, idx, handler;
319 line = new OpenLayers.Geometry.LineString();
320 realPoints = this.editControl.handler.realPoints;
321 for (idx = 0; idx < realPoints.length; idx++) {
322 line.addComponent(realPoints[idx].geometry.clone());
324 this.viewLayer.addFeatures(new OpenLayers.Feature.Vector(line));
325 handler = this.editControl.handler;
326 OpenLayers.Handler.ModifiablePath.prototype.finalize.apply(handler, arguments);
327 // we need to recreate them on next createFeature; otherwise
328 // they'll reference destroyed features
329 delete(handler.handlers.drag);
330 delete(handler.handlers.feature);
331 this.editControl.deactivate();
333 $("geom_data").value = this.wkt.write(new OpenLayers.Feature.Vector(line));
334 this.needsFormResubmit = false;
335 SyjSaveUI.disable.bind(SyjSaveUI).defer();
336 this.messenger.hide();
340 editMode: function() {
341 var components, point0, point, pixels, pixel, idx;
343 this.initEditControl();
345 this.editControl.activate();
346 if (this.viewLayer.features.length) {
347 components = this.viewLayer.features[0].geometry.components;
348 point0 = components[0];
350 pixel = this.map.getPixelFromLonLat(new OpenLayers.LonLat(point0.x, point0.y));
351 this.editControl.handler.createFeature(pixel);
352 this.editControl.handler.lastUp = pixel;
354 for (idx = 1; idx < components.length; idx++) {
355 point = components[idx];
356 pixels.push(this.map.getPixelFromLonLat(new OpenLayers.LonLat(point.x, point.y)));
358 this.editControl.handler.addPoints(pixels);
362 this.viewLayer.destroyFeatures();
365 if (this.editControl.handler.realPoints && this.editControl.handler.realPoints.length >= 2) {
366 SyjSaveUI.show().disableSubmit();
368 SyjSaveUI.show().disable();
372 initEditControl: function() {
375 if (this.editControl) {
379 this.editControl = new OpenLayers.Control.DrawFeature(new OpenLayers.Layer.Vector(), OpenLayers.Handler.SyjModifiablePath, {
381 modify: function(f, line) {
382 if (this.handler.realPoints.length < 2) {
383 SyjSaveUI.show().disable();
385 SyjSaveUI.show().enable();
392 styleMap: styleMap.edit
396 this.map.addControl(this.editControl);
397 if (this.editControl.layer.renderer instanceof OpenLayers.Renderer.Canvas) {
398 // using externalGraphic with canvas renderer is definitively too buggy
399 styles = this.editControl.handler.layerOptions.styleMap.styles;
400 styles.select = styles.select_for_canvas;
404 saveSuccess: function(transport) {
405 if (!$("geom_id").value) {
406 location = "idx/" + transport.responseText;
409 this.messenger.setMessage(SyjStrings.saveSuccess, "success");
413 document.title = $('geom_title').value;
416 saveFailure: function(transport) {
417 var httpCode = 0, message = "";
420 httpCode = transport.getStatus();
425 message = SyjStrings.notReachedError;
430 message = SyjStrings.requestError; // default message
431 if (transport.responseJSON) {
432 switch (transport.responseJSON.message) {
434 message = SyjStrings.unreferencedError;
437 message = SyjStrings.uniquePathError;
445 message = SyjStrings.serverError;
446 this.needsFormResubmit = true;
449 message = SyjStrings.unknownError;
454 // is some cases, we let the user resubmit, in some other cases, he
455 // needs to modify the path before submitting again
456 if (this.needsFormResubmit) {
460 this.messenger.setMessage(message, "error");
464 var SYJModalClass = Class.create({
468 this.area = $(this.type + '_area');
469 this.messenger = $(this.type + "_message");
470 this.modalbox = new SimpleBox(this.area, {
471 closeMethods: ["onescapekey", "onouterclick", "onbutton"]
474 $(this.type + "_control_anchor").observe("click", function(evt) {
475 this.modalbox.show();
477 }.bindAsEventListener(this));
479 document.observe('simplebox:shown', this.observer.bindAsEventListener(this));
480 document.observe('simplebox:hidden', this.observer.bindAsEventListener(this));
482 $(this.type + "form").ajaxize({
483 presubmit: this.presubmit.bind(this),
484 onSuccess: this.success.bind(this),
485 onFailure: this.failure.bind(this)
489 checkNotEmpty: function(input, message) {
490 if ($(input).value.strip().empty()) {
491 this.messenger.setMessage(message, "warn");
492 $(input).highlight('#F08080').activate();
498 observer: function(evt) {
499 var simplebox, input;
501 if (evt.eventName === "simplebox:shown" && evt.memo.element !== $("termsofusearea")) {
502 simplebox = evt.memo;
503 if (simplebox === this.modalbox) {
504 input = this.area.select('input[type="text"]')[0];
509 this.modalbox.hide();
512 } else if (evt.eventName === "simplebox:hidden" && evt.memo.element !== $("termsofusearea")) {
513 simplebox = evt.memo;
514 if (simplebox === this.modalbox) {
520 failure: function(transport) {
521 var httpCode = 0, message = SyjStrings.unknownError, input; // default message error
524 httpCode = transport.getStatus();
529 message = SyjStrings.notReachedError;
534 message = SyjStrings.requestError;
537 message = SyjStrings.serverError;
541 this.messenger.setMessage(message, "error");
542 input = this.area.select('input[type="text"]')[0];
543 input.highlight('#F08080').activate();
547 this.messenger.hide();
548 this.area.select('.message').invoke('setMessageStatus', null);
552 var SYJUserClass = Class.create(SYJModalClass, {
556 init: function($super) {
558 $("termsofusearea").hide();
560 $$("#user_termsofuse_anchor, #geom_termsofuse_anchor").invoke('observe', "click", function(evt) {
562 $("termsofusearea").show();
563 $("termsofuseiframe").setAttribute("src", evt.target.href);
564 this.toubox = new SimpleBox($("termsofusearea"), {
565 closeMethods: ["onescapekey", "onouterclick", "onbutton"]
570 }.bindAsEventListener(this));
572 $$("#login_area_create > a").invoke('observe', 'click',
574 this.modalbox.show();
576 }.bindAsEventListener(this));
578 $("user_password").observe('contentchange', function(evt) {
579 if (evt.target.value.length < 6) {
580 $("user_password-desc").setMessageStatus("warn");
582 $("user_password-desc").setMessageStatus("success");
584 }.bindAsEventListener(this));
586 $("account-info").hide();
587 $("account-info-bullet").observe('click', function(evt) {
588 var elt = $("account-info");
590 evt.target.src = "icons/bullet_arrow_right.png";
593 evt.target.src = "icons/bullet_arrow_down.png";
600 presubmit: function() {
601 if (!(this.checkNotEmpty("user_pseudo", SyjStrings.userEmptyWarn))) {
605 if (!($("user_pseudo").value.match(/^[a-zA-Z0-9_.]+$/))) {
606 this.messenger.setMessage(SyjStrings.invalidPseudo, "warn");
607 $("user_pseudo").highlight('#F08080').activate();
611 if (!(this.checkNotEmpty("user_password", SyjStrings.passwordEmptyWarn))) {
615 if ($("user_password").value.length < 6) {
616 $("user_password-desc").setMessageStatus("warn");
617 $("user_password").highlight('#F08080').activate();
621 if ($("user_password").value !== $("user_password_confirm").value) {
622 this.messenger.setMessage(SyjStrings.passwordNoMatchWarn, "warn");
623 $("user_password").highlight('#F08080').activate();
627 if (!(this.checkNotEmpty("user_email", SyjStrings.emailEmptyWarn))) {
631 if (!$("user_accept").checked) {
632 this.messenger.setMessage(SyjStrings.acceptTermsofuseWarn, "warn");
633 $("user_accept_container").highlight('#F08080');
634 $("user_accept").activate();
642 success: function(transport) {
644 SYJView.messenger.setMessage(SyjStrings.userSuccess, "success");
645 this.modalbox.hide();
646 if (SYJView.needsFormResubmit) {
647 SYJView.messenger.addMessage(SyjStrings.canResubmit);
648 $("geom_submit").activate();
652 failure: function($super, transport) {
653 var httpCode = 0, focusInput = null, message = "";
656 httpCode = transport.getStatus();
664 if (transport.responseJSON) {
665 switch (transport.responseJSON.message) {
667 message = SyjStrings.emailInvalidWarn;
668 focusInput = $("user_email");
671 message = SyjStrings.uniqueUserError;
672 focusInput = $("user_pseudo");
675 message = SyjStrings.uniqueEmailError;
676 focusInput = $("user_email");
684 this.messenger.setMessage(message, "error");
686 focusInput.highlight('#F08080').activate();
694 var SYJUser = new SYJUserClass();
696 var SYJLoginClass = Class.create(SYJModalClass, {
699 init: function($super) {
703 presubmit: function() {
704 if (!(this.checkNotEmpty("login_user", SyjStrings.userEmptyWarn))) {
712 success: function(transport) {
713 if (transport.responseText === "1") {
714 loginMgr.login(true);
718 SYJView.messenger.setMessage(SyjStrings.loginSuccess, "success");
719 this.modalbox.hide();
720 if (SYJView.needsFormResubmit) {
721 SYJView.messenger.addMessage(SyjStrings.canResubmit);
722 $("geom_submit").activate();
726 failure: function($super, transport) {
727 var httpCode = 0, focusInput = null, message = "";
730 httpCode = transport.getStatus();
738 message = SyjStrings.loginFailure;
739 focusInput = $("login_user");
744 this.messenger.setMessage(message, "error");
746 focusInput.highlight('#F08080').activate();
754 var SYJLogin = new SYJLoginClass();
756 var SYJNewpwdClass = Class.create(SYJModalClass, {
759 presubmit: function() {
760 if (!(this.checkNotEmpty("newpwd_email", SyjStrings.emailEmptyWarn))) {
766 success: function(transport) {
767 SYJView.messenger.setMessage(SyjStrings.newpwdSuccess, "success");
768 this.modalbox.hide();
772 var SYJNewpwd = new SYJNewpwdClass();
774 var loginMgr = Object.extend(gLoggedInfo, {
777 updateUI: function() {
778 if (!this.controlsdeck) {
779 this.controlsdeck = new Deck("login_controls");
782 this.controlsdeck.setIndex(1);
783 $$(".logged-hide").invoke('hide');
784 $$(".logged-show").invoke('show');
786 this.controlsdeck.setIndex(0);
787 $$(".logged-hide").invoke('show');
788 $$(".logged-show").invoke('hide');
791 if (this.iscreator) {
792 $("data_controls").show();
794 $("data_controls").hide();
798 login: function(aIsCreator) {
799 if (typeof aIsCreator === "boolean") {
800 this.iscreator = aIsCreator;
807 document.observe("dom:loaded", function() {