annotate media/js/membermap.js @ 174:e9c718065a25

Ticket #58; use Django 1.2's cached template loader.
author Brian Neal <bgneal@gmail.com>
date Wed, 03 Mar 2010 04:04:11 +0000
parents 13d052fbe4f1
children 4e1abeb593c2
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.');
gremmie@1 80 return;
gremmie@1 81 }
bgneal@150 82 $.ajax({
bgneal@150 83 url: '/member_map/add/',
bgneal@150 84 type: 'POST',
bgneal@150 85 data: {
bgneal@150 86 loc : address,
bgneal@150 87 lat : point.lat(),
bgneal@150 88 lon : point.lng(),
bgneal@150 89 msg : $('#id_message').val()
gremmie@1 90 },
bgneal@150 91 dataType: 'json',
bgneal@150 92 success: function(data, textStatus) {
gremmie@1 93 var wasOnMap = mmap.userOnMap;
gremmie@1 94 if (mmap.userOnMap)
gremmie@1 95 {
gremmie@1 96 mmap.map.removeOverlay(mmap.users[mmapUser.userName].marker);
gremmie@1 97 }
gremmie@1 98 else
gremmie@1 99 {
gremmie@1 100 $('#member_map_members').append($('<option />').html(data.name));
bgneal@153 101 $('#member_map_recent').append($('<option />').html(data.name));
gremmie@1 102 mmap.userOnMap = true;
gremmie@1 103 deleteButton.removeAttr('disabled').val('Delete');
gremmie@1 104 }
gremmie@1 105 var marker = new GMarker(new GLatLng(data.lat, data.lon));
gremmie@1 106 marker.bindInfoWindowHtml(data.message);
gremmie@1 107 mmap.map.addOverlay(marker);
gremmie@1 108 mmap.users[mmapUser.userName] = data;
gremmie@1 109 mmap.users[mmapUser.userName].marker = marker;
gremmie@1 110 mmap.clickUser(mmapUser.userName);
gremmie@1 111 submitButton.removeAttr('disabled').val('Update');
gremmie@1 112 $('#member_map_directions').html(mmap.onMapDir);
bgneal@153 113 $('#member_map_count').html($('#member_map_members')[0].length - 1);
gremmie@1 114 alert(wasOnMap ? "Your location has been updated!" :
gremmie@1 115 "You've been added to the map!");
gremmie@1 116 },
bgneal@150 117 error: function (xhr, textStatus, ex) {
bgneal@150 118 alert('Oops, an error occurred. ' + xhr.statusText + ' - ' +
bgneal@150 119 xhr.responseText);
bgneal@150 120 }
bgneal@150 121 });
gremmie@1 122 });
gremmie@1 123 }
gremmie@1 124 return false;
gremmie@1 125 });
gremmie@1 126
gremmie@1 127 deleteButton.click(function() {
gremmie@1 128 deleteButton.attr('disabled', 'disabled').val('Deleting...');
bgneal@150 129 $.ajax({
bgneal@150 130 url: '/member_map/delete/',
bgneal@150 131 type: 'POST',
bgneal@150 132 dataType: 'text',
bgneal@150 133 success: function(data, textStatus) {
gremmie@1 134 $('#id_location').val('');
gremmie@1 135 $('#id_message').val('');
gremmie@1 136 $("#member_map_members option[value='" + mmapUser.userName + "']").remove();
gremmie@1 137 $("#member_map_recent option[value='" + mmapUser.userName + "']").remove();
gremmie@1 138 mmap.map.removeOverlay(mmap.users[mmapUser.userName].marker);
gremmie@1 139 mmap.users[mmapUser.userName].marker = null;
gremmie@1 140 mmap.users[mmapUser.userName] = null;
gremmie@1 141 mmap.userOnMap = false;
gremmie@1 142 deleteButton.val('Delete');
gremmie@1 143 submitButton.removeAttr('disabled').val('Add');
gremmie@1 144 $('#member_map_directions').html(mmap.offMapDir);
bgneal@153 145 $('#member_map_count').html($('#member_map_members')[0].length - 1);
gremmie@1 146 alert("You've been removed from the map.");
gremmie@1 147 },
bgneal@150 148 error: function (xhr, textStatus, ex) {
bgneal@150 149 alert('Oops, an error occurred. ' + xhr.statusText + ' - ' +
bgneal@150 150 xhr.responseText);
bgneal@150 151 }
bgneal@150 152 });
gremmie@1 153 return false;
gremmie@1 154 });
gremmie@1 155
gremmie@1 156 if (mmap.userOnMap)
gremmie@1 157 {
gremmie@1 158 submitButton.val('Update');
gremmie@1 159 $('#member_map_directions').html(mmap.onMapDir);
gremmie@1 160 }
gremmie@1 161 else
gremmie@1 162 {
gremmie@1 163 submitButton.val('Add');
gremmie@1 164 deleteButton.attr('disabled', 'disabled');
gremmie@1 165 $('#member_map_directions').html(mmap.offMapDir);
gremmie@1 166 }
gremmie@1 167 });
gremmie@1 168 $('#member_map_members').change(mmap.userClick);
gremmie@1 169 $('#member_map_recent').change(mmap.userClick);
gremmie@1 170 }
gremmie@1 171 }
gremmie@1 172 });