-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathosblayer.js
1096 lines (952 loc) · 36.8 KB
/
osblayer.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
OpenStreetBugs layer is free software: you can redistribute it
and/or modify it under the terms of the GNU Affero General Public License
as published by the Free Software Foundation, either version 3 of the
License, or (at your option) any later version.
This file is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public
License <http://www.gnu.org/licenses/> for more details.
Copyright © 2009–2011 Candid Dauth
*/
/**
* A fully functional OpenStreetBugs layer. See http://openstreetbugs.schokokeks.org/.
* Even though the OpenStreetBugs API originally does not intend this, you can create multiple instances of this Layer and add them to different maps (or to one single map for whatever crazy reason) without problems.
*/
OpenLayers.Layer.OpenStreetBugs = new OpenLayers.Class(OpenLayers.Layer.Markers, {
/**
* The URL of the OpenStreetBugs API.
* @var String
*/
serverURL : "http://openstreetbugs.schokokeks.org/api/0.1/",
/**
* Associative array (index: bug ID) that is filled with the bugs loaded in this layer
* @var String
*/
bugs : { },
/**
* The username to be used to change or create bugs on OpenStreetBugs
* @var String
*/
username : "NoName",
/**
* The icon to be used for an open bug
* @var OpenLayers.Icon
*/
iconOpen : new OpenLayers.Icon("http://openstreetbugs.schokokeks.org/client/open_bug_marker.png", new OpenLayers.Size(22, 22), new OpenLayers.Pixel(-11, -11)),
/**
* The icon to be used for a closed bug
* @var OpenLayers.Icon
*/
iconClosed : new OpenLayers.Icon("http://openstreetbugs.schokokeks.org/client/closed_bug_marker.png", new OpenLayers.Size(22, 22), new OpenLayers.Pixel(-11, -11)),
/**
* The projection of the coordinates sent by the OpenStreetBugs API.
* @var OpenLayers.Projection
*/
apiProjection : new OpenLayers.Projection("EPSG:4326"),
/**
* If this is set to true, the user may not commit comments or close bugs.
* @var Boolean
*/
readonly : false,
/**
* When the layer is hidden, all open popups are stored in this array in order to be re-opened again when the layer is made visible again.
*/
reopenPopups : [ ],
/**
* The user name will be saved in a cookie if this isn’t set to false.
* @var Boolean
*/
setCookie : true,
/**
* The lifetime of the user name cookie in days.
* @var Number
*/
cookieLifetime : 1000,
/**
* The path where the cookie will be available on this server.
* @var String
*/
cookiePath : null,
/**
* A URL to append lon=123&lat=123&zoom=123 for the Permalinks.
* @var String
*/
permalinkURL : "http://www.openstreetmap.org/",
opacity : 0.7,
projection : new OpenLayers.Projection("EPSG:4326"),
/**
* @param String name
*/
initialize : function(name, options)
{
OpenLayers.Layer.Markers.prototype.initialize.apply(this, arguments);
putAJAXMarker.layers.push(this);
this.events.addEventType("markerAdded");
this.events.register("visibilitychanged", this, this.updatePopupVisibility);
this.events.register("visibilitychanged", this, this.loadBugs);
var cookies = document.cookie.split(/;\s*/);
for(var i=0; i<cookies.length; i++)
{
var cookie = cookies[i].split("=");
if(cookie[0] == "osbUsername")
{
this.username = decodeURIComponent(cookie[1]);
break;
}
}
OpenLayers.Layer.OpenStreetBugs.setCSS();
},
/**
* Is automatically called when the layer is added to an OpenLayers.Map. Initialises the automatic bug loading in the visible bounding box.
*/
afterAdd : function()
{
var ret = OpenLayers.Layer.Markers.prototype.afterAdd.apply(this, arguments);
this.map.events.register("moveend", this, this.loadBugs);
this.loadBugs();
return ret;
},
/**
* At the moment the OSB API responses to requests using JavaScript code. This way the Same Origin Policy can be worked around. Unfortunately, this makes communicating with the API a bit too asynchronous, at the moment there is no way to tell to which request the API actually responses.
* This method creates a new script HTML element that imports the API request URL. The API JavaScript response then executes the global functions provided below.
* @param String url The URL this.serverURL + url is requested.
*/
apiRequest : function(url)
{
var script = document.createElement("script");
script.type = "text/javascript";
script.src = this.serverURL + url + "&nocache="+(new Date()).getTime();
document.body.appendChild(script);
},
/**
* Is automatically called when the visibility of the layer changes. When the layer is hidden, all visible popups
* are closed and their visibility is saved. When the layer is made visible again, these popups are re-opened.
*/
updatePopupVisibility : function()
{
if(this.getVisibility())
{
for(var i=0; i<this.reopenPopups.length; i++)
this.reopenPopups[i].show();
this.reopenPopups = [ ];
}
else
{
for(var i=0; i<this.markers.length; i++)
{
if(this.markers[i].feature.popup && this.markers[i].feature.popup.visible())
{
this.markers[i].feature.popup.hide();
this.reopenPopups.push(this.markers[i].feature.popup);
}
}
}
},
/**
* Sets the user name to be used for interactions with OpenStreetBugs.
*/
setUserName : function(username)
{
if(this.username == username)
return;
this.username = username;
if(this.setCookie)
{
var cookie = "osbUsername="+encodeURIComponent(username);
if(this.cookieLifetime)
cookie += ";expires="+(new Date((new Date()).getTime() + this.cookieLifetime*86400000)).toGMTString();
if(this.cookiePath)
cookie += ";path="+this.cookiePath;
document.cookie = cookie;
}
for(var i=0; i<this.markers.length; i++)
{
if(!this.markers[i].feature.popup) continue;
var els = this.markers[i].feature.popup.contentDom.getElementsByTagName("input");
for(var j=0; j<els.length; j++)
{
if(els[j].className != "osbUsername") continue;
els[j].value = username;
}
}
},
/**
* Returns the currently set username or “NoName” if none is set.
*/
getUserName : function()
{
if(this.username)
return this.username;
else
return "NoName";
},
/**
* Loads the bugs in the current bounding box. Is automatically called by an event handler ("moveend" event) that is created in the afterAdd() method.
*/
loadBugs : function()
{
if(!this.getVisibility())
return true;
var bounds = this.map.getExtent();
if(!bounds) return false;
bounds.transform(this.map.getProjectionObject(), this.apiProjection);
this.apiRequest("getBugs"
+ "?t="+this.round(bounds.top, 5)
+ "&r="+this.round(bounds.right, 5)
+ "&b="+this.round(bounds.bottom, 5)
+ "&l="+this.round(bounds.left, 5));
},
/**
* Rounds the given number to the given number of digits after the floating point.
* @param Number number
* @param Number digits
* @return Number
*/
round : function(number, digits)
{
var factor = Math.pow(10, digits);
return Math.round(number*factor)/factor;
},
/**
* Adds an OpenLayers.Marker representing a bug to the map. Is usually called by loadBugs().
* @param Number id The bug ID
*/
createMarker: function(id)
{
if(this.bugs[id])
{
if(this.bugs[id].popup && !this.bugs[id].popup.visible())
this.setPopupContent(id);
if(this.bugs[id].osbClosed != putAJAXMarker.bugs[id][2])
this.bugs[id].destroy();
else
return;
}
var feature = this._createMarker(id, putAJAXMarker.bugs[id][0], putAJAXMarker.bugs[id][1], putAJAXMarker.bugs[id][2], (putAJAXMarker.bugs[id][2] ? this.iconClosed : this.iconOpen).clone());
feature.osbId = id;
feature.osbClosed = putAJAXMarker.bugs[id][2];
feature.marker.feature = feature;
feature.marker.events.registerPriority("click", feature, this.markerClick);
feature.marker.events.register("mouseover", feature, this.markerMouseOver);
feature.marker.events.register("mouseout", feature, this.markerMouseOut);
this.bugs[id] = feature;
this.events.triggerEvent("markerAdded");
},
/**
* Can be overloaded by subclasses in order to use a different marker creation mechanism. This does not create
* the popup yet, that is created later using the createPopup() method of the returned feature. The parameters
* about the popup content are only passed for the case that a special implementation needs them.
*
* The marker popup class has to support a DOM element as parameter for setContentHTML() and issue a "close" event
* when the popup is closed, like {@link OpenLayers.Popup.FramedCloud.OpenStreetBugs} does.
* @param Number id The bug ID or null when a bug is being created
* @param LonLat lonlat The coordinates in {@link #apiProjection} projection
* @param String comments The comments as HTML code or null when a bug is being created
* @param Boolean closed Whether the bug is closed. null when the bug is being created
* @param OpenLayers.Icon icon The suggested icon for the marker.
* @return OpenLayers.Feature the feature with the marker and the popup
*/
_createMarker: function(id, lonlat, comments, closed, icon)
{
lonlat = lonlat.clone().transform(this.apiProjection, this.map.getProjectionObject());
var feature = new OpenLayers.Feature(this, lonlat, { icon: icon, autoSize: true });
feature.popupClass = OpenLayers.Popup.FramedCloud.OpenStreetBugs;
var marker = feature.createMarker();
this.addMarker(marker);
return feature;
},
/**
* Recreates the content of the popup of a marker.
* @param Number id The bug ID
*/
setPopupContent: function(id) {
if(!this.bugs[id].popup)
return;
var el1,el2,el3;
var layer = this;
var newContent = document.createElement("div");
el1 = document.createElement("h3");
el1.appendChild(document.createTextNode(closed ? OpenLayers.i18n("Fixed Error") : OpenLayers.i18n("Unresolved Error")));
el1.appendChild(document.createTextNode(" ["));
el2 = document.createElement("a");
el2.href = "#";
el2.onclick = function(){ layer.map.setCenter(putAJAXMarker.bugs[id][0].clone().transform(layer.apiProjection, layer.map.getProjectionObject()), 15); };
el2.appendChild(document.createTextNode(OpenLayers.i18n("Zoom")));
el1.appendChild(el2);
el1.appendChild(document.createTextNode("]"));
if(this.permalinkURL)
{
el1.appendChild(document.createTextNode(" ["));
el2 = document.createElement("a");
el2.href = this.permalinkURL + (this.permalinkURL.indexOf("?") == -1 ? "?" : "&") + "lon="+putAJAXMarker.bugs[id][0].lon+"&lat="+putAJAXMarker.bugs[id][0].lat+"&zoom=15";
el2.appendChild(document.createTextNode(OpenLayers.i18n("Permalink")));
el1.appendChild(el2);
el1.appendChild(document.createTextNode("]"));
}
newContent.appendChild(el1);
var containerDescription = document.createElement("div");
newContent.appendChild(containerDescription);
var containerChange = document.createElement("div");
newContent.appendChild(containerChange);
var displayDescription = function(){
containerDescription.style.display = "block";
containerChange.style.display = "none";
layer.bugs[id].popup.updateSize();
};
var displayChange = function(){
containerDescription.style.display = "none";
containerChange.style.display = "block";
layer.bugs[id].popup.updateSize();
};
displayDescription();
el1 = document.createElement("dl");
for(var i=0; i<putAJAXMarker.bugs[id][1].length; i++)
{
el2 = document.createElement("dt");
el2.className = (i == 0 ? "osb-description" : "osb-comment");
el2.appendChild(document.createTextNode(i == 0 ? OpenLayers.i18n("Description") : OpenLayers.i18n("Comment")));
el1.appendChild(el2);
el2 = document.createElement("dd");
el2.className = (i == 0 ? "osb-description" : "osb-comment");
el2.appendChild(document.createTextNode(putAJAXMarker.bugs[id][1][i]));
el1.appendChild(el2);
}
containerDescription.appendChild(el1);
if(putAJAXMarker.bugs[id][2])
{
el1 = document.createElement("p");
el1.className = "osb-fixed";
el2 = document.createElement("em");
el2.appendChild(document.createTextNode(OpenLayers.i18n("Has been fixed.")));
el1.appendChild(el2);
containerDescription.appendChild(el1);
}
else if(!this.readonly)
{
el1 = document.createElement("div");
el2 = document.createElement("input");
el2.setAttribute("type", "button");
el2.onclick = function(){ displayChange(); };
el2.value = OpenLayers.i18n("Comment/Close");
el1.appendChild(el2);
containerDescription.appendChild(el1);
var el_form = document.createElement("form");
el_form.onsubmit = function(){ if(inputComment.value.match(/^\s*$/)) return false; layer.submitComment(id, inputComment.value); layer.hidePopup(id); return false; };
el1 = document.createElement("dl");
el2 = document.createElement("dt");
el2.appendChild(document.createTextNode(OpenLayers.i18n("Nickname")));
el1.appendChild(el2);
el2 = document.createElement("dd");
var inputUsername = document.createElement("input");
inputUsername.value = this.username;
inputUsername.className = "osbUsername";
inputUsername.onkeyup = function(){ layer.setUserName(inputUsername.value); };
el2.appendChild(inputUsername);
el1.appendChild(el2);
el2 = document.createElement("dt");
el2.appendChild(document.createTextNode(OpenLayers.i18n("Comment")));
el1.appendChild(el2);
el2 = document.createElement("dd");
var inputComment = document.createElement("input");
el2.appendChild(inputComment);
el1.appendChild(el2);
el_form.appendChild(el1);
el1 = document.createElement("ul");
el1.className = "buttons";
el2 = document.createElement("li");
el3 = document.createElement("input");
el3.setAttribute("type", "submit");
el3.value = OpenLayers.i18n("Add comment");
el2.appendChild(el3);
el1.appendChild(el2);
el2 = document.createElement("li");
el3 = document.createElement("input");
el3.setAttribute("type", "button");
el3.onclick = function(){ this.form.onsubmit(); layer.closeBug(id); layer.bugs[id].popup.hide(); return false; };
el3.value = OpenLayers.i18n("Mark as fixed");
el2.appendChild(el3);
el1.appendChild(el2);
el_form.appendChild(el1);
containerChange.appendChild(el_form);
el1 = document.createElement("div");
el2 = document.createElement("input");
el2.setAttribute("type", "button");
el2.onclick = function(){ displayDescription(); };
el2.value = OpenLayers.i18n("Cancel");
el1.appendChild(el2);
containerChange.appendChild(el1);
}
this.bugs[id].popup.setContentHTML(newContent);
},
/**
* Creates a new bug.
* @param OpenLayers.LonLat lonlat The coordinates in the API projection.
* @param String description
*/
createBug: function(lonlat, description) {
this.apiRequest("addPOIexec"
+ "?lat="+encodeURIComponent(lonlat.lat)
+ "&lon="+encodeURIComponent(lonlat.lon)
+ "&text="+encodeURIComponent(description + " [" + this.getUserName() + "]")
+ "&format=js"
);
},
/**
* Adds a comment to a bug.
* @param Number id
* @param String comment
*/
submitComment: function(id, comment) {
this.apiRequest("editPOIexec"
+ "?id="+encodeURIComponent(id)
+ "&text="+encodeURIComponent(comment + " [" + this.getUserName() + "]")
+ "&format=js"
);
},
/**
* Marks a bug as fixed.
* @param Number id
*/
closeBug: function(id) {
this.apiRequest("closePOIexec"
+ "?id="+encodeURIComponent(id)
+ "&format=js"
);
},
/**
* Removes the content of a marker popup (to reduce the amount of needed resources).
* @param Number id
*/
resetPopupContent: function(id) {
if(!this.bugs[id].popup)
return;
this.bugs[id].popup.setContentHTML(document.createElement("div"));
},
/**
* Makes the popup of the given marker visible. Makes sure that the popup content is created if it does not exist yet.
* @param Number id
*/
showPopup: function(id) {
var add = null;
if(!this.bugs[id].popup)
{
add = this.bugs[id].createPopup(true);
add.events.register("close", this, function(){ this.resetPopupContent(id); if(this.bugs[id].osbClicked) this.bugs[id].osbClicked = false; });
}
else if(this.bugs[id].popup.visible())
return;
this.setPopupContent(id);
if(add)
this.map.addPopup(add);
this.bugs[id].popup.show();
this.bugs[id].popup.updateSize();
},
/**
* Hides the popup of the given marker.
* @param Number id
*/
hidePopup: function(id) {
if(!this.bugs[id].popup || !this.bugs[id].popup.visible())
return;
this.bugs[id].popup.hide();
this.bugs[id].popup.events.triggerEvent("close");
},
/**
* Is run on the “click” event of a marker in the context of its OpenLayers.Feature. Toggles the visibility of the popup.
*/
markerClick: function(e) {
var feature = this; // Context is the feature
feature.osbClicked = !feature.osbClicked;
if(feature.osbClicked)
feature.layer.showPopup(feature.osbId);
else
feature.layer.hidePopup(feature.osbId);
OpenLayers.Event.stop(e);
return false;
},
/**
* Is run on the “mouseover” event of a marker in the context of its OpenLayers.Feature. Makes the popup visible.
*/
markerMouseOver: function(e) {
var feature = this; // Context is the feature
feature.layer.showPopup(feature.osbId);
OpenLayers.Event.stop(e);
},
/**
* Is run on the “mouseout” event of a marker in the context of its OpenLayers.Feature. Hides the popup (if it has not been clicked).
*/
markerMouseOut: function(e) {
var feature = this; // Context is the feature
if(!feature.osbClicked)
feature.layer.hidePopup(feature.osbId);
OpenLayers.Event.stop(e);
},
CLASS_NAME: "OpenLayers.Layer.OpenStreetBugs"
});
/**
* Is called by the initialize() function and adds the stylesheets to the document.
*/
OpenLayers.Layer.OpenStreetBugs.setCSS = function() {
if(OpenLayers.Layer.OpenStreetBugs.setCSS.done)
return;
else
OpenLayers.Layer.OpenStreetBugs.setCSS.done = true;
var rules = '.olPopupFramedCloudOpenStreetBugs dl { margin:0; padding:0; }' +
'.olPopupFramedCloudOpenStreetBugs dt { margin:0; padding:0; font-weight:bold; float:left; clear:left; }' +
'.olPopupFramedCloudOpenStreetBugs dt:after { content: ": "; }' +
'* html .olPopupFramedCloudOpenStreetBugs dt { margin-right:1ex; }' +
'.olPopupFramedCloudOpenStreetBugs dd { margin:0; padding:0; }' +
'.olPopupFramedCloudOpenStreetBugs ul.buttons { list-style-type:none; padding:0; margin:0; }' +
'.olPopupFramedCloudOpenStreetBugs ul.buttons li { display:inline; margin:0; padding:0; }' +
'.olPopupFramedCloudOpenStreetBugs h3 { font-size:1.2em; margin:.2em 0 .7em 0; }';
var st = document.createElement("style");
st.setAttribute("type", "text/css");
if(st.styleSheet){ //IE method
st.styleSheet.cssText = rules;
}else{
st.appendChild(document.createTextNode(rules));
}
document.getElementsByTagName("head")[0].appendChild(st);
};
/**
* An OpenLayers control to create new bugs on mouse clicks on the map. Add an instance of this to your map using
* the OpenLayers.Map.addControl() method and activate() it.
*/
OpenLayers.Control.OpenStreetBugs = new OpenLayers.Class(OpenLayers.Control, {
title : null, // See below because of translation call
/**
* The icon to be used for the temporary markers that the “create bug” popup belongs to.
* @var OpenLayers.Icon
*/
icon : new OpenLayers.Icon("http://openstreetbugs.schokokeks.org/client/icon_error_add.png", new OpenLayers.Size(22, 22), new OpenLayers.Pixel(-11, -11)),
/**
* An instance of the OpenStreetBugs layer that this control shall be connected to. Is set in the constructor.
* @var OpenLayers.Layer.OpenStreetBugs
*/
osbLayer : null,
/**
* @param OpenLayers.Layer.OpenStreetBugs osbLayer The OpenStreetBugs layer that this control will be connected to.
*/
initialize: function(osbLayer, options) {
this.osbLayer = osbLayer;
this.title = OpenLayers.i18n("Create OpenStreetBug");
OpenLayers.Control.prototype.initialize.apply(this, [ options ]);
this.events.register("activate", this, function() {
if(!this.osbLayer.getVisibility())
this.osbLayer.setVisibility(true);
});
this.osbLayer.events.register("visibilitychanged", this, function() {
if(this.active && !this.osbLayer.getVisibility())
this.osbLayer.setVisibility(true);
});
},
destroy: function() {
if (this.handler)
this.handler.destroy();
this.handler = null;
OpenLayers.Control.prototype.destroy.apply(this, arguments);
},
draw: function() {
this.handler = new OpenLayers.Handler.Click(this, {'click': this.click}, { 'single': true, 'double': false, 'pixelTolerance': 0, 'stopSingle': false, 'stopDouble': false });
},
/**
* Map clicking event handler. Adds a temporary marker with a popup to the map, the popup contains the form to add a bug.
*/
click: function(e) {
if(!this.map) return true;
var control = this;
var lonlat = this.map.getLonLatFromViewPortPx(e.xy).transform(this.map.getProjectionObject(), this.osbLayer.apiProjection);
var feature = this.osbLayer._createMarker(null, lonlat, null, null, this.icon.clone());
feature.marker.feature = feature;
var newContent = document.createElement("div");
var el1,el2,el3;
el1 = document.createElement("h3");
el1.appendChild(document.createTextNode(OpenLayers.i18n("Create bug")));
newContent.appendChild(el1);
var el_form = document.createElement("form");
el_form.onsubmit = function() { control.osbLayer.createBug(lonlat, inputDescription.value); feature.marker.feature = null; feature.destroy(); return false; };
el1 = document.createElement("dl");
el2 = document.createElement("dt");
el2.appendChild(document.createTextNode(OpenLayers.i18n("Nickname")));
el1.appendChild(el2);
el2 = document.createElement("dd");
var inputUsername = document.createElement("input");
inputUsername.value = this.osbLayer.username;
inputUsername.className = "osbUsername";
inputUsername.onkeyup = function(){ control.osbLayer.setUserName(inputUsername.value); };
el2.appendChild(inputUsername);
el1.appendChild(el2);
el2 = document.createElement("dt");
el2.appendChild(document.createTextNode(OpenLayers.i18n("Bug description")));
el1.appendChild(el2);
el2 = document.createElement("dd");
var inputDescription = document.createElement("input");
el2.appendChild(inputDescription);
el1.appendChild(el2);
el_form.appendChild(el1);
el1 = document.createElement("div");
el2 = document.createElement("input");
el2.setAttribute("type", "submit");
el2.value = OpenLayers.i18n("Create");
el1.appendChild(el2);
el_form.appendChild(el1);
newContent.appendChild(el_form);
feature.data.popupContentHTML = newContent;
var popup = feature.createPopup(true);
popup.events.register("close", this, function(){ feature.destroy(); });
this.map.addPopup(popup);
popup.updateSize();
},
CLASS_NAME: "OpenLayers.Control.OpenStreetBugs"
});
/**
* This class changes the usual OpenLayers.Popup.FramedCloud class by using a DOM element instead of an innerHTML string as content for the popup.
* This is necessary for creating valid onclick handlers that still work with multiple OpenStreetBugs layer objects.
*/
OpenLayers.Popup.FramedCloud.OpenStreetBugs = new OpenLayers.Class(OpenLayers.Popup.FramedCloud, {
contentDom : null,
autoSize : true,
/**
* See OpenLayers.Popup.FramedCloud.initialize() for parameters. As fourth parameter, pass a DOM node instead of a string.
*/
initialize: function() {
this.displayClass = this.displayClass + " " + this.CLASS_NAME.replace("OpenLayers.", "ol").replace(/\./g, "");
var args = new Array(arguments.length);
for(var i=0; i<arguments.length; i++)
args[i] = arguments[i];
// Unset original contentHTML parameter
args[3] = null;
var closeCallback = arguments[6];
// Add close event trigger to the closeBoxCallback parameter
args[6] = function(e){ if(closeCallback) closeCallback(); else this.hide(); OpenLayers.Event.stop(e); this.events.triggerEvent("close"); };
OpenLayers.Popup.FramedCloud.prototype.initialize.apply(this, args);
this.events.addEventType("close");
this.setContentHTML(arguments[3]);
},
/**
* Like OpenLayers.Popup.FramedCloud.setContentHTML(), but takes a DOM element as parameter.
*/
setContentHTML: function(contentDom) {
if(contentDom != null)
this.contentDom = contentDom;
if(this.contentDiv == null || this.contentDom == null || this.contentDom == this.contentDiv.firstChild)
return;
while(this.contentDiv.firstChild)
this.contentDiv.removeChild(this.contentDiv.firstChild);
this.contentDiv.appendChild(this.contentDom);
// Copied from OpenLayers.Popup.setContentHTML():
if(this.autoSize)
{
this.registerImageListeners();
this.updateSize();
}
},
destroy: function() {
this.contentDom = null;
OpenLayers.Popup.FramedCloud.prototype.destroy.apply(this, arguments);
},
CLASS_NAME: "OpenLayers.Popup.FramedCloud.OpenStreetBugs"
});
/**
* Necessary improvement to the translate function: Fall back to default language if translated string is not
* available (see http://trac.openlayers.org/ticket/2308).
*/
OpenLayers.i18n = OpenLayers.Lang.translate = function(key, context) {
var message = OpenLayers.Lang[OpenLayers.Lang.getCode()][key];
if(!message)
{
if(OpenLayers.Lang[OpenLayers.Lang.defaultCode][key])
message = OpenLayers.Lang[OpenLayers.Lang.defaultCode][key];
else
message = key;
}
if(context)
message = OpenLayers.String.format(message, context);
return message;
};
/**
* This global function is executed by the OpenStreetBugs API getBugs script.
* Each OpenStreetBugs layer adds itself to the putAJAXMarker.layer array. The putAJAXMarker() function executes the createMarker() method
* on each layer in that array each time it is called. This has the side-effect that bugs displayed in one map on a page are already loaded
* on the other map as well.
*/
function putAJAXMarker(id, lon, lat, text, closed)
{
var comments = text.split(/<hr \/>/);
for(var i=0; i<comments.length; i++)
comments[i] = comments[i].replace(/"/g, "\"").replace(/</g, "<").replace(/>/g, ">").replace(/&/g, "&");
putAJAXMarker.bugs[id] = [
new OpenLayers.LonLat(lon, lat),
comments,
closed
];
for(var i=0; i<putAJAXMarker.layers.length; i++)
putAJAXMarker.layers[i].createMarker(id);
}
/**
* This global function is executed by the OpenStreetBugs API. The “create bug”, “comment” and “close bug” scripts execute it to give information about their success.
* In case of success, this function is called without a parameter, in case of an error, the error message is passed. This is lousy workaround to make it any functional at all, the OSB API is likely to be extended later (then it will provide additional information such as the ID of a created bug and similar).
*/
function osbResponse(error)
{
if(error)
alert("Error: "+error);
for(var i=0; i<putAJAXMarker.layers.length; i++)
putAJAXMarker.layers[i].loadBugs();
}
putAJAXMarker.layers = [ ];
putAJAXMarker.bugs = { };
/* Translations */
OpenLayers.Lang.en = OpenLayers.Util.extend(OpenLayers.Lang.en, {
"Fixed Error" : "Fixed Error",
"Unresolved Error" : "Unresolved Error",
"Description" : "Description",
"Comment" : "Comment",
"Has been fixed." : "This error has been fixed already. However, it might take a couple of days before the map image is updated.",
"Comment/Close" : "Comment/Close",
"Nickname" : "Nickname",
"Add comment" : "Add comment",
"Mark as fixed" : "Mark as fixed",
"Cancel" : "Cancel",
"Create OpenStreetBug" : "Create OpenStreetBug",
"Create bug" : "Create bug",
"Bug description" : "Bug description",
"Create" : "Create",
"Permalink" : "Permalink",
"Zoom" : "Zoom"
});
OpenLayers.Lang.de = OpenLayers.Util.extend(OpenLayers.Lang.de, {
"Fixed Error" : "Behobener Fehler",
"Unresolved Error" : "Offener Fehler",
"Description" : "Beschreibung",
"Comment" : "Kommentar",
"Has been fixed." : "Der Fehler wurde bereits behoben. Es kann jedoch bis zu einigen Tagen dauern, bis die Kartenansicht aktualisiert wird.",
"Comment/Close" : "Kommentieren/Schließen",
"Nickname" : "Benutzername",
"Add comment" : "Kommentar hinzufügen",
"Mark as fixed" : "Als behoben markieren",
"Cancel" : "Abbrechen",
"Create OpenStreetBug" : "OpenStreetBug melden",
"Create bug" : "Bug anlegen",
"Bug description" : "Fehlerbeschreibung",
"Create" : "Anlegen",
"Permalink" : "Permalink",
"Zoom" : "Zoom"
});
OpenLayers.Lang.pl = OpenLayers.Util.extend(OpenLayers.Lang.pl, {
"Fixed Error" : "Naprawiony błąd",
"Unresolved Error" : "Nierozwiązany błąd",
"Description" : "Opis",
"Comment" : "Komentarz",
"Has been fixed." : "Ten problem został już rozwiązany. Jednakże, może to potrwać kilka dni, zanim obraz mapy zostanie zaktualizowany.",
"Comment/Close" : "Skomentuj/zamknij",
"Nickname" : "Nick",
"Add comment" : "Dodaj komentarz",
"Mark as fixed" : "Oznacz jako rozwiązany",
"Cancel" : "Anuluj",
"Create OpenStreetBug" : "Stwórz OpenStreetBug",
"Create bug" : "Stwórz problem",
"Bug description" : "Opis problemu",
"Create" : "Stwórz",
"Permalink" : "Stały link",
"Zoom" : "Przybliż"
});
OpenLayers.Lang.fr = OpenLayers.Util.extend(OpenLayers.Lang.fr, {
"Fixed Error" : "Erreur corrigée",
"Unresolved Error" : "Erreur non corrigée",
"Description" : "Description",
"Comment" : "Commentaire",
"Has been fixed." : "Cette erreur a déjà été corrigée. Cependant, il peut être nécessaire d'attendre quelques jours avant que l'image de la carte ne soit mise à jour.",
"Comment/Close" : "Commenter/Fermer",
"Nickname" : "Surnom",
"Add comment" : "Ajouter un commentaire",
"Mark as fixed" : "Marquer comme corrigé",
"Cancel" : "Annuler",
"Create OpenStreetBug" : "Créer OpenStreetBug",
"Create bug" : "Ajouter un bug",
"Bug description" : "Description du bug",
"Create" : "Créer",
"Permalink" : "Lien permanent",
"Zoom" : "Zoom"
});
OpenLayers.Lang.nl = OpenLayers.Util.extend(OpenLayers.Lang.nl, {
"Fixed Error" : "Fout verholpen",
"Unresolved Error" : "Openstaande fout",
"Description" : "Beschrijving",
"Comment" : "Kommentaar",
"Has been fixed." : "De fout is al eerder opgelost. Het kan echter nog een paar dagen duren voordat het kaartmateriaal geactualiseerd is.",
"Comment/Close" : "Bekommentariëren/Sluiten",
"Nickname" : "Gebruikersnaam",
"Add comment" : "Kommentaar toevoegen",
"Mark as fixed" : "Als opgelost aanmerken",
"Cancel" : "Afbreken",
"Create OpenStreetBug" : "OpenStreetBug melden",
"Create bug" : "Bug melden",
"Bug description" : "Foutomschrijving",
"Create" : "Aanmaken",
"Permalink" : "Permalink",
"Zoom" : "Zoom"
});
OpenLayers.Lang.it = OpenLayers.Util.extend(OpenLayers.Lang.it, {
"Fixed Error" : "Sbaglio coretto",
"Unresolved Error" : "Sbaglio non coretto",
"Description" : "Descrizione",
"Comment" : "Commento",
"Has been fixed." : "Questo sbaglio è già coretto. Forse ci metto qualche giorni per aggiornare anche i quadri.",
"Comment/Close" : "Commenta/Chiude",
"Nickname" : "Nome",
"Add comment" : "Aggiunge commento",
"Mark as fixed" : "Marca che è coretto",
"Cancel" : "Annulla",
"Create OpenStreetBug" : "Aggiunge OpenStreetBug",
"Create bug" : "Aggiunge un sbaglio",
"Bug description" : "Descrizione del sbaglio",
"Create" : "Aggiunge",
"Permalink" : "Permalink",
"Zoom" : "Zoom"
});
OpenLayers.Lang.ro = OpenLayers.Util.extend(OpenLayers.Lang.ro, {
"Fixed Error" : "Eroare rezolvată",
"Unresolved Error" : "Eroare nerezolvată",
"Description" : "Descriere",
"Comment" : "Comentariu",
"Has been fixed." : "Această eroare a fost rezolvată. Totuși este posibil să dureze câteva zile până când imaginea hărții va fi actualizată.",
"Comment/Close" : "Comentariu/Închide",
"Nickname" : "Nume",
"Add comment" : "Adaugă comentariu",
"Mark as fixed" : "Marchează ca rezolvată",
"Cancel" : "Anulează",
"Create OpenStreetBug" : "Crează OpenStreetBug",
"Create bug" : "Adaugă eroare",
"Bug description" : "Descrierea erorii",
"Create" : "Adaugă",
"Permalink" : "Permalink",
"Zoom" : "Zoom"
});
OpenLayers.Lang.hu = OpenLayers.Util.extend(OpenLayers.Lang.hu, {
"Fixed Error" : "Javított hiba",
"Unresolved Error" : "Megoldatlan hiba",
"Description" : "Leírás",
"Comment" : "Megjegyzés",
"Has been fixed." : "Ezt a hibát már javították, azonban eltarthat néhány napig, mire a térkép frissül.",
"Comment/Close" : "Megjegyzés/Bezárás",
"Nickname" : "Becenév",
"Add comment" : "Megjegyzés hozzáadása",
"Mark as fixed" : "Jelölés javítottként",
"Cancel" : "Mégse",
"Create OpenStreetBug" : "OpenStreetBug létrehozása",
"Create bug" : "Hiba létrehozása",
"Bug description" : "Hiba leírása",
"Create" : "Létrehozás",
"Permalink" : "Permalink",
"Zoom" : "Nagyítás"
});
OpenLayers.Lang.es = OpenLayers.Util.extend(OpenLayers.Lang.es, {
"Fixed Error" : "Error Corregido",
"Unresolved Error" : "Error sin corregir",
"Description" : "Descripción",
"Comment" : "Comentario",
"Has been fixed." : "Este error ya ha sido corregido. De todas formas, puede que tarde un par de días antes de que la imagen del mapa se actualice.",
"Comment/Close" : "Comentario/Cerrado",
"Nickname" : "Nombre usuario",
"Add comment" : "Añadir comentario",
"Mark as fixed" : "Marcar como corregido",
"Cancel" : "Cancelar",
"Create OpenStreetBug" : "Crear OpenStreetBug",
"Create bug" : "Crear bug",
"Bug description" : "Descripcion del bug",
"Create" : "Crear",
"Permalink" : "Permalink",
"Zoom" : "Zoom"