query_editor_row.ts 2.2 KB

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