-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathcommon.js
More file actions
1 lines (1 loc) · 8 KB
/
common.js
File metadata and controls
1 lines (1 loc) · 8 KB
1
(()=>{var P="https://ifct2017.deno.dev",K="https://i.imgur.com/PNZBH2d.png",L="https://cdn.jsdelivr.net/npm/@ifct2017/pictures/assets/",C="https://jsr.io/@nodef/ifct2017/1.4.1/",T=new Set(["code","name","scie","lang","grup","regn","tags"]),g=ifct2017.columns,d=ifct2017.hierarchy,V=ifct2017.intakes,O=ifct2017.methods,G=ifct2017.nutrients,h=ifct2017.representations,Q=["whorda","usear","usrdam","usrdaf","euprim","euprif"],X=["ulus","uleu","uljapan"],Y=new Map([["whorda","WHO Recommended Dietary Allowance"],["usear","US Estimated Average Requirement"],["usrdam","US Recommended Dietary Allowance (Male)"],["usrdaf","US Recommended Dietary Allowance (Female)"],["euprim","EU Population Reference Intake (Male)"],["euprif","EU Population Reference Intake (Female)"],["ulus","Tolerable intake Upper Level (US)"],["uleu","Tolerable intake Upper Level (EU)"],["uljapan","Tolerable intake Upper Level (Japan)"]]),j=new Map([["abbr","Abbreviation"],["desc","Description"],["kj","kJ"],["kcal","kcal"]]),W=new Map([[1,"g"],[1e3,"mg"],[1e6,"ug"],[1e9,"ng"]]);function S(e){return Math.round(e*1e6)/1e6}function q(e){var t=[];for(var n of e)t.indexOf(n)<0&&t.push(n);return t}function z(e){for(var t of e)return t}function B(n){var t={},n=n.startsWith("?")?n.substring(1):n;for(var r of n.split("&")){var o=r.split("=");t[decodeURIComponent(o[0])]=decodeURIComponent(o[1]||"")}return t}function Z(){return location.pathname+location.search}function k(e){return location.href=location.origin+e+location.hash}function ee(e){for(var t=e.elements,n={},r=0,o=t.length;r<o;r++)t[r].name&&(n[t[r].name]=t[r].value);return n}function te(e,t){for(var n=e.elements,r=0,o=n.length;r<o;r++)n[r].name&&t[n[r].name]&&(n[r].value=t[n[r].name]);return e}function ne(e){return new Promise(function(t,n){$.getJSON(e,t).fail(n)})}function F(e,t,n,a,u){var a=a||4,u=u||1e3;return $.getJSON(e,t).fail(function(i){if(!a||i.responseJSON)return n(i);setTimeout(function(){F(e,t,n,a-1,u*2)},u)})}function re(e){return new Promise((t,n)=>{Papa.parse(e,{download:!0,header:!0,worker:!0,dynamicTyping:!0,complete:t,error:n})})}function E(e){return e.indexOf('"')>=0?e.replace(/\"(.*?)\"/g,function(t,n){return E(n)}):g.has(e)?g.get(e).name:j.get(e)||e[0].toUpperCase()+e.substring(1)}function A(e){var t="",n=null;if(e.indexOf('"')<0)return g.has(e)?g.get(e).tags:e;for(;(n=/\"(.*?)\"/g.exec())!=null;)t+=A(n[1])+" ";return t.substring(0,t.length-1)}function oe(e){return d.has(e)?d.get(e).parents:null}function ae(e){return d.has(e)?d.get(e).ancestry:null}function ie(e){return d.has(e)?d.get(e).children:null}function ue(e){var t=O.get(e);return t==null?null:t.method+(t.reference?"; "+t.reference:"")}function ce(e){return h.has(e)?h.get(e).type:null}function H(e){return h.has(e)?h.get(e).factor:1}function D(e){return h.has(e)?h.get(e).unit:null}function se(e){e=e.replace(/\[.*?\]/g,"").replace(/\s*\.?$/,""),e=e.replace(/\w+\.\s([\w\',\/\(\)\- ]+)[;\.]?/g,"$1, ");var t=e.split(/,\s*/g);return t[t.length-1]||t.pop(),q(t).join(", ")}function le(e){return L+e+".jpeg"}function pe(e,t){if(e!=="mass")return 1;var n=Math.log(t)/Math.log(1e3),r=-3*Math.round(n-.33);return Math.pow(10,Math.max(r,0))}function fe(e,t){return e!=="mass"?"kJ":W.get(t)}function de(e){var t=[];for(var n in e)n.endsWith("_e")||n.endsWith("_t")||T.has(n)||t.push(n);return t}function he(e,t){var n=[];for(var r of e)n.push(r[t]);return n}function me(e,t,n){var r=[];for(var o of e)r.push([o[t],o[n]]);return r}function ve(e,t,n){var r=[],o=n+"_e";for(var a of e)r.push([a[t],a[n]-(a[o]||0),a[n]+(a[o]||0)]);return r}function xe(e){var t=[];for(var n of e){var r={};for(var o in n)r[o.replace(/\W/g,"_")]=n[o];t.push(r)}return t}function ge(e){var t=[],n=e.length;for(var r of e)t.push(Object.assign({},r));for(var o in e[0]||{})if(!o.endsWith("_e"))for(var a=D(o),u=H(o),i=o+"_e",m=o+"_t",s=0;s<n;s++){var c=e[s][o],p=e[s][i]||0;a==null?t[s][m]=c:t[s][m]=S(c*u)+a+" \xB1 "+S(p*u)}return t}function ye(){console.log("setupForms()");for(var e=document.querySelectorAll("form [type=submit]"),t=0,n=e.length;t<n;t++)e[t].onclick=function(){this.form.submitted=this.name}}function be(){console.log("setupFooter()");var e=document.querySelector("footer");e.offsetTop+e.offsetHeight<e.innerHeight&&(e.style.bottom="0",e.style.position="absolute"),e.style.display="block"}function w(){$(this).text()||$(this).triggerHandler(jQuery.Event("keyup",{keyCode:65,which:65}))}async function Ce(){console.log("setupCorpus()");var e=fetchCsvMap(C+"columns/index.csv").then(function(r){g=r}),t=fetchCsvMap(C+"hierarchy/index.csv").then(function(r){d=r}),n=fetchCsvMap(C+"representations/index.csv").then(function(r){h=r});return Promise.all([e,t,n])}function we(e=!1){console.log("setupQueryAutocomplete()"),$("#text").on("keydown",function(t){t.keyCode===13&&(t.preventDefault(),$("form").submit())}),$("#text").easyAutocomplete({url:function(t){return P+"/api/query/search?text="+encodeURIComponent(t)},getValue:function(t){return t.text},list:{onClickEvent:function(){$("form").submit()}}}).click(w).change(w),e&&w.call(document.querySelector("#text"))}var J=[{title:"Nutrient"},{title:"Value"}];function Ee(e,t){var n=[];for(var r in t)typeof t[r]!="number"||r.endsWith("_e")||r!=="water"&&n.push({name:r,value:t[r]});n.sort(function(u,i){return i.value-u.value}),n=n.slice(0,10);var o='<h4 txt="c">Insights</h4>';o+="<table>",o+=`<caption><strong>${t.name} is a good source of:</strong></caption>`,o+='<thead><tr><th scope="col">Nutrient</th><th scope="col">Value</th></tr></thead>',o+="<tbody>";for(var a of n)o+=`<tr data-tt-id="${a.name}"><td>${E(a.name)}<a href="javascript:void(0);"> \u2139\uFE0F</a></td><td>${Math.round(a.value*100)}% <a href="javascript:void(0);">\u2B06\uFE0F</a></td></tr>`;o+="</tbody>",$(e).html(o)}function U(e,t,n,r,o){var a=ifct2017.columns,u=ifct2017.hierarchy,i=document.createElement("tr"),m=n+"_e",s=n+"_t";n!=="water"&&(i.style.backgroundColor=`rgb(80, 255, 80, ${Math.max(o[n],0)})`),i.setAttribute("data-tt-id",n),r&&i.setAttribute("data-tt-parent-id",r);var c=document.createElement("td");c.setAttribute("data-search",E(n)+" "+A(n)||""),c.textContent=(a.get(n)||{}).name;var p=document.createElement("a");p.setAttribute("href","javascript:void(0);"),p.innerHTML=" \u2139\uFE0F",c.appendChild(p),i.appendChild(c),c=document.createElement("td"),c.setAttribute("data-order",t[n]),c.textContent=t[s],i.appendChild(c),e.appendChild(i);var v=(u.get(n)||{}).children||"";if(v)for(var l of v.split(" "))U(e,t,l,n,o)}function Me(e,t,n,r,o){return console.log("setupCompositionTable()"),t=$(e).DataTable({columns:J,aaSorting:[],pageLength:1e3}),t.on("order.dt",function(){$(e).treetable("expandAll")}),t.on("search.dt",function(){t.search()?$(e).treetable("expandAll"):$(e).treetable("collapseAll")}),setTimeout(function(){window.dispatchEvent(new Event("resize"))},0),t}function Se(e,t,n){console.log("setupCompositionTreetable()");var r=ifct2017.hierarchy,o=document.createDocumentFragment();for(var a in t)if(!(a.endsWith("_e")||a.endsWith("_t")||T.has(a))){var u=(r.get(a)||{}).parents||"";u||U(o,t,a,null,n)}var i=document.createElement("tbody");i.appendChild(o),document.querySelector(e).innerHTML="",document.querySelector(e).appendChild(i),$(e).treetable({expandable:!0,clickableNodeNames:!0})}function Te(){let e=document.createElement("canvas");document.body.appendChild(e);let t=e.getContext("2d"),n,r,o,a,u=0,i=0,m=20,s=0,c=()=>{n=e.width=window.innerWidth,r=e.height=window.innerHeight,o=n/2,a=r/2},p=l=>{let x=l.touches?l.touches[0].clientX:l.clientX,f=l.touches?l.touches[0].clientY:l.clientY;u=x,i=f};window.addEventListener("resize",c),window.addEventListener("mousemove",p),window.addEventListener("touchmove",p),c();let v=()=>{s+=.005,t.fillStyle="white",t.fillRect(0,0,n,r);let l=.1*o+(u-o)*.1,x=.1*a+(i-a)*.1,f=60+5*Math.sin(s*2);t.beginPath(),t.arc(l,x,f,0,Math.PI*2),t.fillStyle="yellow",t.fill();for(let y=0;y<m;y++){let b=y/m*Math.PI*2+s,M=90+10*Math.sin(s*4+y),R=l+Math.cos(b)*(f+5),N=x+Math.sin(b)*(f+5),_=l+Math.cos(b)*(f+M),I=x+Math.sin(b)*(f+M);t.strokeStyle="rgba(255, 215, 0, 0.6)",t.lineWidth=3,t.beginPath(),t.moveTo(R,N),t.lineTo(_,I),t.stroke()}requestAnimationFrame(v)};v()}})();