editor.ts 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. ///<reference path="../../../headers/common.d.ts" />
  2. import _ from 'lodash';
  3. import $ from 'jquery';
  4. import moment from 'moment';
  5. import angular from 'angular';
  6. import {transformers} from './transformers';
  7. import kbn from 'app/core/utils/kbn';
  8. export class TablePanelEditorCtrl {
  9. panel: any;
  10. panelCtrl: any;
  11. transformers: any;
  12. colorModes: any;
  13. columnStyles: any;
  14. columnTypes: any;
  15. fontSizes: any;
  16. dateFormats: any;
  17. addColumnSegment: any;
  18. unitFormats: any;
  19. getColumnNames: any;
  20. /** @ngInject */
  21. constructor($scope, private $q, private uiSegmentSrv) {
  22. $scope.editor = this;
  23. this.panelCtrl = $scope.ctrl;
  24. this.panel = this.panelCtrl.panel;
  25. this.transformers = transformers;
  26. this.unitFormats = kbn.getUnitFormats();
  27. this.colorModes = [
  28. {text: 'Disabled', value: null},
  29. {text: 'Cell', value: 'cell'},
  30. {text: 'Value', value: 'value'},
  31. {text: 'Row', value: 'row'},
  32. ];
  33. this.columnTypes = [
  34. {text: 'Number', value: 'number'},
  35. {text: 'String', value: 'string'},
  36. {text: 'Date', value: 'date'},
  37. ];
  38. this.fontSizes = ['80%', '90%', '100%', '110%', '120%', '130%', '150%', '160%', '180%', '200%', '220%', '250%'];
  39. this.dateFormats = [
  40. {text: 'YYYY-MM-DD HH:mm:ss', value: 'YYYY-MM-DD HH:mm:ss'},
  41. {text: 'MM/DD/YY h:mm:ss a', value: 'MM/DD/YY h:mm:ss a'},
  42. {text: 'MMMM D, YYYY LT', value: 'MMMM D, YYYY LT'},
  43. ];
  44. this.addColumnSegment = uiSegmentSrv.newPlusButton();
  45. // this is used from bs-typeahead and needs to be instance bound
  46. this.getColumnNames = () => {
  47. if (!this.panelCtrl.table) {
  48. return [];
  49. }
  50. return _.map(this.panelCtrl.table.columns, function(col: any) {
  51. return col.text;
  52. });
  53. };
  54. }
  55. getColumnOptions() {
  56. if (!this.panelCtrl.dataRaw) {
  57. return this.$q.when([]);
  58. }
  59. var columns = this.transformers[this.panel.transform].getColumns(this.panelCtrl.dataRaw);
  60. var segments = _.map(columns, (c: any) => this.uiSegmentSrv.newSegment({value: c.text}));
  61. return this.$q.when(segments);
  62. }
  63. addColumn() {
  64. var columns = transformers[this.panel.transform].getColumns(this.panelCtrl.dataRaw);
  65. var column = _.findWhere(columns, {text: this.addColumnSegment.value});
  66. if (column) {
  67. this.panel.columns.push(column);
  68. this.render();
  69. }
  70. var plusButton = this.uiSegmentSrv.newPlusButton();
  71. this.addColumnSegment.html = plusButton.html;
  72. this.addColumnSegment.value = plusButton.value;
  73. }
  74. transformChanged() {
  75. this.panel.columns = [];
  76. this.render();
  77. }
  78. render() {
  79. this.panelCtrl.render();
  80. }
  81. removeColumn(column) {
  82. this.panel.columns = _.without(this.panel.columns, column);
  83. this.panelCtrl.render();
  84. }
  85. setUnitFormat(column, subItem) {
  86. column.unit = subItem.value;
  87. this.panelCtrl.render();
  88. };
  89. addColumnStyle() {
  90. var columnStyleDefaults = {
  91. unit: 'short',
  92. type: 'number',
  93. decimals: 2,
  94. colors: ["rgba(245, 54, 54, 0.9)", "rgba(237, 129, 40, 0.89)", "rgba(50, 172, 45, 0.97)"],
  95. colorMode: null,
  96. pattern: '/.*/',
  97. dateFormat: 'YYYY-MM-DD HH:mm:ss',
  98. thresholds: [],
  99. };
  100. this.panel.styles.push(angular.copy(columnStyleDefaults));
  101. }
  102. removeColumnStyle(style) {
  103. this.panel.styles = _.without(this.panel.styles, style);
  104. }
  105. invertColorOrder(index) {
  106. var ref = this.panel.styles[index].colors;
  107. var copy = ref[0];
  108. ref[0] = ref[2];
  109. ref[2] = copy;
  110. this.panelCtrl.render();
  111. }
  112. }
  113. /** @ngInject */
  114. export function tablePanelEditor($q, uiSegmentSrv) {
  115. 'use strict';
  116. return {
  117. restrict: 'E',
  118. scope: true,
  119. templateUrl: 'public/app/plugins/panel/table/editor.html',
  120. controller: TablePanelEditorCtrl,
  121. };
  122. }