org_switcher.ts 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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(this.getWindowLocationHref().replace(re, 'orgId=' + org.orgId));
  59. });
  60. }
  61. getWindowLocationHref() {
  62. return window.location.href;
  63. }
  64. setWindowLocationHref(href: string) {
  65. window.location.href = href;
  66. }
  67. }
  68. export function orgSwitcher() {
  69. return {
  70. restrict: 'E',
  71. template: template,
  72. controller: OrgSwitchCtrl,
  73. bindToController: true,
  74. controllerAs: 'ctrl',
  75. scope: { dismiss: '&' },
  76. };
  77. }
  78. coreModule.directive('orgSwitcher', orgSwitcher);