column_options.ts 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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. /** @ngInject */
  16. constructor($scope) {
  17. $scope.editor = this;
  18. this.activeStyleIndex = 0;
  19. this.panelCtrl = $scope.ctrl;
  20. this.panel = this.panelCtrl.panel;
  21. this.unitFormats = kbn.getUnitFormats();
  22. this.colorModes = [
  23. { text: 'Disabled', value: null },
  24. { text: 'Cell', value: 'cell' },
  25. { text: 'Value', value: 'value' },
  26. { text: 'Row', value: 'row' },
  27. ];
  28. this.columnTypes = [
  29. { text: 'Number', value: 'number' },
  30. { text: 'String', value: 'string' },
  31. { text: 'Date', value: 'date' },
  32. { text: 'Hidden', value: 'hidden' },
  33. ];
  34. this.fontSizes = ['80%', '90%', '100%', '110%', '120%', '130%', '150%', '160%', '180%', '200%', '220%', '250%'];
  35. this.dateFormats = [
  36. { text: 'YYYY-MM-DD HH:mm:ss', value: 'YYYY-MM-DD HH:mm:ss' },
  37. { text: 'YYYY-MM-DD HH:mm:ss.SSS', value: 'YYYY-MM-DD HH:mm:ss.SSS' },
  38. { text: 'MM/DD/YY h:mm:ss a', value: 'MM/DD/YY h:mm:ss a' },
  39. { text: 'MMMM D, YYYY LT', value: 'MMMM D, YYYY LT' },
  40. ];
  41. this.getColumnNames = () => {
  42. if (!this.panelCtrl.table) {
  43. return [];
  44. }
  45. return _.map(this.panelCtrl.table.columns, function(col: any) {
  46. return col.text;
  47. });
  48. };
  49. this.onColorChange = this.onColorChange.bind(this);
  50. }
  51. render() {
  52. this.panelCtrl.render();
  53. }
  54. setUnitFormat(column, subItem) {
  55. column.unit = subItem.value;
  56. this.panelCtrl.render();
  57. }
  58. addColumnStyle() {
  59. var newStyleRule = {
  60. unit: 'short',
  61. type: 'number',
  62. alias: '',
  63. decimals: 2,
  64. colors: ['rgba(245, 54, 54, 0.9)', 'rgba(237, 129, 40, 0.89)', 'rgba(50, 172, 45, 0.97)'],
  65. colorMode: null,
  66. pattern: '',
  67. dateFormat: 'YYYY-MM-DD HH:mm:ss',
  68. thresholds: [],
  69. };
  70. var styles = this.panel.styles;
  71. var stylesCount = styles.length;
  72. var indexToInsert = stylesCount;
  73. // check if last is a catch all rule, then add it before that one
  74. if (stylesCount > 0) {
  75. var last = styles[stylesCount - 1];
  76. if (last.pattern === '/.*/') {
  77. indexToInsert = stylesCount - 1;
  78. }
  79. }
  80. styles.splice(indexToInsert, 0, newStyleRule);
  81. this.activeStyleIndex = indexToInsert;
  82. }
  83. removeColumnStyle(style) {
  84. this.panel.styles = _.without(this.panel.styles, style);
  85. }
  86. invertColorOrder(index) {
  87. var ref = this.panel.styles[index].colors;
  88. var copy = ref[0];
  89. ref[0] = ref[2];
  90. ref[2] = copy;
  91. this.panelCtrl.render();
  92. }
  93. onColorChange(styleIndex, colorIndex) {
  94. return newColor => {
  95. this.panel.styles[styleIndex].colors[colorIndex] = newColor;
  96. this.render();
  97. };
  98. }
  99. }
  100. /** @ngInject */
  101. export function columnOptionsTab($q, uiSegmentSrv) {
  102. 'use strict';
  103. return {
  104. restrict: 'E',
  105. scope: true,
  106. templateUrl: 'public/app/plugins/panel/table/column_options.html',
  107. controller: ColumnOptionsCtrl,
  108. };
  109. }