-
Notifications
You must be signed in to change notification settings - Fork 6
/
polaris-avatar.js
305 lines (267 loc) · 6.32 KB
/
polaris-avatar.js
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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
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',
'hiddenClass',
'hasImage:Polaris-Avatar--hasImage',
],
layout,
/**
* Size of avatar
*
* @property size
* @public
* @type {String}
* @default 'medium'
*/
size: defaultSize,
/**
* The name of the person
*
* @property name
* @public
* @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 which falls back to initials if the image fails to load
*
* @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',
/**
* @property hasError
* @type {Boolean}
* @default false
* @private
*/
hasError: false,
/**
* @property hasLoaded
* @type {Boolean}
* @default false
* @private
*/
hasLoaded: false,
/**
* @property prevSource
* @type {String}
* @default null
* @private
*/
prevSource: null,
/**
* Image source to use (if any)
* @property finalSource
* @private
* @type {String}
*/
finalSource: or('source', 'customerImageSource').readOnly(),
/**
* Name to use (if any)
* @property nameString
* @private
* @type {String}
*/
nameString: or('name', 'initials').readOnly(),
/**
* Whether we have an image to use
* @property
* @private
* @type {Boolean}
*/
hasImage: computed('source', 'customer', 'hasError', function() {
let { source, customer, hasError } = this.getProperties(
'source',
'customer',
'hasError'
);
return (source || customer) && !hasError;
}).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('nameString', function() {
let nameString = this.get('nameString');
let styleIndex = isEmpty(nameString)
? 0
: nameString.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(),
/**
* Class name to hide avatar when loading
* @property hiddenClass
* @private
* @type {String}
*/
hiddenClass: computed('hasImage', 'hasLoaded', function() {
let { hasImage, hasLoaded } = this;
return hasImage && !hasLoaded ? 'Polaris-Avatar--hidden' : null;
}).readOnly(),
/**
* Image source when displaying a customer avatar
* @property customerImageSource
* @private
* @type {String}
*/
customerImageSource: computed('customer', 'nameString', function() {
if (!this.get('customer')) {
return null;
}
let nameString = this.get('nameString');
let avatarIndex = isEmpty(nameString)
? 0
: nameString.charCodeAt(0) % avatarImages.length;
return `${this.get('avatarSourcePath')}/avatar-${++avatarIndex}.svg`;
}).readOnly(),
/**
* Flag controlling whether the avatar initials should be rendered
* @property shouldShowInitials
* @private
* @type {Boolean}
*/
shouldShowInitials: computed('initials', 'hasImage', function() {
let { initials, hasImage } = this.getProperties('initials', 'hasImage');
return initials && !hasImage;
}).readOnly(),
/**
* Flag controlling whether the avatar image should be rendered
* @property shouldShowImage
* @private
* @type {Boolean}
*/
shouldShowImage: computed('finalSource', 'hasError', function() {
let { finalSource, hasError } = this.getProperties(
'finalSource',
'hasError'
);
return finalSource && !hasError;
}).readOnly(),
didReceiveAttrs() {
if (this.source !== this.prevSource) {
this.setProperties({
prevSource: this.source,
hasError: false,
hasLoaded: false,
});
}
},
actions: {
handleError() {
this.setProperties({ hasError: true, hasLoaded: false });
},
handleLoad() {
this.setProperties({ hasLoaded: true, hasError: false });
},
},
});