1 line
12 KiB
JavaScript
1 line
12 KiB
JavaScript
|
function renderSA(e,t){var r=create(e,t);if(r.finished)return r.asset;throw new Error("SmartAvatar ERROR: something went wrong.")}function create(e,t){var r=["mp","identicon","monsterid","wavatar","retro","robohash"],a={};switch(!0){case"smart"===t.priority.src1&&t.initials:a.asset=generateEl("initEl",t),a.finished=!0;break;case"smart"===t.priority.src1&&t.icon&&!r.includes(t.icon):a.asset=generateEl("iconEl",t),a.finished=!0;break;case"smart"===t.priority.src1&&t.icon&&r.includes(t.icon):a.asset=generateEl("iconImg",t),a.finished=!0;break;default:a.asset=generateComplexImage(e,t),a.finished=!0}return a}function generateEl(e,t){switch(e){case"iconImg":return generateIconImg(t);case"initEl":return generateInitEl(t);case"iconEl":return generateIconEl(t)}}function generateGravatarIconUrl(e){return"https://www.gravatar.com/avatar?d="+e+"&f=y"}function generateIconImg(e){var t=new Image,r=generateGravatarIconUrl(e.icon);if(t.src=r,e.unstyled||(t.style.cssText=(e.round?"border-radius: 50%;":"")+(e.size?"height:"+e.size+"px;width:"+e.size+"px;":"")+"object-fit:cover;"),t.classList.add("smart-avatar"),e.cssClass){const r=e.cssClass.length;for(let a=0;a<r;a++)t.classList.add(e.cssClass[a])}return e.timestamp&&t.setAttribute("sa_timestamp",Date.now()),e.alt&&(t.alt=e.alt),t}function generateInitEl(e){return generateElement("initEl",e)}function generateIconEl(e){return generateElement("iconEl",e)}function generateElement(e,t){var r;"initEl"===e?r=t.initials:"iconEl"===e&&(r=renderSmartFox(t));var a=document.createElement("div");if(t.unstyled||(a.style.cssText=(t.round?"border-radius: 50%;":"")+"align-items:center;"+(t.color?"background-color:"+t.color:"")+(t.textColor?";color:"+t.textColor:"")+';display:flex;font-family:-apple-system,BlinkMacSystemFont,"Segoe UI",Roboto,Oxygen-Sans,Ubuntu,Cantarell,"Helvetica Neue",sans-serif;'+(t.size?"font-size:"+t.size/2+"px;height:"+t.size+"px;":"")+"justify-content:center;margin:0;padding:0;"+(t.size?"width:"+t.size+"px;":"")),a.classList.add("smart-avatar"),t.cssClass){const e=t.cssClass.length;for(let r=0;r<e;r++)a.classList.add(t.cssClass[r])}return t.timestamp&&a.setAttribute("sa_timestamp",Date.now()),a.innerHTML="<span "+(t.unstyled?"":'style="display:flex;"')+">"+r+"</span>",a}function generateComplexImage(e,t){var r=parseComplexAssets(t),a=r.asset1,n=r.asset2,s=r.asset3,i=new Image;if(i.src=a.content,t.unstyled||(i.style.cssText=(t.round?"border-radius: 50%;":"")+(t.size?"height:"+t.size+"px;width:"+t.size+"px;":"")+"object-fit:cover;"),i.onerror=function(){handleErrFallback(e,i,n),"el"!==n.type&&(i.onerror=function(){handleErrFallback(e,i,s)})},i.classList.add("smart-avatar"),t.cssClass){const e=t.cssClass.length;for(let r=0;r<e;r++)i.classList.add(t.cssClass[r])}return t.timestamp&&i.setAttribute("sa_timestamp",Date.now()),t.alt&&(i.alt=t.alt),i}function handleErrFallback(e,t,r){if(t.style.opacity=0,"el"===r.type)delete t.onerror,e.appendChild(r.content),e.removeChild(t);else if(t.src=r.content,t.style.opacity=1,!t.src)throw new Error("SmartAvatar ERROR: internal error - img src recursively undefined")}function parseComplexAssets(e){var t=["mp","identicon","monsterid","wavatar","retro","robohash"],r={asset1:{},asset2:{},asset3:{}};r.asset1.content="gravatar"===e.priority.src1?e.gravatarUrl:e.src,r.asset1.type="url";for(var a=2;a<=3;a++){var n="src"+a,s="asset"+a;"gravatar"===e.priority[n]?(r[s].content=e.gravatarUrl,r[s].type="url"):"src"===e.priority[n]?(r[s].content=e.src,r[s].type="url"):"smart"===e.priority[n]&&(e.initials?(r[s].content=generateEl("initEl",e),r[s].type="el"):e.icon&&!t.includes(e.icon)?(r[s].content=generateEl("iconEl",e),r[s].type="el"):e.icon&&t.includes(e.icon)&&(r[s].content=generateGravatarIconUrl(e.icon),r[s].type="url"))}return r}function renderSmartFox(e){return'<svg viewBox="0 0 600 600" fill="none" xmlns="http://www.w3.org/2000/svg"><circle cx="300" cy="300" r="144" fill="#DE8927" stroke="#333" stroke-width="12"/><rect x="236" y="231" width="24" height="36" rx="12" fill="#333"/><rect x="340" y="231" width="24" height="36" rx="12" fill="#333"/><mask id=
|