editor.ts 2.7 KB

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