prefs_control.ts 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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. $onInit() {
  22. return this.backendSrv.get(`/api/${this.mode}/preferences`).then(prefs => {
  23. this.prefs = prefs;
  24. this.oldTheme = prefs.theme;
  25. });
  26. }
  27. updatePrefs() {
  28. if (!this.prefsForm.$valid) {
  29. return;
  30. }
  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-11">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-11">
  52. Home Dashboard
  53. <info-popover mode="right-normal">
  54. Not finding dashboard you want? Star it first, then it should appear in this select box.
  55. </info-popover>
  56. </span>
  57. <dashboard-selector class="gf-form-select-wrapper max-width-20" model="ctrl.prefs.homeDashboardId">
  58. </dashboard-selector>
  59. </div>
  60. <div class="gf-form">
  61. <label class="gf-form-label width-11">Timezone</label>
  62. <div class="gf-form-select-wrapper max-width-20">
  63. <select class="gf-form-input" ng-model="ctrl.prefs.timezone" ng-options="f.value as f.text for f in ctrl.timezones"></select>
  64. </div>
  65. </div>
  66. <div class="gf-form-button-row">
  67. <button type="submit" class="btn btn-success" ng-click="ctrl.updatePrefs()">Save</button>
  68. </div>
  69. </form>
  70. `;
  71. export function prefsControlDirective() {
  72. return {
  73. restrict: "E",
  74. controller: PrefsControlCtrl,
  75. bindToController: true,
  76. controllerAs: "ctrl",
  77. template: template,
  78. scope: {
  79. mode: "@"
  80. }
  81. };
  82. }
  83. coreModule.directive("prefsControl", prefsControlDirective);