mirror of
https://github.com/TryGhost/Ghost.git
synced 2025-02-03 23:00:14 -05:00
Fix Transparent Background in Gravatar Showing Background Image
Closes #5882 * If a gravatar image is available, remove the default image behind it * If gravatar image is not available, keep or replace the default image
This commit is contained in:
parent
cf8d8474fa
commit
11b2396e18
4 changed files with 20 additions and 39 deletions
|
@ -1,4 +1,5 @@
|
||||||
import Ember from 'ember';
|
import Ember from 'ember';
|
||||||
|
import AjaxService from 'ember-ajax/services/ajax';
|
||||||
|
|
||||||
const {
|
const {
|
||||||
Component,
|
Component,
|
||||||
|
@ -30,6 +31,8 @@ export default Component.extend({
|
||||||
validEmail: '',
|
validEmail: '',
|
||||||
hasUploadedImage: false,
|
hasUploadedImage: false,
|
||||||
fileStorage: true,
|
fileStorage: true,
|
||||||
|
ajax: AjaxService.create(),
|
||||||
|
config: service(),
|
||||||
|
|
||||||
ghostPaths: service(),
|
ghostPaths: service(),
|
||||||
displayGravatar: notEmpty('validEmail'),
|
displayGravatar: notEmpty('validEmail'),
|
||||||
|
@ -61,11 +64,22 @@ export default Component.extend({
|
||||||
imageBackground: computed('validEmail', 'size', function () {
|
imageBackground: computed('validEmail', 'size', function () {
|
||||||
let email = this.get('validEmail');
|
let email = this.get('validEmail');
|
||||||
let size = this.get('size');
|
let size = this.get('size');
|
||||||
|
|
||||||
let style = '';
|
let style = '';
|
||||||
|
|
||||||
if (email) {
|
if (email) {
|
||||||
let url = `//www.gravatar.com/avatar/${window.md5(email)}?s=${size}&d=blank`;
|
let gravatarUrl = `//www.gravatar.com/avatar/${window.md5(email)}?s=${size}&d=404`;
|
||||||
style = `background-image: url(${url})`;
|
|
||||||
|
this.get('ajax').request(gravatarUrl)
|
||||||
|
.catch((data) => {
|
||||||
|
let defaultImageUrl = `url("${this.get('ghostPaths.subdir')}/ghost/img/user-image.png")`;
|
||||||
|
|
||||||
|
if (data.errors !== undefined && Number(data.errors[0].status) === 404) {
|
||||||
|
this.$('.placeholder-img')[0].style.backgroundImage = Ember.String.htmlSafe(defaultImageUrl);
|
||||||
|
} else {
|
||||||
|
this.$('.placeholder-img')[0].style.backgroundImage = 'url()';
|
||||||
|
}
|
||||||
|
});
|
||||||
|
style = `background-image: url(${gravatarUrl})`;
|
||||||
}
|
}
|
||||||
return Ember.String.htmlSafe(style);
|
return Ember.String.htmlSafe(style);
|
||||||
}),
|
}),
|
||||||
|
|
|
@ -357,4 +357,6 @@ export function testConfig() {
|
||||||
user: record
|
user: record
|
||||||
};
|
};
|
||||||
});
|
});
|
||||||
|
|
||||||
|
this.passthrough('http://www.gravatar.com/avatar/**');
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,12 +1,9 @@
|
||||||
<figure class="account-image js-file-upload">
|
<figure class="account-image js-file-upload">
|
||||||
{{#unless hasUploadedImage}}
|
{{#unless hasUploadedImage}}
|
||||||
<div class="placeholder-img" style={{defaultImage}}></div>
|
<div class="placeholder-img" style={{defaultImage}}></div>
|
||||||
|
|
||||||
{{#if displayGravatar}}
|
|
||||||
<div id="account-image" class="gravatar-img" style={{imageBackground}}>
|
<div id="account-image" class="gravatar-img" style={{imageBackground}}>
|
||||||
<span class="sr-only">User image</span>
|
<span class="sr-only">User image</span>
|
||||||
</div>
|
</div>
|
||||||
{{/if}}
|
|
||||||
{{/unless}}
|
{{/unless}}
|
||||||
|
|
||||||
<div class="js-img-preview"></div>
|
<div class="js-img-preview"></div>
|
||||||
|
|
|
@ -56,7 +56,7 @@ describeComponent(
|
||||||
|
|
||||||
it('immediately renders the gravatar if valid email supplied', function () {
|
it('immediately renders the gravatar if valid email supplied', function () {
|
||||||
let email = 'test@example.com';
|
let email = 'test@example.com';
|
||||||
let expectedUrl = `//www.gravatar.com/avatar/${md5(email)}?s=100&d=blank`;
|
let expectedUrl = `//www.gravatar.com/avatar/${md5(email)}?s=100&d=404`;
|
||||||
|
|
||||||
this.set('email', email);
|
this.set('email', email);
|
||||||
|
|
||||||
|
@ -67,37 +67,5 @@ describeComponent(
|
||||||
expect(this.$('.gravatar-img').attr('style'), 'gravatar image style')
|
expect(this.$('.gravatar-img').attr('style'), 'gravatar image style')
|
||||||
.to.equal(`background-image: url(${expectedUrl})`);
|
.to.equal(`background-image: url(${expectedUrl})`);
|
||||||
});
|
});
|
||||||
|
|
||||||
it('throttles gravatar loading as email is changed', function (done) {
|
|
||||||
let email = 'test@example.com';
|
|
||||||
let expectedUrl = `//www.gravatar.com/avatar/${md5(email)}?s=100&d=blank`;
|
|
||||||
|
|
||||||
this.set('email', 'test');
|
|
||||||
|
|
||||||
this.render(hbs`
|
|
||||||
{{gh-profile-image email=email size=100 debounce=300}}
|
|
||||||
`);
|
|
||||||
|
|
||||||
expect(this.$('.gravatar-img').length, '.gravatar-img not shown for invalid email')
|
|
||||||
.to.equal(0);
|
|
||||||
|
|
||||||
run(() => {
|
|
||||||
this.set('email', email);
|
|
||||||
});
|
|
||||||
|
|
||||||
expect(this.$('.gravatar-img').length, '.gravatar-img not immediately changed on email change')
|
|
||||||
.to.equal(0);
|
|
||||||
|
|
||||||
Ember.run.later(this, function () {
|
|
||||||
expect(this.$('.gravatar-img').length, '.gravatar-img still not shown before throttle timeout')
|
|
||||||
.to.equal(0);
|
|
||||||
}, 250);
|
|
||||||
|
|
||||||
Ember.run.later(this, function () {
|
|
||||||
expect(this.$('.gravatar-img').attr('style'), '.gravatar-img style after timeout')
|
|
||||||
.to.equal(`background-image: url(${expectedUrl})`);
|
|
||||||
done();
|
|
||||||
}, 400);
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
|
|
Loading…
Add table
Reference in a new issue