org_switcher.ts 1.9 KB

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