annotate gpp/membermap/static/js/membermap.js @ 491:7dbdbb08e68c

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