Skip to content

Commit

Permalink
Merge pull request #233 from Seshat-Global-History-Databank/regular-i…
Browse files Browse the repository at this point in the history
…ntervals-slider

Regular intervals (centuries) for world map slider
edwardchalstrey1 authored Jan 27, 2025

Unverified

This user has not yet uploaded their public signing key.
2 parents 0213103 + 259ad8e commit eee07a1
Showing 3 changed files with 39 additions and 3 deletions.
15 changes: 13 additions & 2 deletions seshat/apps/core/static/core/js/map_functions.js
Original file line number Diff line number Diff line change
@@ -115,7 +115,7 @@ function playRateValue() {
plotPolities();
}

function setSliderTicks (tickYears) {
function setSliderTicks (tickYears, worldMap=true) {
var datalist = document.getElementById('yearTickmarks');
var tickmarkValuesDiv = document.getElementById('yearTickmarkValues');

@@ -128,6 +128,17 @@ function setSliderTicks (tickYears) {
tickmarkValuesDiv.removeChild(tickmarkValuesDiv.firstChild);
};

var extraCenturyProportion = 0;
if (worldMap) {
// If the last tickmark is not a century, remove it from the list
var lastTick = tickYears[tickYears.length - 1];
if (lastTick % 100 !== 0) {
tickYears.pop();
}
// Calculate number of years over the last century the last tickmark represents
extraCenturyProportion = (lastTick - tickYears[tickYears.length - 1]) / 100;
}

// Loop to add tickmarks
i = 0;
for (const tickValue of tickYears) {
@@ -142,7 +153,7 @@ function setSliderTicks (tickYears) {
span.style.textAlign = 'center';

// Use transform to center the span over the tickmark, with special handling for the first and last span
var leftPercentage = (i / (tickYears.length - 1) * 100);
var leftPercentage = (i / (tickYears.length + extraCenturyProportion - 1) * 100);
span.style.left = `${leftPercentage}%`;
if (i === 0) {
span.style.transform = 'translateX(0%)'; // No translation for the first span
2 changes: 1 addition & 1 deletion seshat/apps/core/templates/core/polity_map.html
Original file line number Diff line number Diff line change
@@ -251,7 +251,7 @@ <h2 class="h2 text-dark federicka-medium">
event.stopPropagation();
});

window.onload = setSliderTicks({{ content.tick_years }});
window.onload = setSliderTicks({{ content.tick_years }}, worldMap = false);

var allPolitiesLoaded = false;

25 changes: 25 additions & 0 deletions seshat/apps/core/views.py
Original file line number Diff line number Diff line change
@@ -4145,6 +4145,22 @@ def get_polity_shape_content(displayed_year="all", seshat_id="all", tick_number=

return content

def tick_centuries(earliest_year, latest_year):
"""
Get the years for the tick marks on the year slider. This is used on the world map.
Args:
earliest_year (int): The earliest year.
latest_year (int): The latest year.
Returns:
str: The JSON representation of the tick years.
"""
ticks = [year for year in range(earliest_year, latest_year + 1, 100)]
if ticks[-1] != latest_year: # We assume that the first year in Cliopatria is a century, but the last year may not be
ticks.append(latest_year)
return json.dumps(ticks)

def get_all_polity_capitals():
"""
Get capital cities for polities that have them.
@@ -4663,6 +4679,9 @@ def map_view_initial(request):
# Add suprapolity relations to the shapes TODO: This is disabled for now as it is not used in the frontend
# content['shapes'] = add_suprapolity_relations_to_shapes(content['shapes'], content['seshat_id_page_id'])

# On the world map, replace the default tickmarks with centuries
content['tick_years'] = tick_centuries(content['earliest_year'], content['latest_year'])

return render(request,
'core/world_map.html',
content
@@ -4702,6 +4721,9 @@ def map_view_all(request):
# Add suprapolity relations to the shapes TODO: This is disabled for now as it is not used in the frontend
# content['shapes'] = add_suprapolity_relations_to_shapes(content['shapes'], content['seshat_id_page_id'])

# On the world map, replace the default tickmarks with centuries
content['tick_years'] = tick_centuries(content['earliest_year'], content['latest_year'])

return JsonResponse(content)

def map_view_all_with_vars(request):
@@ -4750,6 +4772,9 @@ def map_view_all_with_vars(request):
# Add suprapolity relations to the shapes TODO: This is disabled for now as it is not used in the frontend
# content['shapes'] = add_suprapolity_relations_to_shapes(content['shapes'], content['seshat_id_page_id'])

# On the world map, replace the default tickmarks with centuries
content['tick_years'] = tick_centuries(content['earliest_year'], content['latest_year'])

return JsonResponse(content)

def provinces_and_countries_view(request):

0 comments on commit eee07a1

Please sign in to comment.