2017-12-20 05:33:33 -06:00
|
|
|
import angular from 'angular';
|
2022-04-22 08:33:13 -05:00
|
|
|
|
2021-11-09 01:37:16 -06:00
|
|
|
import coreModule from '../core_module';
|
2016-01-13 07:03:50 -06:00
|
|
|
|
|
|
|
class DynamicDirectiveSrv {
|
|
|
|
/** @ngInject */
|
2019-04-28 02:58:12 -05:00
|
|
|
constructor(private $compile: angular.ICompileService) {}
|
2016-01-13 07:03:50 -06:00
|
|
|
|
2019-04-28 02:58:12 -05:00
|
|
|
addDirective(element: any, name: string, scope: any) {
|
2018-08-26 14:52:57 -05:00
|
|
|
const child = angular.element(document.createElement(name));
|
2016-01-13 15:31:29 -06:00
|
|
|
this.$compile(child)(scope);
|
|
|
|
|
2016-01-13 07:03:50 -06:00
|
|
|
element.empty();
|
2016-01-13 15:31:29 -06:00
|
|
|
element.append(child);
|
2016-01-13 07:03:50 -06:00
|
|
|
}
|
|
|
|
|
2019-04-28 02:58:12 -05:00
|
|
|
link(scope: any, elem: JQLite, attrs: any, options: any) {
|
2018-08-25 14:22:50 -05:00
|
|
|
const directiveInfo = options.directive(scope);
|
|
|
|
if (!directiveInfo || !directiveInfo.fn) {
|
|
|
|
elem.empty();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!directiveInfo.fn.registered) {
|
|
|
|
coreModule.directive(attrs.$normalize(directiveInfo.name), directiveInfo.fn);
|
|
|
|
directiveInfo.fn.registered = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
this.addDirective(elem, directiveInfo.name, scope);
|
2016-01-14 05:35:31 -06:00
|
|
|
}
|
|
|
|
|
2019-04-28 02:58:12 -05:00
|
|
|
create(options: any) {
|
2018-08-26 10:14:40 -05:00
|
|
|
const directiveDef = {
|
2017-12-20 05:33:33 -06:00
|
|
|
restrict: 'E',
|
2016-01-13 14:07:57 -06:00
|
|
|
scope: options.scope,
|
2019-04-28 02:58:12 -05:00
|
|
|
link: (scope: any, elem: JQLite, attrs: any) => {
|
2016-01-15 04:22:54 -06:00
|
|
|
if (options.watchPath) {
|
2019-04-28 02:58:12 -05:00
|
|
|
let childScope: any = null;
|
2016-01-15 04:22:54 -06:00
|
|
|
scope.$watch(options.watchPath, () => {
|
2016-01-14 08:15:45 -06:00
|
|
|
if (childScope) {
|
|
|
|
childScope.$destroy();
|
|
|
|
}
|
|
|
|
childScope = scope.$new();
|
|
|
|
this.link(childScope, elem, attrs, options);
|
|
|
|
});
|
2016-01-14 05:35:31 -06:00
|
|
|
} else {
|
|
|
|
this.link(scope, elem, attrs, options);
|
|
|
|
}
|
2017-12-20 05:33:33 -06:00
|
|
|
},
|
2016-01-13 14:07:57 -06:00
|
|
|
};
|
2016-01-13 07:03:50 -06:00
|
|
|
|
2016-01-13 14:07:57 -06:00
|
|
|
return directiveDef;
|
2016-01-13 07:03:50 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-20 05:33:33 -06:00
|
|
|
coreModule.service('dynamicDirectiveSrv', DynamicDirectiveSrv);
|