profile_ctrl.ts 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  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) { return; }
  36. this.backendSrv.put('/api/user/', this.user).then(() => {
  37. this.contextSrv.user.name = this.user.name || this.user.login;
  38. if (this.old_theme !== this.user.theme) {
  39. window.location.href = config.appSubUrl + this.$location.path();
  40. }
  41. });
  42. }
  43. }
  44. coreModule.controller('ProfileCtrl', ProfileCtrl);