query_editor.ts 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. ///<reference path="../../headers/common.d.ts" />
  2. import angular from 'angular';
  3. import _ from 'lodash';
  4. var directivesModule = angular.module('grafana.directives');
  5. function pluginDirectiveLoader($compile, datasourceSrv) {
  6. function getPluginComponentDirective(options) {
  7. return function() {
  8. return {
  9. templateUrl: options.Component.templateUrl,
  10. restrict: 'E',
  11. controller: options.Component,
  12. controllerAs: 'ctrl',
  13. bindToController: true,
  14. scope: options.bindings,
  15. link: (scope, elem, attrs, ctrl) => {
  16. if (ctrl.link) {
  17. ctrl.link(scope, elem, attrs, ctrl);
  18. }
  19. }
  20. };
  21. };
  22. }
  23. function getModule(scope, attrs) {
  24. switch (attrs.type) {
  25. case "metrics-query-editor": {
  26. let datasource = scope.target.datasource || scope.ctrl.panel.datasource;
  27. return datasourceSrv.get(datasource).then(ds => {
  28. return System.import(ds.meta.module).then(dsModule => {
  29. return {
  30. name: 'metrics-query-editor-' + ds.meta.id,
  31. bindings: {target: "=", panelCtrl: "="},
  32. attrs: {"target": "target", "panel-ctrl": "ctrl"},
  33. Component: dsModule.MetricsQueryEditor
  34. };
  35. });
  36. });
  37. }
  38. }
  39. }
  40. function appendAndCompile(scope, elem, componentInfo) {
  41. var child = angular.element(document.createElement(componentInfo.name));
  42. _.each(componentInfo.attrs, (value, key) => {
  43. child.attr(key, value);
  44. });
  45. $compile(child)(scope);
  46. elem.empty();
  47. elem.append(child);
  48. }
  49. function registerPluginComponent(scope, elem, attrs, componentInfo) {
  50. if (!componentInfo.Component.registered) {
  51. var directiveName = attrs.$normalize(componentInfo.name);
  52. var directiveFn = getPluginComponentDirective(componentInfo);
  53. directivesModule.directive(directiveName, directiveFn);
  54. componentInfo.Component.registered = true;
  55. }
  56. appendAndCompile(scope, elem, componentInfo);
  57. }
  58. return {
  59. restrict: 'E',
  60. link: function(scope, elem, attrs) {
  61. getModule(scope, attrs).then(function (componentInfo) {
  62. registerPluginComponent(scope, elem, attrs, componentInfo);
  63. });
  64. }
  65. };
  66. }
  67. /** @ngInject */
  68. function metricsQueryEditor(dynamicDirectiveSrv, datasourceSrv) {
  69. return dynamicDirectiveSrv.create({
  70. watchPath: "ctrl.panel.datasource",
  71. directive: scope => {
  72. let datasource = scope.target.datasource || scope.ctrl.panel.datasource;
  73. return datasourceSrv.get(datasource).then(ds => {
  74. scope.ctrl.datasource = ds;
  75. if (!scope.target.refId) {
  76. scope.target.refId = 'A';
  77. }
  78. return System.import(ds.meta.module).then(dsModule => {
  79. return {
  80. name: 'metrics-query-editor-' + ds.meta.id,
  81. fn: dsModule.metricsQueryEditor,
  82. };
  83. });
  84. });
  85. }
  86. });
  87. }
  88. /** @ngInject */
  89. function metricsQueryOptions(dynamicDirectiveSrv, datasourceSrv) {
  90. return dynamicDirectiveSrv.create({
  91. watchPath: "ctrl.panel.datasource",
  92. directive: scope => {
  93. return datasourceSrv.get(scope.ctrl.panel.datasource).then(ds => {
  94. return System.import(ds.meta.module).then(dsModule => {
  95. return {
  96. name: 'metrics-query-options-' + ds.meta.id,
  97. fn: dsModule.metricsQueryOptions
  98. };
  99. });
  100. });
  101. }
  102. });
  103. }
  104. directivesModule.directive('pluginDirectiveLoader', pluginDirectiveLoader);
  105. directivesModule.directive('metricsQueryEditor', metricsQueryEditor);
  106. directivesModule.directive('metricsQueryOptions', metricsQueryOptions);