query_editor_row.ts 2.4 KB

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