finish history state logic, implement it in python

This commit is contained in:
Laura Klünder 2017-10-31 15:28:49 +01:00
parent 620323b808
commit 91bf8b9652
4 changed files with 65 additions and 19 deletions

View file

@ -27,8 +27,12 @@ c3nav = {
c3nav.init_map(); c3nav.init_map();
$('.locationinput').data('location', null); $('.locationinput').data('location', null);
c3nav.update_state(false, true);
c3nav.update_map_state(true); var state = JSON.parse($('main').attr('data-state'));
history.replaceState(state, window.location.path);
c3nav.load_state(state, true);
c3nav._push_state(state, true);
if (!state.center) c3nav.update_map_state(true);
c3nav.init_locationinputs(); c3nav.init_locationinputs();
@ -97,7 +101,7 @@ c3nav = {
state = $.extend({}, c3nav.state, state); state = $.extend({}, c3nav.state, state);
var old_state = c3nav.state; var old_state = c3nav.state;
if (c3nav._equal_states(old_state, state)) return; if (!replace && c3nav._equal_states(old_state, state)) return;
var url; var url;
if (state.routing) { if (state.routing) {
@ -122,18 +126,20 @@ c3nav = {
// console.log('state popped'); // console.log('state popped');
c3nav.load_state(e.state); c3nav.load_state(e.state);
}, },
load_state: function (state) { load_state: function (state, nofly) {
c3nav._locationinput_set($('#origin-input'), state.origin); c3nav._locationinput_set($('#origin-input'), state.origin);
c3nav._locationinput_set($('#destination-input'), state.destination); c3nav._locationinput_set($('#destination-input'), state.destination);
c3nav._sidebar_state_updated(state); c3nav._sidebar_state_updated(state);
if (state.center) { if (state.center) {
c3nav._levelControl.setLevel(state.level); c3nav._levelControl.setLevel(state.level);
var center = c3nav.map._limitCenter(L.GeoJSON.coordsToLatLng(state.center), state.zoom, c3nav.map.options.maxBounds); var center = c3nav.map._limitCenter(L.GeoJSON.coordsToLatLng(state.center), state.zoom, c3nav.map.options.maxBounds);
c3nav.map.flyTo(center, state.zoom, { if (nofly) {
duration: 1 c3nav.map.setView(center, state.zoom, {animate: false});
}) } else {
c3nav.map.flyTo(center, state.zoom, {duration: 1});
} }
c3nav.state = state; }
$.extend(c3nav.state, state);
}, },
// button handlers // button handlers

View file

@ -20,7 +20,7 @@
<header> <header>
<h1>c3nav</h1> <h1>c3nav</h1>
</header> </header>
<main class="map"> <main class="map" data-state="{{ state }}">
<section id="popup-buttons"> <section id="popup-buttons">
<button class="button-clear as-location">Show only this location</button> <button class="button-clear as-location">Show only this location</button>
<button class="button-clear as-destination">Route to here</button> <button class="button-clear as-destination">Route to here</button>

View file

@ -1,13 +1,12 @@
from django.conf.urls import url from django.conf.urls import url
from c3nav.site.views import main, map_image, map_index, qr_code from c3nav.site.views import map_index
pos = r'(@(?P<level>\d+),(?P<x>\d+(\.\d+)?),(?P<y>\d+(\.\d+)?),(?P<zoom>\d+(\.\d+)?))?'
urlpatterns = [ urlpatterns = [
url(r'^map/(?P<level>[a-z0-9-_:]+)/(?P<area>[a-z0-9-_:]+).png$', map_image, name='site.level_image'), url(r'^r/(?P<origin>[a-z0-9-_:]+)?/(?P<destination>[a-z0-9-_:]+)?/%s$' % pos,
url(r'^qr/(?P<location>[a-z0-9-_:]+).png$', qr_code, name='site.qr'), map_index, name='site.routing', kwargs={'routing': True}),
url(r'^l/(?P<location>[a-z0-9-_:]+)/$', main, name='site.location'), url(r'^l/(?P<destination>[a-z0-9-_:]+)/%s$' % pos, map_index, name='site.location'),
url(r'^o/(?P<origin>[a-z0-9-_:]+)/$', main, name='site.origin'), url(r'^%s$' % pos, map_index, name='site.index')
url(r'^d/(?P<destination>[a-z0-9-_:]+)/$', main, name='site.destination'),
url(r'^r/(?P<origin>[a-z0-9-_:]+)/(?P<destination>[a-z0-9-_:]+)/$', main, name='site.route'),
url(r'^$', map_index, name='site.index')
] ]

View file

@ -1,6 +1,7 @@
# flake8: noqa # flake8: noqa
import json import json
from datetime import timedelta from datetime import timedelta
from typing import Optional
import qrcode import qrcode
from django.core.files import File from django.core.files import File
@ -9,9 +10,11 @@ from django.shortcuts import get_object_or_404, redirect, render
from django.urls import reverse from django.urls import reverse
from django.utils import timezone from django.utils import timezone
from c3nav.mapdata.models import Source from c3nav.mapdata.models import Location, LocationSlug, Source
from c3nav.mapdata.models.level import Level from c3nav.mapdata.models.level import Level
from c3nav.mapdata.models.locations import LocationRedirect, SpecificLocation
from c3nav.mapdata.render.base import set_tile_access_cookie from c3nav.mapdata.render.base import set_tile_access_cookie
from c3nav.mapdata.utils.locations import get_location_by_slug_for_request
ctype_mapping = { ctype_mapping = {
'yes': ('up', 'down'), 'yes': ('up', 'down'),
@ -61,12 +64,50 @@ def qr_code(request, location):
return response return response
def map_index(request): def check_location(location: Optional[str], request) -> Optional[SpecificLocation]:
if location is None:
return None
location = get_location_by_slug_for_request(location, request)
if location is None:
return
if isinstance(location, LocationRedirect):
location: Location = location.target
if location is None:
return None
if not location.can_search:
location = None
return location
def map_index(request, routing=False, origin=None, destination=None, level=None, x=None, y=None, zoom=None):
levels = Level.qs_for_request(request).filter(on_top_of_id__isnull=True) levels = Level.qs_for_request(request).filter(on_top_of_id__isnull=True)
origin = check_location(origin, request)
destination = check_location(destination, request)
state = {
'routing': routing,
'origin': (origin.serialize(detailed=False, simple_geometry=True, geometry=False)
if origin else None),
'destination': (destination.serialize(detailed=False, simple_geometry=True, geometry=False)
if destination else None),
'sidebar': bool(routing or destination),
}
if level is not None:
state.update({
'level': int(level),
'center': (float(x), float(y)),
'zoom': float(zoom),
})
ctx = { ctx = {
'bounds': json.dumps(Source.max_bounds(), separators=(',', ':')), 'bounds': json.dumps(Source.max_bounds(), separators=(',', ':')),
'levels': json.dumps(tuple((level.pk, level.short_label) for level in levels), separators=(',', ':')), 'levels': json.dumps(tuple((level.pk, level.short_label) for level in levels), separators=(',', ':')),
'state': json.dumps(state),
} }
response = render(request, 'site/map.html', ctx) response = render(request, 'site/map.html', ctx)
set_tile_access_cookie(request, response) set_tile_access_cookie(request, response)