query_editor_row.ts 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. import angular from 'angular';
  2. const module = angular.module('grafana.directives');
  3. export class QueryRowCtrl {
  4. collapsedText: string;
  5. canCollapse: boolean;
  6. getCollapsedText: any;
  7. target: any;
  8. queryCtrl: any;
  9. panelCtrl: any;
  10. panel: any;
  11. collapsed: any;
  12. hideEditorRowActions: boolean;
  13. constructor() {
  14. this.panelCtrl = this.queryCtrl.panelCtrl;
  15. this.target = this.queryCtrl.target;
  16. this.panel = this.panelCtrl.panel;
  17. this.hideEditorRowActions = this.panelCtrl.hideEditorRowActions;
  18. if (!this.target.refId) {
  19. this.target.refId = this.panel.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. toggleCollapse(init) {
  35. if (!this.canCollapse) {
  36. return;
  37. }
  38. if (!this.panelCtrl.__collapsedQueryCache) {
  39. this.panelCtrl.__collapsedQueryCache = {};
  40. }
  41. if (init) {
  42. this.collapsed = this.panelCtrl.__collapsedQueryCache[this.target.refId] !== false;
  43. } else {
  44. this.collapsed = !this.collapsed;
  45. this.panelCtrl.__collapsedQueryCache[this.target.refId] = this.collapsed;
  46. }
  47. try {
  48. this.collapsedText = this.queryCtrl.getCollapsedText();
  49. } catch (e) {
  50. const err = e.message || e.toString();
  51. this.collapsedText = 'Error: ' + err;
  52. }
  53. }
  54. toggleEditorMode() {
  55. if (this.canCollapse && this.collapsed) {
  56. this.collapsed = false;
  57. }
  58. this.queryCtrl.toggleEditorMode();
  59. }
  60. removeQuery() {
  61. if (this.panelCtrl.__collapsedQueryCache) {
  62. delete this.panelCtrl.__collapsedQueryCache[this.target.refId];
  63. }
  64. this.panelCtrl.removeQuery(this.target);
  65. }
  66. duplicateQuery() {
  67. const clone = angular.copy(this.target);
  68. this.panelCtrl.addQuery(clone);
  69. }
  70. moveQuery(direction) {
  71. this.panelCtrl.moveQuery(this.target, direction);
  72. }
  73. }
  74. /** @ngInject */
  75. function queryEditorRowDirective() {
  76. return {
  77. restrict: 'E',
  78. controller: QueryRowCtrl,
  79. bindToController: true,
  80. controllerAs: 'ctrl',
  81. templateUrl: 'public/app/features/panel/partials/query_editor_row.html',
  82. transclude: true,
  83. scope: {
  84. queryCtrl: '=',
  85. canCollapse: '=',
  86. hasTextEditMode: '=',
  87. },
  88. };
  89. }
  90. module.directive('queryEditorRow', queryEditorRowDirective);