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 = null, 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();
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();
289 extent = new OpenLayers.Bounds(gMaxExtent.minlon, gMaxExtent.minlat, gMaxExtent.maxlon, gMaxExtent.maxlat)
290 .transform(WGS84, Mercator);
292 this.map.zoomToExtent(extent);
293 document.observe('simplebox:shown', this.observer.bindAsEventListener(this));
296 observer: function(evt) {
297 if (evt.eventName === "simplebox:shown" && evt.memo.element !== $("termsofusearea")) {
298 this.messenger.hide();
302 prepareForm: function(form) {
303 if (!loginMgr.logged && !$("geom_accept").checked) {
304 this.messenger.setMessage(SyjStrings.acceptTermsofuseWarn, "warn");
305 $("geom_accept_container").highlight('#F08080');
306 $("geom_accept").activate();
310 var line, realPoints, idx, handler;
312 line = new OpenLayers.Geometry.LineString();
313 realPoints = this.editControl.handler.realPoints;
314 for (idx = 0; idx < realPoints.length; idx++) {
315 line.addComponent(realPoints[idx].geometry.clone());
317 this.viewLayer.addFeatures(new OpenLayers.Feature.Vector(line));
318 handler = this.editControl.handler;
319 OpenLayers.Handler.ModifiablePath.prototype.finalize.apply(handler, arguments);
320 // we need to recreate them on next createFeature; otherwise
321 // they'll reference destroyed features
322 delete(handler.handlers.drag);
323 delete(handler.handlers.feature);
324 this.editControl.deactivate();
326 $("geom_data").value = this.wkt.write(new OpenLayers.Feature.Vector(line));
327 this.needsFormResubmit = false;
328 SyjSaveUI.disable.bind(SyjSaveUI).defer();
329 this.messenger.hide();
333 editMode: function() {
334 var components, point0, point, pixels, pixel, idx;
336 this.initEditControl();
338 this.editControl.activate();
339 if (this.viewLayer.features.length) {
340 components = this.viewLayer.features[0].geometry.components;
341 point0 = components[0];
343 pixel = this.map.getPixelFromLonLat(new OpenLayers.LonLat(point0.x, point0.y));
344 this.editControl.handler.createFeature(pixel);
345 this.editControl.handler.lastUp = pixel;
347 for (idx = 1; idx < components.length; idx++) {
348 point = components[idx];
349 pixels.push(this.map.getPixelFromLonLat(new OpenLayers.LonLat(point.x, point.y)));
351 this.editControl.handler.addPoints(pixels);
355 this.viewLayer.destroyFeatures();
358 if (this.editControl.handler.realPoints && this.editControl.handler.realPoints.length >= 2) {
359 SyjSaveUI.show().disableSubmit();
361 SyjSaveUI.show().disable();
365 initEditControl: function() {
368 if (this.editControl) {
372 this.editControl = new OpenLayers.Control.DrawFeature(new OpenLayers.Layer.Vector(), OpenLayers.Handler.SyjModifiablePath, {
374 modify: function(f, line) {
375 if (this.handler.realPoints.length < 2) {
376 SyjSaveUI.show().disable();
378 SyjSaveUI.show().enable();
385 styleMap: styleMap.edit
389 this.map.addControl(this.editControl);
390 if (this.editControl.layer.renderer instanceof OpenLayers.Renderer.Canvas) {
391 // using externalGraphic with canvas renderer is definitively too buggy
392 styles = this.editControl.handler.layerOptions.styleMap.styles;
393 styles.select = styles.select_for_canvas;
397 saveSuccess: function(transport) {
398 if (!$("geom_id").value) {
399 location = "idx/" + transport.responseText;
402 this.messenger.setMessage(SyjStrings.saveSuccess, "success");
406 document.title = $('geom_title').value;
409 saveFailure: function(transport) {
410 var httpCode = 0, message = "";
413 httpCode = transport.getStatus();
418 message = SyjStrings.notReachedError;
423 message = SyjStrings.requestError; // default message
424 if (transport.responseJSON) {
425 switch (transport.responseJSON.message) {
427 message = SyjStrings.unreferencedError;
430 message = SyjStrings.uniquePathError;
438 message = SyjStrings.serverError;
439 this.needsFormResubmit = true;
442 message = SyjStrings.unknownError;
447 // is some cases, we let the user resubmit, in some other cases, he
448 // needs to modify the path before submitting again
449 if (this.needsFormResubmit) {
453 this.messenger.setMessage(message, "error");
457 var SYJModalClass = Class.create({
461 this.area = $(this.type + '_area');
462 this.messenger = $(this.type + "_message");
463 this.modalbox = new SimpleBox(this.area, {
464 closeMethods: ["onescapekey", "onouterclick", "onbutton"]
467 $(this.type + "_control_anchor").observe("click", function(evt) {
468 this.modalbox.show();
470 }.bindAsEventListener(this));
472 document.observe('simplebox:shown', this.observer.bindAsEventListener(this));
473 document.observe('simplebox:hidden', this.observer.bindAsEventListener(this));
475 $(this.type + "form").ajaxize({
476 presubmit: this.presubmit.bind(this),
477 onSuccess: this.success.bind(this),
478 onFailure: this.failure.bind(this)
482 checkNotEmpty: function(input, message) {
483 if ($(input).value.strip().empty()) {
484 this.messenger.setMessage(message, "warn");
485 $(input).highlight('#F08080').activate();
491 observer: function(evt) {
492 var simplebox, input;
494 if (evt.eventName === "simplebox:shown" && evt.memo.element !== $("termsofusearea")) {
495 simplebox = evt.memo;
496 if (simplebox === this.modalbox) {
497 input = this.area.select('input[type="text"]')[0];
502 this.modalbox.hide();
505 } else if (evt.eventName === "simplebox:hidden" && evt.memo.element !== $("termsofusearea")) {
506 simplebox = evt.memo;
507 if (simplebox === this.modalbox) {
513 failure: function(transport) {
514 var httpCode = 0, message = SyjStrings.unknownError, input; // default message error
517 httpCode = transport.getStatus();
522 message = SyjStrings.notReachedError;
527 message = SyjStrings.requestError;
530 message = SyjStrings.serverError;
534 this.messenger.setMessage(message, "error");
535 input = this.area.select('input[type="text"]')[0];
536 input.highlight('#F08080').activate();
540 this.messenger.hide();
541 this.area.select('.message').invoke('setMessageStatus', null);
545 var SYJUserClass = Class.create(SYJModalClass, {
549 init: function($super) {
551 $("termsofusearea").hide();
553 $$("#user_termsofuse_anchor, #geom_termsofuse_anchor").invoke('observe', "click", function(evt) {
555 $("termsofusearea").show();
556 $("termsofuseiframe").setAttribute("src", evt.target.href);
557 this.toubox = new SimpleBox($("termsofusearea"), {
558 closeMethods: ["onescapekey", "onouterclick", "onbutton"]
563 }.bindAsEventListener(this));
565 $$("#login_area_create > a").invoke('observe', 'click',
567 this.modalbox.show();
569 }.bindAsEventListener(this));
571 $("user_password").observe('contentchange', function(evt) {
572 if (evt.target.value.length < 6) {
573 $("user_password-desc").setMessageStatus("warn");
575 $("user_password-desc").setMessageStatus("success");
577 }.bindAsEventListener(this));
579 $("account-info").hide();
580 $("account-info-bullet").observe('click', function(evt) {
581 var elt = $("account-info");
583 evt.target.src = "icons/bullet_arrow_right.png";
586 evt.target.src = "icons/bullet_arrow_down.png";
593 presubmit: function() {
594 if (!(this.checkNotEmpty("user_pseudo", SyjStrings.userEmptyWarn))) {
598 if (!($("user_pseudo").value.match(/^[a-zA-Z0-9_.]+$/))) {
599 this.messenger.setMessage(SyjStrings.invalidPseudo, "warn");
600 $("user_pseudo").highlight('#F08080').activate();
604 if (!(this.checkNotEmpty("user_password", SyjStrings.passwordEmptyWarn))) {
608 if ($("user_password").value.length < 6) {
609 $("user_password-desc").setMessageStatus("warn");
610 $("user_password").highlight('#F08080').activate();
614 if ($("user_password").value !== $("user_password_confirm").value) {
615 this.messenger.setMessage(SyjStrings.passwordNoMatchWarn, "warn");
616 $("user_password").highlight('#F08080').activate();
620 if (!(this.checkNotEmpty("user_email", SyjStrings.emailEmptyWarn))) {
624 if (!$("user_accept").checked) {
625 this.messenger.setMessage(SyjStrings.acceptTermsofuseWarn, "warn");
626 $("user_accept_container").highlight('#F08080');
627 $("user_accept").activate();
635 success: function(transport) {
637 SYJView.messenger.setMessage(SyjStrings.userSuccess, "success");
638 this.modalbox.hide();
639 if (SYJView.needsFormResubmit) {
640 SYJView.messenger.addMessage(SyjStrings.canResubmit);
641 $("geom_submit").activate();
645 failure: function($super, transport) {
646 var httpCode = 0, focusInput = null, message = "";
649 httpCode = transport.getStatus();
657 if (transport.responseJSON) {
658 switch (transport.responseJSON.message) {
660 message = SyjStrings.emailInvalidWarn;
661 focusInput = $("user_email");
664 message = SyjStrings.uniqueUserError;
665 focusInput = $("user_pseudo");
668 message = SyjStrings.uniqueEmailError;
669 focusInput = $("user_email");
677 this.messenger.setMessage(message, "error");
679 focusInput.highlight('#F08080').activate();
687 var SYJUser = new SYJUserClass();
689 var SYJLoginClass = Class.create(SYJModalClass, {
692 init: function($super) {
696 presubmit: function() {
697 if (!(this.checkNotEmpty("login_user", SyjStrings.userEmptyWarn))) {
705 success: function(transport) {
706 if (transport.responseText === "1") {
707 loginMgr.login(true);
711 SYJView.messenger.setMessage(SyjStrings.loginSuccess, "success");
712 this.modalbox.hide();
713 if (SYJView.needsFormResubmit) {
714 SYJView.messenger.addMessage(SyjStrings.canResubmit);
715 $("geom_submit").activate();
719 failure: function($super, transport) {
720 var httpCode = 0, focusInput = null, message = "";
723 httpCode = transport.getStatus();
731 message = SyjStrings.loginFailure;
732 focusInput = $("login_user");
737 this.messenger.setMessage(message, "error");
739 focusInput.highlight('#F08080').activate();
747 var SYJLogin = new SYJLoginClass();
749 var SYJNewpwdClass = Class.create(SYJModalClass, {
752 presubmit: function() {
753 if (!(this.checkNotEmpty("newpwd_email", SyjStrings.emailEmptyWarn))) {
759 success: function(transport) {
760 SYJView.messenger.setMessage(SyjStrings.newpwdSuccess, "success");
761 this.modalbox.hide();
765 var SYJNewpwd = new SYJNewpwdClass();
767 var loginMgr = Object.extend(gLoggedInfo, {
770 updateUI: function() {
771 if (!this.controlsdeck) {
772 this.controlsdeck = new Deck("login_controls");
775 this.controlsdeck.setIndex(1);
776 $$(".logged-hide").invoke('hide');
777 $$(".logged-show").invoke('show');
779 this.controlsdeck.setIndex(0);
780 $$(".logged-hide").invoke('show');
781 $$(".logged-show").invoke('hide');
784 if (this.iscreator) {
785 $("data_controls").show();
787 $("data_controls").hide();
791 login: function(aIsCreator) {
792 if (typeof aIsCreator === "boolean") {
793 this.iscreator = aIsCreator;
800 document.observe("dom:loaded", function() {