ES6ify /gr-avatar/*

Bug: Issue 6179
Change-Id: I8755222698aa6dd2383496b0848ae6d2aa74dab0
This commit is contained in:
Kasper Nilsson
2017-05-15 16:09:01 -07:00
parent 8b99460e3e
commit d834da0a4a
2 changed files with 26 additions and 27 deletions

View File

@@ -28,38 +28,38 @@
},
},
created: function() {
created() {
this.hidden = true;
},
attached: function() {
this.$.restAPI.getConfig().then(function(cfg) {
var hasAvatars = !!(cfg && cfg.plugin && cfg.plugin.has_avatars);
attached() {
this.$.restAPI.getConfig().then(cfg => {
const hasAvatars = !!(cfg && cfg.plugin && cfg.plugin.has_avatars);
if (hasAvatars) {
this.hidden = false;
// src needs to be set if avatar becomes visible
this._updateAvatarURL(this.account);
}
}.bind(this));
});
},
_accountChanged: function(account) {
_accountChanged(account) {
this._updateAvatarURL(account);
},
_updateAvatarURL: function(account) {
_updateAvatarURL(account) {
if (!this.hidden && account) {
var url = this._buildAvatarURL(this.account);
const url = this._buildAvatarURL(this.account);
if (url) {
this.style.backgroundImage = 'url("' + url + '")';
}
}
},
_buildAvatarURL: function(account) {
_buildAvatarURL(account) {
if (!account) { return ''; }
var avatars = account.avatars || [];
for (var i = 0; i < avatars.length; i++) {
const avatars = account.avatars || [];
for (let i = 0; i < avatars.length; i++) {
if (avatars[i].height === this.imageSize) {
return avatars[i].url;
}

View File

@@ -32,20 +32,20 @@ limitations under the License.
</test-fixture>
<script>
suite('gr-avatar tests', function() {
var element;
suite('gr-avatar tests', () => {
let element;
setup(function() {
setup(() => {
stub('gr-rest-api-interface', {
getConfig: function() { return Promise.resolve({}); },
getConfig() { return Promise.resolve({}); },
});
element = fixture('basic');
});
test('methods', function() {
test('methods', () => {
assert.equal(element._buildAvatarURL(
{
_account_id: 123
_account_id: 123,
}),
'/accounts/123/avatar?s=16');
assert.equal(element._buildAvatarURL(
@@ -54,15 +54,15 @@ limitations under the License.
avatars: [
{
url: 'https://cdn.example.com/s12-p/photo.jpg',
height: 12
height: 12,
},
{
url: 'https://cdn.example.com/s16-p/photo.jpg',
height: 16
height: 16,
},
{
url: 'https://cdn.example.com/s100-p/photo.jpg',
height: 100
height: 100,
},
],
}),
@@ -73,32 +73,31 @@ limitations under the License.
avatars: [
{
url: 'https://cdn.example.com/s95-p/photo.jpg',
height: 95
height: 95,
},
],
}),
'/accounts/123/avatar?s=16');
});
test('dom for existing account', function() {
test('dom for existing account', () => {
assert.isTrue(element.hasAttribute('hidden'),
'element not hidden initially');
element.hidden = false;
element.imageSize = 64;
element.account = {
_account_id: 123
_account_id: 123,
};
assert.isFalse(element.hasAttribute('hidden'), 'element hidden');
assert.isTrue(element.style.backgroundImage.indexOf(
'/accounts/123/avatar?s=64') > -1);
assert.isTrue(
element.style.backgroundImage.includes('/accounts/123/avatar?s=64'));
});
test('dom for non available account', function() {
test('dom for non available account', () => {
assert.isTrue(element.hasAttribute('hidden'),
'element not hidden initially');
element.account = undefined;
assert.isTrue(element.hasAttribute('hidden'), 'element not hidden');
});
});
</script>