profile_ctrl.ts 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. ///<reference path="../../headers/common.d.ts" />
  2. import config from 'app/core/config';
  3. import {coreModule} from 'app/core/core';
  4. import _ from 'lodash';
  5. export class ProfileCtrl {
  6. user: any;
  7. old_theme: any;
  8. orgs: any = [];
  9. userForm: any;
  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.getUserOrgs();
  17. this.navModel = navModelSrv.getProfileNav();
  18. }
  19. getUser() {
  20. this.backendSrv.get('/api/user').then(user => {
  21. this.user = user;
  22. this.user.theme = user.theme || 'dark';
  23. });
  24. }
  25. getUserOrgs() {
  26. this.backendSrv.get('/api/user/orgs').then(orgs => {
  27. this.orgs = orgs;
  28. this.showOrgsList = orgs.length > 1;
  29. });
  30. }
  31. setUsingOrg(org) {
  32. this.backendSrv.post('/api/user/using/' + org.orgId).then(() => {
  33. window.location.href = config.appSubUrl + '/profile';
  34. });
  35. }
  36. update() {
  37. if (!this.userForm.$valid) { return; }
  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);