importtype{HtmlAttributes,HtmlTag,HtmlTerminalNode,}from"../src/types/html";functionprint(...txt: any[]){returnconsole.log(txt);}/** * Run the provided function on a desktop. */functionrunOnDesktop(fn: ()=>void){if(window.matchMedia("(min-width: 501px)")){returnfn();}}/** * Create an html element with attributes and append it to a parent element */functioncreateParent(elementName: HtmlTag,attributes: HtmlAttributes={},{
parent,
children,}: {parent?: HTMLElement;children?: HtmlTerminalNode[];}){constelem=document.createElement(elementName);for(letkeyinattributes){constattributeValue=attributes?.[key];// If we can explicitly define it, use the assigning function.// Otherwise mutate the element directly.if(attributeValue!==undefined){elem.setAttribute(key,attributeValue);elem[key]=attributeValue;}}if(parent){parent.appendChild(elem);}if(children){children.forEach((child)=>{if(typeofchild==="string"){elem.appendChild(document.createTextNode(child));}elseif(child){elem.appendChild(child);}});}returnelem;}functioncreate(elementName: HtmlTag,attributes: HtmlAttributes,parent?: HTMLElement){returncreateParent(elementName,attributes,{ parent });}functioncreate2(elementName: HtmlTag,attributes: HtmlAttributes,
...children: HtmlTerminalNode[]){returncreateParent(elementName,attributes,{
children,});}varhttpRequest: XMLHttpRequest;typeHttpMethod="GET"|"POST";functionreq(url: string,method: HttpMethod,then: Function){if(window.XMLHttpRequest){httpRequest=newXMLHttpRequest();}elseif(window.ActiveXObject){httpRequest=newActiveXObject("Microsoft.XMLHTTP");}httpRequest.onreadystatechange=function(){if(httpRequest.readyState===XMLHttpRequest.DONE){if(httpRequest.status===200){// All setvarresponse=JSON.parse(httpRequest.responseText);then(response);}else{console.log("There was a problem with the last.fm request.");}}};httpRequest.open(method,url,true);httpRequest.send();}constget=(url: string,then: Function)=>req(url,"GET",then);const$=(selector: string)=>document.querySelector(selector);constall=(selector: string)=>Array.from(document.querySelectorAll(selector));/** * Dynamically load a script provided the src and id. */functiondynLoad(src: string,id: string){vars=document.createElement("script");s.setAttribute("src",src);s.setAttribute("id",id);s.setAttribute("async","true");returns;}export{create,create2,dynLoad,get,$,all,print,runOnDesktop};