thresholds_form.ts 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. import tinycolor from 'tinycolor2';
  2. import coreModule from 'app/angular/core_module';
  3. import config from 'app/core/config';
  4. export class ThresholdFormCtrl {
  5. panelCtrl: any;
  6. panel: any;
  7. disabled = false;
  8. /** @ngInject */
  9. constructor(private $scope: any) {}
  10. $onInit() {
  11. this.panel = this.panelCtrl.panel;
  12. if (this.panel.alert && !config.unifiedAlertingEnabled) {
  13. this.disabled = true;
  14. }
  15. const unbindDestroy = this.$scope.$on('$destroy', () => {
  16. this.panelCtrl.editingThresholds = false;
  17. this.panelCtrl.render();
  18. unbindDestroy();
  19. });
  20. this.panelCtrl.editingThresholds = true;
  21. }
  22. addThreshold() {
  23. this.panel.thresholds.push({
  24. value: undefined,
  25. colorMode: 'critical',
  26. op: 'gt',
  27. fill: true,
  28. line: true,
  29. yaxis: 'left',
  30. });
  31. this.panelCtrl.render();
  32. }
  33. removeThreshold(index: number) {
  34. this.panel.thresholds.splice(index, 1);
  35. this.panelCtrl.render();
  36. }
  37. render() {
  38. this.panelCtrl.render();
  39. }
  40. onFillColorChange(index: number) {
  41. return (newColor: string) => {
  42. this.panel.thresholds[index].fillColor = newColor;
  43. this.render();
  44. };
  45. }
  46. onLineColorChange(index: number) {
  47. return (newColor: string) => {
  48. this.panel.thresholds[index].lineColor = newColor;
  49. this.render();
  50. };
  51. }
  52. onThresholdTypeChange(index: number) {
  53. // Because of the ng-model binding, threshold's color mode is already set here
  54. if (this.panel.thresholds[index].colorMode === 'custom') {
  55. this.panel.thresholds[index].fillColor = tinycolor(config.theme.palette.blue85).setAlpha(0.2).toRgbString();
  56. this.panel.thresholds[index].lineColor = tinycolor(config.theme.palette.blue77).setAlpha(0.6).toRgbString();
  57. }
  58. this.panelCtrl.render();
  59. }
  60. }
  61. coreModule.directive('graphThresholdForm', () => {
  62. return {
  63. restrict: 'E',
  64. templateUrl: 'public/app/plugins/panel/graph/thresholds_form.html',
  65. controller: ThresholdFormCtrl,
  66. bindToController: true,
  67. controllerAs: 'ctrl',
  68. scope: {
  69. panelCtrl: '=',
  70. },
  71. };
  72. });