From 338e5a4ead69eb33877d2da04a7299f07ca79c9e Mon Sep 17 00:00:00 2001 From: Sanj Date: Mon, 20 Feb 2012 19:32:53 +0530 Subject: [PATCH] edit stops should fetch stops for area / route --- chaloBEST/mumbai/apiviews.py | 7 +++-- chaloBEST/mumbai/models.py | 11 ++++++++ chaloBEST/static/css/editstops.css | 27 +++++++++++++++++++ chaloBEST/static/js/editstops.js | 42 +++++++++++++++++++++++++++--- chaloBEST/templates/editstops.html | 8 +++--- 5 files changed, 86 insertions(+), 9 deletions(-) diff --git a/chaloBEST/mumbai/apiviews.py b/chaloBEST/mumbai/apiviews.py index 40caa52..10d3424 100644 --- a/chaloBEST/mumbai/apiviews.py +++ b/chaloBEST/mumbai/apiviews.py @@ -18,8 +18,11 @@ def area(request, slug): area = get_object_or_404_json(Area, slug=slug) stops = [stop.get_geojson() for stop in Stop.objects.filter(area=area)] return render_to_json_response({ - 'type': 'FeatureCollection', - 'features': stops + 'area': area.get_dict(), + 'stops': { + 'type': 'FeatureCollection', + 'features': stops + } }) def routes(request): diff --git a/chaloBEST/mumbai/models.py b/chaloBEST/mumbai/models.py index 5fc40d0..6529d05 100644 --- a/chaloBEST/mumbai/models.py +++ b/chaloBEST/mumbai/models.py @@ -51,6 +51,17 @@ class Area(models.Model): geometry = models.PolygonField(blank=True, null=True) alt_names = generic.GenericRelation("AlternativeName") + def get_dict(self): + return { + 'id': self.id, + 'code': self.code, + 'slug': self.slug, + 'name': self.name, + 'name_mr': self.name_mr, + 'display_name': self.display_name + #FIXME add alt_names and geometry + } + def get_absolute_url(self): return "/area/%s/" % self.name diff --git a/chaloBEST/static/css/editstops.css b/chaloBEST/static/css/editstops.css index e69de29..d51faa0 100644 --- a/chaloBEST/static/css/editstops.css +++ b/chaloBEST/static/css/editstops.css @@ -0,0 +1,27 @@ +html, body { + width: 100%; +} + +#wrapper { + width: 100%; +} + +#listsCol { + width: 24%; + float: left; +} + +#mapCol { + width: 50%; + float: left; +} + +#formCol { + width: 24%; + float: left; +} + +.listWrapper { + display: none; +} + diff --git a/chaloBEST/static/js/editstops.js b/chaloBEST/static/js/editstops.js index b255cc9..9cd8a4c 100644 --- a/chaloBEST/static/js/editstops.js +++ b/chaloBEST/static/js/editstops.js @@ -13,14 +13,16 @@ $(function() { $that.data("loaded", true); var $list = $('#' + name + 'List'); var url = API_BASE + name + "/"; - $.get(url, {}, function(items) { + var $loadingLi = $('
').text("Loading...").appendTo($list); + $.getJSON(url, {}, function(items) { + $loadingLi.remove(); $.each(items, function(i,v) { - var $li = $('
  • ') + var $li = $('
    ') .addClass("listItem") .text(v) .appendTo($list); }); - }, "json"); + }); } $('.listWrapper').hide(); @@ -29,4 +31,38 @@ $(function() { $that.addClass("selected"); }); + $('.list').click(function(e) { + var name = $(this).attr("id").replace("List", ""); + var $target = $(e.target); + if (!target.hasClass('list')) { + return; + } + if ($target.data("loading")) { + return; + } + if ($target.data("hasList")) { + $target.find(".stopList").toggle(); + return; + } + var url = API_BASE + name + "/" + $target.text(); + $target.data("loading", true); + $.getJSON(url, {}, function(area) { + var stops = area.stops.features; + var $stopsList = getStopsList(stops); + $target.append($stopsList); + $target.data("hasList", true); + $target.data("loading", false); + }); + }); + }); + +function getStopsList(stops) { + var $ul = $('
      ').addClass("stopsList"); + $.each(stops, function(i,v) { + var props = v.properties; + var geom = v.geometry; + var $li = $('
    • ').addClass("stopItem").data("slug", props.slug).data("geometry", geom).text(props.display_name).appendTo($ul); + }); + return $ul; +} diff --git a/chaloBEST/templates/editstops.html b/chaloBEST/templates/editstops.html index 9320288..6d28c6d 100644 --- a/chaloBEST/templates/editstops.html +++ b/chaloBEST/templates/editstops.html @@ -19,13 +19,13 @@
      -
        -
      +
      +
      -
        -
      +
      +