diff --git a/core/client/app/components/gh-profile-image.js b/core/client/app/components/gh-profile-image.js index 14f2383b73..9a53faa69f 100644 --- a/core/client/app/components/gh-profile-image.js +++ b/core/client/app/components/gh-profile-image.js @@ -5,46 +5,52 @@ import Ember from 'ember'; * A component to manage a user profile image. By default it just handles picture uploads, * but if passed a bound 'email' property it will render the user's gravatar image * - * Example: {{gh-profile-image email=controllerEmailProperty setImage="controllerActionName"}} + * Example: {{gh-profile-image email=controllerEmailProperty setImage="controllerActionName" debounce=500}} * - * @param {int} size The size of the image to render - * @param {String} email Reference to a bound email object if gravatar image behavior is desired. - * @param {String} setImage The string name of the action on the controller to be called when an image is added. - * @property {Boolean} hasUploadedImage Whether or not the user has uploaded an image (whether or not to show the default image/gravatar image) - * @property {String} defaultImage String containing the background-image css property of the default user profile image - * @property {String} imageBackground String containing the background-image css property with the gravatar url + * @param {int} size The size of the image to render + * @param {String} email Reference to a bound email object if gravatar image behavior is desired. + * @param {String|action} setImage The string name of the action on the controller to be called when an image is added. + * @param {int} debounce Period to wait after changes to email before attempting to load gravatar + * @property {Boolean} hasUploadedImage Whether or not the user has uploaded an image (whether or not to show the default image/gravatar image) + * @property {String} defaultImage String containing the background-image css property of the default user profile image + * @property {String} imageBackground String containing the background-image css property with the gravatar url */ export default Ember.Component.extend({ email: '', - validEmail: '', size: 90, + debounce: 300, + + validEmail: '', hasUploadedImage: false, fileStorage: true, ghostPaths: Ember.inject.service('ghost-paths'), - hasEmail: Ember.computed.notEmpty('email'), + displayGravatar: Ember.computed.notEmpty('validEmail'), defaultImage: Ember.computed('ghostPaths', function () { - var url = this.get('ghostPaths.url').asset('/shared/img/user-image.png'); - return `background-image: url(${url})`.htmlSafe(); + const url = this.get('ghostPaths.url').asset('/shared/img/user-image.png'); + return Ember.String.htmlSafe(`background-image: url(${url})`); }), trySetValidEmail: function () { - var email = this.get('email'); - this.set('validEmail', validator.isEmail(email) ? email : ''); + if (!this.get('isDestroyed')) { + const email = this.get('email'); + this.set('validEmail', validator.isEmail(email) ? email : ''); + } }, - didReceiveAttrs: function () { - Ember.run.debounce(this, 'trySetValidEmail', 500); + didReceiveAttrs: function (attrs) { + const timeout = parseInt(attrs.newAttrs.throttle || this.get('debounce')); + Ember.run.debounce(this, 'trySetValidEmail', timeout); }, imageBackground: Ember.computed('validEmail', 'size', function () { - var email = this.get('validEmail'), - size = this.get('size'), - url; + const email = this.get('validEmail'), + size = this.get('size'); + if (email) { - url = `http://www.gravatar.com/avatar/${md5(email)}?s=${size}&d=blank`; - return `background-image: url(${url})`.htmlSafe(); + let url = `http://www.gravatar.com/avatar/${md5(email)}?s=${size}&d=blank`; + return Ember.String.htmlSafe(`background-image: url(${url})`); } }), @@ -52,6 +58,9 @@ export default Ember.Component.extend({ var size = this.get('size'), uploadElement = this.$('.js-file-input'); + // Fire this immediately in case we're initialized with a valid email + this.trySetValidEmail(); + // while theoretically the 'add' and 'processalways' functions could be // added as properties of the hash passed to fileupload(), for some reason // they needed to be placed in an on() call for the add method to work correctly @@ -69,11 +78,13 @@ export default Ember.Component.extend({ }, willDestroyElement: function () { - this.$('.js-file-input').fileupload('destroy'); + if (this.$('.js-file-input').data()['blueimp-fileupload']) { + this.$('.js-file-input').fileupload('destroy'); + } }, queueFile: function (e, data) { - var fileName = data.files[0].name; + const fileName = data.files[0].name; if ((/\.(gif|jpe?g|png|svg?z)$/i).test(fileName)) { this.sendAction('setImage', data); @@ -81,7 +92,7 @@ export default Ember.Component.extend({ }, triggerPreview: function (e, data) { - var file = data.files[data.index]; + const file = data.files[data.index]; if (file.preview) { this.set('hasUploadedImage', true); // necessary jQuery code because file.preview is a raw DOM object diff --git a/core/client/app/templates/components/gh-profile-image.hbs b/core/client/app/templates/components/gh-profile-image.hbs index bf8c1889ba..876767bab6 100644 --- a/core/client/app/templates/components/gh-profile-image.hbs +++ b/core/client/app/templates/components/gh-profile-image.hbs @@ -2,7 +2,7 @@ {{#unless hasUploadedImage}}
- {{#if hasEmail}} + {{#if displayGravatar}}