plugin_component.ts 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. ///<reference path="../../headers/common.d.ts" />
  2. import angular from 'angular';
  3. import _ from 'lodash';
  4. import coreModule from '../core_module';
  5. function pluginDirectiveLoader($compile, datasourceSrv, $rootScope) {
  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. // QueryCtrl
  26. case "query-ctrl": {
  27. let datasource = scope.target.datasource || scope.ctrl.panel.datasource;
  28. return datasourceSrv.get(datasource).then(ds => {
  29. scope.datasource = ds;
  30. return System.import(ds.meta.module).then(dsModule => {
  31. return {
  32. name: 'query-ctrl-' + ds.meta.id,
  33. bindings: {target: "=", panelCtrl: "=", datasource: "="},
  34. attrs: {"target": "target", "panel-ctrl": "ctrl", datasource: "datasource"},
  35. Component: dsModule.QueryCtrl
  36. };
  37. });
  38. });
  39. }
  40. // QueryOptionsCtrl
  41. case "query-options-ctrl": {
  42. return datasourceSrv.get(scope.ctrl.panel.datasource).then(ds => {
  43. return System.import(ds.meta.module).then((dsModule): any => {
  44. if (!dsModule.QueryOptionsCtrl) {
  45. return {notFound: true};
  46. }
  47. return {
  48. name: 'query-options-ctrl-' + ds.meta.id,
  49. bindings: {panelCtrl: "="},
  50. attrs: {"panel-ctrl": "ctrl"},
  51. Component: dsModule.QueryOptionsCtrl
  52. };
  53. });
  54. });
  55. }
  56. // ConfigCtrl
  57. case 'datasource-config-ctrl': {
  58. return System.import(scope.datasourceMeta.module).then(function(dsModule) {
  59. return {
  60. name: 'ds-config-' + scope.datasourceMeta.id,
  61. bindings: {meta: "=", current: "="},
  62. attrs: {meta: "datasourceMeta", current: "current"},
  63. Component: dsModule.ConfigCtrl,
  64. };
  65. });
  66. }
  67. default: {
  68. $rootScope.appEvent('alert-error', ['Plugin component error', 'could not find component '+ attrs.type]);
  69. }
  70. }
  71. }
  72. function appendAndCompile(scope, elem, componentInfo) {
  73. var child = angular.element(document.createElement(componentInfo.name));
  74. _.each(componentInfo.attrs, (value, key) => {
  75. child.attr(key, value);
  76. });
  77. $compile(child)(scope);
  78. elem.empty();
  79. elem.append(child);
  80. }
  81. function registerPluginComponent(scope, elem, attrs, componentInfo) {
  82. if (componentInfo.notFound) {
  83. elem.empty();
  84. return;
  85. }
  86. if (!componentInfo.Component.registered) {
  87. var directiveName = attrs.$normalize(componentInfo.name);
  88. var directiveFn = getPluginComponentDirective(componentInfo);
  89. coreModule.directive(directiveName, directiveFn);
  90. componentInfo.Component.registered = true;
  91. }
  92. appendAndCompile(scope, elem, componentInfo);
  93. }
  94. return {
  95. restrict: 'E',
  96. link: function(scope, elem, attrs) {
  97. getModule(scope, attrs).then(function (componentInfo) {
  98. registerPluginComponent(scope, elem, attrs, componentInfo);
  99. });
  100. }
  101. };
  102. }
  103. coreModule.directive('pluginComponent', pluginDirectiveLoader);