Просмотр исходного кода

Changed some controllers into components

Rafał Pitoń 10 лет назад
Родитель
Сommit
7e512490eb
31 измененных файлов с 190 добавлено и 73 удалено
  1. 19 0
      misago/emberapp/app/components/guest-nav.js
  2. 115 0
      misago/emberapp/app/components/login-modal.js
  3. 6 0
      misago/emberapp/app/components/toast-message.js
  4. 1 1
      misago/emberapp/app/controllers/forgotten-password/change-form.js
  5. 0 9
      misago/emberapp/app/controllers/guest-nav.js
  6. 2 6
      misago/emberapp/app/controllers/login-modal.js
  7. 4 0
      misago/emberapp/app/controllers/register.js
  8. 5 0
      misago/emberapp/app/controllers/user-nav.js
  9. 2 0
      misago/emberapp/app/initializers/auth-service.js
  10. 3 1
      misago/emberapp/app/initializers/clock-service.js
  11. 1 1
      misago/emberapp/app/initializers/csrf-service.js
  12. 8 5
      misago/emberapp/app/initializers/misago-settings.js
  13. 0 14
      misago/emberapp/app/initializers/page-title-service.js
  14. 1 1
      misago/emberapp/app/initializers/rpc-service.js
  15. 1 1
      misago/emberapp/app/routes/activation/activate.js
  16. 0 12
      misago/emberapp/app/routes/application.js
  17. 4 0
      misago/emberapp/app/services/auth.js
  18. 1 1
      misago/emberapp/app/services/clock.js
  19. 2 2
      misago/emberapp/app/templates/application.hbs
  20. 0 0
      misago/emberapp/app/templates/components/.gitkeep
  21. 7 0
      misago/emberapp/app/templates/components/guest-nav.hbs
  22. 0 0
      misago/emberapp/app/templates/components/login-modal.hbs
  23. 3 0
      misago/emberapp/app/templates/components/toast-message.hbs
  24. 0 9
      misago/emberapp/app/templates/guest-nav.hbs
  25. 1 1
      misago/emberapp/app/templates/navbar.hbs
  26. 0 5
      misago/emberapp/app/templates/toast-message.hbs
  27. 1 1
      misago/emberapp/app/templates/user-nav.hbs
  28. 1 1
      misago/emberapp/tests/helpers/toast-message.js
  29. 1 1
      misago/emberapp/tests/unit/components/guest-nav-test.js
  30. 1 1
      misago/emberapp/tests/unit/components/login-modal-test.js
  31. 0 0
      misago/emberapp/tests/unit/services/toast-message-test.js

+ 19 - 0
misago/emberapp/app/components/guest-nav.js

@@ -0,0 +1,19 @@
+import Ember from 'ember';
+
+export default Ember.Component.extend({
+  classNames: ['guest-nav', 'navbar-right'],
+
+  router: function() {
+    return this.container.lookup('router:main');
+  }.property(),
+
+  actions: {
+    login: function() {
+      this.auth.openLoginModal();
+    },
+
+    register: function() {
+      this.get('router').transitionTo('register');
+    }
+  }
+});

+ 115 - 0
misago/emberapp/app/components/login-modal.js

