annotate media/js/membermap.js @ 150:b43e1288ff80

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