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 {
|
2018-10-04 06:01:43 -05:00
|
|
|
/** @ngInject */
|
2019-10-14 03:27:47 -05:00
|
|
|
constructor(private $compile: any, private $rootScope: GrafanaRootScope) {}
|
2018-10-04 06:01:43 -05:00
|
|
|
|
2019-04-28 02:58:12 -05:00
|
|
|
load(elem: any, 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);
|