Skip to content

Commit

Permalink
[#113] Better in-code documentation and code cleanup
Browse files Browse the repository at this point in the history
Add more comments describing the calling aPI for the map template tag
and some comments on the inner  workings of the different parts

Get rid of warnings using jslint

Remove old code
  • Loading branch information
zzgvh committed Dec 5, 2012
1 parent 73f4147 commit 997cce3
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 50 deletions.
38 changes: 19 additions & 19 deletions akvo/mediaroot/akvo/js/src/akvo-maps.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@
// Django template tag. Each map element will have a div element and
// a JavaScript literal with data which are used to create a map with
// corresponding locations from the RSR API
// There are currently three kinds of maps:
// "static" is used on the RSR home page where we want a world map with small markers
// "small" is used for the single object maps for projects and organisations
// "dynamic" are the large all-object maps for projects and organisations

var addMap, addPin, populateMap, mapOptions, prepareNextRequest, getResourceUrl;

Expand Down Expand Up @@ -41,25 +45,18 @@
getResourceUrl = function (map) {
var opts, url, limit;
opts = map.mapOpts;
//TODO: derive the host from the current page URL instead maybe?
url = opts.host + 'api/v1/' + opts.resource + '/';
//limit = 0 means all objects. If this becomes too heavy limit can be set to get the objects in multiple chunks
limit = 0;
// if object_id holds a value then that's the ID of the object we want to fetch
if (opts.object_id) {
url += opts.object_id + '/?format=jsonp&depth=1&callback=?'
url += opts.object_id + '/?format=jsonp&depth=1&callback=?';
// otherwise we want all objects of the resource's type
} else {
url += '?format=jsonp&limit=' + limit + '&callback=?'
url += '?format=jsonp&limit=' + limit + '&callback=?';
}
return url;

// if (map.mapOpts.objectType === 'project') {
// return url + 'project/' + map.mapOpts.object + '/?format=jsonp&depth=1&callback=?';
// } else if (map.mapOpts.objectType === 'organisation') {
// return url + 'organisation/' + map.mapOpts.object + '/?format=jsonp&depth=1&callback=?';
// } else if (map.mapOpts.objectType === 'projects') {
// return url + 'project/?format=jsonp&limit=' + limit + '&callback=?';
// } else if (map.mapOpts.objectType === 'organisations') {
// return url + 'organisation/?format=jsonp&limit=' + limit + '&callback=?';
// }
};


Expand Down Expand Up @@ -89,10 +86,9 @@
'</a>' +
'</div>' +
'{{/if}}' +
'</div>'
);
'</div>');

// populate the location object with data from the Organisation
// populate the location object with data from an Organisation model object
addOrganisationData = function (object, location) {
location.url = object.absolute_url;
location.title = object.name;
Expand All @@ -102,7 +98,7 @@
return location;
};

// populate the location object with data from the Project
// populate the location object with data from a Project model object
addProjectData = function (object, location) {
location.url = object.absolute_url;
location.title = object.title;
Expand Down Expand Up @@ -152,6 +148,7 @@
opts = map.mapOpts;
marker = opts.host + 'rsr/media/core/img/';

// get a custom marker if there is one, otherwise it's red for organisations and blue for projects
if (opts.marker_icon) {
marker = marker + opts.marker_icon;
} else if (opts.resource === 'organisation') {
Expand All @@ -161,6 +158,7 @@
}

if (opts.type === 'static' || opts.type === 'small') {
// shrink the marker for "static" maps
if (opts.type === 'static') {
marker = new google.maps.MarkerImage(marker, null, null, null, new google.maps.Size(10.5, 17));
}
Expand All @@ -170,9 +168,11 @@
'icon': marker,
'bounds': true
});
// if the map is zoomed in a lot we zoom out a bit
if ($(map.mapElement).gmap('get', 'map').getZoom() > 8) {
$(map.mapElement).gmap('get', 'map').setZoom(8)
$(map.mapElement).gmap('get', 'map').setZoom(8);
}
// "dynamic"
} else {
$(map.mapElement).gmap('addMarker', {
'position': new google.maps.LatLng(location.latitude, location.longitude),
Expand Down Expand Up @@ -204,10 +204,10 @@
});
};


