org_switcher.ts 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. ///<reference path="../../headers/common.d.ts" />
  2. import coreModule from 'app/core/core_module';
  3. import config from 'app/core/config';
  4. import {contextSrv} from 'app/core/services/context_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">
  17. <div class="gf-form-group">
  18. <table class="filter-table form-inline">
  19. <thead>
  20. <tr>
  21. <th>Name</th>
  22. <th>Role</th>
  23. <th></th>
  24. </tr>
  25. </thead>
  26. <tbody>
  27. <tr ng-repeat="org in ctrl.orgs">
  28. <td>{{org.name}}</td>
  29. <td>{{org.role}}</td>
  30. <td class="text-right">
  31. <span class="btn btn-primary btn-mini" ng-show="org.orgId === ctrl.currentOrgId">
  32. Current
  33. </span>
  34. <a ng-click="ctrl.setUsingOrg(org)" class="btn btn-inverse btn-mini" ng-show="org.orgId !== ctrl.currentOrgId">
  35. Switch to
  36. </a>
  37. </td>
  38. </tr>
  39. </tbody>
  40. </table>
  41. </div>
  42. </div>`;
  43. export class OrgSwitchCtrl {
  44. orgs: any[];
  45. currentOrgId: any;
  46. /** @ngInject */
  47. constructor(private backendSrv) {
  48. this.currentOrgId = contextSrv.user.orgId;
  49. this.getUserOrgs();
  50. }
  51. getUserOrgs() {
  52. this.backendSrv.get('/api/user/orgs').then(orgs => {
  53. this.orgs = orgs;
  54. });
  55. }
  56. setUsingOrg(org) {
  57. this.backendSrv.post('/api/user/using/' + org.orgId).then(() => {
  58. window.location.href = window.location.href;
  59. });
  60. }
  61. }
  62. export function orgSwitcher() {
  63. return {
  64. restrict: 'E',
  65. template: template,
  66. controller: OrgSwitchCtrl,
  67. bindToController: true,
  68. controllerAs: 'ctrl',
  69. scope: {dismiss: "&"},
  70. };
  71. }
  72. coreModule.directive('orgSwitcher', orgSwitcher);