/*global $, google */ /* **INFO** KLISS: KS Legislature RESTian Interface Source: http://kslegislature.org/klois/includes/kliss_restian_interface_guide_v12.pdf */ const klAjax = 'https://ksbar-site.uc.r.appspot.com/getkliss'; const getAjax = 'https://ksbar-site.uc.r.appspot.com/get'; const klHost = 'http://kslegislature.org/li'; const api = 'api'; const version = 'v12/rev-1'; const apiKey = 'AIzaSyBHnYpEqDQaRrMF_TNRvbTZhmPEKEsw_2E'; var people, geo, geoJSON, combined; let items = [], ids = [], billIds = [], parent, membersParent = 'MembersListing', data, modalOrig, hsBase = `${getAjax}?q=${klHost}/s/images/pics/`, session, // = 'b2021_22', floor = 'senate', floorFrml = 'Senate', json, userTable = [], hiddenRows = [], matchedRows = []; //console.log('KLISS API file loaded'); /* ********************************** */ var fetchData = function (data) { return $.ajax({ url: getAjax, data: data, dataType: 'json' }); }; var map = new google.maps.Map(document.getElementById('map_canvas'), { zoom: 7, center: { lat: 38.40625379485267, lng: -98.2383671845704 } }), infowindow = new google.maps.InfoWindow(); function makeMap(info) { map = new google.maps.Map(document.getElementById('map_canvas'), { zoom: 7, center: { lat: 38.40625379485267, lng: -98.2383671845704 } }); map.data.addGeoJson(info); /* Use map.data.loadGeoJson('https://...'); for External file/URL */ /* map.data.loadGeoJson('https://ksbar-site.appspot.com/get?q=' + encodeURI(url)); */ // Set the global styles. map.data.setStyle(function (feature) { var color = '#666666'; // if (feature.getProperty('Party') === undefined) { // console.log('PARTY', feature.getProperty('Party')); // color = 'pink'; //feature.getProperty('color'); // } if (feature.getProperty('Party') === 'Republican') { color = '#ff0000'; // 'red' } else if (feature.getProperty('Party') === 'Democrat') { color = '#0000ff'; // 'blue' } else if (feature.getProperty('Party') === 'Independent') { color = '#ff9900'; // 'yellow' } return ({ fillColor: color, fillOpacity: '.25', strokeColor: '#666666', strokeWeight: 1, strokeOpacity: 0.5 }); }); console.log('all done loading map'); map.data.addListener('click', function (event) { console.log('event.feature', event.feature); let District = event.feature.getProperty('District'); let KPID = event.feature.getProperty('KPID'); var distMatch = $.map(combined.features, function (obj) { if (obj.District === event.feature.getProperty('District')) { return obj; } }); //console.log(District, KPID, event.feature.h, distMatch[0]); console.log('distMatch[0]', distMatch); distMatch = distMatch[0]; console.log('distMatch', distMatch); function infoWindowCreate(distMatch, e) { console.log('distMatch', distMatch, 'e.latLng', e.latLng); let html = `
' + k + ': ' + v + '
'); // //console.log(k, v); // }); console.log(distMatch.properties['First_Term']); distMatch.properties['Full_Name'] = response.content.FULLNAME; if (distMatch.properties['First_Term'] === '' || distMatch.properties['First_Term'] === undefined) { distMatch.properties['First_Term'] = response.content.FIRSTTERM; } else if (response.content.FIRSTTERM === '') { response.content.FIRSTTERM = distMatch.properties['First_Term'] } distMatch.properties['Seat'] = response.content.SEATNUM; console.log('distMatch:', distMatch); event.feature.setProperty('First_Name', response.content.FIRSTNAME); event.feature.setProperty('Full_Name', response.content.FULLNAME); event.feature.setProperty('Photo', getAjax+ 'q?=' + klHost + '/s/images/pics/' + KPID + '.jpg'); event.feature.setProperty('Webpage', `${klHost}/${session}/members/${KPID}/`); event.feature.setProperty('Party', response.content.PARTY); event.feature.setProperty('First_Term', response.content.FIRSTTERM); event.feature.setProperty('Seat', response.content.SEATNUM); event.feature.setProperty('Room', response.content.OFFICENUM); event.feature.setProperty('Phone', response.content.OFFPH); event.feature.setProperty('Email', response.content.EMAIL); event.feature.setProperty('Committees', response.content.COMMITTEES); event.feature.setProperty('Chaired_Committees', response.content.CHAIRED_COMMITTEES); event.feature.setProperty('Sbills', response.content.SBILLS); event.feature.setProperty('clicked', true); console.log(District, KPID); console.table(distMatch); infoWindowCreate(response.content, event); console.log('else infoWindowCreate response.content'); }); //}); } else { infoWindowCreate(distMatch, event); console.log('else infoWindowCreate'); } }); // Set the fill color to red when the feature is moused over. // Stroke weight remains 1. map.data.addListener('mouseover', function (event) { map.data.overrideStyle(event.feature, { strokeWeight: 2, strokeOpacity: 1, fillOpacity: .45 }); }); map.data.addListener('mouseout', function (event) { map.data.overrideStyle(event.feature, { strokeWeight: 1, strokeOpacity: 0.5, fillOpacity: .25 }); }); $.each(info.features, function (k, v) { //console.log(v.properties); v.properties.Label = 'Rep.'; function color(c) { if (c === 'Democrat') { return '#0000ff25'; } else if (c === 'Republican') { return '#ff000025'; } else if (c === 'Independent') { return '#ff990025'; // 'yellow' } } var cor = color(v.properties.Party); $('#' + v.properties.KPID) .attr({ 'data-party': v.properties.Party, 'data-dist': v.properties.District }) .attr({ 'style': 'background-color: ' + cor }) .children('td').children('a').children('span').text(v.properties.Label + ' ' + v.properties.First_Name + ' ' + v.properties.Last_Name); $('#' + v.properties.KPID + ' td') .next().attr({ 'class': 'details', 'data-id': v.properties.Party }).text(v.properties.Party) .next().attr({ 'class': 'details', 'data-id': v.properties.District }).text(v.properties.District) .next().attr({ 'class': 'details', 'data-id': v.properties.Gender }).text(v.properties.Gender) .next().attr({ 'class': 'details', 'data-id': v.properties.First_Term }).text(v.properties.First_Term); }); } function initMap() { var url = 'https://storage.googleapis.com/ksbar-leg.appspot.com/2012-2022/' + floor + '_districts_simple.json?key=' + apiKey + '&v=20220809-1355'; var jsonFetch = fetchData({ q: url }); var fetchLeg = fetchData({ q: 'https://storage.googleapis.com/ksbar-leg.appspot.com/' + floor + '_latest.json?key=' + apiKey }); $.when(jsonFetch, fetchLeg).done(function (geoData, details) { geoJSON = geoData[0]; people = details[0]; console.log('geoJSON:', geoJSON); // "Congressional districts" console.log('people:', people); // "People" geo = geoJSON.features; // var result = [ // [v1, v2].reduce((m, a) => (a.forEach(o => m.has(o.ObjId) && Object.assign(m.get(o.ObjId), o) || m.set(o.ObjId, o)), m), new Map).values() // ]; var res = []; for (let i = 0; i < geo.length; i++) { let geoDist = geo[i].properties.District; res.push({ ...geo[i], ...(people.features.find((itmInner) => itmInner.District === geoDist)) }); //console.log('res[i].properties:',res[i].properties); res[i].properties.District = geoDist; //$('#' + res[i].properties.KPID).attr('data-dist', geoDist); // sets district data to HTML element. } //console.log('res: ', res); combined = { "type": "FeatureCollection", "name": floorFrml, "features": res }; console.log('combined: ', combined); makeMap(combined); }); /* .fail(function(jqXHR, textStatus, errorThrown) { console.log("error " + textStatus); console.log("incoming Text " + jqXHR.responseText); console.log("Error Thrown " + errorThrown); }); */ } initMap(); /* ********************************** */ function toTitleCase(str) { return str.replace(/[a-zA-Z]+/g, function (txt) { // /\w\S*/g return txt.charAt(0).toUpperCase() + txt.substr(1).toLowerCase(); }); } // ref: http://stackoverflow.com/a/1293163/2343 // This will parse a delimited string into an array of // arrays. The default delimiter is the comma, but this // can be overriden in the second argument. function CSVToArray(strData, strDelimiter) { // Check to see if the delimiter is defined. If not, // then default to comma. console.log('CSVToArray Called', strData); strDelimiter = (strDelimiter || ","); // Create a regular expression to parse the CSV values. var objPattern = new RegExp( ( // Delimiters. "(\\" + strDelimiter + "|\\r?\\n|\\r|^)" + // Quoted fields. "(?:\"([^\"]*(?:\"\"[^\"]*)*)\"|" + // Standard fields. "([^\"\\" + strDelimiter + "\\r\\n]*))" ), "gi" ); console.log('objPattern', objPattern); // Create an array to hold our data. Give the array // a default empty first row. var arrData = [ [] ]; // Create an array to hold our individual pattern // matching groups. var arrMatches = null; console.log(arrMatches == objPattern.exec(strData)); // Keep looping over the regular expression matches // until we can no longer find a match. while (arrMatches == objPattern.exec(strData)) { console.log('ALERT',strMatchedDelimiter, arrMatches); // Get the delimiter that was found. var strMatchedDelimiter = arrMatches[1]; console.log(strMatchedDelimiter); // Check to see if the given delimiter has a length // (is not the start of string) and if it matches // field delimiter. If id does not, then we know // that this delimiter is a row delimiter. if ( strMatchedDelimiter.length && strMatchedDelimiter !== strDelimiter ) { // Since we have reached a new row of data, // add an empty row to our data array. arrData.push([]); } var strMatchedValue; // Now that we have our delimiter out of the way, // let's check to see which kind of value we // captured (quoted or unquoted). if (arrMatches[2]) { // We found a quoted value. When we capture // this value, unescape any double quotes. strMatchedValue = arrMatches[2].replace( new RegExp("\"\"", "g"), "\"" ); } else { // We found a non-quoted value. strMatchedValue = arrMatches[3]; } // Now that we have our value string, let's add // it to the data array. arrData[arrData.length - 1].push(strMatchedValue); } // Return the parsed data. return (arrData); } function doQuery(listing, id, status, floor) { //console.log('doQuery: ', listing); data = { listing: listing, detail: id, status: status, floor: floor }; data.version = version; // current API version $.ajax({ url: klAjax, method: 'GET', crossDomain: true, dataType: 'json', data: data, success: function (response) { //console.log('response:', response, listing); parse(response, listing); }, failure: function (response) { console.log(response.message); throw response.message; } }); } function parse(resp, listing) { console.log('query listing: ', listing); //let JSONdata = JSON.parse(data); //console.log(JSONdata); let itemRow; items.length = 0; let branches = [{ 'chamber': 'house' }, { 'chamber': 'senate' }], category, delay; if (listing === 'members') { $.each(branches, function (k, v) { items.length = 0; category = v['chamber']; //console.log(category); //let bran = ; itemRow = resp.content[category + '_members']; itemRow.forEach(function (e) { e['CAT'] = category; }); //console.log(itemRow); //itemRow = itemRow.bran; itemRow.sort(compare); $.each(itemRow, members); write(items); }); //console.log('ids', ids); delay = 3000; // $.each(ids, function (k, v) { // setTimeout(function () { // console.log(k, v.KPID); // getDetails('members', v.KPID); // }, k * delay); // }); // console.log('resource/resmgr/legislative/2021-22/maps/' + floor + '_districts.txt', ','); // console.log(CSVToArray('resource/resmgr/api/kliss/kansas_senate_legislator_info.csv', ',')); } if (listing === 'bill_status') { itemRow = resp.content; //console.log(itemRow); itemRow.sort(compareBills); $.each(itemRow, bills); //console.log(items); //writeTable(items); } if (listing === 'calendar') { itemRow = resp.content[0].document.split('/'); session = itemRow[4]; console.log(session, itemRow); //itemRow[0].document; //$.each(itemRow, bills); //console.log(items); //writeTable(items); } function members() { let m = this; //console.log('this', this); /* Headshot Base URL */ //console.log(m); let kpid = m.KPID; let memName = m.NAME; let parObj = membersParent; let idSplit = kpid, nameSplit = memName; //kpid.substr(kpid.length - 1); idSplit = toTitleCase(kpid); idSplit = idSplit.split('_'); nameSplit = nameSplit.split(' ')[1]; idSplit.pop(); let title = idSplit.shift() + '.'; //console.log(idSplit, idSplit.length); if (idSplit.length <= 2) { //console.log('normal'); idSplit.push(idSplit.shift()); } else { //idSplit.push(idSplit.shift()); //idSplit.push(idSplit.shift()); idSplit.shift(); idSplit.shift(); idSplit.push(nameSplit); //console.log('not normal', idSplit); } idSplit.unshift(title); //console.log(idSplit, idSplit.length); var nameJoin = idSplit.join(' '); var fullName = toTitleCase(nameJoin); //console.log(title, idSplit[0], idSplit[1], idSplit[2]); //console.log(nameJoin); let nameLast = toTitleCase(fullName), nameFirst = toTitleCase(fullName), nameNext = toTitleCase(fullName); //console.log('First Name: ', nameFirst + '-' + nameNext, 'Last Name:', nameLast, 'Full Name:', fullName); ids.push({ 'KPID': kpid, 'NAME': memName, 'FULLNAME': fullName }); //console.log(ids); // ids['KPID'] = kpid; // ids['NAME'] = memName; //getDetails('members', kpid); if (m.CAT === floor) { items.push('' + k + ': ' + v + '
'); console.log(k, v); }); var first_term_match; if (idMatch.length > 0) {first_term_match = idMatch[0].properties.First_Term} else {first_term_match = "Current session"}; //console.log("FIRSTTERM :", idMatch[0], '' !== idMatch[0].length ? idMatch[0].properties.First_Term :'Current'); let District = response.content.DISTRICT; let First_Name = response.content.FIRSTNAME; let Full_Name = response.content.FULLNAME; let Full_Long = response.content.JEMEMBFULLLONG; //let Webpage = event.feature.getProperty('Webpage'); let Party = '' !== response.content.PARTY ? response.content.PARTY : idMatch[0].properties.Party; let First_Term = '' !== response.content.FIRSTTERM ? response.content.FIRSTTERM : first_term_match; //'2019 '; let Seat = response.content.SEATNUM; let Room = response.content.OFFICENUM; let Phone = response.content.OFFPH; let Email = response.content.EMAIL; let Home_Phys_City = response.content.HMPHYSCITY; let SBills = response.content.SBILLS; //let KPID = event.feature.getProperty('KPID'); //console.log('District:', District, 'KPID:', KPID); //console.table(response.content); //if (typeof District == "undefined") District = event.feature.getProperty('KPID'); //if (typeof name == "undefined") name = event.feature.getProperty('district'); let html = //`Bill No. | Short Title | `; html += SponsoredBills; for (var i = 0; i < SBills.length; i++) { //console.log(SBills[i]); var bill = SBills[i]["BILLNO"], //name = SBills[i]["NAME"], stitle = SBills[i]["SHORTTITLE"]; html += `
---|---|
${bill} | ${stitle} |