column_options.ts 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. import _ from 'lodash';
  2. import { getValueFormats } from '@grafana/ui';
  3. export class ColumnOptionsCtrl {
  4. panel: any;
  5. panelCtrl: any;
  6. colorModes: any;
  7. columnStyles: any;
  8. columnTypes: any;
  9. fontSizes: any;
  10. dateFormats: any;
  11. addColumnSegment: any;
  12. unitFormats: any;
  13. getColumnNames: any;
  14. activeStyleIndex: number;
  15. mappingTypes: any;
  16. /** @ngInject */
  17. constructor($scope: any) {
  18. $scope.editor = this;
  19. this.activeStyleIndex = 0;
  20. this.panelCtrl = $scope.ctrl;
  21. this.panel = this.panelCtrl.panel;
  22. this.unitFormats = getValueFormats();
  23. this.colorModes = [
  24. { text: 'Disabled', value: null },
  25. { text: 'Cell', value: 'cell' },
  26. { text: 'Value', value: 'value' },
  27. { text: 'Row', value: 'row' },
  28. ];
  29. this.columnTypes = [
  30. { text: 'Number', value: 'number' },
  31. { text: 'String', value: 'string' },
  32. { text: 'Date', value: 'date' },
  33. { text: 'Hidden', value: 'hidden' },
  34. ];
  35. this.fontSizes = ['80%', '90%', '100%', '110%', '120%', '130%', '150%', '160%', '180%', '200%', '220%', '250%'];
  36. this.dateFormats = [
  37. { text: 'YYYY-MM-DD HH:mm:ss', value: 'YYYY-MM-DD HH:mm:ss' },
  38. { text: 'YYYY-MM-DD HH:mm:ss.SSS', value: 'YYYY-MM-DD HH:mm:ss.SSS' },
  39. { text: 'MM/DD/YY h:mm:ss a', value: 'MM/DD/YY h:mm:ss a' },
  40. { text: 'MMMM D, YYYY LT', value: 'MMMM D, YYYY LT' },
  41. { text: 'YYYY-MM-DD', value: 'YYYY-MM-DD' },
  42. ];
  43. this.mappingTypes = [{ text: 'Value to text', value: 1 }, { text: 'Range to text', value: 2 }];
  44. this.getColumnNames = () => {
  45. if (!this.panelCtrl.table) {
  46. return [];
  47. }
  48. return _.map(this.panelCtrl.table.columns, (col: any) => {
  49. return col.text;
  50. });
  51. };
  52. this.onColorChange = this.onColorChange.bind(this);
  53. }
  54. render() {
  55. this.panelCtrl.render();
  56. }
  57. setUnitFormat(column: any, subItem: any) {
  58. column.unit = subItem.value;
  59. this.panelCtrl.render();
  60. }
  61. addColumnStyle() {
  62. const newStyleRule = {
  63. unit: 'short',
  64. type: 'number',
  65. alias: '',
  66. decimals: 2,
  67. colors: ['rgba(245, 54, 54, 0.9)', 'rgba(237, 129, 40, 0.89)', 'rgba(50, 172, 45, 0.97)'],
  68. colorMode: null,
  69. pattern: '',
  70. dateFormat: 'YYYY-MM-DD HH:mm:ss',
  71. thresholds: [],
  72. mappingType: 1,
  73. };
  74. const styles = this.panel.styles;
  75. const stylesCount = styles.length;
  76. let indexToInsert = stylesCount;
  77. // check if last is a catch all rule, then add it before that one
  78. if (stylesCount > 0) {
  79. const last = styles[stylesCount - 1];
  80. if (last.pattern === '/.*/') {
  81. indexToInsert = stylesCount - 1;
  82. }
  83. }
  84. styles.splice(indexToInsert, 0, newStyleRule);
  85. this.activeStyleIndex = indexToInsert;
  86. }
  87. removeColumnStyle(style: any) {
  88. this.panel.styles = _.without(this.panel.styles, style);
  89. }
  90. invertColorOrder(index: number) {
  91. const ref = this.panel.styles[index].colors;
  92. const copy = ref[0];
  93. ref[0] = ref[2];
  94. ref[2] = copy;
  95. this.panelCtrl.render();
  96. }
  97. onColorChange(style: any, colorIndex: number) {
  98. return (newColor: string) => {
  99. style.colors[colorIndex] = newColor;
  100. this.render();
  101. };
  102. }
  103. addValueMap(style: any) {
  104. if (!style.valueMaps) {
  105. style.valueMaps = [];
  106. }
  107. style.valueMaps.push({ value: '', text: '' });
  108. this.panelCtrl.render();
  109. }
  110. removeValueMap(style: any, index: number) {
  111. style.valueMaps.splice(index, 1);
  112. this.panelCtrl.render();
  113. }
  114. addRangeMap(style: any) {
  115. if (!style.rangeMaps) {
  116. style.rangeMaps = [];
  117. }
  118. style.rangeMaps.push({ from: '', to: '', text: '' });
  119. this.panelCtrl.render();
  120. }
  121. removeRangeMap(style: any, index: number) {
  122. style.rangeMaps.splice(index, 1);
  123. this.panelCtrl.render();
  124. }
  125. }
  126. /** @ngInject */
  127. export function columnOptionsTab($q: any, uiSegmentSrv: any) {
  128. 'use strict';
  129. return {
  130. restrict: 'E',
  131. scope: true,
  132. templateUrl: 'public/app/plugins/panel/table/column_options.html',
  133. controller: ColumnOptionsCtrl,
  134. };
  135. }