/* Battle Widget - Vue.js app Summary: Loads top 10 (or so) races from Senate and Gov Loads House Map Dependencies: Vue js 2.5.13 Axios 0.18.0 Browser Requirements: IE9+ */ Vue.component('rc-container', { template: '\
\
\
\
\ \
\ RealClearPolitics Election 2018\ \
\ \
\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \
Top Senate RacesFinal Results
\ \ \ \ \ \
\ \
\ \ \ \
2018 House Election Results Map
\ \ \ \
\ \
\ \ \ \ \ \ \ \ \ \ \ \ \ \ \
Top Governor RacesFinal Results
\ \ \ \ \ \
\
\
\
\
\ ', data : function() { return { promises : [], promises_senate : [], promises_governor : [], house_latest_avg_gain : '', house_latest_avg_gain_aff_abbr : '', generic_ballot_race_id : 6185, generic_ballot_data : { race_url : '', aff_abbr : '', spread_str : '' }, senate_data : [ /*{ id: 6280, state:'mo', arrow:'', spread_str:'', highest_aff:'', data: [] }, // Missouri, Hawley vs. McCaskill { id: 6308, state:'tn', arrow:'', spread_str:'', highest_aff:'', data: [] }, // Tennessee, Blackburn vs. Bredesen { id: 6246, state:'fl', arrow:'', spread_str:'', highest_aff:'', data: [] }, // Florida, Scott vs. Nelson { id: 6306, state:'mt', arrow:'', spread_str:'', highest_aff:'', data: [] }, // Montana, Rosendale vs. Tester { id: 6485, state:'nd', arrow:'', spread_str:'', highest_aff:'', data: [] }, // North Dakota, Cramer vs. Heitkamp { id: 6304, state:'nv', arrow:'', spread_str:'', highest_aff:'', data: [] }, // Nevada, Heller vs. Rosen { id: 6328, state:'az', arrow:'', spread_str:'', highest_aff:'', data: [] }, // Arizona, McSally vs. Sinema { id: 6310, state:'tx', arrow:'', spread_str:'', highest_aff:'', data: [] }, // Texas, Cruz vs. O'Rourke { id: 6258, state:'wv', arrow:'', spread_str:'', highest_aff:'', data: [] }, // West Virginia, Morrisey vs. Manchin { id: 6313, state:'wi', arrow:'', spread_str:'', highest_aff:'', data: [] }, // Wisconsin, Vukmir vs. Baldwin { id: 6303, state:'mn', arrow:'', spread_str:'', highest_aff:'', data: [] }, // Minnesota, Housley vs. Smith { id: 6506, state:'nj', arrow:'', spread_str:'', highest_aff:'', data: [] },*/ // New Jersey, Hugin vs. Menendez ], governor_data : [], senate_top_level_data : { 'map_url' : '', 'map_ntu_url' : '', 'dem_num' : 0, 'gop_num' : 0, 'tossups' : 0, 'change' : '', 'spread_aff' : '' }, governor_top_level_data : { 'map_url' : '', 'map_ntu_url' : '', 'dem_num' : 0, 'gop_num' : 0, 'tossups' : 0, 'change' : '', 'spread_aff' : '' }, house_top_level_data : { 'map_url' : '', 'dem_num' : 0, 'gop_num' : 0, 'tossups' : 0 }, top_results_senate_dem : '4*(-3)', top_results_senate_gop : '52(+3)', top_results_house_dem : '225(+30)', top_results_house_gop : '198(-30)', top_results_governor_dem : '23(+7)', top_results_governor_gop : '2*(-6)', page_list: ['senate', 'house', 'governor'], cache_bust : Math.floor((new Date()).getTime() / 25000), // Changes every 25 seconds current_page : 0, // senate page_timeout : 0, rotation_seconds : [45,45,45], } }, created : function() { // This cache bust duplicates what we have on main election widgets in app_v#.js var rcp_cache_bust = Math.round(new Date().getTime() / 10000); axios.get('/elections/live_results/2018/homepage.json?cache_bust='+rcp_cache_bust) .then(function(response) { var w = response.data.won_seats; var n = response.data.net_seats; this.top_results_senate_dem = w.senate.dem+'('+this.plus_if_plus(n.senate.dem)+n.senate.dem+')'; this.top_results_senate_gop = w.senate.gop+'('+this.plus_if_plus(n.senate.gop)+n.senate.gop+')'; this.top_results_house_dem = w.house.dem+'('+this.plus_if_plus(n.house.dem)+n.house.dem+')'; this.top_results_house_gop = w.house.gop+'('+this.plus_if_plus(n.house.gop)+n.house.gop+')'; this.top_results_governor_dem = w.governor.dem+'('+this.plus_if_plus(n.governor.dem)+n.governor.dem+')'; // Temporarily set to 27 because only in this widget we consider GA a GOP win this.top_results_governor_gop = '27('+this.plus_if_plus(n.governor.gop)+n.governor.gop+')'; }.bind(this)); this.load_content(); this.start_rotation(); }, methods : { plus_if_plus : function(num_str) { if(parseInt(num_str) >= 0) { return '+'; } return ''; }, start_rotation : function() { var vue_this = this; window.clearTimeout(this.page_timeout); this.page_timeout = window.setTimeout(function() { vue_this.current_page++; if(vue_this.current_page > vue_this.page_list.length - 1) { vue_this.current_page = 0; } vue_this.start_rotation(); }.bind(vue_this), this.rotation_seconds[this.current_page] * 1000); }, page_nav : function(page) { this.current_page = this.page_list.indexOf(page); this.start_rotation(); }, isXML : function( elem ) { // documentElement is verified for cases where it doesn't yet exist // (such as loading iframes in IE - #4833) var documentElement = (elem ? elem.ownerDocument || elem : 0).documentElement; return documentElement ? documentElement.nodeName !== "HTML" : false; }, id_to_historical_data_recent_url : function(id) { return '/poll/race/'+id+'/historical_data.json?cache_bust='+this.cache_bust; }, id_to_final_results_url : function(id) { return '/poll/race/'+id+'/final_results.json?cache_bust='+this.cache_bust; }, widget_state_abbr_to_title : function(abbr) { if(typeof abbr === 'undefined') { return ''; } if(abbr.toLowerCase() == 'us') { return 'National'; } else if(abbr.toLowerCase() == 'ia') { return 'Iowa'; } else if(abbr.toLowerCase() == 'nh') { return 'New Hampshire'; } else if(abbr.toLowerCase() == 'sc') { return 'South Carolina'; } else if(abbr.toLowerCase() == 'mi') { return 'Michigan'; } else if(abbr.toLowerCase() == 'nv') { return 'Nevada'; } else if(abbr.toLowerCase() == 'tx') { return 'Texas'; } else if(abbr.toLowerCase() == 'ma') { return 'Massachusetts'; } else if(abbr.toLowerCase() == 'ga') { return 'Georgia'; } else if(abbr.toLowerCase() == 'ok') { return 'Oklahoma'; } else if(abbr.toLowerCase() == 'va') { return 'Virginia'; } else if(abbr.toLowerCase() == 'tn') { return 'Tennessee'; } else if(abbr.toLowerCase() == 'nc') { return 'North Carolina'; } else if(abbr.toLowerCase() == 'il') { return 'Illinois'; } else if(abbr.toLowerCase() == 'fl') { return 'Florida'; } else if(abbr.toLowerCase() == 'la') { return 'Louisiana'; } else if(abbr.toLowerCase() == 'ms') { return 'Mississippi'; } else if(abbr.toLowerCase() == 'oh') { return 'Ohio'; } else if(abbr.toLowerCase() == 'az') { return 'Arizona'; } else if(abbr.toLowerCase() == 'ca') { return 'California'; } else if(abbr.toLowerCase() == 'ny') { return 'New York'; } else if(abbr.toLowerCase() == 'pa') { return 'Pennsylvania'; } else if(abbr.toLowerCase() == 'wi') { return 'Wisconsin'; } else if(abbr.toLowerCase() == 'md') { return 'Maryland'; } else if(abbr.toLowerCase() == 'ct') { return 'Connecticut'; } else if(abbr.toLowerCase() == 'in') { return 'Indiana'; } else if(abbr.toLowerCase() == 'nj') { return 'New Jersey'; } else if(abbr.toLowerCase() == 'wv') { return 'West Virginia'; } else if(abbr.toLowerCase() == 'co') { return 'Colorado'; } else if(abbr.toLowerCase() == 'mo') { return 'Missouri'; } else if(abbr.toLowerCase() == 'me') { return 'Maine'; } else if(abbr.toLowerCase() == 'al') { return 'Alabama'; } else if(abbr.toLowerCase() == 'ak') { return 'Alaska'; } else if(abbr.toLowerCase() == 'ar') { return 'Arkansas'; } else if(abbr.toLowerCase() == 'de') { return 'Delaware'; } else if(abbr.toLowerCase() == 'hi') { return 'Hawaii'; } else if(abbr.toLowerCase() == 'id') { return 'Idaho'; } else if(abbr.toLowerCase() == 'ks') { return 'Kansas'; } else if(abbr.toLowerCase() == 'ky') { return 'Kentucky'; } else if(abbr.toLowerCase() == 'mn') { return 'Minnesota'; } else if(abbr.toLowerCase() == 'mt') { return 'Montana'; } else if(abbr.toLowerCase() == 'ne') { return 'Nebraska'; } else if(abbr.toLowerCase() == 'nm') { return 'New Mexico'; } else if(abbr.toLowerCase() == 'nd') { return 'North Dakota'; } else if(abbr.toLowerCase() == 'or') { return 'Oregon'; } else if(abbr.toLowerCase() == 'ri') { return 'Rhode Island'; } else if(abbr.toLowerCase() == 'sd') { return 'South Dakota'; } else if(abbr.toLowerCase() == 'ut') { return 'Utah'; } else if(abbr.toLowerCase() == 'vt') { return 'Vermont'; } else if(abbr.toLowerCase() == 'wa') { return 'Washington'; } else if(abbr.toLowerCase() == 'wy') { return 'Wyoming'; } else if(abbr.toLowerCase() == 'dc') { return 'Washington D.C.'; } return ''; }, show_arrow : function( race, c1, c2, spread_current, p1, p2 ) { var arrow = ''; if(race['poll_override_trend'] == '1') { return arrow; } // Instead of using latest RCP avg for date, find latest rcp avg that has a spread DIFFERENT from the previous var first_avg = race['rcp_avg'][0]; var prev_avg = first_avg; var latest_different_rcp_avg = first_avg; if(typeof race['rcp_avg'][1] !== 'undefined') { for(i = 1; i < race['rcp_avg'].length; i++) { var avg = race['rcp_avg'][i]; for(var j = 0; j < avg['candidate'].length; j++) { if(avg['candidate'][j]['name'] == c1) { c1_score = avg['candidate'][j]['value']; } else if(avg['candidate'][j]['name'] == c2) { c2_score = avg['candidate'][j]['value']; } } var spread_old = parseFloat(c1_score) - parseFloat(c2_score); spread_old = Math.round(spread_old * 10) / 10; if(spread_current != spread_old) { latest_different_rcp_avg = race['rcp_avg'][i-1]; prev_avg = avg; break; } } } var rcp_avg_date = new Date(latest_different_rcp_avg['date']); var time_diff = Math.abs(today_date.getTime() - rcp_avg_date.getTime()); var days_diff = Math.ceil(time_diff / (1000 * 3600 * 24)); var c1_score = 0; var c2_score = 0; if(days_diff <= 2 && typeof race['rcp_avg'][1] !== 'undefined') { // It's within 24 hours (technically 47:59 I think) // Compare previous score to new score for(var j = 0; j < prev_avg['candidate'].length; j++) { if(prev_avg['candidate'][j]['name'] == c1) { c1_score = prev_avg['candidate'][j]['value']; } else if(prev_avg['candidate'][j]['name'] == c2) { c2_score = prev_avg['candidate'][j]['value']; } } var spread_old = parseFloat(c1_score) - parseFloat(c2_score); spread_old = Math.round(spread_old * 10) / 10; if(spread_current > spread_old) { arrow = 'Trending Up'; } else if(spread_current < spread_old) { arrow = 'Trending Down'; } } if(days_diff <= 2) { // Arrow editor overrides var editor_arrow = race['rcp_avg'][0]['arrow_spread']; if(editor_arrow == '1') { arrow = ''; } else if(editor_arrow == '2') { arrow = 'Trending Up'; } else if(editor_arrow == '3') { arrow = 'Trending Down'; } } return arrow; }, full_aff_to_abbr : function(aff_full) { aff_full = aff_full.toLowerCase(); if(aff_full == 'republican') { return 'gop'; } else if(aff_full == 'democrat') { return 'dem'; } else { return 'ind'; } }, getParameterByName : function(name, url) { if (!url) url = window.location.href; name = name.replace(/[\[\]]/g, "\\$&"); var regex = new RegExp("[?&]" + name + "(=([^&#]*)|&|#|$)"), results = regex.exec(url); if (!results) return null; if (!results[2]) return ''; return decodeURIComponent(results[2].replace(/\+/g, " ")); }, // load_content : function() { // Editor configurable json for senate page races var bgwidget_senate_json_filename = 'bgwidget_senate_2018_races'; if(this.getParameterByName('bgwidget_preview') == '1') { bgwidget_senate_json_filename = 'bgwidget_senate_2018_races_preview'; } this.promises.push(axios.get('/widgets/'+bgwidget_senate_json_filename+'.json?cache_bust='+this.cache_bust)); // Editor configurable json for governor page races var bgwidget_governor_json_filename = 'bgwidget_governor_2018_races'; if(this.getParameterByName('bgwidget_preview') == '1') { bgwidget_governor_json_filename = 'bgwidget_governor_2018_races_preview'; } this.promises.push(axios.get('/widgets/'+bgwidget_governor_json_filename+'.json?cache_bust='+this.cache_bust)); // Generic Ballot (used for house page) this.promises.push(axios.get('/poll/race/'+this.generic_ballot_race_id+'/historical_data_recent.json?cache_bust='+this.cache_bust)); // Load senate snapshot this.promises.push(axios({ method: 'get', url: '/epolls/2018/senate/35_seats.xml?cache_bust='+this.cache_bust, responseType: 'document' })); // Load senate ntu snapshot this.promises.push(axios({ method: 'get', url: '/epolls/2018/senate/35_ntu_seats.xml?cache_bust='+this.cache_bust, responseType: 'document' })); // Load governor snapshot this.promises.push(axios({ method: 'get', url: '/epolls/2018/governor/36_seats.xml?cache_bust='+this.cache_bust, responseType: 'document' })); // Load governor ntu snapshot this.promises.push(axios({ method: 'get', url: '/epolls/2018/governor/36_ntu_seats.xml?cache_bust='+this.cache_bust, responseType: 'document' })); // Load house snapshot this.promises.push(axios({ method: 'get', url: '/epolls/2018/house/37_seats.xml?cache_bust='+this.cache_bust, responseType: 'document' })); axios.all(this.promises) .catch(function(error) { console.log(error); }) .then(function(results) { // Populate senate data for(var i = 0; i < results.length; i++) { if(typeof results[i].data['race_ids_senate_2018'] !== 'undefined') { var ids = results[i].data['race_ids_senate_2018']; for(var j = 0; j < ids.length; j++) { this.senate_data.push({ id: ids[j], state:'', arrow:'', spread_str:'', highest_aff:'', data: [] }); } } } // Load all senate json this.senate_data.forEach(function(singleElement) { this.promises_senate.push(axios({ method : 'get', url : this.id_to_final_results_url(singleElement.id), validateStatus: function (status) { return status < 500; } })); }.bind(this)); axios.all(this.promises_senate) .catch(function(error) { console.log(error); }) .then(function(results) { // Top Senate for(var i = 0; i < this.senate_data.length; i++) { for(var j = 0; j < results.length; j++) { if(typeof results[j].data !== 'undefined' && typeof results[j].data.id !== 'undefined' && this.senate_data[i].id == results[j].data.id) { this.senate_data[i].data = results[j].data; // Also add state abbr this.senate_data[i].state = results[j].data.state; break; } } } // Senate data massage /*for(var i = 0; i < this.senate_data.length; i++) { if(typeof this.senate_data[i]['data']['rcp_avg'] !== 'undefined') { var highest_score = 0; var highest_name = ''; var highest_aff = ''; var second_highest_score = 0; var second_highest_name = ''; var second_highest_aff = ''; for(var j = 0; j < this.senate_data[i]['data']['rcp_avg'][0]['candidate'].length; j++) { var c = this.senate_data[i]['data']['rcp_avg'][0]['candidate'][j]; var this_name = c['name']; var this_score = Number(c['value']); var this_aff = c['affiliation']; if(this_score > highest_score) { second_highest_score = highest_score; second_highest_name = highest_name; second_highest_aff = highest_aff; highest_score = this_score; highest_name = this_name; highest_aff = this_aff; } else if(this_score > second_highest_score) { second_highest_score = this_score; second_highest_name = this_name; second_highest_aff = this_aff; } } var spread_current = parseFloat(highest_score) - parseFloat(second_highest_score); var spread = Math.round(spread_current * 10) / 10; var spread_str = ''; if(spread > 0) { spread_str = highest_name+' ('+highest_aff.toUpperCase().charAt(0)+') +'+(Math.floor(spread*10)/10).toFixed(1); } else { spread_str = 'Tie'; } var arrow = this.show_arrow(this.senate_data[i]['data'], highest_name, second_highest_name, spread, this.full_aff_to_abbr(highest_aff), this.full_aff_to_abbr(second_highest_aff)); this.senate_data[i]['arrow'] = arrow; this.senate_data[i]['spread_str'] = spread_str; this.senate_data[i]['highest_aff'] = highest_aff; this.senate_data[i]['state'] = this.senate_data[i]['data']['state']; } }*/ }.bind(this)); // Populate governor data for(var i = 0; i < results.length; i++) { if(typeof results[i].data['race_ids_governor_2018'] !== 'undefined') { var ids = results[i].data['race_ids_governor_2018']; for(var j = 0; j < ids.length; j++) { this.governor_data.push({ id: ids[j], state:'', arrow:'', spread_str:'', highest_aff:'', data: [] }); } } } // Load all governor json this.governor_data.forEach(function(singleElement) { this.promises_governor.push(axios({ method : 'get', url : this.id_to_final_results_url(singleElement.id), validateStatus: function (status) { return status < 500; } })); }.bind(this)); axios.all(this.promises_governor) .catch(function(error) { console.log(error); }) .then(function(results) { // Top Governor for(var i = 0; i < this.governor_data.length; i++) { for(var j = 0; j < results.length; j++) { if(typeof results[j].data !== 'undefined' && typeof results[j].data.id !== 'undefined' && this.governor_data[i].id == results[j].data.id) { this.governor_data[i].data = results[j].data; // Also add state abbr this.governor_data[i].state = results[j].data.state; break; } } } // Governor data massage /*for(var i = 0; i < this.governor_data.length; i++) { if(typeof this.governor_data[i]['data']['rcp_avg'] !== 'undefined') { var highest_score = 0; var highest_name = ''; var highest_aff = ''; var second_highest_score = 0; var second_highest_name = ''; var second_highest_aff = ''; for(var j = 0; j < this.governor_data[i]['data']['rcp_avg'][0]['candidate'].length; j++) { var c = this.governor_data[i]['data']['rcp_avg'][0]['candidate'][j]; var this_name = c['name']; var this_score = Number(c['value']); var this_aff = c['affiliation']; if(this_score > highest_score) { second_highest_score = highest_score; second_highest_name = highest_name; second_highest_aff = highest_aff; highest_score = this_score; highest_name = this_name; highest_aff = this_aff; } else if(this_score > second_highest_score) { second_highest_score = this_score; second_highest_name = this_name; second_highest_aff = this_aff; } } var spread_current = parseFloat(highest_score) - parseFloat(second_highest_score); var spread = Math.round(spread_current * 10) / 10; var spread_str = ''; if(spread > 0) { spread_str = highest_name+' ('+highest_aff.toUpperCase().charAt(0)+') +'+(Math.floor(spread*10)/10).toFixed(1); } else { spread_str = 'Tie'; } var arrow = this.show_arrow(this.governor_data[i]['data'], highest_name, second_highest_name, spread, this.full_aff_to_abbr(highest_aff), this.full_aff_to_abbr(second_highest_aff)); this.governor_data[i]['arrow'] = arrow; this.governor_data[i]['spread_str'] = spread_str; this.governor_data[i]['highest_aff'] = highest_aff; this.governor_data[i]['state'] = this.governor_data[i]['data']['state']; } }*/ }.bind(this)); // Senate / House / Governor snapshots for(var i = 0; i < results.length; i++) { if(this.isXML(results[i].data) && results[i].data.firstChild.getAttribute('name') == 'Battle for the Senate 2018') { this.senate_top_level_data['dem_num'] = results[i].data.querySelector('affiliation[name="Democrats"]').getAttribute('projected_seats'); this.senate_top_level_data['gop_num'] = results[i].data.querySelector('affiliation[name="Republicans"]').getAttribute('projected_seats'); this.senate_top_level_data['tossups'] = results[i].data.querySelector('election').getAttribute('toss_up'); this.senate_top_level_data['map_url'] = results[i].data.querySelector('election').getAttribute('url'); } } for(var i = 0; i < results.length; i++) { if(this.isXML(results[i].data) && results[i].data.firstChild.getAttribute('name') == 'Senate No Toss Ups 2018') { var change = results[i].data.querySelector('election').getAttribute('change'); var spread_aff = ''; if(change.substring(0, 3).toLowerCase() == 'dem') { spread_aff = 'dem'; } else if(change.substring(0, 3).toLowerCase() == 'gop') { spread_aff = 'gop'; } else { change = 'No Net'; } this.senate_top_level_data['spread_aff'] = spread_aff; this.senate_top_level_data['change'] = change; this.senate_top_level_data['map_ntu_url'] = results[i].data.querySelector('election').getAttribute('url'); } } for(var i = 0; i < results.length; i++) { if(this.isXML(results[i].data) && results[i].data.firstChild.getAttribute('name') == '2018 Governor Races') { this.governor_top_level_data['dem_num'] = results[i].data.querySelector('affiliation[name="Democrats"]').getAttribute('projected_seats'); this.governor_top_level_data['gop_num'] = results[i].data.querySelector('affiliation[name="Republicans"]').getAttribute('projected_seats'); this.governor_top_level_data['tossups'] = results[i].data.querySelector('election').getAttribute('toss_up'); this.governor_top_level_data['map_url'] = results[i].data.querySelector('election').getAttribute('url'); } } for(var i = 0; i < results.length; i++) { if(this.isXML(results[i].data) && results[i].data.firstChild.getAttribute('name') == '2018 Governor No Toss Ups') { var change = results[i].data.querySelector('election').getAttribute('change'); var spread_aff = 'gop'; if(change.substring(0, 3).toLowerCase() == 'dem') { spread_aff = 'dem'; } this.governor_top_level_data['spread_aff'] = spread_aff; this.governor_top_level_data['change'] = change; this.governor_top_level_data['map_ntu_url'] = results[i].data.querySelector('election').getAttribute('url'); } } for(var i = 0; i < results.length; i++) { if(this.isXML(results[i].data) && results[i].data.firstChild.getAttribute('name') == 'Battle for the House 2018') { this.house_top_level_data['dem_num'] = results[i].data.querySelector('affiliation[name="Democrats"]').getAttribute('projected_seats'); this.house_top_level_data['gop_num'] = results[i].data.querySelector('affiliation[name="Republicans"]').getAttribute('projected_seats'); this.house_top_level_data['dem_pickups'] = results[i].data.querySelector('affiliation[name="Democrats"]').getAttribute('pickups'); this.house_top_level_data['gop_pickups'] = results[i].data.querySelector('affiliation[name="Republicans"]').getAttribute('pickups'); this.house_top_level_data['tossups'] = results[i].data.querySelector('election').getAttribute('toss_up'); this.house_top_level_data['map_url'] = results[i].data.querySelector('election').getAttribute('url'); // Calculate Avg Gain var seats_dem_2018 = 195; var seats_gop_2018 = 240; var pickups_balance = this.house_top_level_data['dem_pickups'] - this.house_top_level_data['gop_pickups']; var remaining_dem = (seats_dem_2018 - this.house_top_level_data['dem_num']) + pickups_balance; var remaining_gop = (seats_gop_2018 - this.house_top_level_data['gop_num']) - pickups_balance; // Pick-up Range var r1 = (-1 * remaining_dem) + pickups_balance; var r2 = remaining_gop + pickups_balance; var avg_gain_str = 'Avg Pick-Up: '; var avg_gain = (r1 + r2) / 2; var span_class = ''; if(avg_gain == 0) { avg_gain_str = 'No Net Pick-Ups'; span_class = 'no_net'; } else if(avg_gain > 0) { avg_gain_str += 'Dem +'; span_class = 'dem'; } else if(avg_gain < 0) { avg_gain_str += 'GOP +'; span_class = 'gop'; } avg_gain_str += Math.round(avg_gain * 10) / 10; this.house_latest_avg_gain = avg_gain_str; this.house_latest_avg_gain_aff_abbr = span_class; } } // Generic Ballot for(var i = 0; i < results.length; i++) { if(results[i].data.id == this.generic_ballot_race_id) { var dem_num = 0; var gop_num = 0; for(var j = 0; j < results[i].data.rcp_avg[0].candidate.length; j++) { if(results[i].data.rcp_avg[0].candidate[j].affiliation == 'Democrat') { dem_num = results[i].data.rcp_avg[0].candidate[j].value; } else { gop_num = results[i].data.rcp_avg[0].candidate[j].value; } } var spread = parseFloat(dem_num) - parseFloat(gop_num); var spread_aff = ''; var aff_abbr = ''; var spread_str = ''; if(spread > 0) { spread_aff = 'Democrats'; aff_abbr = 'dem'; spread_str = spread_aff + ' +' + Math.abs(Math.round(spread * 10) / 10).toFixed(1); } else if(spread < 0) { spread_aff = 'Republicans'; aff_abbr = 'gop'; spread_str = spread_aff + ' +' + Math.abs(Math.round(spread * 10) / 10).toFixed(1); } else { spread_str = 'Tie'; } this.generic_ballot_data.race_url = results[i].data.link; this.generic_ballot_data.aff_abbr = aff_abbr; this.generic_ballot_data.spread_str = spread_str; break; } } }.bind(this)); } } }); // Start App if(document.getElementById('battle-widget-app')) { new Vue({ el: '#battle-widget-app' }); } if(document.getElementById('battle-widget-app-mobile')) { new Vue({ el: '#battle-widget-app-mobile' }); }