diff --git a/app/assets/javascripts/discourse/controllers/avatar_selector_controller.js b/app/assets/javascripts/discourse/controllers/avatar_selector_controller.js new file mode 100644 index 00000000000..d4aebbc9793 --- /dev/null +++ b/app/assets/javascripts/discourse/controllers/avatar_selector_controller.js @@ -0,0 +1,46 @@ +/** + The modal for selecting an avatar + + @class AvatarSelectorController + @extends Discourse.Controller + @namespace Discourse + @uses Discourse.ModalFunctionality + @module Discourse +**/ +Discourse.AvatarSelectorController = Discourse.Controller.extend(Discourse.ModalFunctionality, { + init: function() { + // copy some data to support the cancel action + this.setProperties(this.get("currentUser").getProperties( + "username", + "has_uploaded_avatar", + "use_uploaded_avatar", + "gravatar_template", + "uploaded_avatar_template" + )); + }, + + toggleUseUploadedAvatar: function(toggle) { + this.set("use_uploaded_avatar", toggle); + }, + + saveAvatarSelection: function() { + // sends the information to the server if it has changed + if (this.get("use_uploaded_avatar") !== this.get("currentUser.use_uploaded_avatar")) { + var data = { use_uploaded_avatar: this.get("use_uploaded_avatar") }; + Discourse.ajax("/users/" + this.get("currentUser.username") + "/preferences/avatar/toggle", { type: 'PUT', data: data }); + } + // saves the data back to the currentUser object + var currentUser = this.get("currentUser"); + currentUser.setProperties(this.getProperties( + "has_uploaded_avatar", + "use_uploaded_avatar", + "gravatar_template", + "uploaded_avatar_template" + )); + if (this.get("use_uploaded_avatar")) { + currentUser.set("avatar_template", this.get("uploaded_avatar_template")); + } else { + currentUser.set("avatar_template", this.get("gravatar_template")); + } + } +}); diff --git a/app/assets/javascripts/discourse/controllers/preferences_avatar_controller.js b/app/assets/javascripts/discourse/controllers/preferences_avatar_controller.js deleted file mode 100644 index 41180495972..00000000000 --- a/app/assets/javascripts/discourse/controllers/preferences_avatar_controller.js +++ /dev/null @@ -1,84 +0,0 @@ -/** - This controller supports actions related to updating one's avatar - - @class PreferencesAvatarController - @extends Discourse.ObjectController - @namespace Discourse - @module Discourse -**/ -Discourse.PreferencesAvatarController = Discourse.ObjectController.extend({ - uploading: false, - uploadProgress: 0, - uploadDisabled: Em.computed.or("uploading"), - useGravatar: Em.computed.not("use_uploaded_avatar"), - useUploadedAvatar: Em.computed.alias("use_uploaded_avatar"), - - toggleUseUploadedAvatar: function(toggle) { - if (this.get("use_uploaded_avatar") !== toggle) { - var controller = this; - this.set("use_uploaded_avatar", toggle); - Discourse.ajax("/users/" + this.get("username") + "/preferences/avatar/toggle", { type: 'PUT', data: { use_uploaded_avatar: toggle }}) - .then(function(result) { controller.set("avatar_template", result.avatar_template); }); - } - }, - - uploadButtonText: function() { - return this.get("uploading") ? I18n.t("user.change_avatar.uploading") : I18n.t("user.change_avatar.upload"); - }.property("uploading"), - - uploadAvatar: function() { - var controller = this; - var $upload = $("#avatar-input"); - - // do nothing if no file is selected - if (Em.isEmpty($upload.val())) { return; } - - this.set("uploading", true); - - // define the upload endpoint - $upload.fileupload({ - url: Discourse.getURL("/users/" + this.get("username") + "/preferences/avatar"), - dataType: "json", - timeout: 20000 - }); - - // when there is a progression for the upload - $upload.on("fileuploadprogressall", function (e, data) { - var progress = parseInt(data.loaded / data.total * 100, 10); - controller.set("uploadProgress", progress); - }); - - // when the upload is successful - $upload.on("fileuploaddone", function (e, data) { - // set some properties - controller.setProperties({ - has_uploaded_avatar: true, - use_uploaded_avatar: true, - avatar_template: data.result.url, - uploaded_avatar_template: data.result.url - }); - }); - - // when there has been an error with the upload - $upload.on("fileuploadfail", function (e, data) { - Discourse.Utilities.displayErrorForUpload(data); - }); - - // when the upload is done - $upload.on("fileuploadalways", function (e, data) { - // prevent automatic upload when selecting a file - $upload.fileupload("destroy"); - $upload.off(); - // clear file input - $upload.val(""); - // indicate upload is done - controller.setProperties({ - uploading: false, - uploadProgress: 0 - }); - }); - - // *actually* launch the upload - $("#avatar-input").fileupload("add", { fileInput: $("#avatar-input") }); - } -}); diff --git a/app/assets/javascripts/discourse/helpers/application_helpers.js b/app/assets/javascripts/discourse/helpers/application_helpers.js index b941f29144b..34ee470db11 100644 --- a/app/assets/javascripts/discourse/helpers/application_helpers.js +++ b/app/assets/javascripts/discourse/helpers/application_helpers.js @@ -171,24 +171,9 @@ Handlebars.registerHelper('avatar', function(user, options) { Ember.Handlebars.registerBoundHelper('boundAvatar', function(user, options) { return new Handlebars.SafeString(Discourse.Utilities.avatarImg({ size: options.hash.imageSize, - avatarTemplate: Em.get(user, 'avatar_template') + avatarTemplate: Em.get(user, options.hash.template || 'avatar_template') })); -}, 'avatar_template'); - -/** - Bound avatar helper. - Will rerender whenever the "uploaded_avatar_template" changes. - Only available for the current user. - - @method boundUploadedAvatar - @for Handlebars -**/ -Ember.Handlebars.registerBoundHelper('boundUploadedAvatar', function(user, options) { - return new Handlebars.SafeString(Discourse.Utilities.avatarImg({ - size: options.hash.imageSize, - avatarTemplate: Em.get(user, 'uploaded_avatar_template') - })); -}, 'uploaded_avatar_template'); +}, 'avatar_template', 'uploaded_avatar_template', 'gravatar_template'); /** Nicely format a date without a binding since the date doesn't need to change. diff --git a/app/assets/javascripts/discourse/routes/preferences_routes.js b/app/assets/javascripts/discourse/routes/preferences_routes.js index 55427f4b9ff..0ddc31a4579 100644 --- a/app/assets/javascripts/discourse/routes/preferences_routes.js +++ b/app/assets/javascripts/discourse/routes/preferences_routes.js @@ -13,6 +13,13 @@ Discourse.PreferencesRoute = Discourse.RestrictedUserRoute.extend({ renderTemplate: function() { this.render('preferences', { into: 'user', outlet: 'userOutlet', controller: 'preferences' }); + }, + + events: { + showAvatarSelector: function() { + Discourse.Route.showModal(this, 'avatarSelector'); + this.controllerFor("avatarSelector").init(); + } } }); @@ -117,32 +124,3 @@ Discourse.PreferencesUsernameRoute = Discourse.RestrictedUserRoute.extend({ controller.setProperties({ model: user, newUsername: user.get('username') }); } }); - - -/** - The route for updating a user's avatar - - @class PreferencesAvatarRoute - @extends Discourse.RestrictedUserRoute - @namespace Discourse - @module Discourse -**/ -Discourse.PreferencesAvatarRoute = Discourse.RestrictedUserRoute.extend({ - model: function() { - return this.modelFor('user'); - }, - - renderTemplate: function() { - return this.render({ into: 'user', outlet: 'userOutlet' }); - }, - - // A bit odd, but if we leave to /preferences we need to re-render that outlet - exit: function() { - this._super(); - this.render('preferences', { into: 'user', outlet: 'userOutlet', controller: 'preferences' }); - }, - - setupController: function(controller, user) { - controller.setProperties({ model: user }); - } -}); diff --git a/app/assets/javascripts/discourse/templates/modal/auto_close.js.handlebars b/app/assets/javascripts/discourse/templates/modal/auto_close.js.handlebars index 49d1d824e7d..7b400d1c8fd 100644 --- a/app/assets/javascripts/discourse/templates/modal/auto_close.js.handlebars +++ b/app/assets/javascripts/discourse/templates/modal/auto_close.js.handlebars @@ -5,6 +5,6 @@
\ No newline at end of file + diff --git a/app/assets/javascripts/discourse/templates/modal/avatar_selector.js.handlebars b/app/assets/javascripts/discourse/templates/modal/avatar_selector.js.handlebars new file mode 100644 index 00000000000..46e2be87b9d --- /dev/null +++ b/app/assets/javascripts/discourse/templates/modal/avatar_selector.js.handlebars @@ -0,0 +1,29 @@ +