Skip to content

Commit

Permalink
Add static JSON file for constituency data
Browse files Browse the repository at this point in the history
  • Loading branch information
pixeltrix committed Nov 20, 2024
1 parent 8d6ee8d commit 6ac0f59
Show file tree
Hide file tree
Showing 3 changed files with 3,915 additions and 127 deletions.
23 changes: 0 additions & 23 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -48,35 +48,12 @@ <h1>Petition Map <span class="built-by">built by <a href="http://unboxed.co/">Un
<label for="constituency">Find constituency</label>
<select name="constituency" id="constituency"></select>
</div>
<button class="flat_button share-button">Share map</button>
<button class="flat_button about-button">About</button>
<button class="flat_button hide-ui">Hide UI</button>
</footer>

<main>
<section id="constituency_info">
</section>
<section id="petition_info" hidden>
<div class="petition-details"></div>

<details class="petition-choice">
<summary class="flat_button">Show a different petition</summary>
<div>
<div class="choice-row">
<label for="petition_url">Enter a petition url</label>
<input type="text" id="petition_url" placeholder="https://petitions.parliament.uk/petitions/105991">
<button class="flat_button" type="button" id="petition_button">and show it</button>
</div>

<p class="choice-row">Or</p>

<div class="choice-row">
<label for="petition_dropdown" id="select_text">Choose a popular Petition</label>
<select name="petition" id="petition_dropdown"></select>
</div>
</div>
</details>
</section>
<section id="key" hidden>
<table>
<tr>
Expand Down
111 changes: 7 additions & 104 deletions js/form.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,33 +36,9 @@
target = document.getElementById('spinner_area'),
spinner = new Spinner(spinnerOpts).spin(target);

function populatePetitionDropdown() {
return $.getJSON(baseUrl + '/petitions.json?state=open')
.then(function (data) {
if (data.data.length == 0) {
// No open petitions means parliament is dissolved or the committee is waiting
// to be reconstituted after a general election so load the closed petitions
return $.getJSON(baseUrl + '/petitions.json?state=closed');
} else {
return data;
}
})
.done(function (data) {
var petitions = data.data;
$.each(petitions, function (index, item) {
var dropdown_text = item.attributes.action;
$('#petition_dropdown').append(
$('<option></option>').val(item.id).html(dropdown_text)
);
});
});
};

// On start load the petition data
$(document).ready(function() {
$.when(populatePetitionDropdown()).then(function() {
preparePetitionAndView();
});
preparePetitionAndView();
});

// Setup petition ui and map based on initial url params
Expand All @@ -71,23 +47,12 @@
area,
petition_id;

if (variables.petition !== undefined) {
petition_id = variables.petition;
} else {
petition_id = $('#petition_dropdown').val();
}

petition_id = '700086';
area = 'uk';
if (variables.area !== undefined) {
if (possibleAreas().indexOf(variables.area) !== -1) {
area = variables.area;
}
}

PetitionMap.current_area = area;
$('input[name=area][value=' + area + ']').prop('checked',true);
$('#petition_dropdown option[value=' + petition_id + ']').prop('selected', true);
return loadPetition(petition_id, false);
return loadPetition();
}

function possibleAreas() {
Expand Down Expand Up @@ -171,14 +136,14 @@
}

// Loads petition from UK Government Petition site
function loadPetition(petitionReference) {
var petitionUrl = getPetitionUrlFromReference(petitionReference),
function loadPetition() {
var petitionUrl = '/json/signatures',
deferredPetitionLoadedAndDrawn = new $.Deferred();

$.getJSON(petitionUrl + '.json')
.done(function (data) {
PetitionMap.type = data.data.type
PetitionMap.period = (PetitionMap.type == 'archived-petition' ? data.data.parliament.period : 'current')
PetitionMap.type = 'petition'
PetitionMap.period = 'current'
PetitionMap.current_petition = data;

$.when(loadPopulationData(), loadMPData()).then(function() {
Expand All @@ -191,7 +156,6 @@
}, function() {
deferredPetitionLoadedAndDrawn.reject();
});
addPetitionToDropdown(petitionReference, data.data.attributes.action);
});
})
.fail(function() {
Expand Down Expand Up @@ -322,31 +286,11 @@
function reloadMap() {
var dataFile = 'json/uk/' + PetitionMap.period + '/' + PetitionMap.current_area + '/topo_wpc.json';
return $.when(PetitionMap.loadMapData(dataFile, 'wpc')).then(function() {
displayPetitionInfo();
$('#key').fadeIn();
spinner.stop();
});
}

function showPetitionFromDropdown() {
spinner.spin(target);
var petition_id = $('#petition_dropdown').val()

$.when(loadPetition(petition_id, false)).then(function() {
pushstateHandler();
});
};

function showPetitionFromUrlInput() {
spinner.spin(target);

var petition_url = $('#petition_url').val()

$.when(loadPetition(petition_url, false)).then(function() {
pushstateHandler();
});
};

function highlightConstituencyFromDropdown() {
var ons_code = $('#constituency').val(),
constituency_data = {
Expand Down Expand Up @@ -419,36 +363,6 @@
}
};

function invokeAboutModal() {
// Clone modal
var modal_panel = $('#about-modal .about-panel').clone();

// Open modal
vex.dialog.open({
message: modal_panel,
buttons: [
$.extend({}, vex.dialog.buttons.NO, { text: 'Close' })
]
});
}

function invokeShareModal() {
var state = buildCurrentState(),
url = buildCurrentURL(state);

// Clone modal
var modal_panel = $('#share-modal .share-panel').clone();
modal_panel.find('input[name=petition-link]').val(url);

// Open modal
vex.dialog.open({
message: modal_panel,
buttons: [
$.extend({}, vex.dialog.buttons.NO, { text: 'Close' })
]
});
};

////////////////////////////////////////////////////////////////////////


Expand All @@ -458,11 +372,6 @@
// Weighted selection
$('input[name="weighted"]').on('change', changeColouring);

// Petition selection
$('#petition_dropdown').on('change', showPetitionFromDropdown);

$('#petition_button').on('click', showPetitionFromUrlInput);

// Constituency selection
$('#constituency').on('change', highlightConstituencyFromDropdown);

Expand All @@ -472,15 +381,9 @@

$(window).on('petitionmap:constituency-off', hideConstituencyInfo);

// Create & open sharing modal
$('.share-button').on('click', invokeShareModal);

// Button to hide UI
$('.hide-ui').on('click', toggleFormUI);

// Create & open about modal
$('.about-button').on('click', invokeAboutModal);

function buildCurrentState() {
var state = {};
if (PetitionMap.current_petition !== undefined) {
Expand Down
Loading

0 comments on commit 6ac0f59

Please sign in to comment.