query_editor_row.ts 2.4 KB

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