editor.ts 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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. {text: 'Hidden', value: 'hidden'}
  38. ];
  39. this.fontSizes = ['80%', '90%', '100%', '110%', '120%', '130%', '150%', '160%', '180%', '200%', '220%', '250%'];
  40. this.dateFormats = [
  41. {text: 'YYYY-MM-DD HH:mm:ss', value: 'YYYY-MM-DD HH:mm:ss'},
  42. {text: 'MM/DD/YY h:mm:ss a', value: 'MM/DD/YY h:mm:ss a'},
  43. {text: 'MMMM D, YYYY LT', value: 'MMMM D, YYYY LT'},
  44. ];
  45. this.addColumnSegment = uiSegmentSrv.newPlusButton();
  46. // this is used from bs-typeahead and needs to be instance bound
  47. this.getColumnNames = () => {
  48. if (!this.panelCtrl.table) {
  49. return [];
  50. }
  51. return _.map(this.panelCtrl.table.columns, function(col: any) {
  52. return col.text;
  53. });
  54. };
  55. }
  56. getColumnOptions() {
  57. if (!this.panelCtrl.dataRaw) {
  58. return this.$q.when([]);
  59. }
  60. var columns = this.transformers[this.panel.transform].getColumns(this.panelCtrl.dataRaw);
  61. var segments = _.map(columns, (c: any) => this.uiSegmentSrv.newSegment({value: c.text}));
  62. return this.$q.when(segments);
  63. }
  64. addColumn() {
  65. var columns = transformers[this.panel.transform].getColumns(this.panelCtrl.dataRaw);
  66. var column = _.find(columns, {text: this.addColumnSegment.value});
  67. if (column) {
  68. this.panel.columns.push(column);
  69. this.render();
  70. }
  71. var plusButton = this.uiSegmentSrv.newPlusButton();
  72. this.addColumnSegment.html = plusButton.html;
  73. this.addColumnSegment.value = plusButton.value;
  74. }
  75. transformChanged() {
  76. this.panel.columns = [];
  77. this.render();
  78. }
  79. render() {
  80. this.panelCtrl.render();
  81. }
  82. removeColumn(column) {
  83. this.panel.columns = _.without(this.panel.columns, column);
  84. this.panelCtrl.render();
  85. }
  86. setUnitFormat(column, subItem) {
  87. column.unit = subItem.value;
  88. this.panelCtrl.render();
  89. };
  90. addColumnStyle() {
  91. var columnStyleDefaults = {
  92. unit: 'short',
  93. type: 'number',
  94. decimals: 2,
  95. colors: ["rgba(245, 54, 54, 0.9)", "rgba(237, 129, 40, 0.89)", "rgba(50, 172, 45, 0.97)"],
  96. colorMode: null,
  97. pattern: '/.*/',
  98. dateFormat: 'YYYY-MM-DD HH:mm:ss',
  99. thresholds: [],
  100. };
  101. this.panel.styles.push(angular.copy(columnStyleDefaults));
  102. }
  103. removeColumnStyle(style) {
  104. this.panel.styles = _.without(this.panel.styles, style);
  105. }
  106. invertColorOrder(index) {
  107. var ref = this.panel.styles[index].colors;
  108. var copy = ref[0];
  109. ref[0] = ref[2];
  110. ref[2] = copy;
  111. this.panelCtrl.render();
  112. }
  113. }
  114. /** @ngInject */
  115. export function tablePanelEditor($q, uiSegmentSrv) {
  116. 'use strict';
  117. return {
  118. restrict: 'E',
  119. scope: true,
  120. templateUrl: 'public/app/plugins/panel/table/editor.html',
  121. controller: TablePanelEditorCtrl,
  122. };
  123. }