org_switcher.ts 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. import coreModule from 'app/core/core_module';
  2. import { contextSrv } from 'app/core/services/context_srv';
  3. import config from 'app/core/config';
  4. import { BackendSrv } from '../services/backend_srv';
  5. const template = `
  6. <div class="modal-body">
  7. <div class="modal-header">
  8. <h2 class="modal-header-title">
  9. <i class="fa fa-random"></i>
  10. <span class="p-l-1">Switch Organization</span>
  11. </h2>
  12. <a class="modal-header-close" ng-click="ctrl.dismiss();">
  13. <i class="fa fa-remove"></i>
  14. </a>
  15. </div>
  16. <div class="modal-content modal-content--has-scroll" grafana-scrollbar>
  17. <table class="filter-table form-inline">
  18. <thead>
  19. <tr>
  20. <th>Name</th>
  21. <th>Role</th>
  22. <th></th>
  23. </tr>
  24. </thead>
  25. <tbody>
  26. <tr ng-repeat="org in ctrl.orgs">
  27. <td>{{org.name}}</td>
  28. <td>{{org.role}}</td>
  29. <td class="text-right">
  30. <span class="btn btn-primary btn-small" ng-show="org.orgId === ctrl.currentOrgId">
  31. Current
  32. </span>
  33. <a ng-click="ctrl.setUsingOrg(org)" class="btn btn-inverse btn-small" ng-show="org.orgId !== ctrl.currentOrgId">
  34. Switch to
  35. </a>
  36. </td>
  37. </tr>
  38. </tbody>
  39. </table>
  40. </div>
  41. </div>`;
  42. export class OrgSwitchCtrl {
  43. orgs: any[];
  44. currentOrgId: any;
  45. /** @ngInject */
  46. constructor(private backendSrv: BackendSrv) {
  47. this.currentOrgId = contextSrv.user.orgId;
  48. this.getUserOrgs();
  49. }
  50. getUserOrgs() {
  51. this.backendSrv.get('/api/user/orgs').then((orgs: any) => {
  52. this.orgs = orgs;
  53. });
  54. }
  55. setUsingOrg(org: any) {
  56. return this.backendSrv.post('/api/user/using/' + org.orgId).then(() => {
  57. this.setWindowLocation(config.appSubUrl + (config.appSubUrl.endsWith('/') ? '' : '/') + '?orgId=' + org.orgId);
  58. });
  59. }
  60. setWindowLocation(href: string) {
  61. window.location.href = href;
  62. }
  63. }
  64. export function orgSwitcher() {
  65. return {
  66. restrict: 'E',
  67. template: template,
  68. controller: OrgSwitchCtrl,
  69. bindToController: true,
  70. controllerAs: 'ctrl',
  71. scope: { dismiss: '&' },
  72. };
  73. }
  74. coreModule.directive('orgSwitcher', orgSwitcher);