query_editor_row.ts 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. ///<reference path="../../headers/common.d.ts" />
  2. import angular from 'angular';
  3. import _ from 'lodash';
  4. var module = angular.module('grafana.directives');
  5. export class QueryRowCtrl {
  6. collapsedText: string;
  7. canCollapse: boolean;
  8. getCollapsedText: any;
  9. target: any;
  10. queryCtrl: any;
  11. panelCtrl: any;
  12. panel: any;
  13. collapsed: any;
  14. constructor() {
  15. this.panelCtrl = this.queryCtrl.panelCtrl;
  16. this.target = this.queryCtrl.target;
  17. this.panel = this.panelCtrl.panel;
  18. if (!this.target.refId) {
  19. this.target.refId = this.getNextQueryLetter();
  20. }
  21. this.toggleCollapse(true);
  22. if (this.target.isNew) {
  23. delete this.target.isNew;
  24. this.toggleCollapse(false);
  25. }
  26. if (this.panel.targets.length < 4) {
  27. this.collapsed = false;
  28. }
  29. }
  30. toggleHideQuery() {
  31. this.target.hide = !this.target.hide;
  32. this.panelCtrl.refresh();
  33. }
  34. getNextQueryLetter() {
  35. var letters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
  36. return _.find(letters, refId => {
  37. return _.every(this.panel.targets, function(other) {
  38. return other.refId !== refId;
  39. });
  40. });
  41. }
  42. toggleCollapse(init) {
  43. if (!this.canCollapse) {
  44. return;
  45. }
  46. if (!this.panelCtrl.__collapsedQueryCache) {
  47. this.panelCtrl.__collapsedQueryCache = {};
  48. }
  49. if (init) {
  50. this.collapsed = this.panelCtrl.__collapsedQueryCache[this.target.refId] !== false;
  51. } else {
  52. this.collapsed = !this.collapsed;
  53. this.panelCtrl.__collapsedQueryCache[this.target.refId] = this.collapsed;
  54. }
  55. try {
  56. this.collapsedText = this.queryCtrl.getCollapsedText();
  57. } catch (e) {
  58. var err = e.message || e.toString();
  59. this.collapsedText = 'Error: ' + err;
  60. }
  61. }
  62. toggleEditorMode() {
  63. if (this.canCollapse && this.collapsed) {
  64. this.collapsed = false;
  65. }
  66. this.queryCtrl.toggleEditorMode();
  67. }
  68. removeQuery() {
  69. if (this.panelCtrl.__collapsedQueryCache) {
  70. delete this.panelCtrl.__collapsedQueryCache[this.target.refId];
  71. }
  72. this.panel.targets = _.without(this.panel.targets, this.target);
  73. this.panelCtrl.refresh();
  74. }
  75. duplicateQuery() {
  76. var clone = angular.copy(this.target);
  77. clone.refId = this.getNextQueryLetter();
  78. this.panel.targets.push(clone);
  79. }
  80. moveQuery(direction) {
  81. var index = _.indexOf(this.panel.targets, this.target);
  82. _.move(this.panel.targets, index, index + direction);
  83. }
  84. }
  85. /** @ngInject **/
  86. function queryEditorRowDirective() {
  87. return {
  88. restrict: 'E',
  89. controller: QueryRowCtrl,
  90. bindToController: true,
  91. controllerAs: "ctrl",
  92. templateUrl: 'public/app/features/panel/partials/query_editor_row.html',
  93. transclude: true,
  94. scope: {
  95. queryCtrl: "=",
  96. canCollapse: "=",
  97. hasTextEditMode: "=",
  98. },
  99. };
  100. }
  101. module.directive('queryEditorRow', queryEditorRowDirective);