org_switcher.ts 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. import coreModule from 'app/core/core_module';
  2. import { contextSrv } from 'app/core/services/context_srv';
  3. const template = `
  4. <div class="modal-body">
  5. <div class="modal-header">
  6. <h2 class="modal-header-title">
  7. <i class="fa fa-random"></i>
  8. <span class="p-l-1">Switch Organization</span>
  9. </h2>
  10. <a class="modal-header-close" ng-click="ctrl.dismiss();">
  11. <i class="fa fa-remove"></i>
  12. </a>
  13. </div>
  14. <div class="modal-content">
  15. <div class="gf-form-group">
  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. const re = /orgId=\d+/gi;
  57. this.setWindowLocationHref(this.getWindowLocationHref().replace(re, 'orgId=' + org.orgId));
  58. });
  59. }
  60. getWindowLocationHref() {
  61. return window.location.href;
  62. }
  63. setWindowLocationHref(href: string) {
  64. window.location.href = href;
  65. }
  66. }
  67. export function orgSwitcher() {
  68. return {
  69. restrict: 'E',
  70. template: template,
  71. controller: OrgSwitchCtrl,
  72. bindToController: true,
  73. controllerAs: 'ctrl',
  74. scope: { dismiss: '&' },
  75. };
  76. }
  77. coreModule.directive('orgSwitcher', orgSwitcher);