context_srv.ts 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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. 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.pinned = store.getBool('grafana.sidemenu.pinned', false);
  29. if (this.pinned) {
  30. this.sidemenu = true;
  31. }
  32. if (!config.buildInfo) {
  33. config.buildInfo = {};
  34. }
  35. if (!config.bootData) {
  36. config.bootData = {user: {}, settings: {}};
  37. }
  38. this.version = config.buildInfo.version;
  39. this.user = new User();
  40. this.isSignedIn = this.user.isSignedIn;
  41. this.isGrafanaAdmin = this.user.isGrafanaAdmin;
  42. this.isEditor = this.hasRole('Editor') || this.hasRole('Admin');
  43. }
  44. hasRole(role) {
  45. return this.user.orgRole === role;
  46. }
  47. setPinnedState(val) {
  48. this.pinned = val;
  49. store.set('grafana.sidemenu.pinned', val);
  50. }
  51. isGrafanaVisible() {
  52. return !!(document.visibilityState === undefined || document.visibilityState === 'visible');
  53. }
  54. toggleSideMenu() {
  55. this.sidemenu = !this.sidemenu;
  56. if (!this.sidemenu) {
  57. this.setPinnedState(false);
  58. }
  59. }
  60. }
  61. var contextSrv = new ContextSrv();
  62. export {contextSrv};
  63. coreModule.factory('contextSrv', function() {
  64. return contextSrv;
  65. });