column_options.ts 4.3 KB

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