avatar-initials
Version:
JavaScript library for showing Gravatars or generating user avatars from initials.
2 lines (1 loc) • 11.8 kB
JavaScript
var AvatarComponent=function(t,e){"use strict";function a(t,e){return t+e&4294967295}function i(t,e,i,s,r,l){return e=a(a(e,t),a(s,l)),a(e<<r|e>>>32-r,i)}function s(t,e,a,s,r,l,n){return i(e&a|~e&s,t,e,r,l,n)}function r(t,e,a,s,r,l,n){return i(e&s|a&~s,t,e,r,l,n)}function l(t,e,a,s,r,l,n){return i(e^a^s,t,e,r,l,n)}function n(t,e,a,s,r,l,n){return i(a^(e|~s),t,e,r,l,n)}function o(t,e){let i=t[0],o=t[1],c=t[2],h=t[3];i=s(i,o,c,h,e[0],7,-680876936),h=s(h,i,o,c,e[1],12,-389564586),c=s(c,h,i,o,e[2],17,606105819),o=s(o,c,h,i,e[3],22,-1044525330),i=s(i,o,c,h,e[4],7,-176418897),h=s(h,i,o,c,e[5],12,1200080426),c=s(c,h,i,o,e[6],17,-1473231341),o=s(o,c,h,i,e[7],22,-45705983),i=s(i,o,c,h,e[8],7,1770035416),h=s(h,i,o,c,e[9],12,-1958414417),c=s(c,h,i,o,e[10],17,-42063),o=s(o,c,h,i,e[11],22,-1990404162),i=s(i,o,c,h,e[12],7,1804603682),h=s(h,i,o,c,e[13],12,-40341101),c=s(c,h,i,o,e[14],17,-1502002290),o=s(o,c,h,i,e[15],22,1236535329),i=r(i,o,c,h,e[1],5,-165796510),h=r(h,i,o,c,e[6],9,-1069501632),c=r(c,h,i,o,e[11],14,643717713),o=r(o,c,h,i,e[0],20,-373897302),i=r(i,o,c,h,e[5],5,-701558691),h=r(h,i,o,c,e[10],9,38016083),c=r(c,h,i,o,e[15],14,-660478335),o=r(o,c,h,i,e[4],20,-405537848),i=r(i,o,c,h,e[9],5,568446438),h=r(h,i,o,c,e[14],9,-1019803690),c=r(c,h,i,o,e[3],14,-187363961),o=r(o,c,h,i,e[8],20,1163531501),i=r(i,o,c,h,e[13],5,-1444681467),h=r(h,i,o,c,e[2],9,-51403784),c=r(c,h,i,o,e[7],14,1735328473),o=r(o,c,h,i,e[12],20,-1926607734),i=l(i,o,c,h,e[5],4,-378558),h=l(h,i,o,c,e[8],11,-2022574463),c=l(c,h,i,o,e[11],16,1839030562),o=l(o,c,h,i,e[14],23,-35309556),i=l(i,o,c,h,e[1],4,-1530992060),h=l(h,i,o,c,e[4],11,1272893353),c=l(c,h,i,o,e[7],16,-155497632),o=l(o,c,h,i,e[10],23,-1094730640),i=l(i,o,c,h,e[13],4,681279174),h=l(h,i,o,c,e[0],11,-358537222),c=l(c,h,i,o,e[3],16,-722521979),o=l(o,c,h,i,e[6],23,76029189),i=l(i,o,c,h,e[9],4,-640364487),h=l(h,i,o,c,e[12],11,-421815835),c=l(c,h,i,o,e[15],16,530742520),o=l(o,c,h,i,e[2],23,-995338651),i=n(i,o,c,h,e[0],6,-198630844),h=n(h,i,o,c,e[7],10,1126891415),c=n(c,h,i,o,e[14],15,-1416354905),o=n(o,c,h,i,e[5],21,-57434055),i=n(i,o,c,h,e[12],6,1700485571),h=n(h,i,o,c,e[3],10,-1894986606),c=n(c,h,i,o,e[10],15,-1051523),o=n(o,c,h,i,e[1],21,-2054922799),i=n(i,o,c,h,e[8],6,1873313359),h=n(h,i,o,c,e[15],10,-30611744),c=n(c,h,i,o,e[6],15,-1560198380),o=n(o,c,h,i,e[13],21,1309151649),i=n(i,o,c,h,e[4],6,-145523070),h=n(h,i,o,c,e[11],10,-1120210379),c=n(c,h,i,o,e[2],15,718787259),o=n(o,c,h,i,e[9],21,-343485551),t[0]=a(i,t[0]),t[1]=a(o,t[1]),t[2]=a(c,t[2]),t[3]=a(h,t[3])}function c(t){const e=[];for(let a=0;a<64;a+=4)e[a>>2]=t.charCodeAt(a)+(t.charCodeAt(a+1)<<8)+(t.charCodeAt(a+2)<<16)+(t.charCodeAt(a+3)<<24);return e}const h="0123456789abcdef".split("");function f(t){let e="",a=0;for(;a<4;a++)e+=h[t>>8*a+4&15]+h[t>>8*a&15];return e}const g=t=>function(t){for(let e=0;e<t.length;e++)t[e]=f(t[e]);return t.join("")}(function(t){const e=t.length,a=[1732584193,-271733879,-1732584194,271733878];let i;for(i=64;i<=t.length;i+=64)o(a,c(t.slice(i-64,i)));t=t.slice(Math.max(0,i-64));const s=[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0];for(i=0;i<t.length;i++)s[i>>2]|=t.charCodeAt(i)<<(i%4<<3);if(s[i>>2]|=128<<(i%4<<3),i>55)for(o(a,s),i=0;i<16;i++)s[i]=0;return s[14]=8*e,o(a,s),a}(t));class u{constructor(t){let e=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{};if(!t)throw new Error("No image element provided.");this.element=t,this.settings={size:80,fallbackImage:"data:image/svg+xml,%3Csvg width='60' xmlns='http://www.w3.org/2000/svg' height='60' viewBox='0 0 60 60'%3E%3Cpath fill='%23bcc7ce' d='M0 0h60v60h-60z'/%3E%3Cg fill-rule='evenodd'%3E%3Cpath fill='%23a4b1b9' d='M30.1 54.8c-6.7 0-13.1-2.8-17.6-7.7l-.5-.5v-2.6h.2c.4-4 1.6-6.7 3.4-7.6 1.3-.6 2.9-1.1 4.9-1.6v-1l1-.3s.7-.2 1.7-.5c0-.5-.1-.7-.1-.9-.6-1-1.5-3.3-2.1-6l-1.7-1.4.2-.9s.2-.9 0-1.9c-.2-.9.1-1.5.3-1.8.3-.3.6-.5 1-.6.3-1.6.9-3.1 1.7-4.3-1.3-1.5-1.7-2.6-1.5-3.5.2-.9 1-1.5 1.9-1.5.7 0 1.3.3 1.9.6.3-.7.9-1.1 1.7-1.1.7 0 1.4.4 2.4.8.5.3 1.2.6 1.6.7 3.4.1 7.6 2.2 8.9 7.6.3.1.6.3.8.5.4.5.5 1.1.3 1.9-.2 1.2 0 2.4 0 2.4l.2.8-1.2 1.2c-.5 2.8-1.6 5.4-2.2 6.5-.1.1-.1.4-.1.8 1 .3 1.7.5 1.7.5l1 .4v.8c2.5.5 4.6 1.1 6.1 1.9 1.8.9 2.9 3.5 3.4 7.8l.1.6-.4.5c-4.6 5.9-11.5 9.4-19 9.4z'/%3E%3Cpath fill='%23fff' d='M45.4 36.8c-1.5-.8-3.9-1.5-7-2v-.9s-1-.4-2.6-.7c-.2-.8-.3-2 .2-2.8.5-.9 1.7-3.6 2.1-6.5l.9-.9s-.3-1.4 0-3c.2-.9-.4-.7-.9-.5-.9-7.1-6.3-7.7-7.8-7.7-1.4-.2-3.9-2.2-4.1-1.3-.1.9 1.2 1.8-.4 1.4-1.6-.4-3.1-1.8-3.3-.8-.2.7 1.2 2.3 2 3.1-1.2 1.3-2.1 3.2-2.4 6.1-.5-.3-1.4-.7-1.1.2.3 1.3 0 2.6 0 2.6l1.4 1.2c.5 2.7 1.5 5.1 2 6 .5.8.3 2.1.2 2.8-1.5.3-2.6.7-2.6.7v1.2c-2.5.5-4.4 1.1-5.8 1.7-2 1-2.6 5.7-2.7 7.9v.4c4.1 4.4 10 7.2 16.5 7.2 7.3 0 13.7-3.5 17.8-8.8-.1-2.3-.8-5.7-2.4-6.6z'/%3E%3C/g%3E%3C/svg%3E",setSourceCallback:()=>{},primarySource:"",initials:"",color:"#888888",background:"#f4f6f7",fontSize:0,fontWeight:100,fontFamily:"'Lato', 'Lato-Regular', 'Helvetica Neue'",offsetX:void 0,offsetY:void 0,width:void 0,height:void 0,useGravatar:!0,useGravatarFallback:!1,hash:"",email:"",fallback:"mm",rating:"x",forcedefault:!1,githubId:0,...e};let a=this.settings.fallbackImage;this.settings.primarySource?a=this.settings.primarySource:this.settings.useGravatar&&this.settings.useGravatarFallback?a=u.gravatarUrl(this.settings):this.settings.useGravatar?this.gravatarValid():this.settings.githubId?a=u.githubAvatar(this.settings):this.settings.initials.length>0&&(a=u.initialAvatar(this.settings)),this.setSource(a)}static from(t,e){return new u(t,e)}setSource(t){if(!this.element)throw new Error("No image element set.");t&&(this.element.src=t,this.settings.setSourceCallback(t))}gravatarValid(){if(!this.settings.email&&!this.settings.hash)return;const t=this.settings.email?g(this.settings.email):this.settings.hash,e=new window.Image;e.addEventListener("load",this.gravatarValidOnLoad.bind(this)),e.addEventListener("error",this.gravatarValidOnError.bind(this)),e.src=`https://secure.gravatar.com/avatar/${t}?d=404`}gravatarValidOnLoad(){this.setSource(u.gravatarUrl(this.settings))}gravatarValidOnError(){this.settings.initials.length>0?this.setSource(u.initialAvatar(this.settings)):this.setSource(this.settings.fallbackImage)}static initialAvatar(t){let e;try{e=document.createElement("canvas")}catch(t){return console.error("Canvas related error:",t),""}const a=t.width?t.width:t.size,i=t.height?t.height:t.size,s=Math.max(window.devicePixelRatio,1);e.width=a*s,e.height=i*s,e.style.width=`${a}px`,e.style.height=`${i}px`;const r=e.getContext("2d");if(!r)return console.error("Canvas context error."),"";const l=t.offsetX?t.offsetX:a/2,n=t.offsetY?t.offsetY:i/2;return r.scale(s,s),r.rect(0,0,e.width,e.height),r.fillStyle=t.background,r.fill(),r.font=`${t.fontWeight} ${t.fontSize||i/2}px ${t.fontFamily}`,r.textAlign="center",r.textBaseline="middle",r.fillStyle=t.color,r.fillText(t.initials,l,n),e.toDataURL("image/png")}static gravatarUrl(t){let{size:e=80,email:a="",hash:i="",fallback:s="mm",rating:r="x",forcedefault:l=!1}=t;e=e&&e>=1&&e<=2048?e:80;let n=i||a;n=n.toLowerCase().trim(),n&&"string"==typeof n||(n="00000000000000000000000000000000"),i=n.includes("@")?g(n):n;return`https://secure.gravatar.com/avatar/${i}?s=${e}&d=${s?encodeURIComponent(s):"mm"}&r=${r||"x"}${l?"&f=y":""}`}static githubAvatar(t){let{githubId:e=0,size:a=80}=t;return`https://avatars.githubusercontent.com/u/${e}?s=${a}&v=4`}}var d=u;function v(t,{fallbackImage:e,primarySource:a,useGravatar:i,size:s,email:r,hash:l,fallback:n,rating:o,forcedefault:c,githubId:h,width:f,height:g,initials:u,offsetX:v,offsetY:m,background:b,color:w,fontFamily:p,fontSize:C,fontWeight:E}){a?t(a):i?t(d.gravatarUrl({size:s,email:r,hash:l,fallback:n,rating:o,forcedefault:c})):h?t(d.githubAvatar({size:s,githubId:h})):u.length>0?t(d.initialAvatar({size:s,width:f,height:g,initials:u,offsetX:v,offsetY:m,background:b,color:w,fontFamily:p,fontSize:C,fontWeight:E})):t(e)}function m({classes:t="",useGravatar:a=!0,useGravatarFallback:i=!1,initials:s="",color:r="#888888",background:l="#f4f6f7",fontSize:n=0,fontWeight:o=100,fontFamily:c="'Sofia', 'Helvetica Neue', sans-serif",offsetX:h,offsetY:f,hash:g="",email:u="",size:d=80,width:m,height:b,fallback:w="mm",rating:p="x",forcedefault:C=!1,fallbackImage:E="data:image/svg+xml,%3Csvg width='60' xmlns='http://www.w3.org/2000/svg' height='60' viewBox='0 0 60 60'%3E%3Cpath fill='%23bcc7ce' d='M0 0h60v60h-60z'/%3E%3Cg fill-rule='evenodd'%3E%3Cpath fill='%23a4b1b9' d='M30.1 54.8c-6.7 0-13.1-2.8-17.6-7.7l-.5-.5v-2.6h.2c.4-4 1.6-6.7 3.4-7.6 1.3-.6 2.9-1.1 4.9-1.6v-1l1-.3s.7-.2 1.7-.5c0-.5-.1-.7-.1-.9-.6-1-1.5-3.3-2.1-6l-1.7-1.4.2-.9s.2-.9 0-1.9c-.2-.9.1-1.5.3-1.8.3-.3.6-.5 1-.6.3-1.6.9-3.1 1.7-4.3-1.3-1.5-1.7-2.6-1.5-3.5.2-.9 1-1.5 1.9-1.5.7 0 1.3.3 1.9.6.3-.7.9-1.1 1.7-1.1.7 0 1.4.4 2.4.8.5.3 1.2.6 1.6.7 3.4.1 7.6 2.2 8.9 7.6.3.1.6.3.8.5.4.5.5 1.1.3 1.9-.2 1.2 0 2.4 0 2.4l.2.8-1.2 1.2c-.5 2.8-1.6 5.4-2.2 6.5-.1.1-.1.4-.1.8 1 .3 1.7.5 1.7.5l1 .4v.8c2.5.5 4.6 1.1 6.1 1.9 1.8.9 2.9 3.5 3.4 7.8l.1.6-.4.5c-4.6 5.9-11.5 9.4-19 9.4z'/%3E%3Cpath fill='%23fff' d='M45.4 36.8c-1.5-.8-3.9-1.5-7-2v-.9s-1-.4-2.6-.7c-.2-.8-.3-2 .2-2.8.5-.9 1.7-3.6 2.1-6.5l.9-.9s-.3-1.4 0-3c.2-.9-.4-.7-.9-.5-.9-7.1-6.3-7.7-7.8-7.7-1.4-.2-3.9-2.2-4.1-1.3-.1.9 1.2 1.8-.4 1.4-1.6-.4-3.1-1.8-3.3-.8-.2.7 1.2 2.3 2 3.1-1.2 1.3-2.1 3.2-2.4 6.1-.5-.3-1.4-.7-1.1.2.3 1.3 0 2.6 0 2.6l1.4 1.2c.5 2.7 1.5 5.1 2 6 .5.8.3 2.1.2 2.8-1.5.3-2.6.7-2.6.7v1.2c-2.5.5-4.4 1.1-5.8 1.7-2 1-2.6 5.7-2.7 7.9v.4c4.1 4.4 10 7.2 16.5 7.2 7.3 0 13.7-3.5 17.8-8.8-.1-2.3-.8-5.7-2.4-6.6z'/%3E%3C/g%3E%3C/svg%3E",githubId:z=0,alt:k="",title:S="",primarySource:x=""}){const[y,I]=e.useState(x||E);return m=m||d,b=b||d,e.useEffect((()=>{v(I,{fallbackImage:E,primarySource:x,useGravatar:a,size:d,email:u,hash:g,fallback:w,rating:p,forcedefault:C,githubId:z,width:m,height:b,initials:s,offsetX:h,offsetY:f,background:l,color:r,fontFamily:c,fontSize:n,fontWeight:o})}),[x,E,u,w,C,z,g,s,h,f,l,r,c,n,o,p,d,a]),e.createElement("img",{src:y,width:m,height:b,alt:k,title:S,className:t})}return m.displayName="AvatarComponent",m.defaultProps={classes:"",useGravatar:!0,useGravatarFallback:!1,initials:"",color:"#888888",background:"#f4f6f7",fontSize:0,fontWeight:100,fontFamily:"'Sofia', 'Helvetica Neue', sans-serif",offsetX:void 0,offsetY:void 0,hash:"",email:"",size:80,width:void 0,height:void 0,fallback:"mm",rating:"x",forcedefault:!1,fallbackImage:"data:image/svg+xml,%3Csvg width='60' xmlns='http://www.w3.org/2000/svg' height='60' viewBox='0 0 60 60'%3E%3Cpath fill='%23bcc7ce' d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