editor.ts 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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. activeStyleIndex: number;
  21. /** @ngInject */
  22. constructor($scope, private $q, private uiSegmentSrv) {
  23. $scope.editor = this;
  24. this.activeStyleIndex = 0;
  25. this.panelCtrl = $scope.ctrl;
  26. this.panel = this.panelCtrl.panel;
  27. this.transformers = transformers;
  28. this.unitFormats = kbn.getUnitFormats();
  29. this.colorModes = [
  30. {text: 'Disabled', value: null},
  31. {text: 'Cell', value: 'cell'},
  32. {text: 'Value', value: 'value'},
  33. {text: 'Row', value: 'row'},
  34. ];
  35. this.columnTypes = [
  36. {text: 'Number', value: 'number'},
  37. {text: 'String', value: 'string'},
  38. {text: 'Date', value: 'date'},
  39. {text: 'Hidden', value: 'hidden'}
  40. ];
  41. this.fontSizes = ['80%', '90%', '100%', '110%', '120%', '130%', '150%', '160%', '180%', '200%', '220%', '250%'];
  42. this.dateFormats = [
  43. {text: 'YYYY-MM-DD HH:mm:ss', value: 'YYYY-MM-DD HH:mm:ss'},
  44. {text: 'MM/DD/YY h:mm:ss a', value: 'MM/DD/YY h:mm:ss a'},
  45. {text: 'MMMM D, YYYY LT', value: 'MMMM D, YYYY LT'},
  46. ];
  47. this.addColumnSegment = uiSegmentSrv.newPlusButton();
  48. // this is used from bs-typeahead and needs to be instance bound
  49. this.getColumnNames = () => {
  50. if (!this.panelCtrl.table) {
  51. return [];
  52. }
  53. return _.map(this.panelCtrl.table.columns, function(col: any) {
  54. return col.text;
  55. });
  56. };
  57. }
  58. getColumnOptions() {
  59. if (!this.panelCtrl.dataRaw) {
  60. return this.$q.when([]);
  61. }
  62. var columns = this.transformers[this.panel.transform].getColumns(this.panelCtrl.dataRaw);
  63. var segments = _.map(columns, (c: any) => this.uiSegmentSrv.newSegment({value: c.text}));
  64. return this.$q.when(segments);
  65. }
  66. addColumn() {
  67. var columns = transformers[this.panel.transform].getColumns(this.panelCtrl.dataRaw);
  68. var column = _.find(columns, {text: this.addColumnSegment.value});
  69. if (column) {
  70. this.panel.columns.push(column);
  71. this.render();
  72. }
  73. var plusButton = this.uiSegmentSrv.newPlusButton();
  74. this.addColumnSegment.html = plusButton.html;
  75. this.addColumnSegment.value = plusButton.value;
  76. }
  77. transformChanged() {
  78. this.panel.columns = [];
  79. this.render();
  80. }
  81. render() {
  82. this.panelCtrl.render();
  83. }
  84. removeColumn(column) {
  85. this.panel.columns = _.without(this.panel.columns, column);
  86. this.panelCtrl.render();
  87. }
  88. setUnitFormat(column, subItem) {
  89. column.unit = subItem.value;
  90. this.panelCtrl.render();
  91. }
  92. addColumnStyle() {
  93. var newStyleRule = {
  94. unit: 'short',
  95. type: 'number',
  96. alias: '',
  97. decimals: 2,
  98. colors: ["rgba(245, 54, 54, 0.9)", "rgba(237, 129, 40, 0.89)", "rgba(50, 172, 45, 0.97)"],
  99. colorMode: null,
  100. pattern: '',
  101. dateFormat: 'YYYY-MM-DD HH:mm:ss',
  102. thresholds: [],
  103. };
  104. var styles = this.panel.styles;
  105. var stylesCount = styles.length;
  106. var indexToInsert = stylesCount;
  107. // check if last is a catch all rule, then add it before that one
  108. if (stylesCount > 0) {
  109. var last = styles[stylesCount-1];
  110. if (last.pattern === '/.*/') {
  111. indexToInsert = stylesCount-1;
  112. }
  113. }
  114. styles.splice(indexToInsert, 0, newStyleRule);
  115. this.activeStyleIndex = indexToInsert;
  116. }
  117. removeColumnStyle(style) {
  118. this.panel.styles = _.without(this.panel.styles, style);
  119. }
  120. invertColorOrder(index) {
  121. var ref = this.panel.styles[index].colors;
  122. var copy = ref[0];
  123. ref[0] = ref[2];
  124. ref[2] = copy;
  125. this.panelCtrl.render();
  126. }
  127. }
  128. /** @ngInject */
  129. export function tablePanelEditor($q, uiSegmentSrv) {
  130. 'use strict';
  131. return {
  132. restrict: 'E',
  133. scope: true,
  134. templateUrl: 'public/app/plugins/panel/table/editor.html',
  135. controller: TablePanelEditorCtrl,
  136. };
  137. }