@@ -0,0 +1,115 @@
+import Ember from 'ember';
+
+export default Ember.Component.extend({
+  modal: null,
+
+  isLoading: false,
+  showActivation: false,
+
+  username: '',
+  password: '',
+
+  router: function() {
+    return this.container.lookup('router:main');
+  }.property(),
+
+  setup: function() {
+    this.modal = Ember.$('#loginModal').modal({show: false});
+
+    this.modal.on('shown.bs.modal', function () {
+      Ember.$('#loginModal').focus();
+    });
+
+    var self = this;
+    this.modal.on('hidden.bs.modal', function() {
+      self.reset();
+    });
+  }.on('didInsertElement'),
+
+  reset: function() {
+    this.setProperties({
+      'username': '',
+      'password': '',
+
+      'isLoading': false,
+      'showActivation': false
+    });
+  },
+
+  success: function(credentials) {
+    var $form = Ember.$('#hidden-login-form');
+
+    // refresh CSRF token because parent api call changed it
+    this.csrf.updateFormToken($form);
+
+    // fill out form with user credentials and submit it, this will tell
+    // misago to redirect user back to right page, and will trigger browser's
+    // key ring feature
+    $form.find('input[name=redirect_to]').val(window.location.href);
+    $form.find('input[name=username]').val(credentials.username);
+    $form.find('input[name=password]').val(credentials.password);
+    $form.submit();
+  },
+
+  toastError: 'toastError',
+  showBan: 'showBan',
+
+  error: function(jqXHR) {
+    var rejection = jqXHR.responseJSON;
+    if (jqXHR.status !== 400) {
+      this.sendAction('toastError', jqXHR);
+    } else if (rejection.code === 'inactive_admin') {
+      this.toast.info(rejection.detail);
+    } else if (rejection.code === 'inactive_user') {
+      this.toast.info(rejection.detail);
+      this.set('showActivation', true);
+    } else if (rejection.code === 'banned') {
+      this.sendAction('showBan', rejection.detail);
+      Ember.run(function() {
+        Ember.$('#loginModal').modal('hide');
+      });
+    } else {
+      this.toast.error(rejection.detail);
+    }
+  },
+
+  actions: {
+    submit: function() {
+      if (this.get('isLoading')) {
+        return;
+      }
+
+      var credentials = {
+        username: Ember.$.trim(this.get('username')),
+        password: Ember.$.trim(this.get('password'))
+      };
+
+      if (!credentials.username || !credentials.password) {
+        this.toast.warning(gettext("Fill out both fields."));
+        return;
+      }
+
+      var self = this;
+      this.rpc.ajax(this.get('settings.loginApiUrl'), credentials
+      ).then(function() {
+        self.success(credentials);
+      }, function(jqXHR) {
+        self.error(jqXHR);
+      }).finally(function() {
+        self.set('isLoading', false);
+      });
+    },
+
+    // Go-to links
+
+    activateAccount: function() {
+      this.get('router').transitionTo('activation');
+      Ember.$('#loginModal').modal('hide');
+    },
+
+    forgotPassword: function() {
+      this.get('router').transitionTo('forgotten-password');
+      Ember.$('#loginModal').modal('hide');
+    }
+  }
+});

+ 6 - 0
misago/emberapp/app/components/toast-message.js

@@ -0,0 +1,6 @@
+import Ember from 'ember';
+
+export default Ember.Component.extend({
+  classNames: ['toast-message'],
+  classNameBindings: ['toast.isVisible:visible'],
+});

+ 1 - 1
misago/emberapp/app/controllers/forgotten-password/change-form.js

