UNPKG

17.2 kBJavaScriptView Raw
1(function (global, factory) {
2 typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports, require('@angular/core'), require('ngx-bootstrap/utils'), require('ngx-bootstrap/positioning')) :
3 typeof define === 'function' && define.amd ? define('ngx-bootstrap/component-loader', ['exports', '@angular/core', 'ngx-bootstrap/utils', 'ngx-bootstrap/positioning'], factory) :
4 (global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory((global['ngx-bootstrap'] = global['ngx-bootstrap'] || {}, global['ngx-bootstrap']['component-loader'] = {}), global.ng.core, global.utils, global.i1));
5}(this, (function (exports, i0, utils, i1) { 'use strict';
6
7 var BsComponentRef = /** @class */ (function () {
8 function BsComponentRef() {
9 }
10 return BsComponentRef;
11 }());
12
13 /**
14 * @copyright Valor Software
15 * @copyright Angular ng-bootstrap team
16 */
17 var ContentRef = /** @class */ (function () {
18 function ContentRef(
19 // eslint-disable-next-line @typescript-eslint/no-explicit-any
20 nodes, viewRef,
21 // eslint-disable-next-line @typescript-eslint/no-explicit-any
22 componentRef) {
23 this.nodes = nodes;
24 this.viewRef = viewRef;
25 this.componentRef = componentRef;
26 }
27 return ContentRef;
28 }());
29
30 // todo: add delay support
31 var ComponentLoader = /** @class */ (function () {
32 /**
33 * Do not use this directly, it should be instanced via
34 * `ComponentLoadFactory.attach`
35 * @internal
36 */
37 function ComponentLoader(_viewContainerRef, _renderer, _elementRef, _injector, _componentFactoryResolver, _ngZone, _applicationRef, _posService) {
38 this._viewContainerRef = _viewContainerRef;
39 this._renderer = _renderer;
40 this._elementRef = _elementRef;
41 this._injector = _injector;
42 this._componentFactoryResolver = _componentFactoryResolver;
43 this._ngZone = _ngZone;
44 this._applicationRef = _applicationRef;
45 this._posService = _posService;
46 this.onBeforeShow = new i0.EventEmitter();
47 this.onShown = new i0.EventEmitter();
48 this.onBeforeHide = new i0.EventEmitter();
49 this.onHidden = new i0.EventEmitter();
50 this._providers = [];
51 this._isHiding = false;
52 /**
53 * A selector used if container element was not found
54 */
55 this.containerDefaultSelector = 'body';
56 this._listenOpts = {};
57 this._globalListener = Function.prototype;
58 }
59 Object.defineProperty(ComponentLoader.prototype, "isShown", {
60 get: function () {
61 if (this._isHiding) {
62 return false;
63 }
64 return !!this._componentRef;
65 },
66 enumerable: false,
67 configurable: true
68 });
69 ComponentLoader.prototype.attach = function (compType) {
70 this._componentFactory = this._componentFactoryResolver
71 .resolveComponentFactory(compType);
72 return this;
73 };
74 // todo: add behaviour: to target element, `body`, custom element
75 ComponentLoader.prototype.to = function (container) {
76 this.container = container || this.container;
77 return this;
78 };
79 ComponentLoader.prototype.position = function (opts) {
80 if (!opts) {
81 return this;
82 }
83 this.attachment = opts.attachment || this.attachment;
84 this._elementRef = opts.target || this._elementRef;
85 return this;
86 };
87 ComponentLoader.prototype.provide = function (provider) {
88 this._providers.push(provider);
89 return this;
90 };
91 // todo: appendChild to element or document.querySelector(this.container)
92 ComponentLoader.prototype.show = function (opts) {
93 if (opts === void 0) { opts = {}; }
94 this._subscribePositioning();
95 this._innerComponent = void 0;
96 if (!this._componentRef) {
97 this.onBeforeShow.emit();
98 this._contentRef = this._getContentRef(opts.content, opts.context, opts.initialState);
99 var injector = i0.Injector.create({
100 providers: this._providers,
101 parent: this._injector
102 });
103 if (!this._componentFactory) {
104 return;
105 }
106 this._componentRef = this._componentFactory.create(injector, this._contentRef.nodes);
107 this._applicationRef.attachView(this._componentRef.hostView);
108 // this._componentRef = this._viewContainerRef
109 // .createComponent(this._componentFactory, 0, injector, this._contentRef.nodes);
110 this.instance = this._componentRef.instance;
111 Object.assign(this._componentRef.instance, opts);
112 if (this.container instanceof i0.ElementRef) {
113 this.container.nativeElement.appendChild(this._componentRef.location.nativeElement);
114 }
115 if (typeof this.container === 'string' && typeof document !== 'undefined') {
116 var selectedElement = document.querySelector(this.container) ||
117 document.querySelector(this.containerDefaultSelector);
118 if (!selectedElement) {
119 return;
120 }
121 selectedElement.appendChild(this._componentRef.location.nativeElement);
122 }
123 if (!this.container &&
124 this._elementRef &&
125 this._elementRef.nativeElement.parentElement) {
126 this._elementRef.nativeElement.parentElement.appendChild(this._componentRef.location.nativeElement);
127 }
128 // we need to manually invoke change detection since events registered
129 // via
130 // Renderer::listen() are not picked up by change detection with the
131 // OnPush strategy
132 if (this._contentRef.componentRef) {
133 this._innerComponent = this._contentRef.componentRef.instance;
134 this._contentRef.componentRef.changeDetectorRef.markForCheck();
135 this._contentRef.componentRef.changeDetectorRef.detectChanges();
136 }
137 this._componentRef.changeDetectorRef.markForCheck();
138 this._componentRef.changeDetectorRef.detectChanges();
139 this.onShown.emit(opts.id ? { id: opts.id } : this._componentRef.instance);
140 }
141 this._registerOutsideClick();
142 return this._componentRef;
143 };
144 ComponentLoader.prototype.hide = function (id) {
145 var _a, _b, _c;
146 if (!this._componentRef) {
147 return this;
148 }
149 this._posService.deletePositionElement(this._componentRef.location);
150 this.onBeforeHide.emit(this._componentRef.instance);
151 var componentEl = this._componentRef.location.nativeElement;
152 componentEl.parentNode.removeChild(componentEl);
153 if ((_a = this._contentRef) === null || _a === void 0 ? void 0 : _a.componentRef) {
154 this._contentRef.componentRef.destroy();
155 }
156 if (this._viewContainerRef && ((_b = this._contentRef) === null || _b === void 0 ? void 0 : _b.viewRef)) {
157 this._viewContainerRef.remove(this._viewContainerRef.indexOf(this._contentRef.viewRef));
158 }
159 if ((_c = this._contentRef) === null || _c === void 0 ? void 0 : _c.viewRef) {
160 this._contentRef.viewRef.destroy();
161 }
162 this._contentRef = void 0;
163 this._componentRef = void 0;
164 this._removeGlobalListener();
165 this.onHidden.emit(id ? { id: id } : null);
166 return this;
167 };
168 ComponentLoader.prototype.toggle = function () {
169 if (this.isShown) {
170 this.hide();
171 return;
172 }
173 this.show();
174 };
175 ComponentLoader.prototype.dispose = function () {
176 if (this.isShown) {
177 this.hide();
178 }
179 this._unsubscribePositioning();
180 if (this._unregisterListenersFn) {
181 this._unregisterListenersFn();
182 }
183 };
184 ComponentLoader.prototype.listen = function (listenOpts) {
185 var _this = this;
186 var _a;
187 this.triggers = listenOpts.triggers || this.triggers;
188 this._listenOpts.outsideClick = listenOpts.outsideClick;
189 this._listenOpts.outsideEsc = listenOpts.outsideEsc;
190 listenOpts.target = listenOpts.target || ((_a = this._elementRef) === null || _a === void 0 ? void 0 : _a.nativeElement);
191 var hide = (this._listenOpts.hide = function () { return listenOpts.hide ? listenOpts.hide() : void _this.hide(); });
192 var show = (this._listenOpts.show = function (registerHide) {
193 listenOpts.show ? listenOpts.show(registerHide) : _this.show(registerHide);
194 registerHide();
195 });
196 // eslint-disable-next-line @typescript-eslint/no-explicit-any
197 var toggle = function (registerHide) {
198 _this.isShown ? hide() : show(registerHide);
199 };
200 if (this._renderer) {
201 this._unregisterListenersFn = utils.listenToTriggersV2(this._renderer, {
202 target: listenOpts.target,
203 triggers: listenOpts.triggers,
204 show: show,
205 hide: hide,
206 toggle: toggle
207 });
208 }
209 return this;
210 };
211 ComponentLoader.prototype._removeGlobalListener = function () {
212 if (this._globalListener) {
213 this._globalListener();
214 this._globalListener = Function.prototype;
215 }
216 };
217 ComponentLoader.prototype.attachInline = function (vRef,
218 // eslint-disable-next-line @typescript-eslint/no-explicit-any
219 template) {
220 if (vRef && template) {
221 this._inlineViewRef = vRef.createEmbeddedView(template);
222 }
223 return this;
224 };
225 ComponentLoader.prototype._registerOutsideClick = function () {
226 var _this = this;
227 if (!this._componentRef || !this._componentRef.location) {
228 return;
229 }
230 // why: should run after first event bubble
231 if (this._listenOpts.outsideClick) {
232 var target_1 = this._componentRef.location.nativeElement;
233 setTimeout(function () {
234 if (_this._renderer && _this._elementRef) {
235 _this._globalListener = utils.registerOutsideClick(_this._renderer, {
236 targets: [target_1, _this._elementRef.nativeElement],
237 outsideClick: _this._listenOpts.outsideClick,
238 hide: function () { return _this._listenOpts.hide && _this._listenOpts.hide(); }
239 });
240 }
241 });
242 }
243 if (this._listenOpts.outsideEsc && this._renderer && this._elementRef) {
244 var target = this._componentRef.location.nativeElement;
245 this._globalListener = utils.registerEscClick(this._renderer, {
246 targets: [target, this._elementRef.nativeElement],
247 outsideEsc: this._listenOpts.outsideEsc,
248 hide: function () { return _this._listenOpts.hide && _this._listenOpts.hide(); }
249 });
250 }
251 };
252 ComponentLoader.prototype.getInnerComponent = function () {
253 return this._innerComponent;
254 };
255 ComponentLoader.prototype._subscribePositioning = function () {
256 var _this = this;
257 if (this._zoneSubscription || !this.attachment) {
258 return;
259 }
260 this.onShown.subscribe(function () {
261 var _a;
262 _this._posService.position({
263 element: (_a = _this._componentRef) === null || _a === void 0 ? void 0 : _a.location,
264 target: _this._elementRef,
265 attachment: _this.attachment,
266 appendToBody: _this.container === 'body'
267 });
268 });
269 this._zoneSubscription = this._ngZone.onStable.subscribe(function () {
270 if (!_this._componentRef) {
271 return;
272 }
273 _this._posService.calcPosition();
274 });
275 };
276 ComponentLoader.prototype._unsubscribePositioning = function () {
277 if (!this._zoneSubscription) {
278 return;
279 }
280 this._zoneSubscription.unsubscribe();
281 this._zoneSubscription = void 0;
282 };
283 ComponentLoader.prototype._getContentRef = function (
284 // eslint-disable-next-line @typescript-eslint/no-explicit-any
285 content,
286 // eslint-disable-next-line @typescript-eslint/no-explicit-any
287 context,
288 // eslint-disable-next-line @typescript-eslint/no-explicit-any
289 initialState) {
290 if (!content) {
291 return new ContentRef([]);
292 }
293 if (content instanceof i0.TemplateRef) {
294 if (this._viewContainerRef) {
295 var _viewRef = this._viewContainerRef
296 .createEmbeddedView(content, context);
297 _viewRef.markForCheck();
298 return new ContentRef([_viewRef.rootNodes], _viewRef);
299 }
300 var viewRef = content.createEmbeddedView({});
301 this._applicationRef.attachView(viewRef);
302 return new ContentRef([viewRef.rootNodes], viewRef);
303 }
304 if (typeof content === 'function') {
305 var contentCmptFactory = this._componentFactoryResolver.resolveComponentFactory(content);
306 var modalContentInjector = i0.Injector.create({
307 providers: this._providers,
308 parent: this._injector
309 });
310 var componentRef = contentCmptFactory.create(modalContentInjector);
311 Object.assign(componentRef.instance, initialState);
312 this._applicationRef.attachView(componentRef.hostView);
313 return new ContentRef([[componentRef.location.nativeElement]], componentRef.hostView, componentRef);
314 }
315 var nodes = this._renderer
316 ? [this._renderer.createText("" + content)]
317 : [];
318 return new ContentRef([nodes]);
319 };
320 return ComponentLoader;
321 }());
322
323 var ComponentLoaderFactory = /** @class */ (function () {
324 function ComponentLoaderFactory(_componentFactoryResolver, _ngZone, _injector, _posService, _applicationRef) {
325 this._componentFactoryResolver = _componentFactoryResolver;
326 this._ngZone = _ngZone;
327 this._injector = _injector;
328 this._posService = _posService;
329 this._applicationRef = _applicationRef;
330 }
331 /**
332 *
333 * @param _elementRef
334 * @param _viewContainerRef
335 * @param _renderer
336 */
337 ComponentLoaderFactory.prototype.createLoader = function (_elementRef, _viewContainerRef, _renderer) {
338 return new ComponentLoader(_viewContainerRef, _renderer, _elementRef, this._injector, this._componentFactoryResolver, this._ngZone, this._applicationRef, this._posService);
339 };
340 return ComponentLoaderFactory;
341 }());
342 ComponentLoaderFactory.ɵprov = i0.ɵɵdefineInjectable({ factory: function ComponentLoaderFactory_Factory() { return new ComponentLoaderFactory(i0.ɵɵinject(i0.ComponentFactoryResolver), i0.ɵɵinject(i0.NgZone), i0.ɵɵinject(i0.INJECTOR), i0.ɵɵinject(i1.PositioningService), i0.ɵɵinject(i0.ApplicationRef)); }, token: ComponentLoaderFactory, providedIn: "root" });
343 ComponentLoaderFactory.decorators = [
344 { type: i0.Injectable, args: [{ providedIn: 'root' },] }
345 ];
346 ComponentLoaderFactory.ctorParameters = function () { return [
347 { type: i0.ComponentFactoryResolver },
348 { type: i0.NgZone },
349 { type: i0.Injector },
350 { type: i1.PositioningService },
351 { type: i0.ApplicationRef }
352 ]; };
353
354 /**
355 * Generated bundle index. Do not edit.
356 */
357
358 exports.BsComponentRef = BsComponentRef;
359 exports.ComponentLoader = ComponentLoader;
360 exports.ComponentLoaderFactory = ComponentLoaderFactory;
361 exports.ContentRef = ContentRef;
362
363 Object.defineProperty(exports, '__esModule', { value: true });
364
365})));
366//# sourceMappingURL=ngx-bootstrap-component-loader.umd.js.map