profile_ctrl.ts 1.6 KB

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