@@ -38,7 +38,7 @@ export default Ember.ObjectController.extend({
     success: function() {
       this.set('password', '');
 
-      this.send('openLoginModal');
+      this.auth.openLoginModal();
       this.toast.success(gettext("Your password has been changed."));
     },
 

+ 0 - 9
misago/emberapp/app/controllers/guest-nav.js

@@ -1,9 +0,0 @@
-import Ember from 'ember';
-
-export default Ember.Controller.extend({
-  actions: {
-    register: function() {
-      this.transitionTo('register');
-    }
-  }
-});

+ 2 - 6
misago/emberapp/app/controllers/login-modal.js

@@ -1,6 +1,6 @@
 import Ember from 'ember';
 
-export default Ember.Controller.extend({
+export default Ember.Component.extend({
   modal: null,
 
   isLoading: false,
@@ -9,10 +9,6 @@ export default Ember.Controller.extend({
   username: '',
   password: '',
 
-  scheduleSetup: function() {
-    Ember.run.scheduleOnce('afterRender', this, this.setup);
-  }.on('init'),
-
   setup: function() {
     this.modal = Ember.$('#loginModal').modal({show: false});
 
@@ -24,7 +20,7 @@ export default Ember.Controller.extend({
     this.modal.on('hidden.bs.modal', function() {
       self.reset();
     });
-  },
+  }.on('didInsertElement'),
 
   reset: function() {
     this.setProperties({

+ 4 - 0
misago/emberapp/app/controllers/register.js

@@ -0,0 +1,4 @@
+import Ember from 'ember';
+
+export default Ember.Controller.extend({
+});

+ 5 - 0
misago/emberapp/app/controllers/user-nav.js

@@ -1,4 +1,9 @@
 import Ember from 'ember';
 
 export default Ember.Controller.extend({
+  actions: {
+    logout: function() {
+      this.auth.logout();
+    }
+  }
 });

+ 2 - 0
misago/emberapp/app/initializers/auth-service.js

@@ -10,10 +10,12 @@ export function initialize(container, application) {
   application.register('misago:auth', auth, { instantiate: false });
   application.inject('route', 'auth', 'misago:auth');
   application.inject('controller', 'auth', 'misago:auth');
+  application.inject('component', 'auth', 'misago:auth');
 
   application.register('misago:user', auth.get('user'), { instantiate: false });
   application.inject('route', 'user', 'misago:user');
   application.inject('controller', 'user', 'misago:user');
+  application.inject('component', 'user', 'misago:user');
 
 }
 

+ 3 - 1
misago/emberapp/app/initializers/clock-service.js

@@ -1,5 +1,7 @@
 export function initialize(container, application) {
-  application.inject('controller', 'clock', 'service:clock');
+  [ 'controller', 'component' ].forEach((factory) => {
+    application.inject(factory, 'clock', 'service:clock');
+  });
 }
 
 export default {

+ 1 - 1
misago/emberapp/app/initializers/csrf-service.js

@@ -5,7 +5,7 @@ export function initialize(container, application) {
 
   application.inject('service:csrf', 'preloadStore', 'service:preload-store');
 
-  [ 'controller', 'adapter' ].forEach((factory) => {
+  [ 'component', 'adapter' ].forEach((factory) => {
     application.inject(factory, 'csrf', 'service:csrf');
   });
 }

+ 8 - 5
misago/emberapp/app/initializers/misago-settings.js

@@ -2,14 +2,17 @@ import PreloadStore from 'misago/services/preload-store';
 
 export function initialize(container, application) {
   application.register('misago:settings', PreloadStore.get('misagoSettings'), { instantiate: false });
-  application.inject('route', 'settings', 'misago:settings');
-  application.inject('controller', 'settings', 'misago:settings');
+  [ 'route', 'controller', 'component' ].forEach((factory) => {
+    application.inject(factory, 'settings', 'misago:settings');
+  });
 
   application.register('misago:static-url', PreloadStore.get('staticUrl'), { instantiate: false });
-  application.inject('controller', 'staticUrl', 'misago:static-url');
-
   application.register('misago:media-url', PreloadStore.get('mediaUrl'), { instantiate: false });
-  application.inject('controller', 'mediaUrl', 'misago:media-url');
+
+  [ 'controller', 'component' ].forEach((factory) => {
+    application.inject(factory, 'staticUrl', 'misago:static-url');
+    application.inject(factory, 'mediaUrl', 'misago:media-url');
+  });
 }
 
 export default {

+ 0 - 14
misago/emberapp/app/initializers/page-title-service.js

@@ -1,14 +0,0 @@
-import PageTitleService from 'misago/services/page-title';
-
-export function initialize(container, application) {
-  application.inject('service:page-title', 'settings', 'misago:settings');
-
-  application.register('service:page-title', PageTitleService, { singleton: true });
-  application.inject('route', 'title', 'service:page-title');
-}
-
-export default {
-  name: 'page-title-service',
-  after: 'misago-settings',
-  initialize: initialize
-};

+ 1 - 1
misago/emberapp/app/initializers/rpc-service.js

@@ -5,7 +5,7 @@ export function initialize(_container, application) {
 
   application.inject('service:rpc', 'store', 'store:main');
 
-  [ 'route', 'controller' ].forEach((factory) => {
+  [ 'route', 'controller', 'component' ].forEach((factory) => {
     application.inject(factory, 'rpc', 'service:rpc');
   });
 }

+ 1 - 1
misago/emberapp/app/routes/activation/activate.js

@@ -7,7 +7,7 @@ export default MisagoRoute.extend(ResetScroll, {
   },
 
   afterModel: function(model) {
-    this.send('openLoginModal');
+    this.auth.openLoginModal();
     this.toast.success(model.detail);
     return this.transitionTo('index');
   },

+ 0 - 12
misago/emberapp/app/routes/application.js

@@ -1,4 +1,3 @@
-import Ember from 'ember';
 import MisagoRoute from 'misago/routes/misago';
 
 export default MisagoRoute.extend({
@@ -68,17 +67,6 @@ export default MisagoRoute.extend({
     showBan: function(ban) {
       this.set('title', gettext('You are banned'));
       this.intermediateTransitionTo('error-banned', ban);
-    },
-
-    // Auth
-
-    openLoginModal: function() {
-      this.controllerFor("loginModal").send('open');
-    },
-
-    logOut: function() {
-      this.get('auth').logout();
-      Ember.$('#hidden-logout-form').submit();
     }
   }
 });

+ 4 - 0
misago/emberapp/app/services/auth.js

@@ -6,6 +6,10 @@ export default Ember.Service.extend({
   }.property('isAuthenticated'),
 
   logout: function() {
+    Ember.$('#hidden-logout-form').submit();
+  },
 
+  openLoginModal: function() {
+    Ember.$('#loginModal').modal('show');
   }
 });

+ 1 - 1
misago/emberapp/app/services/clock.js

@@ -11,7 +11,7 @@ export default Ember.Service.extend({
     if (ENV.environment !== 'test') {
       // running this loop in tests will block promises resolution
       Ember.run.later(function () {
-        self.set('_tick', !self.get('_tick'));
+        self.toggleProperty('_tick');
       }, ENV.APP.TICK_FREQUENCY);
     }
   }.observes('_tick').on('init'),

+ 2 - 2
misago/emberapp/app/templates/application.hbs

@@ -1,9 +1,9 @@
 {{render "navbar"}}
-{{render "toast-message"}}
+{{toast-message}}
 
 <div class="main-outlet">
   {{outlet}}
 </div>
 
 {{render "footer"}}
-{{render "login-modal"}}
+{{login-modal}}

+ 0 - 0
misago/emberapp/app/templates/components/.gitkeep


+ 7 - 0
misago/emberapp/app/templates/components/guest-nav.hbs

@@ -0,0 +1,7 @@
+<button type="button" class="btn btn-default btn-login navbar-btn btn-sm" {{action "login"}}>
+  {{gettext "Sign in"}}
+</button>
+
+<button type="button" class="btn btn-info btn-logout navbar-btn btn-sm" {{action "register"}}>
+  {{gettext "Join now"}}
+</button>

+ 0 - 0
misago/emberapp/app/templates/login-modal.hbs → misago/emberapp/app/templates/components/login-modal.hbs


+ 3 - 0
misago/emberapp/app/templates/components/toast-message.hbs

@@ -0,0 +1,3 @@
+<p {{bind-attr class="toast.isInfo:message-info toast.isSuccess:message-success toast.isWarning:message-warning toast.isError:message-danger"}}>
+  {{toast.message}}
+</p>

+ 0 - 9
misago/emberapp/app/templates/guest-nav.hbs

@@ -1,9 +0,0 @@
-<div class="guest-nav navbar-right">
-  <button type="button" class="btn btn-default btn-login navbar-btn btn-sm" {{action "openLoginModal"}}>
-    {{gettext "Sign in"}}
-  </button>
-
-  <button type="button" class="btn btn-info btn-logout navbar-btn btn-sm" {{action "register"}}>
-    {{gettext "Join now"}}
-  </button>
-</div>

+ 1 - 1
misago/emberapp/app/templates/navbar.hbs

@@ -9,7 +9,7 @@
     {{#if user.isAuthenticated}}
     {{render 'user-nav'}}
     {{else}}
-    {{render 'guest-nav'}}
+    {{guest-nav}}
     {{/if}}
 
   </div><!-- /.container -->

+ 0 - 5
misago/emberapp/app/templates/toast-message.hbs

@@ -1,5 +0,0 @@
-<div {{bind-attr class=":toast-message toast.isVisible:visible"}}>
-  <p {{bind-attr class="toast.isInfo:message-info toast.isSuccess:message-success toast.isWarning:message-warning toast.isError:message-danger"}}>
-    {{toast.message}}
-  </p>
-</div>

+ 1 - 1
misago/emberapp/app/templates/user-nav.hbs

@@ -1,7 +1,7 @@
 <div class="user-nav navbar-right">
   <p class="navbar-text">{{user.username}}</p>
 
-  <button type="button" class="btn btn-info navbar-btn btn-sm" {{action "logOut"}}>
+  <button type="button" class="btn btn-info navbar-btn btn-sm" {{action "logout"}}>
     {{gettext "Log out"}}
   </button>
 </div>

+ 1 - 1
misago/emberapp/tests/helpers/toast-message.js

@@ -1,5 +1,5 @@
 import Ember from 'ember';
 
 export default function getToastMessage() {
-  return Ember.$.trim(find('.toast-message p').text());
+  return Ember.$.trim(Ember.$('.toast-message p').text());
 }

+ 1 - 1
misago/emberapp/tests/unit/controllers/guest-nav-test.js → misago/emberapp/tests/unit/components/guest-nav-test.js

@@ -3,7 +3,7 @@ import {
   test
 } from 'ember-qunit';
 
-moduleFor('controller:guest-nav', 'GuestNavController');
+moduleFor('component:guest-nav', 'GuestNavController');
 
 test('it exists', function(assert) {
   assert.expect(1);

+ 1 - 1
misago/emberapp/tests/unit/controllers/login-modal-test.js → misago/emberapp/tests/unit/components/login-modal-test.js

@@ -4,7 +4,7 @@ import {
   test
 } from 'ember-qunit';
 
-moduleFor('controller:login-modal', 'LoginModalController');
+moduleFor('component:login-modal', 'LoginModalController');
 
 test('it exists', function(assert) {
   assert.expect(1);

+ 0 - 0
misago/emberapp/tests/unit/controllers/toast-message-test.js → misago/emberapp/tests/unit/services/toast-message-test.js