profile_ctrl.ts 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. import config from 'app/core/config';
  2. import { coreModule } from 'app/core/core';
  3. export class ProfileCtrl {
  4. user: any;
  5. old_theme: any;
  6. orgs: any = [];
  7. userForm: any;
  8. showOrgsList = false;
  9. readonlyLoginFields = config.disableLoginForm;
  10. navModel: any;
  11. /** @ngInject **/
  12. constructor(private backendSrv, private contextSrv, private $location, navModelSrv) {
  13. this.getUser();
  14. this.getUserOrgs();
  15. this.navModel = navModelSrv.getNav('profile', 'profile-settings', 0);
  16. }
  17. getUser() {
  18. this.backendSrv.get('/api/user').then(user => {
  19. this.user = user;
  20. this.user.theme = user.theme || 'dark';
  21. });
  22. }
  23. getUserOrgs() {
  24. this.backendSrv.get('/api/user/orgs').then(orgs => {
  25. this.orgs = orgs;
  26. this.showOrgsList = orgs.length > 1;
  27. });
  28. }
  29. setUsingOrg(org) {
  30. this.backendSrv.post('/api/user/using/' + org.orgId).then(() => {
  31. window.location.href = config.appSubUrl + '/profile';
  32. });
  33. }
  34. update() {
  35. if (!this.userForm.$valid) {
  36. return;
  37. }
  38. this.backendSrv.put('/api/user/', this.user).then(() => {
  39. this.contextSrv.user.name = this.user.name || this.user.login;
  40. if (this.old_theme !== this.user.theme) {
  41. window.location.href = config.appSubUrl + this.$location.path();
  42. }
  43. });
  44. }
  45. }
  46. coreModule.controller('ProfileCtrl', ProfileCtrl);