context_srv.ts 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. import config from 'app/core/config';
  2. import _ from 'lodash';
  3. import coreModule from 'app/core/core_module';
  4. import store from 'app/core/store';
  5. export class User {
  6. isGrafanaAdmin: any;
  7. isSignedIn: any;
  8. orgRole: any;
  9. orgId: number;
  10. timezone: string;
  11. helpFlags1: number;
  12. lightTheme: boolean;
  13. constructor() {
  14. if (config.bootData.user) {
  15. _.extend(this, config.bootData.user);
  16. }
  17. }
  18. }
  19. export class ContextSrv {
  20. pinned: any;
  21. version: any;
  22. user: User;
  23. isSignedIn: any;
  24. isGrafanaAdmin: any;
  25. isEditor: any;
  26. sidemenu: any;
  27. constructor() {
  28. this.sidemenu = store.getBool('grafana.sidemenu', false);
  29. if (!config.buildInfo) {
  30. config.buildInfo = {};
  31. }
  32. if (!config.bootData) {
  33. config.bootData = {user: {}, settings: {}};
  34. }
  35. this.version = config.buildInfo.version;
  36. this.user = new User();
  37. this.isSignedIn = this.user.isSignedIn;
  38. this.isGrafanaAdmin = this.user.isGrafanaAdmin;
  39. this.isEditor = this.hasRole('Editor') || this.hasRole('Admin');
  40. }
  41. hasRole(role) {
  42. return this.user.orgRole === role;
  43. }
  44. isGrafanaVisible() {
  45. return !!(document.visibilityState === undefined || document.visibilityState === 'visible');
  46. }
  47. toggleSideMenu() {
  48. this.sidemenu = !this.sidemenu;
  49. store.set('grafana.sidemenu',this.sidemenu);
  50. }
  51. }
  52. var contextSrv = new ContextSrv();
  53. export {contextSrv};
  54. coreModule.factory('contextSrv', function() {
  55. return contextSrv;
  56. });