annotate media/js/membermap.js @ 201:bc958bc3c6eb

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