prefs_control.ts 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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="section 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 class="gf-form-select-wrapper max-width-20 gf-form-select-wrapper--has-help-icon"
  53. model="ctrl.prefs.homeDashboardId">
  54. </dashboard-selector>
  55. </div>
  56. <div class="gf-form">
  57. <label class="gf-form-label width-9">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()">Update</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);