query_editor_row.ts 2.4 KB

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