// Static or dynamic map options
mapOptions = function (mapType) {
var options;
// "static" and "small" are set to zoom 0 to begin with so that you see a world map until it has been populated
// scroll wheel zoom is only enabled for the large "dynamic" maps
if (mapType === 'static') {
options = {
'draggable': false,
Expand Down
45 changes: 15 additions & 30 deletions akvo/rsr/templatetags/map.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import os
from django import template
from django.conf import settings
from django.http import Http404

from akvo.rsr.models import Project, Organisation

Expand All @@ -18,24 +19,22 @@
#PROJECT_MARKER_ICON = getattr(settings, 'GOOGLE_MAPS_PROJECT_MARKER_ICON', '')
#ORGANISATION_MARKER_ICON = getattr(settings, 'GOOGLE_MAPS_ORGANISATION_MARKER_ICON', '')

#RAW_HOST = getattr(settings, 'DOMAIN_NAME', 'akvo.org')
#
## Production server uses leading 'www'
#if RAW_HOST == 'akvo.org':
# HOST = 'http://www.akvo.org/'
#else:
# HOST = 'http://%s/' % RAW_HOST

# TODO: this will break on partner sites I think
# TODO: this should be fixed so partner sites use their own domain
HOST = 'http://%s/' % getattr(settings, 'DOMAIN_NAME', 'akvo.org')

@register.inclusion_tag('inclusion_tags/map.html')
def map(resource, width, height, type="dynamic", marker_icon=""):

is_project = isinstance(object, Project)
is_organisation = isinstance(object, Organisation)
is_all_projects = isinstance(object, basestring) and (object == 'projects')
is_all_organisations = isinstance(object, basestring) and (object == 'organisations')
"""
Generic google map inclusion tag tha ses the RSR api to fetch data
params:
resource: the name of the resource as a string or an object of that resource's model
currently "project" and "organisation" can be used
width, height: the dimensions of the map
type: there are three types currently: "dynamic", "static" and "small" the two first are used for global maps
the "small" map is used for a single project or organisation's marker(s)
marker_icon: the name of an icon to use rather than the default. the icon must be in mediaroot/core/img/
"""

# We want a unique id for each map element id
map_id = 'akvo_map_%s' % os.urandom(8).encode('hex')
Expand All @@ -47,27 +46,13 @@ def map(resource, width, height, type="dynamic", marker_icon=""):
'host': HOST,
'marker_icon': marker_icon,
}

#extend this to support more models that have a location
supported_models = (Project, Organisation)

if isinstance(resource, basestring):
# determine if we have the name of the resource or an object of that kind
if isinstance(resource, basestring) and resource in [model_name.__name__.lower() for model_name in supported_models]:
template_context['resource'] = resource
elif isinstance(resource, supported_models):
template_context['resource'] = resource.__class__.__name__.lower()
template_context['object_id'] = resource.id
return template_context


# if is_project:
# template_context['object'] = object.id
# template_context['objectType'] = 'project'
# elif is_all_projects:
# template_context['object'] = object
# template_context['objectType'] = 'projects'
# elif is_organisation:
# template_context['object'] = object.id
# template_context['objectType'] = 'organisation'
# elif is_all_organisations:
# template_context['object'] = object
# template_context['objectType'] = 'organisations'
# return template_context
2 changes: 1 addition & 1 deletion akvo/templates/rsr/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ <h1 class="white last" style="line-height: 1em">&euro; {{projects_budget}}M</h1>
<div class="span-6 first last">
<a href="{% url 'global_project_map' %}">
{# google_global_project_map 'static' 315 190 1 #}
{% map 'project' 315 190 'static' %}
{% map 'projecto' 315 190 'static' %}
</a>
</div>
<div style="padding: .5em .5em .7em .5em;">
Expand Down

0 comments on commit 997cce3

Please sign in to comment.