ngx-bootstrap-component-loader.umd.js 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562
  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 = global || self, factory((global['ngx-bootstrap'] = global['ngx-bootstrap'] || {}, global['ngx-bootstrap']['component-loader'] = {}), global.ng.core, global.utils, global.positioning));
  5. }(this, function (exports, core, utils, positioning) { 'use strict';
  6. /**
  7. * @fileoverview added by tsickle
  8. * @suppress {checkTypes,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc
  9. */
  10. /**
  11. * @template T
  12. */
  13. var /**
  14. * @template T
  15. */
  16. BsComponentRef = /** @class */ (function () {
  17. function BsComponentRef() {
  18. }
  19. return BsComponentRef;
  20. }());
  21. /**
  22. * @fileoverview added by tsickle
  23. * @suppress {checkTypes,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc
  24. */
  25. /**
  26. * @copyright Valor Software
  27. * @copyright Angular ng-bootstrap team
  28. */
  29. var ContentRef = /** @class */ (function () {
  30. function ContentRef(
  31. /* tslint:disable-next-line: no-any */
  32. nodes, viewRef,
  33. /* tslint:disable-next-line: no-any */
  34. componentRef) {
  35. this.nodes = nodes;
  36. this.viewRef = viewRef;
  37. this.componentRef = componentRef;
  38. }
  39. return ContentRef;
  40. }());
  41. /**
  42. * @fileoverview added by tsickle
  43. * @suppress {checkTypes,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc
  44. */
  45. /**
  46. * @template T
  47. */
  48. var /**
  49. * @template T
  50. */
  51. ComponentLoader = /** @class */ (function () {
  52. /**
  53. * Do not use this directly, it should be instanced via
  54. * `ComponentLoadFactory.attach`
  55. * @internal
  56. */
  57. // tslint:disable-next-line
  58. function ComponentLoader(_viewContainerRef, _renderer, _elementRef, _injector, _componentFactoryResolver, _ngZone, _applicationRef, _posService) {
  59. this._viewContainerRef = _viewContainerRef;
  60. this._renderer = _renderer;
  61. this._elementRef = _elementRef;
  62. this._injector = _injector;
  63. this._componentFactoryResolver = _componentFactoryResolver;
  64. this._ngZone = _ngZone;
  65. this._applicationRef = _applicationRef;
  66. this._posService = _posService;
  67. this.onBeforeShow = new core.EventEmitter();
  68. /* tslint:disable-next-line: no-any*/
  69. this.onShown = new core.EventEmitter();
  70. /* tslint:disable-next-line: no-any*/
  71. this.onBeforeHide = new core.EventEmitter();
  72. this.onHidden = new core.EventEmitter();
  73. this._providers = [];
  74. this._isHiding = false;
  75. /**
  76. * A selector used if container element was not found
  77. */
  78. this.containerDefaultSelector = 'body';
  79. this._listenOpts = {};
  80. this._globalListener = Function.prototype;
  81. }
  82. Object.defineProperty(ComponentLoader.prototype, "isShown", {
  83. get: /**
  84. * @return {?}
  85. */
  86. function () {
  87. if (this._isHiding) {
  88. return false;
  89. }
  90. return !!this._componentRef;
  91. },
  92. enumerable: true,
  93. configurable: true
  94. });
  95. /**
  96. * @param {?} compType
  97. * @return {?}
  98. */
  99. ComponentLoader.prototype.attach = /**
  100. * @param {?} compType
  101. * @return {?}
  102. */
  103. function (compType) {
  104. this._componentFactory = this._componentFactoryResolver
  105. .resolveComponentFactory(compType);
  106. return this;
  107. };
  108. // todo: add behaviour: to target element, `body`, custom element
  109. // todo: add behaviour: to target element, `body`, custom element
  110. /**
  111. * @param {?=} container
  112. * @return {?}
  113. */
  114. ComponentLoader.prototype.to =
  115. // todo: add behaviour: to target element, `body`, custom element
  116. /**
  117. * @param {?=} container
  118. * @return {?}
  119. */
  120. function (container) {
  121. this.container = container || this.container;
  122. return this;
  123. };
  124. /**
  125. * @param {?=} opts
  126. * @return {?}
  127. */
  128. ComponentLoader.prototype.position = /**
  129. * @param {?=} opts
  130. * @return {?}
  131. */
  132. function (opts) {
  133. this.attachment = opts.attachment || this.attachment;
  134. /* tslint:disable-next-line: no-unnecessary-type-assertion */
  135. this._elementRef = ((/** @type {?} */ (opts.target))) || this._elementRef;
  136. return this;
  137. };
  138. /**
  139. * @param {?} provider
  140. * @return {?}
  141. */
  142. ComponentLoader.prototype.provide = /**
  143. * @param {?} provider
  144. * @return {?}
  145. */
  146. function (provider) {
  147. this._providers.push(provider);
  148. return this;
  149. };
  150. // todo: appendChild to element or document.querySelector(this.container)
  151. // todo: appendChild to element or document.querySelector(this.container)
  152. /**
  153. * @param {?=} opts
  154. * @return {?}
  155. */
  156. ComponentLoader.prototype.show =
  157. // todo: appendChild to element or document.querySelector(this.container)
  158. /**
  159. * @param {?=} opts
  160. * @return {?}
  161. */
  162. function (opts) {
  163. if (opts === void 0) { opts = {}; }
  164. this._subscribePositioning();
  165. this._innerComponent = null;
  166. if (!this._componentRef) {
  167. this.onBeforeShow.emit();
  168. this._contentRef = this._getContentRef(opts.content, opts.context, opts.initialState);
  169. /** @type {?} */
  170. var injector = core.Injector.create({
  171. providers: this._providers,
  172. parent: this._injector
  173. });
  174. this._componentRef = this._componentFactory.create(injector, this._contentRef.nodes);
  175. this._applicationRef.attachView(this._componentRef.hostView);
  176. // this._componentRef = this._viewContainerRef
  177. // .createComponent(this._componentFactory, 0, injector, this._contentRef.nodes);
  178. this.instance = this._componentRef.instance;
  179. Object.assign(this._componentRef.instance, opts);
  180. if (this.container instanceof core.ElementRef) {
  181. this.container.nativeElement.appendChild(this._componentRef.location.nativeElement);
  182. }
  183. if (typeof this.container === 'string' && typeof document !== 'undefined') {
  184. /** @type {?} */
  185. var selectedElement = document.querySelector(this.container) ||
  186. document.querySelector(this.containerDefaultSelector);
  187. selectedElement.appendChild(this._componentRef.location.nativeElement);
  188. }
  189. if (!this.container &&
  190. this._elementRef &&
  191. this._elementRef.nativeElement.parentElement) {
  192. this._elementRef.nativeElement.parentElement.appendChild(this._componentRef.location.nativeElement);
  193. }
  194. // we need to manually invoke change detection since events registered
  195. // via
  196. // Renderer::listen() are not picked up by change detection with the
  197. // OnPush strategy
  198. if (this._contentRef.componentRef) {
  199. this._innerComponent = this._contentRef.componentRef.instance;
  200. this._contentRef.componentRef.changeDetectorRef.markForCheck();
  201. this._contentRef.componentRef.changeDetectorRef.detectChanges();
  202. }
  203. this._componentRef.changeDetectorRef.markForCheck();
  204. this._componentRef.changeDetectorRef.detectChanges();
  205. this.onShown.emit(this._componentRef.instance);
  206. }
  207. this._registerOutsideClick();
  208. return this._componentRef;
  209. };
  210. /**
  211. * @return {?}
  212. */
  213. ComponentLoader.prototype.hide = /**
  214. * @return {?}
  215. */
  216. function () {
  217. if (!this._componentRef) {
  218. return this;
  219. }
  220. this._posService.deletePositionElement(this._componentRef.location);
  221. this.onBeforeHide.emit(this._componentRef.instance);
  222. /** @type {?} */
  223. var componentEl = this._componentRef.location.nativeElement;
  224. componentEl.parentNode.removeChild(componentEl);
  225. if (this._contentRef.componentRef) {
  226. this._contentRef.componentRef.destroy();
  227. }
  228. this._componentRef.destroy();
  229. if (this._viewContainerRef && this._contentRef.viewRef) {
  230. this._viewContainerRef.remove(this._viewContainerRef.indexOf(this._contentRef.viewRef));
  231. }
  232. if (this._contentRef.viewRef) {
  233. this._contentRef.viewRef.destroy();
  234. }
  235. this._contentRef = null;
  236. this._componentRef = null;
  237. this._removeGlobalListener();
  238. this.onHidden.emit();
  239. return this;
  240. };
  241. /**
  242. * @return {?}
  243. */
  244. ComponentLoader.prototype.toggle = /**
  245. * @return {?}
  246. */
  247. function () {
  248. if (this.isShown) {
  249. this.hide();
  250. return;
  251. }
  252. this.show();
  253. };
  254. /**
  255. * @return {?}
  256. */
  257. ComponentLoader.prototype.dispose = /**
  258. * @return {?}
  259. */
  260. function () {
  261. if (this.isShown) {
  262. this.hide();
  263. }
  264. this._unsubscribePositioning();
  265. if (this._unregisterListenersFn) {
  266. this._unregisterListenersFn();
  267. }
  268. };
  269. /**
  270. * @param {?} listenOpts
  271. * @return {?}
  272. */
  273. ComponentLoader.prototype.listen = /**
  274. * @param {?} listenOpts
  275. * @return {?}
  276. */
  277. function (listenOpts) {
  278. var _this = this;
  279. this.triggers = listenOpts.triggers || this.triggers;
  280. this._listenOpts.outsideClick = listenOpts.outsideClick;
  281. this._listenOpts.outsideEsc = listenOpts.outsideEsc;
  282. listenOpts.target = listenOpts.target || this._elementRef.nativeElement;
  283. /** @type {?} */
  284. var hide = (this._listenOpts.hide = (/**
  285. * @return {?}
  286. */
  287. function () {
  288. return listenOpts.hide ? listenOpts.hide() : void _this.hide();
  289. }));
  290. /** @type {?} */
  291. var show = (this._listenOpts.show = (/**
  292. * @param {?} registerHide
  293. * @return {?}
  294. */
  295. function (registerHide) {
  296. listenOpts.show ? listenOpts.show(registerHide) : _this.show(registerHide);
  297. registerHide();
  298. }));
  299. /** @type {?} */
  300. var toggle = (/**
  301. * @param {?} registerHide
  302. * @return {?}
  303. */
  304. function (registerHide) {
  305. _this.isShown ? hide() : show(registerHide);
  306. });
  307. this._unregisterListenersFn = utils.listenToTriggersV2(this._renderer, {
  308. target: listenOpts.target,
  309. triggers: listenOpts.triggers,
  310. show: show,
  311. hide: hide,
  312. toggle: toggle
  313. });
  314. return this;
  315. };
  316. /**
  317. * @return {?}
  318. */
  319. ComponentLoader.prototype._removeGlobalListener = /**
  320. * @return {?}
  321. */
  322. function () {
  323. if (this._globalListener) {
  324. this._globalListener();
  325. this._globalListener = null;
  326. }
  327. };
  328. /**
  329. * @param {?} vRef
  330. * @param {?} template
  331. * @return {?}
  332. */
  333. ComponentLoader.prototype.attachInline = /**
  334. * @param {?} vRef
  335. * @param {?} template
  336. * @return {?}
  337. */
  338. function (vRef,
  339. /* tslint:disable-next-line: no-any*/
  340. template) {
  341. this._inlineViewRef = vRef.createEmbeddedView(template);
  342. return this;
  343. };
  344. /**
  345. * @return {?}
  346. */
  347. ComponentLoader.prototype._registerOutsideClick = /**
  348. * @return {?}
  349. */
  350. function () {
  351. var _this = this;
  352. if (!this._componentRef || !this._componentRef.location) {
  353. return;
  354. }
  355. // why: should run after first event bubble
  356. if (this._listenOpts.outsideClick) {
  357. /** @type {?} */
  358. var target_1 = this._componentRef.location.nativeElement;
  359. setTimeout((/**
  360. * @return {?}
  361. */
  362. function () {
  363. _this._globalListener = utils.registerOutsideClick(_this._renderer, {
  364. targets: [target_1, _this._elementRef.nativeElement],
  365. outsideClick: _this._listenOpts.outsideClick,
  366. hide: (/**
  367. * @return {?}
  368. */
  369. function () { return _this._listenOpts.hide(); })
  370. });
  371. }));
  372. }
  373. if (this._listenOpts.outsideEsc) {
  374. /** @type {?} */
  375. var target = this._componentRef.location.nativeElement;
  376. this._globalListener = utils.registerEscClick(this._renderer, {
  377. targets: [target, this._elementRef.nativeElement],
  378. outsideEsc: this._listenOpts.outsideEsc,
  379. hide: (/**
  380. * @return {?}
  381. */
  382. function () { return _this._listenOpts.hide(); })
  383. });
  384. }
  385. };
  386. /**
  387. * @return {?}
  388. */
  389. ComponentLoader.prototype.getInnerComponent = /**
  390. * @return {?}
  391. */
  392. function () {
  393. return this._innerComponent;
  394. };
  395. /**
  396. * @private
  397. * @return {?}
  398. */
  399. ComponentLoader.prototype._subscribePositioning = /**
  400. * @private
  401. * @return {?}
  402. */
  403. function () {
  404. var _this = this;
  405. if (this._zoneSubscription || !this.attachment) {
  406. return;
  407. }
  408. this.onShown.subscribe((/**
  409. * @return {?}
  410. */
  411. function () {
  412. _this._posService.position({
  413. element: _this._componentRef.location,
  414. target: _this._elementRef,
  415. attachment: _this.attachment,
  416. appendToBody: _this.container === 'body'
  417. });
  418. }));
  419. this._zoneSubscription = this._ngZone.onStable.subscribe((/**
  420. * @return {?}
  421. */
  422. function () {
  423. if (!_this._componentRef) {
  424. return;
  425. }
  426. _this._posService.calcPosition();
  427. }));
  428. };
  429. /**
  430. * @private
  431. * @return {?}
  432. */
  433. ComponentLoader.prototype._unsubscribePositioning = /**
  434. * @private
  435. * @return {?}
  436. */
  437. function () {
  438. if (!this._zoneSubscription) {
  439. return;
  440. }
  441. this._zoneSubscription.unsubscribe();
  442. this._zoneSubscription = null;
  443. };
  444. /**
  445. * @private
  446. * @param {?} content
  447. * @param {?=} context
  448. * @param {?=} initialState
  449. * @return {?}
  450. */
  451. ComponentLoader.prototype._getContentRef = /**
  452. * @private
  453. * @param {?} content
  454. * @param {?=} context
  455. * @param {?=} initialState
  456. * @return {?}
  457. */
  458. function (
  459. /* tslint:disable-next-line: no-any*/
  460. content,
  461. /* tslint:disable-next-line: no-any*/
  462. context,
  463. /* tslint:disable-next-line: no-any*/
  464. initialState) {
  465. if (!content) {
  466. return new ContentRef([]);
  467. }
  468. if (content instanceof core.TemplateRef) {
  469. if (this._viewContainerRef) {
  470. /** @type {?} */
  471. var _viewRef = this._viewContainerRef
  472. .createEmbeddedView(content, context);
  473. _viewRef.markForCheck();
  474. return new ContentRef([_viewRef.rootNodes], _viewRef);
  475. }
  476. /** @type {?} */
  477. var viewRef = content.createEmbeddedView({});
  478. this._applicationRef.attachView(viewRef);
  479. return new ContentRef([viewRef.rootNodes], viewRef);
  480. }
  481. if (typeof content === 'function') {
  482. /** @type {?} */
  483. var contentCmptFactory = this._componentFactoryResolver.resolveComponentFactory(content);
  484. /** @type {?} */
  485. var modalContentInjector = core.Injector.create({
  486. providers: this._providers,
  487. parent: this._injector
  488. });
  489. /** @type {?} */
  490. var componentRef = contentCmptFactory.create(modalContentInjector);
  491. Object.assign(componentRef.instance, initialState);
  492. this._applicationRef.attachView(componentRef.hostView);
  493. return new ContentRef([[componentRef.location.nativeElement]], componentRef.hostView, componentRef);
  494. }
  495. return new ContentRef([[this._renderer.createText("" + content)]]);
  496. };
  497. return ComponentLoader;
  498. }());
  499. /**
  500. * @fileoverview added by tsickle
  501. * @suppress {checkTypes,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc
  502. */
  503. var ComponentLoaderFactory = /** @class */ (function () {
  504. function ComponentLoaderFactory(_componentFactoryResolver, _ngZone, _injector, _posService, _applicationRef) {
  505. this._componentFactoryResolver = _componentFactoryResolver;
  506. this._ngZone = _ngZone;
  507. this._injector = _injector;
  508. this._posService = _posService;
  509. this._applicationRef = _applicationRef;
  510. }
  511. /**
  512. *
  513. * @param _elementRef
  514. * @param _viewContainerRef
  515. * @param _renderer
  516. */
  517. /**
  518. *
  519. * @template T
  520. * @param {?} _elementRef
  521. * @param {?} _viewContainerRef
  522. * @param {?} _renderer
  523. * @return {?}
  524. */
  525. ComponentLoaderFactory.prototype.createLoader = /**
  526. *
  527. * @template T
  528. * @param {?} _elementRef
  529. * @param {?} _viewContainerRef
  530. * @param {?} _renderer
  531. * @return {?}
  532. */
  533. function (_elementRef, _viewContainerRef, _renderer) {
  534. return new ComponentLoader(_viewContainerRef, _renderer, _elementRef, this._injector, this._componentFactoryResolver, this._ngZone, this._applicationRef, this._posService);
  535. };
  536. ComponentLoaderFactory.decorators = [
  537. { type: core.Injectable }
  538. ];
  539. /** @nocollapse */
  540. ComponentLoaderFactory.ctorParameters = function () { return [
  541. { type: core.ComponentFactoryResolver },
  542. { type: core.NgZone },
  543. { type: core.Injector },
  544. { type: positioning.PositioningService },
  545. { type: core.ApplicationRef }
  546. ]; };
  547. return ComponentLoaderFactory;
  548. }());
  549. exports.BsComponentRef = BsComponentRef;
  550. exports.ComponentLoader = ComponentLoader;
  551. exports.ComponentLoaderFactory = ComponentLoaderFactory;
  552. exports.ContentRef = ContentRef;
  553. Object.defineProperty(exports, '__esModule', { value: true });
  554. }));
  555. //# sourceMappingURL=ngx-bootstrap-component-loader.umd.js.map