editor.ts 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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. 'cell': {text: 'Cell'},
  19. 'value': {text: 'Value'},
  20. 'row': {text: 'Row'},
  21. };
  22. scope.columnTypes = {
  23. 'number': {text: 'Number'},
  24. 'string': {text: 'String'},
  25. 'date': {text: 'Date'},
  26. };
  27. scope.updateJsonFieldsMenu = function(data) {
  28. scope.jsonFieldsMenu = [];
  29. if (!data || data.length === 0) {
  30. return;
  31. }
  32. var names = {};
  33. for (var i = 0; i < data.length; i++) {
  34. var series = data[i];
  35. if (series.type !== 'docs') {
  36. continue;
  37. }
  38. for (var y = 0; y < series.datapoints.length; y++) {
  39. var doc = series.datapoints[y];
  40. for (var propName in doc) {
  41. names[propName] = true;
  42. }
  43. }
  44. }
  45. _.each(names, function(value, key) {
  46. scope.jsonFieldsMenu.push({text: key});
  47. });
  48. };
  49. scope.updateJsonFieldsMenu(scope.dataRaw);
  50. scope.$on('render', function(event, table, rawData) {
  51. scope.updateJsonFieldsMenu(rawData);
  52. });
  53. scope.addJsonField = function(menuItem) {
  54. scope.panel.fields.push({name: menuItem.text});
  55. scope.render();
  56. };
  57. scope.removeJsonField = function(field) {
  58. scope.panel.fields = _.without(scope.panel.fields, field);
  59. scope.render();
  60. };
  61. scope.setUnitFormat = function(column, subItem) {
  62. column.unit = subItem.value;
  63. scope.render();
  64. };
  65. scope.addColumnStyle = function() {
  66. var columnStyleDefaults = {
  67. unit: 'short',
  68. type: 'number',
  69. decimals: 2,
  70. colors: ["rgba(245, 54, 54, 0.9)", "rgba(237, 129, 40, 0.89)", "rgba(50, 172, 45, 0.97)"],
  71. colorMode: 'value',
  72. pattern: '/.*/',
  73. };
  74. scope.panel.columns.push(angular.copy(columnStyleDefaults));
  75. };
  76. scope.removeColumnStyle = function(col) {
  77. scope.panel.columns = _.without(scope.panel.columns, col);
  78. };
  79. scope.getColumnNames = function() {
  80. if (!scope.table) {
  81. return [];
  82. }
  83. return _.map(scope.table.columns, function(col: any) {
  84. return col.text;
  85. });
  86. };
  87. }
  88. };
  89. }