prefs_control.ts 2.6 KB

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