editor.ts 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. ///<reference path="../../headers/common.d.ts" />
  2. import angular = require('angular');
  3. import $ = require('jquery');
  4. import _ = require('lodash');
  5. import kbn = require('app/core/utils/kbn');
  6. import moment = require('moment');
  7. import {transformers} from './transformers';
  8. export function tablePanelEditor() {
  9. 'use strict';
  10. return {
  11. restrict: 'E',
  12. scope: true,
  13. templateUrl: 'app/panels/table/editor.html',
  14. link: function(scope, elem) {
  15. scope.transformers = transformers;
  16. scope.unitFormats = kbn.getUnitFormats();
  17. scope.colorModes = [
  18. {text: 'Disabled', value: null},
  19. {text: 'Cell', value: 'cell'},
  20. {text: 'Value', value: 'value'},
  21. {text: 'Row', value: 'row'},
  22. ];
  23. scope.columnTypes = [
  24. {text: 'Number', value: 'number'},
  25. {text: 'String', value: 'string'},
  26. {text: 'Date', value: 'date'},
  27. ];
  28. scope.updateJsonFieldsMenu = function(data) {
  29. scope.jsonFieldsMenu = [];
  30. if (!data || data.length === 0) {
  31. return;
  32. }
  33. var names = {};
  34. for (var i = 0; i < data.length; i++) {
  35. var series = data[i];
  36. if (series.type !== 'docs') {
  37. continue;
  38. }
  39. for (var y = 0; y < series.datapoints.length; y++) {
  40. var doc = series.datapoints[y];
  41. for (var propName in doc) {
  42. names[propName] = true;
  43. }
  44. }
  45. }
  46. _.each(names, function(value, key) {
  47. scope.jsonFieldsMenu.push({text: key});
  48. });
  49. };
  50. scope.updateJsonFieldsMenu(scope.dataRaw);
  51. scope.$on('render', function(event, table, rawData) {
  52. scope.updateJsonFieldsMenu(rawData);
  53. });
  54. scope.addJsonField = function(menuItem) {
  55. scope.panel.fields.push({name: menuItem.text});
  56. scope.render();
  57. };
  58. scope.removeJsonField = function(field) {
  59. scope.panel.fields = _.without(scope.panel.fields, field);
  60. scope.render();
  61. };
  62. scope.setUnitFormat = function(column, subItem) {
  63. column.unit = subItem.value;
  64. scope.render();
  65. };
  66. scope.addColumnStyle = function() {
  67. var columnStyleDefaults = {
  68. unit: 'short',
  69. type: 'number',
  70. decimals: 2,
  71. colors: ["rgba(245, 54, 54, 0.9)", "rgba(237, 129, 40, 0.89)", "rgba(50, 172, 45, 0.97)"],
  72. colorMode: null,
  73. pattern: '/.*/',
  74. thresholds: [],
  75. };
  76. scope.panel.columns.push(angular.copy(columnStyleDefaults));
  77. };
  78. scope.removeColumnStyle = function(col) {
  79. scope.panel.columns = _.without(scope.panel.columns, col);
  80. };
  81. scope.getColumnNames = function() {
  82. if (!scope.table) {
  83. return [];
  84. }
  85. return _.map(scope.table.columns, function(col: any) {
  86. return col.text;
  87. });
  88. };
  89. }
  90. };
  91. }