';
$('#map_anchor').after(html);
$('#map_of_returns').slideDown(800);
matchChartToTableWidth();
link_elem.html(link_elem.html().replace(/Map this/g,'Hide map for this'));
$.scrollTo( '#map_of_returns', 800);
});
}
}
/**
* toggleSubMap
* map slides over the main map for a drill-down view
*/
function toggleSubMap(id,link_elem) {
var mid = null;
if(link_elem && link_elem.length) {
var mid = link_elem.attr('id').replace('link-to-map-for-mid-','');
}
// Hide all existing in DOM
$('#map_of_returns .sub_map').fadeOut(500);
if(!mid) { return; }
var map_elem = $('#map-for-mid-'+mid);
if(map_elem.length && !map_elem.is(':visible')) {
map_elem.fadeIn(500);
//link_elem.html(link_elem.html().replace(/Map this/g,'Hide map for this'));
}
else if(map_elem.length && map_elem.is(':visible')) {
map_elem.fadeOut(500);
//link_elem.html(link_elem.html().replace(/Hide map for this/g,'Map this'));
}
else {
$.ajaxSetup({context: {mid: mid, link_elem: link_elem }});
$.get(link_elem.attr('href'),{},function(data) {
var html = '
'+data+'
';
link_elem.closest('#map_of_returns').prepend(html);
$('#map-for-mid-'+mid+' .map_wrapper .map_title').append('
x');
$('#map-for-mid-'+mid).fadeIn(500);
//link_elem.html(link_elem.html().replace(/Map this/g,'Hide map for this'));
});
}
}
function matchChartToTableWidth() {
var w_table = $("#precinct_data").width();
var chart = $(".three-fourth.precinct_results div.chart_wrapper");
if(chart && chart.width() != w_table) {
chart.width(w_table);
}
// Also match map, if exists
var map = $('#map_of_returns');
if(map.length && map.width() != w_table) {
map.width(w_table);
}
// Also match pre-table toolbar, if exists
var toolbar = $('.for_table');
if(toolbar.length && toolbar.width() != w_table) {
toolbar.width(w_table);
}
}
/**
* Precinct table will jump down and stretch to full width if it is too wide for the 3/4 column.
* Also, th sort icons will disappear if width is too small
*/
function responsiveTable() {
var column_table_container = $('.three-fourth.search_results');
var table = $('#precinct_data');
var overflow_table_container = $('.single-column.search_results.precinct_results > .inner');
var chart = $(".three-fourth.precinct_results div.chart_wrapper");
var w_compare = null;
// If table in column
if(column_table_container.find('#precinct_data').length) {
// Move table to full width, and turn off chart size matching
if(table.width() > column_table_container.width()) {
// Move toolbar too
$('div.for_table').appendTo(overflow_table_container);
table_min_width = table.width();
table.appendTo(overflow_table_container);
chart.width('auto');
table.width('100%');
// Table horizontal scrollbar if too wide even for full-width page
if(table.width() > $('body').width()) {
//console.log('table wider than body');
table.css({
display: "block",
'overflow-x': 'auto'
});
}
else {
table.css({ display: "table" });
}
} else {
matchChartToTableWidth();
}
}
// If table in overflow
else if(overflow_table_container.find('#precinct_data').length) {
w_compare = table_min_width? table_min_width: table.width();
// Move table back to column and re-run chart width matching
if(w_compare <= column_table_container.width()) {
// Move toolbar too
$('div.for_table').appendTo(column_table_container);
table.appendTo(column_table_container);
// Remove table horizontal scrollbar trigger
table.css({
display: "table",
'overflow-x': 'auto'
});
matchChartToTableWidth();
}
}
if($('#fixedtableheader0').length) {
$('#fixedtableheader0').css({width: table.width()});
}
/* If table too narrow, remove sort column icons bc they collide w label text */
var sort_icons = table.find('th span.ui-icon');
if(sort_icons.length) {
if(table.width() > 800) { sort_icons.show(); }
else { sort_icons.hide(); }
}
}
function closeAllLocalityDetails(selector) {
var table = $(selector);
if(!table.length) { return false; }
table.find('tr').each(function(k,e) {
var e = $(e);
if(!e.attr('id')) { return; }
var m_id = e.attr('id').replace("locality-id-","");
var rows = e.siblings('.precinct-for-'+m_id);
// Is visible? Collapse.
if(rows.length && rows.is(':visible')) {
toggleLocalityDetails(m_id,e.find('a.expand_toggle'));
};
});
}
function placeAllLocalityDetails(selector) {
var table = $(selector);
if(!table.length) { return false; }
table.find('tr').each(function(k,e) {
var e = $(e);
if(!e.attr('id')) { return; }
var m_id = e.attr('id').replace("locality-id-","");
var rows = e.siblings('.precinct-for-'+m_id);
// Are precinct rows detached from muni parent? Correct them.
if(rows.length) {
if(!e.next('.precinct-for-'+m_id).length) {
e.after(rows);
}
};
});
}
function toggleLocalityDetails(m_id,switch_element) {
var table = switch_element.parents('table.precinct_data');
var e_id = null;
$.each(table.attr('class').split(' '),function(k,v) {
e_id = v.replace('election-id-','');
if(e_id.length < v.length) { return false; }
});
var target = switch_element.parents('tr#locality-id-'+m_id);
var label_more = 'More »';
var label_less = 'Less «';
var candidate_classes = [];
candidate_classes = {"38768":"democratic_party","40474":"republican_party","35891":"libertarian_party","40475":"republican_party","40477":"democratic_party","40476":"democratic_party"}
var rows = target.siblings('.precinct-for-'+m_id);
// Is visible? Collapse.
if(rows.length && rows.is(':visible')) {
rows.hide();
target.css({fontWeight:'normal'});
switch_element.html(label_more);
}
// If not visible but in the DOM, place next to the parent, then show.
else if(rows.length && !rows.is(':visible')) {
target.css({fontWeight:'bold'});
if(!target.next('.precinct-for-'+m_id).length) {
target.after(rows);
}
rows.show("fade",{}, 200);
switch_element.html(label_less);
}
// If not visible AND not in the DOM, then pull from server.
else {
loadingRows(switch_element);
// Set up AJAX call
var post_data = {id:e_id, locality_id: m_id};
// This sends the post data to the server
$.ajaxSetup({
// Use the 'context' key to pass in variables to the AJAX callback.
context:{ e_id: e_id, m_id:m_id, switch_element:switch_element,target:target,label_more:label_more,label_less:label_less, candidate_classes: candidate_classes }
});
var req_url = "/elections/getElectionLocalityData/"+post_data.id+"/"+post_data.locality_id+"/0/json";
$.get(req_url, {}, function(data){ return insert_precinct_rows_into_table(data, target, e_id, m_id, switch_element,label_more, label_less, candidate_classes, "Election"); });
} // END else{}
} // END function
block_loader_until = '';
function loadingRows(next_to_element) {
no_mouse_loader = true;
var loader = $(next_to_element).siblings('div.loader');
if(new Date().getTime() > block_loader_until) {
//message = typeof(message) == "undefined" ? "Please wait...": message;
//loader.find(".message").html(message);
loader.show("fade",{}, 200);
}
}
function endLoadingRows(next_to_element) {
var loader = $(next_to_element).siblings('div.loader');
if(loader.is(":visible")) {
loader.hide("fade",{}, 150);
block_loader_until = new Date().getTime() + 1000;
}
no_mouse_loader = false; // allow mouse loader
}
/**
* insert_precinct_rows_into_table
* AJAX callback
*
*/
function insert_precinct_rows_into_table(data, target, e_id, m_id, switch_element,label_more, label_less, candidate_classes, model) {
data = safeParseJSON(data);
var html = '';
if(data.length) {
var primary_colors = ["#89A54E","#80699B","#DB843D","#86773C"];
var colors_default_to_light = {"#AA4643":"#F3E0E0","#4572A7":"#E1E9F2","#89A54E":"#ECF1E2","#80699B":"#EAE6EE","#DB843D":"#F9E9DB","#222222":"#E9E9E9","#86773C":"#F2EFE1"};
html = '';
var audits = null;
var showed_alert = false;
var is_show_percentage = false;
var np = $('.toolbar a.number_or_percent');
is_show_percentage = (np.length && $(np[1]).hasClass('selected'))? true: false;
$.each(data,function(k,v) {
var ep_str = 'ElectionToPrecinct';
var cep_str = 'CandidateToElectionToPrecinct';
if(model == 'BallotQuestion') {
ep_str = 'BallotQuestionToPrecinct';
cep_str = 'BallotQuestionToPrecinct';
var y = v[ep_str].n_yes_votes * 1;
var n = v[ep_str].n_no_votes * 1;
v.CandidateToElectionToPrecinct = {
n_yes_votes:{id: v[ep_str].id, n_yes_votes: y, pct_yes_votes: v[ep_str].pct_yes_votes, is_winner: 0, candidate_id: 0},
n_no_votes:{id: v[ep_str].id, n_no_votes: n, pct_no_votes: v[ep_str].pct_no_votes, is_winner: 0, candidate_id: 0}
};
if(y != n) {
if(y > n) { v.CandidateToElectionToPrecinct.n_yes_votes.is_winner = 1; }
else { v.CandidateToElectionToPrecinct.n_no_votes.is_winner = 1; }
}
}
var is_audit_failure = false;
var row_class = 'precinct precinct-for-'+m_id;
var is_admin = false;
var wn = v.Precinct.ward_number;
var pn = v.Precinct.precinct_number;
if(wn == wn*1) { wn = wn*1; }
if(pn == pn*1) { pn = pn*1; }
if(!wn) { wn = '-'; }
html += "\n"+'
';
var wd_pct_label = (wn == '-'? '': wn+' - ')+pn;
var wd_pct_label = ' Ward '+wn;
html += ''+wd_pct_label+' | ';
n_candidates = 0;
var n_color = 0;
var vote_count = vote_pct = null;
var yn = null;
$.each(v.CandidateToElectionToPrecinct,function(k1,v1) {
n_candidates++;
model == 'BallotQuestion'? yn = (k1.search('n_yes')>-1? 'yes':'no'): '';
var td_class = '';
var div_class = '';
var content = '';
var color_attr = '';
if(election_data[e_id]['Election']['party_primary']) {
color_attr = ' style="background-color: '+colors_default_to_light[primary_colors[n_color]]+' !important;"';
n_color++;
if(typeof primary_colors[n_color] == 'undefined') { n_color = 0; }
}
if((v1.is_winner*1)) {
if(v1.candidate_id in candidate_classes) {
td_class += candidate_classes[v1.candidate_id];
}
td_class += ' party_background_extra_light winner '+(model == 'BallotQuestion'? yn+'_party': 'candidate-id-'+v1.candidate_id);
div_class = 'party_background_extra_light';
} else {
color_attr = '';
}
vote_count = $.trim(model=='Election'? v1.n_votes: v1['n_'+yn+'_votes']);
vote_pct = $.trim(model=='Election'? v1.pct_candidate_votes: v1['pct_'+yn+'_votes']);
content = number_format(vote_count);
if(is_show_percentage) {
content = number_format(vote_pct*100,1)+'%'
}
if(is_admin) { // OLD: if(is_audit_failure)
content = createEditableField(cep_str, (model=='Election'? 'n_votes': k1) ,v1.id,content);
}
td_class += ' number_'+vote_count+' percent_'+vote_pct.replace('.','-');
if(typeof v1.is_failure != "undefined" && v1.is_failure*1) {
td_class += ' data_audit_has_failures';
}
html += ' | ';
});
td_class = '';
// No candidates
if(!n_candidates) {
html += ' | ';
}
var offices_having_blank = [];
var offices_having_no_preference = [];
var offices_having_spoiled = [];
$.each({"n_all_other_votes":"All Others","n_no_preference_votes":"No Preference","n_blank_votes":"Blanks","n_spoiled_votes":"Spoiled","n_total_votes":"Total Votes Cast"}, function(k1,v1) {
//console.log(k1);
// n_no_preference will be null for any non-Presidential election
if(k1 in v[ep_str] && v[ep_str][k1] != null) {
var oid = election_data[e_id]['Election']['office_id'] * 1;
if(k1 == 'n_no_preference_votes' && (offices_having_no_preference !== true && offices_having_no_preference.indexOf(oid) == -1) ) {
return true; // same as continue;
}
else if(k1 == 'n_spoiled_votes' && (offices_having_spoiled !== true && offices_having_spoiled.indexOf(oid) == -1) ) {
return true; // same as continue;
}
else if(k1 == 'n_blank_votes' && (offices_having_blank !== true && offices_having_blank.indexOf(oid) == -1) ) {
return true; // same as continue;
}
content = number_format(v[ep_str][k1]*1);
if(is_admin) { // OLD: if(is_audit_failure)
content = createEditableField(ep_str,k1,v[ep_str]['id'],content);
}
html += ''+content+ ' | ';
}
});
html += '
';
}); // END each(data)
target.css({fontWeight:'bold'});
target.after(html);
$(".precinct-for-"+m_id).show("fade",{}, 200);
switch_element.html(label_less);
endLoadingRows(switch_element);
} else {
// some error handling?
}
}
';
$('#flash_from_js_43554').after(data);
// Optional: jQueryUI effects
if(msg.search(/denied/i) > -1) {
$('#'+key+'.flash-message-43554').delay(5000).fadeOut();
}
if(key == 'flash_error_fixed_to_top') {
window.setTimeout(function() { $('#'+key).fadeOut(3000); }, 10000);
}
// Now delete them. (this was set from AccessComponent::setFlash())
$.removeCookie('elstats_flash_message',{domain:'.nh.electionstats.com',path:'/'});
$.removeCookie('elstats_flash_key',{domain:'.nh.electionstats.com',path:'/'});
// Not working (from Cake Cookie component)
//$.removeCookie('CakeCookie[Flash][message]',{domain:'.nh.electionstats.com',path:'/'});
//$.removeCookie('CakeCookie[Flash][key]',{domain:'.nh.electionstats.com',path:'/'});
//console.log('after flash:');
//console.log(document.cookie);
}
/**
$.post('/flash_from_js/',{},function(data) {
if(data) {
$('#flash_from_ajax_43554').after(data);
}
});
**/
Actions
';
link_element_parent.after(html_loader);
}
link_element_parent.siblings('.loader').show();
var n_checks = 0;
var tsBeforeCheckLoop = Date.now()/1000;
checkForFile = window.setInterval(function fn() {
n_checks++;
no_mouse_loader = true;
var timeout = 180; // in seconds, if it takes this long, then stop polling and display an error message.
var shouldBreak = ((Date.now()/1000) - tsBeforeCheckLoop > timeout)? true: false;
if(shouldBreak) {
// BUG: This function is not working, error message is not displaying.
// This might have to do with the ajax mouse loader stuff.
displayFlashError('flash_error','Error: Timed out when trying to build the download file.');
window.clearInterval(checkForFile);
showLoaderUntilTrueIsRunning[url] = false;
no_mouse_loader = false;
link_element_parent.siblings('.loader').hide();
link_element.removeClass('disabled');
return;
}
// POST not GET so this does NOT cache with Varnish!
$.post(url,{context:{url:url, shouldBreak: shouldBreak}},function(data) {
if(shouldBreak) { return; }
if(data == 1) {
//console.log('cache file found!');
window.clearInterval(checkForFile);
showLoaderUntilTrueIsRunning[url] = false;
no_mouse_loader = false;
link_element_parent.siblings('.loader').hide();
link_element.removeClass('disabled');
}
else {
//console.log('no cache file yet');
}
});
},interval);
}
// CSV download link
if($('.download_csv').length) {
var interval = 400;
$('body').on('click','.download_csv',function(e) {
var url = $(this).attr('href').replace(/\/precincts_include/,'/check_for_cache_only:1/precincts_include');
if(showLoaderUntilTrueIsRunning[url]) {
return false;
}
showLoaderUntilTrue($(this),url,interval);
});
}