comparison membermap/static/js/membermap.js @ 581:ee87ea74d46b

For Django 1.4, rearranged project structure for new manage.py.
author Brian Neal <bgneal@gmail.com>
date Sat, 05 May 2012 17:10:48 -0500
parents gpp/membermap/static/js/membermap.js@88b2b9cb8c1f
children 6164cc091649
comparison
equal deleted inserted replaced
580:c525f3e0b5d0 581:ee87ea74d46b
1 var mmap = {
2 map: null,
3 geocoder: null,
4 users: Object,
5 userOnMap: false,
6 userClick: function() {
7 var name = $('option:selected', this).text();
8 if (name != mmap.selectText)
9 {
10 mmap.clickUser(name);
11 }
12 },
13 clickUser: function(name) {
14 pt = new GLatLng(mmap.users[name].lat, mmap.users[name].lon);
15 mmap.map.setCenter(pt);
16 mmap.users[name].marker.openInfoWindowHtml(mmap.users[name].message);
17 },
18 clear: function() {
19 mmap.users.length = 0;
20 },
21 selectText: "(select)",
22 onMapDir: 'You have previously added yourself to the member map. Your information appears below. You may change ' +
23 'the information if you wish. To delete yourself from the map, click the Delete button.',
24 offMapDir: 'Your location is not on the map. If you would like to appear on the map, please fill out the form below ' +
25 'and click the Submit button.'
26 };
27 $(document).ready(function() {
28 if (GBrowserIsCompatible())
29 {
30 $(window).unload(GUnload);
31 mmap.map = new GMap2($('#member_map_map')[0]);
32 mmap.map.setCenter(new GLatLng(15.0, -30.0), 2);
33 mmap.map.enableScrollWheelZoom();
34 mmap.map.addControl(new GLargeMapControl());
35 mmap.map.addControl(new GMapTypeControl());
36 mmap.geocoder = new GClientGeocoder();
37
38 if (mmapUser.userName)
39 {
40 $.getJSON('/member_map/query/',
41 function(data) {
42 mmap.map.clearOverlays();
43 var sel = $('#member_map_members');
44 sel[0].length = 0;
45 sel.append($('<option />').html(mmap.selectText));
46 mmap.clear();
47 $.each(data.users, function(i, item) {
48 sel.append($('<option />').html(item.name));
49 var marker = new GMarker(new GLatLng(item.lat, item.lon));
50 marker.bindInfoWindowHtml(item.message);
51 mmap.map.addOverlay(marker);
52 mmap.users[item.name] = item;
53 mmap.users[item.name].marker = marker;
54 if (mmapUser.userName == item.name)
55 {
56 mmap.userOnMap = true;
57 }
58 });
59 $('#member_map_count').html(data.users.length);
60
61 sel = $('#member_map_recent');
62 sel[0].length = 0;
63 sel.append($('<option />').html(mmap.selectText));
64 $.each(data.recent, function(i, item) {
65 sel.append($('<option />').html(item));
66 });
67 var submitButton = $('#member_map_submit');
68 var deleteButton = $('#member_map_delete');
69
70 submitButton.click(function() {
71 if (mmap.geocoder)
72 {
73 $(this).attr('disabled', 'disabled').val('Updating Map...');
74 var address = $('#id_location').val();
75 mmap.geocoder.getLatLng(address,
76 function(point) {
77 if (!point)
78 {
79 alert(address + ' could not be found on Google Maps.');
80 submitButton.removeAttr('disabled').val('Update');
81 return;
82 }
83 $.ajax({
84 url: '/member_map/add/',
85 type: 'POST',
86 data: {
87 loc : address,
88 lat : point.lat(),
89 lon : point.lng(),
90 msg : $('#id_message').val()
91 },
92 dataType: 'json',
93 success: function(data, textStatus) {
94 var wasOnMap = mmap.userOnMap;
95 if (mmap.userOnMap)
96 {
97 mmap.map.removeOverlay(mmap.users[mmapUser.userName].marker);
98 }
99 else
100 {
101 $('#member_map_members').append($('<option />').html(data.name));
102 $('#member_map_recent').append($('<option />').html(data.name));
103 mmap.userOnMap = true;
104 deleteButton.removeAttr('disabled').val('Delete');
105 }
106 var marker = new GMarker(new GLatLng(data.lat, data.lon));
107 marker.bindInfoWindowHtml(data.message);
108 mmap.map.addOverlay(marker);
109 mmap.users[mmapUser.userName] = data;
110 mmap.users[mmapUser.userName].marker = marker;
111 mmap.clickUser(mmapUser.userName);
112 submitButton.removeAttr('disabled').val('Update');
113 $('#member_map_directions').html(mmap.onMapDir);
114 $('#member_map_count').html($('#member_map_members')[0].length - 1);
115 alert(wasOnMap ? "Your location has been updated!" :
116 "You've been added to the map!");
117 },
118 error: function (xhr, textStatus, ex) {
119 alert('Oops, an error occurred. ' + xhr.statusText + ' - ' +
120 xhr.responseText);
121 }
122 });
123 });
124 }
125 return false;
126 });
127
128 deleteButton.click(function() {
129 deleteButton.attr('disabled', 'disabled').val('Deleting...');
130 $.ajax({
131 url: '/member_map/delete/',
132 type: 'POST',
133 dataType: 'text',
134 success: function(data, textStatus) {
135 $('#id_location').val('');
136 $('#id_message').val('');
137 $("#member_map_members option[value='" + mmapUser.userName + "']").remove();
138 $("#member_map_recent option[value='" + mmapUser.userName + "']").remove();
139 mmap.map.removeOverlay(mmap.users[mmapUser.userName].marker);
140 mmap.users[mmapUser.userName].marker = null;
141 mmap.users[mmapUser.userName] = null;
142 mmap.userOnMap = false;
143 deleteButton.val('Delete');
144 submitButton.removeAttr('disabled').val('Add');
145 $('#member_map_directions').html(mmap.offMapDir);
146 $('#member_map_count').html($('#member_map_members')[0].length - 1);
147 alert("You've been removed from the map.");
148 },
149 error: function (xhr, textStatus, ex) {
150 alert('Oops, an error occurred. ' + xhr.statusText + ' - ' +
151 xhr.responseText);
152 }
153 });
154 return false;
155 });
156
157 if (mmap.userOnMap)
158 {
159 submitButton.val('Update');
160 $('#member_map_directions').html(mmap.onMapDir);
161 }
162 else
163 {
164 submitButton.val('Add');
165 deleteButton.attr('disabled', 'disabled');
166 $('#member_map_directions').html(mmap.offMapDir);
167 }
168 });
169 $('#member_map_members').change(mmap.userClick);
170 $('#member_map_recent').change(mmap.userClick);
171 }
172 }
173 });