import coreModule from 'app/core/core_module'; import { contextSrv } from 'app/core/services/context_srv'; import config from 'app/core/config'; import { BackendSrv } from '../services/backend_srv'; const template = `
`; export class OrgSwitchCtrl { orgs: any[]; currentOrgId: any; /** @ngInject */ constructor(private backendSrv: BackendSrv) { this.currentOrgId = contextSrv.user.orgId; this.getUserOrgs(); } getUserOrgs() { this.backendSrv.get('/api/user/orgs').then((orgs: any) => { this.orgs = orgs; }); } setUsingOrg(org: any) { return this.backendSrv.post('/api/user/using/' + org.orgId).then(() => { this.setWindowLocation(config.appSubUrl + (config.appSubUrl.endsWith('/') ? '' : '/') + '?orgId=' + org.orgId); }); } setWindowLocation(href: string) { window.location.href = href; } } export function orgSwitcher() { return { restrict: 'E', template: template, controller: OrgSwitchCtrl, bindToController: true, controllerAs: 'ctrl', scope: { dismiss: '&' }, }; } coreModule.directive('orgSwitcher', orgSwitcher);