|
| 1 | +<template> |
| 2 | + <div><div id="avatar" v-bind:style="style"> |
| 3 | + <span v-if="!this.src">{{ userInitial }}</span> |
| 4 | + </div></div> |
| 5 | +</template> |
| 6 | + |
| 7 | +<script type="text/babel"> |
| 8 | +export default { |
| 9 | +
|
| 10 | + props: { |
| 11 | + username: { |
| 12 | + type: String, |
| 13 | + required: true |
| 14 | + }, |
| 15 | + backgroundColor: { |
| 16 | + type: String |
| 17 | + }, |
| 18 | + color: { |
| 19 | + type: String |
| 20 | + }, |
| 21 | + size: { |
| 22 | + type: Number, |
| 23 | + default: 50 |
| 24 | + }, |
| 25 | + src: { |
| 26 | + type: String |
| 27 | + }, |
| 28 | + rounded: { |
| 29 | + type: Boolean, |
| 30 | + default: true |
| 31 | + }, |
| 32 | + lighten: { |
| 33 | + type: Number, |
| 34 | + default: 80 |
| 35 | + } |
| 36 | + }, |
| 37 | +
|
| 38 | + data() { |
| 39 | + return { |
| 40 | + backgroundColors: [ |
| 41 | + '#F44336', '#FF4081', '#9C27B0', '#673AB7', |
| 42 | + '#3F51B5', '#2196F3', '#03A9F4', '#00BCD4', '#009688', |
| 43 | + '#4CAF50', '#8BC34A', '#CDDC39', /*'#FFEB3B',*/ '#FFC107', |
| 44 | + '#FF9800', '#FF5722', '#795548', '#9E9E9E', '#607D8B'] |
| 45 | + } |
| 46 | + }, |
| 47 | +
|
| 48 | + computed: { |
| 49 | + background() { |
| 50 | + return this.backgroundColor |
| 51 | + || this.randomBackgroundColor(this.username.length, this.backgroundColors) |
| 52 | + }, |
| 53 | +
|
| 54 | + fontColor() { |
| 55 | + return this.color || this.lightenColor(this.background, this.lighten) |
| 56 | + }, |
| 57 | +
|
| 58 | + isImage() { |
| 59 | + return this.src != undefined |
| 60 | + }, |
| 61 | +
|
| 62 | + style() { |
| 63 | + const style = { |
| 64 | + width: this.size + 'px', |
| 65 | + height: this.size + 'px', |
| 66 | + borderRadius: (this.rounded) ? '50%' : 0, |
| 67 | + textAlign: 'center', |
| 68 | + verticalAlign: 'middle' |
| 69 | + } |
| 70 | +
|
| 71 | + const backgroundAndFontStyle = (this.isImage) |
| 72 | + ? { |
| 73 | + background: 'url(' + this.src + ') no-repeat', |
| 74 | + backgroundSize: this.size + 'px ' + this.size + 'px', |
| 75 | + backgroundOrigin: 'content-box' |
| 76 | + } |
| 77 | + : { |
| 78 | + backgroundColor: this.background, |
| 79 | + font: Math.floor(this.size / 2.5) + 'px/100px Helvetica, Arial, sans-serif', |
| 80 | + fontWeight: "bold", |
| 81 | + color: this.fontColor, |
| 82 | + lineHeight: (this.size + Math.floor(this.size/20)) + 'px' |
| 83 | + } |
| 84 | +
|
| 85 | + Object.assign(style, backgroundAndFontStyle) |
| 86 | +
|
| 87 | + return style; |
| 88 | + }, |
| 89 | +
|
| 90 | + userInitial() { |
| 91 | + return this.initial(this.username) |
| 92 | + } |
| 93 | + }, |
| 94 | +
|
| 95 | + methods: { |
| 96 | + initial(username) { |
| 97 | + let parts = username.split(/[ -]/), |
| 98 | + initials = '' |
| 99 | +
|
| 100 | + for (var i=0; i < parts.length; i++) |
| 101 | + initials += parts[i].charAt(0) |
| 102 | +
|
| 103 | + if (initials.length > 3 && initials.search(/[A-Z]/) != -1) |
| 104 | + initials = initials.replace(/[a-z]+/g, '') |
| 105 | +
|
| 106 | + initials = initials.substr(0, 3).toUpperCase() |
| 107 | +
|
| 108 | + return initials |
| 109 | + }, |
| 110 | +
|
| 111 | + randomBackgroundColor(seed, colors) { |
| 112 | + return colors[seed % (colors.length)]; |
| 113 | + }, |
| 114 | +
|
| 115 | + lightenColor(hex, amt) { |
| 116 | + // From https://css-tricks.com/snippets/javascript/lighten-darken-color/ |
| 117 | + var usePound = false |
| 118 | +
|
| 119 | + if (hex[0] == "#") { |
| 120 | + hex = hex.slice(1) |
| 121 | + usePound = true |
| 122 | + } |
| 123 | +
|
| 124 | + var num = parseInt(hex,16) |
| 125 | + var r = (num >> 16) + amt |
| 126 | +
|
| 127 | + if (r > 255) r = 255 |
| 128 | + else if (r < 0) r = 0 |
| 129 | +
|
| 130 | + var b = ((num >> 8) & 0x00FF) + amt |
| 131 | +
|
| 132 | + if (b > 255) b = 255 |
| 133 | + else if (b < 0) b = 0 |
| 134 | +
|
| 135 | + var g = (num & 0x0000FF) + amt |
| 136 | +
|
| 137 | + if (g > 255) g = 255 |
| 138 | + else if (g < 0) g = 0 |
| 139 | +
|
| 140 | + return (usePound?"#":"") + (g | (b << 8) | (r << 16)).toString(16) |
| 141 | + } |
| 142 | + } |
| 143 | +} |
| 144 | +</script> |
0 commit comments