editor.ts 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. import _ from 'lodash';
  2. import { transformers } from './transformers';
  3. import { IQService } from 'angular';
  4. import { Column } from 'react-virtualized';
  5. export class TablePanelEditorCtrl {
  6. panel: any;
  7. panelCtrl: any;
  8. transformers: any;
  9. fontSizes: any;
  10. addColumnSegment: any;
  11. getColumnNames: any;
  12. canSetColumns: boolean;
  13. columnsHelpMessage: string;
  14. /** @ngInject */
  15. constructor($scope: any, private $q: IQService, private uiSegmentSrv: any) {
  16. $scope.editor = this;
  17. this.panelCtrl = $scope.ctrl;
  18. this.panel = this.panelCtrl.panel;
  19. this.transformers = transformers;
  20. this.fontSizes = ['80%', '90%', '100%', '110%', '120%', '130%', '150%', '160%', '180%', '200%', '220%', '250%'];
  21. this.addColumnSegment = uiSegmentSrv.newPlusButton();
  22. this.updateTransformHints();
  23. }
  24. updateTransformHints() {
  25. this.canSetColumns = false;
  26. this.columnsHelpMessage = '';
  27. switch (this.panel.transform) {
  28. case 'timeseries_aggregations': {
  29. this.canSetColumns = true;
  30. break;
  31. }
  32. case 'json': {
  33. this.canSetColumns = true;
  34. break;
  35. }
  36. case 'table': {
  37. this.columnsHelpMessage = 'Columns and their order are determined by the data query';
  38. }
  39. }
  40. }
  41. getColumnOptions() {
  42. if (!this.panelCtrl.dataRaw) {
  43. return this.$q.when([]);
  44. }
  45. const columns = this.transformers[this.panel.transform].getColumns(this.panelCtrl.dataRaw);
  46. const segments = _.map(columns, (c: any) => this.uiSegmentSrv.newSegment({ value: c.text }));
  47. return this.$q.when(segments);
  48. }
  49. addColumn() {
  50. const columns = transformers[this.panel.transform].getColumns(this.panelCtrl.dataRaw);
  51. const column: any = _.find(columns, { text: this.addColumnSegment.value });
  52. if (column) {
  53. this.panel.columns.push(column);
  54. this.render();
  55. }
  56. const plusButton = this.uiSegmentSrv.newPlusButton();
  57. this.addColumnSegment.html = plusButton.html;
  58. this.addColumnSegment.value = plusButton.value;
  59. }
  60. transformChanged() {
  61. this.panel.columns = [];
  62. if (this.panel.transform === 'timeseries_aggregations') {
  63. this.panel.columns.push({ text: 'Avg', value: 'avg' });
  64. }
  65. this.updateTransformHints();
  66. this.render();
  67. }
  68. render() {
  69. this.panelCtrl.render();
  70. }
  71. removeColumn(column: Column) {
  72. this.panel.columns = _.without(this.panel.columns, column);
  73. this.panelCtrl.render();
  74. }
  75. }
  76. /** @ngInject */
  77. export function tablePanelEditor($q: IQService, uiSegmentSrv: any) {
  78. 'use strict';
  79. return {
  80. restrict: 'E',
  81. scope: true,
  82. templateUrl: 'public/app/plugins/panel/table/editor.html',
  83. controller: TablePanelEditorCtrl,
  84. };
  85. }