prefs_control.ts 2.6 KB

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