FIX: avatar thumbnail won't save after upload

This commit is contained in:
Régis Hanol 2013-08-27 23:01:35 +02:00
parent 73489b652e
commit 96772af35b
5 changed files with 73 additions and 38 deletions

View File

@ -8,7 +8,15 @@
@module Discourse @module Discourse
**/ **/
Discourse.AvatarSelectorController = Discourse.Controller.extend(Discourse.ModalFunctionality, { Discourse.AvatarSelectorController = Discourse.Controller.extend(Discourse.ModalFunctionality, {
toggleUseUploadedAvatar: function(toggle) { useUploadedAvatar: function() {
this.set("use_uploaded_avatar", toggle); this.set("use_uploaded_avatar", true);
} },
useGravatar: function() {
this.set("use_uploaded_avatar", false);
},
avatarTemplate: function() {
return this.get("use_uploaded_avatar") ? this.get("uploaded_avatar_template") : this.get("gravatar_template");
}.property("use_uploaded_avatar", "uploaded_avatar_template", "gravatar_template")
}); });

View File

@ -28,7 +28,11 @@ Discourse.User = Discourse.Model.extend({
searchContext: function() { searchContext: function() {
return ({ type: 'user', id: this.get('username_lower'), user: this }); return {
type: 'user',
id: this.get('username_lower'),
user: this
};
}.property('username_lower'), }.property('username_lower'),
/** /**
@ -101,7 +105,7 @@ Discourse.User = Discourse.Model.extend({
@returns Result of ajax call @returns Result of ajax call
**/ **/
changeUsername: function(newUsername) { changeUsername: function(newUsername) {
return Discourse.ajax("/users/" + (this.get('username_lower')) + "/preferences/username", { return Discourse.ajax("/users/" + this.get('username_lower') + "/preferences/username", {
type: 'PUT', type: 'PUT',
data: { new_username: newUsername } data: { new_username: newUsername }
}); });
@ -115,7 +119,7 @@ Discourse.User = Discourse.Model.extend({
@returns Result of ajax call @returns Result of ajax call
**/ **/
changeEmail: function(email) { changeEmail: function(email) {
return Discourse.ajax("/users/" + (this.get('username_lower')) + "/preferences/email", { return Discourse.ajax("/users/" + this.get('username_lower') + "/preferences/email", {
type: 'PUT', type: 'PUT',
data: { email: email } data: { email: email }
}); });
@ -173,9 +177,7 @@ Discourse.User = Discourse.Model.extend({
changePassword: function() { changePassword: function() {
return Discourse.ajax("/session/forgot_password", { return Discourse.ajax("/session/forgot_password", {
dataType: 'json', dataType: 'json',
data: { data: { login: this.get('username') },
login: this.get('username')
},
type: 'POST' type: 'POST'
}); });
}, },
@ -266,11 +268,14 @@ Discourse.User = Discourse.Model.extend({
Change avatar selection Change avatar selection
@method toggleAvatarSelection @method toggleAvatarSelection
@param {Boolean} useUploadedAvatar true if the user is using the uploaded avatar
@returns {Promise} the result of the toggle avatar selection @returns {Promise} the result of the toggle avatar selection
*/ */
toggleAvatarSelection: function() { toggleAvatarSelection: function(useUploadedAvatar) {
var data = { use_uploaded_avatar: this.get("use_uploaded_avatar") }; return Discourse.ajax("/users/" + this.get("username_lower") + "/preferences/avatar/toggle", {
return Discourse.ajax("/users/" + this.get("username") + "/preferences/avatar/toggle", { type: 'PUT', data: data }); type: 'PUT',
data: { use_uploaded_avatar: useUploadedAvatar }
});
} }
}); });

View File

@ -18,35 +18,29 @@ Discourse.PreferencesRoute = Discourse.RestrictedUserRoute.extend({
events: { events: {
showAvatarSelector: function() { showAvatarSelector: function() {
Discourse.Route.showModal(this, 'avatarSelector'); Discourse.Route.showModal(this, 'avatarSelector');
var user = this.modelFor("user"); // all the properties needed for displaying the avatar selector modal
console.log(user); var avatarSelector = this.modelFor('user').getProperties(
this.controllerFor("avatarSelector").setProperties(user.getProperties( 'username', 'email',
"username", 'has_uploaded_avatar', 'use_uploaded_avatar',
"email", 'gravatar_template', 'uploaded_avatar_template');
"has_uploaded_avatar", this.controllerFor('avatarSelector').setProperties(avatarSelector);
"use_uploaded_avatar",
"gravatar_template",
"uploaded_avatar_template"
));
}, },
saveAvatarSelection: function() { saveAvatarSelection: function() {
var user = this.modelFor("user"); var user = this.modelFor('user');
var avatar = this.controllerFor("avatarSelector"); var avatarSelector = this.controllerFor('avatarSelector');
// sends the information to the server if it has changed // sends the information to the server if it has changed
if (avatar.get("use_uploaded_avatar") !== user.get("use_uploaded_avatar")) { user.toggleAvatarSelection(); } if (avatarSelector.get('use_uploaded_avatar') !== user.get('use_uploaded_avatar')) {
// saves the data back user.toggleAvatarSelection(avatarSelector.get('use_uploaded_avatar'));
user.setProperties(avatar.getProperties(
"has_uploaded_avatar",
"use_uploaded_avatar",
"gravatar_template",
"uploaded_avatar_template"
));
if (avatar.get("use_uploaded_avatar")) {
user.set("avatar_template", avatar.get("uploaded_avatar_template"));
} else {
user.set("avatar_template", avatar.get("gravatar_template"));
} }
// saves the data back
user.setProperties(avatarSelector.getProperties(
'has_uploaded_avatar',
'use_uploaded_avatar',
'gravatar_template',
'uploaded_avatar_template'
));
user.set('avatar_template', avatarSelector.get('avatarTemplate'));
} }
} }
}); });

View File

@ -1,12 +1,12 @@
<div class="modal-body"> <div class="modal-body">
<div> <div>
<div> <div>
<input type="radio" id="avatar" name="avatar" value="gravatar" {{action toggleUseUploadedAvatar false}}> <input type="radio" id="avatar" name="avatar" value="gravatar" {{action useGravatar}}>
<label class="radio" for="avatar">{{avatar controller imageSize="large" template="gravatar_template"}} {{{i18n user.change_avatar.gravatar}}} {{email}}</label> <label class="radio" for="avatar">{{avatar controller imageSize="large" template="gravatar_template"}} {{{i18n user.change_avatar.gravatar}}} {{email}}</label>
<a href="//gravatar.com/emails" target="_blank" title="{{i18n user.change_avatar.gravatar_title}}" class="btn"><i class="icon-pencil"></i></a> <a href="//gravatar.com/emails" target="_blank" title="{{i18n user.change_avatar.gravatar_title}}" class="btn"><i class="icon-pencil"></i></a>
</div> </div>
<div> <div>
<input type="radio" id="uploaded_avatar" name="avatar" value="uploaded_avatar" {{action toggleUseUploadedAvatar true}}> <input type="radio" id="uploaded_avatar" name="avatar" value="uploaded_avatar" {{action useUploadedAvatar}}>
<label class="radio" for="uploaded_avatar"> <label class="radio" for="uploaded_avatar">
{{#if has_uploaded_avatar}} {{#if has_uploaded_avatar}}
{{boundAvatar controller imageSize="large" template="uploaded_avatar_template"}} {{i18n user.change_avatar.uploaded_avatar}} {{boundAvatar controller imageSize="large" template="uploaded_avatar_template"}} {{i18n user.change_avatar.uploaded_avatar}}

View File

@ -0,0 +1,28 @@
var avatarSelector = Em.Object.create({
use_uploaded_avatar: false,
gravatar_template: "//www.gravatar.com/avatar/c6e17f2ae2a215e87ff9e878a4e63cd9.png?s={size}&r=pg&d=identicon",
uploaded_avatar_template: "//cdn.discourse.org/uploads/meta_discourse/avatars/093/607/185cff113e/{size}.jpg"
});
module("Discourse.AvatarSelectorController");
test("avatarTemplate", function() {
var avatarSelectorController = controllerFor("avatarSelector");
avatarSelectorController.setProperties(avatarSelector);
equal(avatarSelectorController.get("avatarTemplate"),
avatarSelector.get("gravatar_template"),
"we are using gravatar by default");
avatarSelectorController.useUploadedAvatar();
equal(avatarSelectorController.get("avatarTemplate"),
avatarSelector.get("uploaded_avatar_template"),
"calling useUploadedAvatar switches to using the uploaded avatar");
avatarSelectorController.useGravatar();
equal(avatarSelectorController.get("avatarTemplate"),
avatarSelector.get("gravatar_template"),
"calling useGravatar switches to using gravatar");
});