prefs_control.ts 2.5 KB

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