editor.ts 2.5 KB

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