context_srv.ts 1.5 KB

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