initial commit
[map.git] / honeymap-master / static / main.js.bak
1 /** config **/
2 var markers_visible_max = 150;
3 var markersrc_color = { fill: 'red', stroke: 'darkred' };
4 var markerdst_color = { fill: '#F8E23B', stroke: '#383F47' };
5
6 var regionhits = {};
7 var regionhits_countonly = {};
8 var markerhits = {};
9 var markercaptions = {};
10 var markers_total = 0;
11
12 function marker_animation(x, y, css) {
13   $("#world-map").append(
14     $('<div class="marker_animation ' + css + '"></div>')
15     .css('left', x + 'px')
16     .css('top', y + 'px')
17     .css({ opacity: 1, scale: 0 })
18     .transition({ opacity: 0, scale: 1 }, 1000, 'linear', function(){ $(this).remove(); })
19   );
20 }
21
22 function marker_animation_ll(lat, lng, css) {
23   var xy = mapobj.latLngToPoint(lat, lng);
24   marker_animation(xy.x, xy.y, css);
25 }
26
27 function get_regioncode(x, y) {
28   // HACKHACKHACK
29   var efp = $(document.elementFromPoint(x + $("#world-map").offset().left, y + $("#world-map").offset().top));
30   if(efp.is('path')) {
31     return efp.attr('data-code');
32   } else if(efp.is('circle') || (efp.is('div') && efp.hasClass('marker_animation'))) {
33     // This is as ugly as it gets. If we hit an existing marker, make it invisible,
34     // look again and then make it visible again.
35     efp.hide();
36     var rc = get_regioncode(x, y);
37     efp.show();
38     return rc;
39   } else {
40     return null;
41   }
42 }
43
44 function get_regioncode_ll(lat, lng) {
45   var xy = mapobj.latLngToPoint(lat, lng);
46   return get_regioncode(xy.x, xy.y);
47 }
48
49 function get_regionname_ll(lat, lng) {
50   var code = get_regioncode_ll(lat,lng);
51   return code ? mapobj.getRegionName(code) : null;
52 }
53
54 function add_log(msg) {
55   // remove old log entries from time to time
56   entries = $("#log div.log_entry");
57   if(entries.length > markers_visible_max) {
58     // dont remove them all, only a few, so we dont see them disappear
59     entries.slice(0, markers_visible_max/2).remove();
60     // remove abandoned line breaks
61     $('#log br').nextUntil('div.log_entry', 'br').remove();
62   }
63   // only automatically scroll down if the user did not manually scroll up before :-)
64   var scroll = $('#log').scrollTop() + $('#log').innerHeight() == $('#log')[0].scrollHeight;
65   $('#log').append('<div class="log_entry">' + msg + '</div><br/>');
66   if(scroll) {
67     $("#log").scrollTop($("#log")[0].scrollHeight);
68   }
69 }
70
71 function remove_oldest_marker() {
72   // only remove src markers
73   toremove = $($("#world-map svg g circle.jvectormap-marker[fill=" + markersrc_color.fill + "]")[0]);
74   par = toremove.parent();
75   mapobj.removeMarkers( [ toremove.attr('data-index') ]);
76   par.remove(); // Remove parent node too (jVectorMap does not do this by itself)
77 }
78
79 function add_marker_ll(lat, lng, type, eventname, region) {
80   if(eventname == null) { eventname = "other"; }
81   if(type == null) { type = 'src'; }
82   if(type == 'src') {
83     // only count src markers which are within a valid region
84     if(region == null) { region = get_regioncode_ll(lat, lng); }
85     if(region != null) {
86       if(regionhits[region] == null) { regionhits[region] = {}; regionhits_countonly[region] = 0; }
87       if(regionhits[region][eventname] == null) { regionhits[region][eventname] = 0; }
88       regionhits[region][eventname]++;
89       regionhits_countonly[region]++;
90     }
91   }
92   var markerkey = lat + "," + lng;
93   if(markerhits[markerkey] == null) { markerhits[markerkey] = {}; }
94   if(markerhits[markerkey][eventname] == null) { markerhits[markerkey][eventname] = 0; }
95   markerhits[markerkey][eventname]++;
96   marker_animation_ll(lat, lng, type == 'dst' ? 'markerdst' : 'markersrc');
97   // only add markers which do not exist yet
98   if(mapobj.markers[markerkey] == null) {
99     if(markers_total >= markers_visible_max) {
100       remove_oldest_marker();
101     }
102     if(type == 'dst') {
103       mapobj.addMarker(markerkey, {
104        latLng: [ lat, lng ], name: "(" + lat + ", " + lng + ")",
105        style: markerdst_color
106       }, []);
107     } else {
108       mapobj.addMarker(markerkey, {
109        latLng: [ lat, lng ], name: "(" + lat + ", " + lng + ")"
110       }, []);
111     }
112     markers_total++;
113   }
114 }
115
116 function update_regioncolors() {
117   // Force recomputation of min and max for correct color scaling
118   mapobj.series.regions[0].params.min = null;
119   mapobj.series.regions[0].params.max = null;
120   // Update data
121   mapobj.series.regions[0].setValues(regionhits_countonly);
122 }