/*
SECC Agency Listings and Details Decoupled Ajax Client
Data is fed from restful API endpoint from within a legacy Classic ASP site: www.utmb.edu/secc-pledge
HTML Markup:
Author (API and Ajax Client): Mike Cooper
Last update: 9/26/2018
*/
(function ($) {
// HTML endcoding and decoding - string replace for safety
// see: -https://stackoverflow.com/questions/1219860/html-encoding-lost-when-attribute-read-from-input-field/7124052#7124052
function htmlEscape(str) {
return str
.replace(/&/g, '&')
.replace(/"/g, '"')
.replace(/'/g, ''')
.replace(//g, '>');
}
// I needed the opposite function today, so adding here too:
function htmlUnescape(str) {
return str
.replace(/"/g, '"')
.replace(/'/g, "'")
.replace(/</g, '<')
.replace(/>/g, '>')
.replace(/&/g, '&')
.replace(/’/g, "'");
}
// All Agencies
function loadAllAgencies() {
var $content = $('#seccAgenciesContent');
$content.find("header").remove();
$content.find("ul").remove();
var list = $('');
$.getJSON('/secc-pledge/api/agencies/default.asp', function (data) {
$.each(data, function (key, val) {
var groupCode = val['GroupCode'];
var federation = val['Federation'];
var link = $('',
{
id: groupCode,
text: htmlUnescape(federation),
href: '/secc-pledge/api/agencies/listings?id=' + groupCode
});
var li = $('').append(link);
list.append(li);
});
// header
var header = $('');
var headline = $('').append('All Agency Listings ' + 'Local and statewide participating agencies');
var instructions = $('').append('Click on the categories below to see lists of the participating agencies and information about them.');
header.append(headline);
header.append(instructions);
$content.append(header);
$content.append(list);
});
}
// Agency Listings
function loadAgencyListings(currentGroupCode, currentAgencyName) {
var $content = $('#seccAgenciesContent');
$content.find("header").remove();
$content.find("ul").remove();
var list = $('');
$.getJSON('/secc-pledge/api/agencies/listings/?id=' + currentGroupCode, function (data) {
$.each(data, function (key, val) {
var fiscalYear = val['FiscalYear'];
var groupCode = val['GroupCode'];
var charityCode = val['CharityCode'];
var charityName = val['CharityName'];
var website = val['Website'];
var overheadHundreths = val['OverheadHundreths'];
var statement = val['Statement'];
// item (article) container
var item = $('',
{
id: charityCode
});
// Title
var titleTxt = '' + charityCode + ' ' + charityName;
var title = $('').append('').append(titleTxt);
item.append(title);
// Website link
var link = $('',
{
text: website,
href: 'http://' + website,
target: '_blank'
});
item.append(link);
// Descript and Overhundreths
var details = ' ' + statement + ' - ' + overheadHundreths + '%';
item.append(details);
var li = $('').append(item);
list.append(li);
});
// header
var header = $('');
var headline = $('').append('Listings by Agency ' + 'Currently viewing: ' + currentAgencyName + '');
//var instructions = $('').append('Click on the categories below to see lists of the participating agencies and information about them.');
header.append(headline);
//header.append(instructions);
// back to agencies link
var backBtn = $('');
var backLink = $('',
{
class: 'btn btn-success back-to-agencies',
text: 'Back to Agency List',
href: '/secc-pledge/api/agencies/'
});
backBtn.append(backLink);
$content.append(header);
$content.append(backBtn);
$content.append(list);
});
}
// Agency Click (Get Listings)
$(document.body).on('click', 'ul.all-agencies li a', function (event) {
event.preventDefault();
console.log('You clicked: ' + $(this).text());
loadAgencyListings($(this).attr('id'), $(this).text());
});
// Agency Click
$(document.body).on('click', '.back-to-agencies', function (event) {
event.preventDefault();
console.log('You clicked: ' + $(this).text());
loadAllAgencies();
$('.back-btn-container').remove();
});
// Do it when ready...
$(document).ready(function () {
loadAllAgencies();
});
})(jQuery);