org_switcher.ts 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. ///<reference path="../../headers/common.d.ts" />
  2. import coreModule from 'app/core/core_module';
  3. import { contextSrv } from 'app/core/services/context_srv';
  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">
  16. <div class="gf-form-group">
  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-mini" ng-show="org.orgId === ctrl.currentOrgId">
  31. Current
  32. </span>
  33. <a ng-click="ctrl.setUsingOrg(org)" class="btn btn-inverse btn-mini" 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) {
  47. this.currentOrgId = contextSrv.user.orgId;
  48. this.getUserOrgs();
  49. }
  50. getUserOrgs() {
  51. this.backendSrv.get('/api/user/orgs').then(orgs => {
  52. this.orgs = orgs;
  53. });
  54. }
  55. setUsingOrg(org) {
  56. return this.backendSrv.post('/api/user/using/' + org.orgId).then(() => {
  57. const re = /orgId=\d+/gi;
  58. this.setWindowLocationHref(
  59. this.getWindowLocationHref().replace(re, 'orgId=' + org.orgId)
  60. );
  61. });
  62. }
  63. getWindowLocationHref() {
  64. return window.location.href;
  65. }
  66. setWindowLocationHref(href: string) {
  67. window.location.href = href;
  68. }
  69. }
  70. export function orgSwitcher() {
  71. return {
  72. restrict: 'E',
  73. template: template,
  74. controller: OrgSwitchCtrl,
  75. bindToController: true,
  76. controllerAs: 'ctrl',
  77. scope: { dismiss: '&' },
  78. };
  79. }
  80. coreModule.directive('orgSwitcher', orgSwitcher);