modal.js 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. (function (Misago) {
  2. 'use strict';
  3. var ViewModel = function(user, _) {
  4. var self = this;
  5. this.isBusy = false;
  6. this.user = user;
  7. this.options = null;
  8. this.component = _.component('change-avatar:loading', self);
  9. this.api = _.ajax.buildApiUrl(['users', user.id, 'avatar']);
  10. // Load user avatar options
  11. _.ajax.get(this.api).then(function(options) {
  12. m.startComputation();
  13. self.options = options;
  14. self.component = _.component('change-avatar:start', self);
  15. m.endComputation();
  16. }, function(rejection) {
  17. if (rejection.status === 403) {
  18. m.startComputation();
  19. self.component = _.component('change-avatar:denied', rejection);
  20. m.endComputation();
  21. } else {
  22. _.modal();
  23. _.ajax.error(rejection);
  24. }
  25. });
  26. // Avatar actions
  27. var avatarRpc = function(avatarType) {
  28. if (self.isBusy) {
  29. return;
  30. }
  31. self.isBusy = true;
  32. _.ajax.post(self.api, {avatar: avatarType}).then(function(response) {
  33. m.startComputation();
  34. _.alert.success(response.detail);
  35. self.options = response.options;
  36. self.user.avatar_hash = response.avatar_hash;
  37. self.isBusy = false;
  38. m.endComputation();
  39. }, function(rejection) {
  40. _.modal();
  41. _.ajax.error(rejection);
  42. })
  43. };
  44. this.downloadGravatar = function() {
  45. avatarRpc('gravatar');
  46. };
  47. this.generateAvatar = function() {
  48. avatarRpc('generated');
  49. };
  50. };
  51. var persistent = function(el, isInit, context) {
  52. context.retain = true;
  53. };
  54. var modal = {
  55. class: '.modal-dialog.modal-form.modal-change-avatar',
  56. controller: function(user, _) {
  57. return {
  58. vm: new ViewModel(user, _)
  59. };
  60. },
  61. view: function(ctrl, user, _) {
  62. return m(this.class + '[role="document"]', {config: persistent},
  63. m('.modal-content', [
  64. _.component('modal:header', gettext("Change avatar")),
  65. ctrl.vm.component
  66. ])
  67. );
  68. }
  69. };
  70. Misago.addService('modal:change-avatar', function(_) {
  71. _.modal('change-avatar', modal);
  72. },
  73. {
  74. after: 'modals'
  75. });
  76. }(Misago.prototype));