prefs_control.ts 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. import config from 'app/core/config';
  2. import coreModule from 'app/core/core_module';
  3. export class PrefsControlCtrl {
  4. prefs: any;
  5. oldTheme: any;
  6. prefsForm: any;
  7. mode: string;
  8. timezones: any = [
  9. { value: '', text: 'Default' },
  10. { value: 'browser', text: 'Local browser time' },
  11. { value: 'utc', text: 'UTC' },
  12. ];
  13. themes: any = [{ value: '', text: 'Default' }, { value: 'dark', text: 'Dark' }, { value: 'light', text: 'Light' }];
  14. /** @ngInject **/
  15. constructor(private backendSrv, private $location) {}
  16. $onInit() {
  17. return this.backendSrv.get(`/api/${this.mode}/preferences`).then(prefs => {
  18. this.prefs = prefs;
  19. this.oldTheme = prefs.theme;
  20. });
  21. }
  22. updatePrefs() {
  23. if (!this.prefsForm.$valid) {
  24. return;
  25. }
  26. var cmd = {
  27. theme: this.prefs.theme,
  28. timezone: this.prefs.timezone,
  29. homeDashboardId: this.prefs.homeDashboardId,
  30. };
  31. this.backendSrv.put(`/api/${this.mode}/preferences`, cmd).then(() => {
  32. window.location.href = config.appSubUrl + this.$location.path();
  33. });
  34. }
  35. }
  36. var template = `
  37. <form name="ctrl.prefsForm" class="section gf-form-group">
  38. <h3 class="page-heading">Preferences</h3>
  39. <div class="gf-form">
  40. <span class="gf-form-label width-11">UI Theme</span>
  41. <div class="gf-form-select-wrapper max-width-20">
  42. <select class="gf-form-input" ng-model="ctrl.prefs.theme" ng-options="f.value as f.text for f in ctrl.themes"></select>
  43. </div>
  44. </div>
  45. <div class="gf-form">
  46. <span class="gf-form-label width-11">
  47. Home Dashboard
  48. <info-popover mode="right-normal">
  49. Not finding dashboard you want? Star it first, then it should appear in this select box.
  50. </info-popover>
  51. </span>
  52. <dashboard-selector class="gf-form-select-wrapper max-width-20" model="ctrl.prefs.homeDashboardId">
  53. </dashboard-selector>
  54. </div>
  55. <div class="gf-form">
  56. <label class="gf-form-label width-11">Timezone</label>
  57. <div class="gf-form-select-wrapper max-width-20">
  58. <select class="gf-form-input" ng-model="ctrl.prefs.timezone" ng-options="f.value as f.text for f in ctrl.timezones"></select>
  59. </div>
  60. </div>
  61. <div class="gf-form-button-row">
  62. <button type="submit" class="btn btn-success" ng-click="ctrl.updatePrefs()">Save</button>
  63. </div>
  64. </form>
  65. `;
  66. export function prefsControlDirective() {
  67. return {
  68. restrict: 'E',
  69. controller: PrefsControlCtrl,
  70. bindToController: true,
  71. controllerAs: 'ctrl',
  72. template: template,
  73. scope: {
  74. mode: '@',
  75. },
  76. };
  77. }
  78. coreModule.directive('prefsControl', prefsControlDirective);