UNPKG

co2offset.io

Version:

Greenhouse Gas Accounting - ISO14064

1 lines 14.5 kB
void 0===window.app&&(window.app={}),window.app.formatGrams=function(t,e){t=Math.round(t),void 0!==e&&null!=e||(e=t),e<t&&(e=t);let a=t.toLocaleString()+"g";return Math.abs(e)>1e6?a=(t/1e6).toFixed(3).toLocaleString()+"t":Math.abs(e)>1e4&&(a=(t/1e3).toFixed(2).toLocaleString()+"kg"),a},window.app.parseMustache=function(t,e){return t.replace(/{{\s*([\w\.]+)\s*}}/g,(function(t,a){for(var n=a.split("."),o=e,i=n.length,d=0;d<i;){try{o=o[n[d]]}catch(t){return""}d++}return o}))},window.app.buildTaxonomy=async function(){let t=await window.co2accounting.listEvents(),e={},a=[],n=[],o=(new Date).getTime();for(let e=0;e<t.length;e++)t[e].sortkey="Scope "+t[e].scope+"_"+t[e].category+"_"+(i=o-t[e].endTime,d=void 0,(d="000000000000"+i).substr(d.length-9)+"_")+t[e].event,a[t[e].sortkey]=t[e],n.push(t[e].sortkey);var i,d;n.sort();for(let t=0;t<n.length;t++){let o=a[n[t]];"undefined"==o.category&&(o.category="unspecified"),void 0===e["Scope "+o.scope]&&(e["Scope "+o.scope]=[]),void 0===e["Scope "+o.scope][o.category]&&(e["Scope "+o.scope][o.category]=[]),e["Scope "+o.scope][o.category].push(o)}return window.sortedevents=a,window.taxonomy=e,e},window.app.logout=async function(t){window.indexedDB.deleteDatabase("co2events"),window.indexedDB.deleteDatabase("co2certificates"),window.localStorage.clear(),t&&(location.href="./")},window.app.clearCache=async function(){const t=window.localStorage.getItem("rapidapi_key");window.co2accounting.isOnline&&(window.indexedDB.deleteDatabase("co2events"),window.indexedDB.deleteDatabase("co2certificates"),window.localStorage.clear()),window.localStorage.setItem("rapidapi_key",t)},window.app.renderBalance=async function(){let t=await window.co2accounting.certificates(),e={VER:0,LSTCC:0};for(let a=0;a<t.length;a++)try{e.VER+=t[a].co2requested}catch(t){}e.LSTCC,e.VER;let a=0,n=await window.co2accounting.balance({subbalances:!0}),o={balance:n.liabilities+-1*e.VER,assets:-1*e.VER*1,presafings:-1*n.presafings,liabilities:n.liabilities+-1*e.VER};for(const[t,e]of Object.entries(o))Math.abs(e)>a&&(a=e);$(".balance_balance").html(window.app.formatGrams(o.balance,a)),$(".balance_balance").attr("data",o.balance),$(".balance_assets").html(window.app.formatGrams(o.assets,a)),$(".balance_liabilities").html(window.app.formatGrams(o.liabilities,a)),$(".balance_presafings").html(window.app.formatGrams(o.presafings,a))},window.app.renderEvent=async function(t){let e=await window.co2accounting.identityLookup(t);$(".co2eventTitle").html(e.event.title),void 0===e.event.presafing&&(e.event.presafing=0);let a='<table class="table table-condensed">';a+='<tr><td>Date</td><td class="text-end">'+new Date(e.event.timestamp).toLocaleString()+"</td></tr>",a+='<tr><td>Pre Safing</td><td class="text-end">'+window.app.formatGrams(e.event.presafing)+"</td></tr>",a+='<tr><td>Emission</td><td class="text-end">'+window.app.formatGrams(e.event.co2eq)+"</td></tr>",a+='<tr><td>Offset</td><td class="text-end">'+window.app.formatGrams(e.event.offset)+"</td></tr>",a+='<tr><td><strong>Balance</strong></td><td class="text-end">'+window.app.formatGrams(e.event.co2eq-e.event.offset)+"</td></tr>";let n="";if(void 0!==e.event.projecion&&0==e.event.projecion&&(n='checked="checked"'),a+='<tr><td>Projection</td><td class="text-end"><input class="form-check-input metaProjection" data-identity="'+e.event.event+'" '+n+' type="checkbox" value="on" style="margin-right:5px;"><label class="form-check-label text-dark">ignore</label></td></tr>',a+="</table>",a+="<h5>Meta Information</h5>",a+='<table class="table table-condensed">',void 0!==e.event.scope&&(a+='<tr><td>Scope</td><td class="text-end">'+e.event.scope+"</td></tr>"),void 0!==e.event.category&&(a+='<tr><td>Category</td><td class="text-end">'+e.event.category+"</td></tr>"),void 0!==e.event.timestamp&&(a+='<tr><td>Reported</td><td class="text-end">'+new Date(1*e.event.timestamp).toLocaleString()+"</td></tr>"),void 0!==e.event.startTime&&(a+='<tr><td>Start</td><td class="text-end">'+new Date(1*e.event.startTime).toLocaleString()+"</td></tr>"),void 0!==e.event.endTime&&(a+='<tr><td>End</td><td class="text-end">'+new Date(1*e.event.endTime).toLocaleString()+"</td></tr>"),a+="</table>",void 0!==e.event.generation){a+="<h5>Upstream Disaggregation</h5>",a+='<table class="table table-condensed">',a+='<tr><td>Grid Distance</td><td class="text-end">'+e.event.generation.avgdistance.toFixed(1)+"km</td></tr>",a+='<tr><td>Final MPO Postal Code</td><td class="text-end">'+e.event.zip+"</td></tr>",a+='<tr><td>Demand</td><td class="text-end">'+e.event.electricity.totalConsumption+"Wh</td></tr>",a+="</table>",a+='<table class="table table-condensed">',a+='<tr><th>Generation</th><th>%</th><th>&nbsp;</th><th class="text-end">Wh</th><th class="text-end">Co2</th></tr>';for(let t=0;t<e.event.generation.mix.length;t++)Math.round(100*e.event.generation.mix[t].ratio)>1&&(a+="<tr>",a+="<td>"+e.event.generation.mix[t].type+"</td>",a+="<td>"+(100*e.event.generation.mix[t].ratio).toFixed(1)+"%</td>",a+="<td><div class='bg-success' style='border:1px solid #000000;height:10px;width:"+Math.round(100*e.event.generation.mix[t].ratio)+"px'></div></td>",a+="<td class='text-end'>"+e.event.generation.mix[t].wh+"</td>",a+="<td class='text-end'>"+Math.round(e.event.co2eq*e.event.generation.mix[t].ratio)+"g</td>",a+="</tr>");a+="</table>"}$(".co2eventTable").html(a),$("#co2event").modal("show"),$("#actionFooterCO2Event").show(),$(".metaProjection").change((function(t){window.co2accounting.eventModify($(this).attr("data-identity"),{projecion:$(this).is(":checked")})})),$("#btnco2eventCompensate").attr("data-identity",t),$("#btnco2eventCompensate").unbind(),$("#btnco2eventCompensate").click((async function(){$("#co2event").modal("hide"),await window.co2accounting.eventCompensate($("#btnco2eventCompensate").attr("data-identity")),await window.app.renderEvents(),window.app.renderBalance()})),$("#btnco2eventDelete").click((async function(){$("#co2event").modal("hide"),await window.co2accounting.eventDelete($("#btnco2eventCompensate").attr("data-identity")),await window.app.clearCache(),await window.app.renderEvents(),window.app.renderBalance()})),$("#btnTransfer").click((async function(){$("#co2event").modal("hide"),$("#transferEventModal").modal("show"),$("#doTransfer").unbind(),$("#doTransfer").click((async function(){await window.co2accounting.transfer($("#btnco2eventCompensate").attr("data-identity"),$("#transferRecipient").val()),$("#transferEventModal").modal("hide"),await window.app.renderEvents(),window.app.renderBalance()}))}))},window.app.renderEvents=async function(){let t=await window.co2accounting.listEvents();t.sort((t,e)=>1*t.endTime>1*e.endTime?1:-1);let e="",a="";for(let n=t.length-1;n>=0&&n>t.length-20;n--){e+='<li class="list-group-item">',e+='<div class="row align-items-center no-gutters">',e+='<div class="col me-2">',e+='<h6 class="mb-0"><strong class="text-truncate">'+t[n].title+'</strong></h6><span class="text-xs">'+new Date(1*t[n].startTime).toLocaleString()+" - "+new Date(1*t[n].endTime).toLocaleString()+"</span>",e+="</div>",e+='<div class="col-auto" style="min-width:95px;">';let o="btn-success";t[n].offset<t[n].co2eq&&(o="btn-danger"),e+='<button class="btn btn-block btn-sm '+o+' btn-event" style="width:100%" type="button" data-identity="'+t[n].event+'">'+window.app.formatGrams(t[n].offset-t[n].co2eq)+"</button>",e+="</div>",e+="</div>",e+="</li>",a+='<option value="'+t[n].event+'">'+t[n].title+"</option>"}$("#selectedEvent").html(a),$("#lastEventsLI").html(e),$(".btn-event").click((function(){window.app.renderEvent($(this).attr("data-identity"))}));let n=new Date,o=n;t.length>0&&(n=new Date(1*t[0].endTime),o=new Date(1*t[t.length-1].endTime));let i=window.app.getDatesRange(n,o),d=[],s=[],r=[],c=0;for(let e=0;e<i.length;e++){let a=0,n=0,o=0;for(let d=0;d<t.length;d++)void 0===t[d].offset&&(t[d].offset=0),void 0===t[d].co2eq&&(t[d].co2eq=0),new Date(1*t[d].endTime).toLocaleDateString()==i[e]&&(a+=1*t[d].co2eq-1*t[d].offset,n+=1*t[d].offset,o+=1*t[d].co2eq),void 0!==t[d].co2eq&&t[d].co2eq-t[d].offset>0&&c++;d.push(a),s.push(n),r.push(o)}for(let t=0;t<i.length;t++){let e=i[t].split(".");i[t]=e[0]+"."+e[1]}try{new Chart(document.getElementById("timeline"),{type:"line",data:{labels:i,datasets:[{label:"Balance",data:d,backgroundColor:["rgba(0, 0, 0, 0.2)"],borderColor:["rgba(0, 0, 0, 1)"],borderWidth:2},{label:"Compensated",data:s,backgroundColor:["rgba(0, 158, 119, 0.2)"],borderColor:["rgba(0, 158, 119, 0.5)"],borderWidth:2},{label:"Emission",data:r,backgroundColor:["rgba(255, 0, 0, 0.2)"],borderColor:["rgba(255, 0, 0, 0.5)"],borderWidth:2}]},options:{title:{display:!1},elements:{point:{radius:.2}},plugins:{legend:{display:!0},tooltip:{callbacks:{label:function(t){return t.dataset.data[t.dataIndex].toLocaleString(void 0,{minimumFractionDigits:0})+" g"}}}},scales:{y:{title:{display:!0,text:"Gram CO2"}}}}})}catch(t){console.error(t)}$(".open_events").html(c)},window.app.renderAddNewModal=async function(){$("#addNewModal").modal("show")},window.app.renderCertificate=async function(t){let e=await window.co2accounting.identityLookup(t);$(".co2eventTitle").html("Digital GHG Compensation Asset");let a='<h5 class="text-center w-100">'+t+"</h5>";a+='<table class="table table-condensed">',a+='<tr><td>Date of Allocation</td><td class="text-end">'+new Date(1*e.tx.timeStamp).toLocaleString()+"</td></tr>",a+='<tr><td>Offset CO<sub>2</sub>eq</td><td class="text-end">'+window.app.formatGrams(e.tx.co2requested)+"</td></tr>",a+="</table>",a+="<h5>VER - Gold Standard Credit</h5>",a+='<table class="table table-condensed">',a+='<tr><td>Share of Block</td><td class="text-end">'+e.tx.gsc.tx.from.serial_number+"</td></tr>",$("#co2event").modal("show"),$(".co2eventTable").html(a+'</table><span class="text-muted">loading...</span>'),$("#actionFooterCO2Event").hide(),$.getJSON("https://api.corrently.io/v2.0/co2/goldstandard/credits?query="+e.tx.gsc.tx.from.serial_number,(function(t){t=t[0],a+='<tr><td>Batch</td><td class="text-end">'+t.batch_number+"</td></tr>",a+='<tr><td>Vintage</td><td class="text-end">'+t.vintage+"</td></tr>",a+='<tr><td>Project</td><td class="text-end">'+t.project.name+"</td></tr>",a+='<tr><td>Type</td><td class="text-end">'+t.project.type+"</td></tr>",a+='<tr><td>Methodology</td><td class="text-end">'+t.project.methodology+"</td></tr>",a+='<tr><td>Sustainability Certificate</td><td class="text-end"><a target="_blank" href="'+t.project.sustaincert_url+'">open</a></td></tr>',a+='<tr><td>Developer</td><td class="text-end">'+t.project.project_developer+"</td></tr>",a+='<tr><td>Country</td><td class="text-end">'+t.project.country+"</td></tr>",a+="</table>",a+="<h5>LSTCC - Planting Trees</h5>",a+='<table class="table table-condensed">',console.log(e.tx),a+='<tr><td>Digital Twin-ID</td><td class="text-end">'+e.tx.certificate.tree+"</td></tr>",a+='<tr><td>Developer</td><td class="text-end">'+e.tx.certificate.issuer+"</td></tr>",a+='<tr><td>Location</td><td class="text-end">'+e.tx.certificate.meta+"</td></tr>",a+='<tr><td>Planted</td><td class="text-end">'+new Date(e.tx.certificate.issueDate).toLocaleString()+"</td></tr>",a+="</table>",$(".co2eventTable").html(a)}))},window.app.renderCertificates=async function(){let t=await window.co2accounting.certificates(),e={},a={VER:0,LSTCC:0};for(let n=0;n<t.length;n++)void 0!==t[n].gsc&&void 0!==t[n].gsc.tx&&(void 0===e[t[n].gsc.tx.from]&&(e[t[n].gsc.tx.from]={type:"Voluntary Emission Reduction (VER)",co2:0,events:0,identity:t[n].gsc.tx.from}),e[t[n].gsc.tx.from].co2+=t[n].co2requested,a.VER+=t[n].co2requested,e[t[n].gsc.tx.from].events++,void 0===e[t[n].certificate.tree]&&(e[t[n].certificate.tree]={type:"Certified Long- to Short-Term-Carbon-Cycle Conversation (LSTCC)",co2:0,events:0,identity:t[n].certificate.tree}),e[t[n].certificate.tree].co2+=t[n].co2requested,a.LSTCC+=t[n].co2requested,e[t[n].certificate.tree].events++);let n=0;for(const[t,a]of Object.entries(e))a.co2>n&&(n=a.co2);let o='<table width="100%" style="max-width:100%" class="table table-condensed table striped" id="tblCertificates"><thead><tr><th>Acquired</th><th>CO<sub>2</sub> Credits</th><th>Digital Asset</th><th>&nbsp;</th></tr></thead>';o+="<tbody>",t.sort((t,e)=>t.timeStamp>e.timeStamp?1:-1);for(let e=0;e<t.length;e++)o+="<tr>",o+='<td data-sort="'+t[e].timeStamp+'">'+new Date(t[e].timeStamp).toLocaleString()+"</td>",o+='<td class="text-end">'+window.app.formatGrams(t[e].co2requested,n)+"</td>",o+="<td class='text-truncate' title='"+t[e].compensation+"'>"+t[e].compensation+"</td>",o+="<td class='text-end'>",o+='<button type="button" class="btn btn-sm certificateDetail d-print-none" data="'+t[e].compensation+'"><i class="fa fa-info"></button></i>',o+="</td>",o+="</tr>";o+="</tbody></table>",$("#certificateTable").html(o),$("#tblCertificates").DataTable(),$(".certificateDetail").unbind(),$(".certificateDetail").click((function(t){window.app.renderCertificate($(this).attr("data"))}));let i=a.LSTCC+a.VER;return $(".certifiedLSTCC").html(window.app.formatGrams(a.LSTCC,i)),$(".certifiedVER").html(window.app.formatGrams(a.VER,i)),$(".certifiedTotal").html(window.app.formatGrams(i,i)),t},window.app.renderSearchResults=async function(){if($("#searchQuery").val().length>2){let t=await window.co2accounting.searchFootprint($("#searchQuery").val());window.searchResults={};let e='<table class="table table-condensed table-hover">';for(let a=0;a<t.length;a++)void 0===window.searchResults[t[a]._source.title]&&(e+='<tr class="activitySelection" data-identity="'+t[a]._source.activity+'"><td>'+t[a]._source.en.title+"</td>",e+='<td class="text-end">'+window.app.formatGrams(t[a]._source.co2eq)+"/"+t[a]._source.unit+"</td>",e+="</tr>",window.searchResults[t[a]._source.activity]=t[a]._source,window.searchResults[t[a]._source.en.title]=1*t[a]._source.co2eq);e+="</table>",$("#searchResults").html(e),$("#searchResultToast").modal("show"),$(".activitySelection").unbind(),$(".activitySelection").click((function(t){$("#addTitle").val(window.searchResults[$(this).attr("data-identity")].en.title),$("#addFactor").val(window.searchResults[$(this).attr("data-identity")].co2eq),$("#addUnit").val(window.searchResults[$(this).attr("data-identity")].unit),$("#addScope").val(3),$("#addCategory").val(window.searchResults[$(this).attr("data-identity")].en.tags[0]),$("#addNewModal").modal("show"),$("#searchResultToast").modal("hide")}))}},window.app.getDatesRange=function(t,e){for(var a=[],n=new Date(t);n<=e;)a.push(new Date(n).toLocaleDateString()),n=n-1+1+864e5;return a};