column_options.ts 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. import _ from 'lodash';
  2. import kbn from 'app/core/utils/kbn';
  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) {
  18. $scope.editor = this;
  19. this.activeStyleIndex = 0;
  20. this.panelCtrl = $scope.ctrl;
  21. this.panel = this.panelCtrl.panel;
  22. this.unitFormats = kbn.getUnitFormats();
  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. ];
  42. this.mappingTypes = [{ text: 'Value to text', value: 1 }, { text: 'Range to text', value: 2 }];
  43. this.getColumnNames = () => {
  44. if (!this.panelCtrl.table) {
  45. return [];
  46. }
  47. return _.map(this.panelCtrl.table.columns, function(col: any) {
  48. return col.text;
  49. });
  50. };
  51. this.onColorChange = this.onColorChange.bind(this);
  52. }
  53. render() {
  54. this.panelCtrl.render();
  55. }
  56. setUnitFormat(column, subItem) {
  57. column.unit = subItem.value;
  58. this.panelCtrl.render();
  59. }
  60. addColumnStyle() {
  61. var newStyleRule = {
  62. unit: 'short',
  63. type: 'number',
  64. alias: '',
  65. decimals: 2,
  66. colors: ['rgba(245, 54, 54, 0.9)', 'rgba(237, 129, 40, 0.89)', 'rgba(50, 172, 45, 0.97)'],
  67. colorMode: null,
  68. pattern: '',
  69. dateFormat: 'YYYY-MM-DD HH:mm:ss',
  70. thresholds: [],
  71. mappingType: 1,
  72. };
  73. var styles = this.panel.styles;
  74. var stylesCount = styles.length;
  75. var indexToInsert = stylesCount;
  76. // check if last is a catch all rule, then add it before that one
  77. if (stylesCount > 0) {
  78. var last = styles[stylesCount - 1];
  79. if (last.pattern === '/.*/') {
  80. indexToInsert = stylesCount - 1;
  81. }
  82. }
  83. styles.splice(indexToInsert, 0, newStyleRule);
  84. this.activeStyleIndex = indexToInsert;
  85. }
  86. removeColumnStyle(style) {
  87. this.panel.styles = _.without(this.panel.styles, style);
  88. }
  89. invertColorOrder(index) {
  90. var ref = this.panel.styles[index].colors;
  91. var copy = ref[0];
  92. ref[0] = ref[2];
  93. ref[2] = copy;
  94. this.panelCtrl.render();
  95. }
  96. onColorChange(styleIndex, colorIndex) {
  97. return newColor => {
  98. this.panel.styles[styleIndex].colors[colorIndex] = newColor;
  99. this.render();
  100. };
  101. }
  102. addValueMap(style) {
  103. if (!style.valueMaps) {
  104. style.valueMaps = [];
  105. }
  106. style.valueMaps.push({ value: '', text: '' });
  107. this.panelCtrl.render();
  108. }
  109. removeValueMap(style, index) {
  110. style.valueMaps.splice(index, 1);
  111. this.panelCtrl.render();
  112. }
  113. addRangeMap(style) {
  114. if (!style.rangeMaps) {
  115. style.rangeMaps = [];
  116. }
  117. style.rangeMaps.push({ from: '', to: '', text: '' });
  118. this.panelCtrl.render();
  119. }
  120. removeRangeMap(style, index) {
  121. style.rangeMaps.splice(index, 1);
  122. this.panelCtrl.render();
  123. }
  124. }
  125. /** @ngInject */
  126. export function columnOptionsTab($q, uiSegmentSrv) {
  127. 'use strict';
  128. return {
  129. restrict: 'E',
  130. scope: true,
  131. templateUrl: 'public/app/plugins/panel/table/column_options.html',
  132. controller: ColumnOptionsCtrl,
  133. };
  134. }