all files / addon/components/ polaris-avatar.js

100% Statements 25/25
92.86% Branches 13/14
100% Functions 4/4
100% Lines 25/25
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212                                                                                                                                                                                                                                                                                                13×   13×     12×     10×                       12× 12× 12×   12×                                         13× 12×            
import Component from '@ember/component';
import { isEmpty } from '@ember/utils';
import { computed } from '@ember/object';
import { or } from '@ember/object/computed';
import { classify } from '@ember/string';
import layout from '../templates/components/polaris-avatar';
 
const allowedSizes = [
  'small',
  'medium',
  'large'
];
const defaultSize = 'medium';
 
// TODO currently we don't need these...recheck when icons are properly handled
const avatarImages = [
  'avatarOne',
  'avatarTwo',
  'avatarThree',
  'avatarFour',
  'avatarFive',
  'avatarSix',
  'avatarSeven',
  'avatarEight',
  'avatarNine'
];
 
const styleClasses = [
  'one',
  'two',
  'three',
  'four',
  'five',
  'six'
];
 
export default Component.extend({
  tagName: 'span',
  attributeBindings: [
    'role',
    'label:aria-label'
  ],
  classNames: ['Polaris-Avatar'],
  classNameBindings: [
    'styleClass',
    'sizeClass'
  ],
 
  layout,
 
  /**
   * Size of avatar
   *
   * @property size
   * @type {String}
   * @default 'medium'
   */
  size: defaultSize,
 
  /**
   * The name of the person
   *
   * @property name
   * @type {String}
   * @default null
   */
  name: null,
 
  /**
   * Initials of person to display
   *
   * @property initials
   * @public
   * @type {String}
   * @default null
   */
  initials: null,
 
  /**
   * Whether the avatar is for a customer
   *
   * @property customer
   * @public
   * @type {boolean}
   * @default false
   */
  customer: false,
 
  /**
   * URL of the avatar image
   *
   * @property source
   * @public
   * @type {String}
   * @default null
   */
  source: null,
 
  /**
   * Accessible label for the avatar image
   *
   * @property accessibilityLabel
   * @public
   * @type {String}
   * @default null
   */
  accessibilityLabel: null,
 
  /**
   * Path to the Polaris avatar images
   * TODO: read this from config? Need a way to set this by default?
   * @property avatarSourcePath
   * @private
   * @type {String}
   */
  avatarSourcePath: '',
 
  /**
   * Role attribute value
   * @property role
   * @private
   * @type {String}
   */
  role: 'img',
 
  /**
   * Image source to use (if any)
   * @property finalSource
   * @private
   * @type {String}
   */
  finalSource: or('source', 'customerImageSource').readOnly(),
 
  /**
   * Name to use (if any)
   * @property finalName
   * @private
   * @type {String}
   */
  finalName: or('name', 'initials').readOnly(),
 
  /**
   * Accessibility label to apply to avatar
   * @property label
   * @private
   * @type {String}
   */
  label: computed('accessibilityLabel', 'name', 'initials', function() {
    let { accessibilityLabel, name, initials } = this.getProperties('accessibilityLabel', 'name', 'initials');
 
    if (accessibilityLabel) {
      return accessibilityLabel;
    }
 
    if (name) {
      return name;
    }
 
    if (initials) {
      return `Avatar with initials ${ initials.split('').join(' ') }`;
    }
 
    return 'Avatar';
  }).readOnly(),
 
  /**
   * Class name to set avatar style
   * @property styleClass
   * @private
   * @type {String}
   */
  styleClass: computed('finalName', function() {
    let finalName = this.get('finalName');
    let styleIndex = isEmpty(finalName) ? 0 : finalName.charCodeAt(0) % styleClasses.length;
    let style = styleClasses[styleIndex];
 
    return `Polaris-Avatar--style${ classify(style) }`;
  }).readOnly(),
 
  /**
   * Class name to set avatar size
   * @property sizeClass
   * @private
   * @type {String}
   */
  sizeClass: computed('size', function() {
    let size = this.get('size');
    if (allowedSizes.indexOf(size) === -1) {
      size = defaultSize;
    }
 
    return `Polaris-Avatar--size${ classify(size) }`;
  }).readOnly(),
 
  /**
   * Image source when displaying a customer avatar
   * @property customerImageSource
   * @private
   * @type {String}
   */
  customerImageSource: computed('customer', 'finalName', function() {
    if (!this.get('customer')) {
      return null;
    }
 
    let finalName = this.get('finalName');
    let avatarIndex = isEmpty(finalName) ? 0 : finalName.charCodeAt(0) % avatarImages.length;
    return `${ this.get('avatarSourcePath') }/avatar-${ ++avatarIndex }.svg`;
  }).readOnly(),
});