2018-10-04 13:01:43 +02:00
|
|
|
import angular from 'angular';
|
2021-04-21 08:38:00 +01:00
|
|
|
import { assign } from 'lodash';
|
2018-10-04 13:01:43 +02:00
|
|
|
|
2021-11-09 08:37:16 +01:00
|
|
|
import { AngularComponent, AngularLoader as AngularLoaderInterface } from '@grafana/runtime';
|
2021-11-10 11:05:36 +01:00
|
|
|
import { GrafanaRootScope } from 'app/angular/GrafanaCtrl';
|
2022-04-22 14:33:13 +01:00
|
|
|
import coreModule from 'app/angular/core_module';
|
2018-10-04 13:01:43 +02:00
|
|
|
|
2019-06-04 09:19:55 +02:00
|
|
|
export class AngularLoader implements AngularLoaderInterface {
|
2023-01-23 12:15:05 +01:00
|
|
|
static $inject = ['$compile', '$rootScope'];
|
|
|
|
|
|
2023-07-17 15:58:22 +01:00
|
|
|
constructor(
|
2023-08-23 10:11:59 +01:00
|
|
|
private $compile: angular.ICompileService,
|
2023-07-17 15:58:22 +01:00
|
|
|
private $rootScope: GrafanaRootScope
|
|
|
|
|
) {}
|
2018-10-04 13:01:43 +02:00
|
|
|
|
2023-08-23 10:11:59 +01:00
|
|
|
load(elem: HTMLElement, scopeProps: any, template: string): AngularComponent {
|
2018-10-04 13:01:43 +02:00
|
|
|
const scope = this.$rootScope.$new();
|
|
|
|
|
|
2021-04-21 08:38:00 +01:00
|
|
|
assign(scope, scopeProps);
|
2018-10-04 13:01:43 +02:00
|
|
|
|
|
|
|
|
const compiledElem = this.$compile(template)(scope);
|
|
|
|
|
const rootNode = angular.element(elem);
|
|
|
|
|
rootNode.append(compiledElem);
|
|
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
destroy: () => {
|
|
|
|
|
scope.$destroy();
|
|
|
|
|
compiledElem.remove();
|
|
|
|
|
},
|
2018-11-05 13:39:57 +01:00
|
|
|
digest: () => {
|
2019-02-14 15:18:55 +01:00
|
|
|
if (!scope.$$phase) {
|
|
|
|
|
scope.$digest();
|
|
|
|
|
}
|
2018-11-05 13:39:57 +01:00
|
|
|
},
|
2018-11-15 09:46:21 +01:00
|
|
|
getScope: () => {
|
|
|
|
|
return scope;
|
|
|
|
|
},
|
2018-10-04 13:01:43 +02:00
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-06-04 09:19:55 +02:00
|
|
|
coreModule.service('angularLoader', AngularLoader);
|