context_srv.ts 1.6 KB

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