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