ngx-bootstrap-collapse.js 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. import { style, animate, AnimationBuilder } from '@angular/animations';
  2. import { EventEmitter, Directive, ElementRef, Renderer2, Output, HostBinding, Input, NgModule } from '@angular/core';
  3. /**
  4. * @fileoverview added by tsickle
  5. * @suppress {checkTypes,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc
  6. */
  7. /** @type {?} */
  8. const COLLAPSE_ANIMATION_TIMING = '400ms cubic-bezier(0.4,0.0,0.2,1)';
  9. /** @type {?} */
  10. const expandAnimation = [
  11. style({ height: 0, visibility: 'hidden' }),
  12. animate(COLLAPSE_ANIMATION_TIMING, style({ height: '*', visibility: 'visible' }))
  13. ];
  14. /** @type {?} */
  15. const collapseAnimation = [
  16. style({ height: '*', visibility: 'visible' }),
  17. animate(COLLAPSE_ANIMATION_TIMING, style({ height: 0, visibility: 'hidden' }))
  18. ];
  19. /**
  20. * @fileoverview added by tsickle
  21. * @suppress {checkTypes,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc
  22. */
  23. class CollapseDirective {
  24. /**
  25. * @param {?} _el
  26. * @param {?} _renderer
  27. * @param {?} _builder
  28. */
  29. constructor(_el, _renderer, _builder) {
  30. this._el = _el;
  31. this._renderer = _renderer;
  32. /**
  33. * This event fires as soon as content collapses
  34. */
  35. this.collapsed = new EventEmitter();
  36. /**
  37. * This event fires when collapsing is started
  38. */
  39. this.collapses = new EventEmitter();
  40. /**
  41. * This event fires as soon as content becomes visible
  42. */
  43. this.expanded = new EventEmitter();
  44. /**
  45. * This event fires when expansion is started
  46. */
  47. this.expands = new EventEmitter();
  48. // shown
  49. this.isExpanded = true;
  50. // hidden
  51. this.isCollapsed = false;
  52. // stale state
  53. this.isCollapse = true;
  54. // animation state
  55. this.isCollapsing = false;
  56. /**
  57. * turn on/off animation
  58. */
  59. this.isAnimated = false;
  60. this._display = 'block';
  61. this._stylesLoaded = false;
  62. this._COLLAPSE_ACTION_NAME = 'collapse';
  63. this._EXPAND_ACTION_NAME = 'expand';
  64. this._factoryCollapseAnimation = _builder.build(collapseAnimation);
  65. this._factoryExpandAnimation = _builder.build(expandAnimation);
  66. }
  67. /**
  68. * @param {?} value
  69. * @return {?}
  70. */
  71. set display(value) {
  72. if (!this.isAnimated) {
  73. this._renderer.setStyle(this._el.nativeElement, 'display', value);
  74. return;
  75. }
  76. this._display = value;
  77. if (value === 'none') {
  78. this.hide();
  79. return;
  80. }
  81. this.show();
  82. }
  83. /**
  84. * A flag indicating visibility of content (shown or hidden)
  85. * @param {?} value
  86. * @return {?}
  87. */
  88. set collapse(value) {
  89. if (!this._player || this._isAnimationDone) {
  90. this.isExpanded = value;
  91. this.toggle();
  92. }
  93. }
  94. /**
  95. * @return {?}
  96. */
  97. get collapse() {
  98. return this.isExpanded;
  99. }
  100. /**
  101. * @return {?}
  102. */
  103. ngAfterViewChecked() {
  104. this._stylesLoaded = true;
  105. if (!this._player || !this._isAnimationDone) {
  106. return;
  107. }
  108. this._player.reset();
  109. this._renderer.setStyle(this._el.nativeElement, 'height', '*');
  110. }
  111. /**
  112. * allows to manually toggle content visibility
  113. * @return {?}
  114. */
  115. toggle() {
  116. if (this.isExpanded) {
  117. this.hide();
  118. }
  119. else {
  120. this.show();
  121. }
  122. }
  123. /**
  124. * allows to manually hide content
  125. * @return {?}
  126. */
  127. hide() {
  128. this.isCollapsing = true;
  129. this.isExpanded = false;
  130. this.isCollapsed = true;
  131. this.isCollapsing = false;
  132. this.collapses.emit(this);
  133. this._isAnimationDone = false;
  134. this.animationRun(this.isAnimated, this._COLLAPSE_ACTION_NAME)((/**
  135. * @return {?}
  136. */
  137. () => {
  138. this._isAnimationDone = true;
  139. this.collapsed.emit(this);
  140. this._renderer.setStyle(this._el.nativeElement, 'display', 'none');
  141. }));
  142. }
  143. /**
  144. * allows to manually show collapsed content
  145. * @return {?}
  146. */
  147. show() {
  148. this._renderer.setStyle(this._el.nativeElement, 'display', this._display);
  149. this.isCollapsing = true;
  150. this.isExpanded = true;
  151. this.isCollapsed = false;
  152. this.isCollapsing = false;
  153. this.expands.emit(this);
  154. this._isAnimationDone = false;
  155. this.animationRun(this.isAnimated, this._EXPAND_ACTION_NAME)((/**
  156. * @return {?}
  157. */
  158. () => {
  159. this._isAnimationDone = true;
  160. this.expanded.emit(this);
  161. }));
  162. }
  163. /**
  164. * @param {?} isAnimated
  165. * @param {?} action
  166. * @return {?}
  167. */
  168. animationRun(isAnimated, action) {
  169. if (!isAnimated || !this._stylesLoaded) {
  170. return (/**
  171. * @param {?} callback
  172. * @return {?}
  173. */
  174. (callback) => callback());
  175. }
  176. this._renderer.setStyle(this._el.nativeElement, 'overflow', 'hidden');
  177. this._renderer.addClass(this._el.nativeElement, 'collapse');
  178. /** @type {?} */
  179. const factoryAnimation = (action === this._EXPAND_ACTION_NAME)
  180. ? this._factoryExpandAnimation
  181. : this._factoryCollapseAnimation;
  182. if (this._player) {
  183. this._player.destroy();
  184. }
  185. this._player = factoryAnimation.create(this._el.nativeElement);
  186. this._player.play();
  187. return (/**
  188. * @param {?} callback
  189. * @return {?}
  190. */
  191. (callback) => this._player.onDone(callback));
  192. }
  193. }
  194. CollapseDirective.decorators = [
  195. { type: Directive, args: [{
  196. selector: '[collapse]',
  197. exportAs: 'bs-collapse',
  198. host: {
  199. '[class.collapse]': 'true'
  200. }
  201. },] }
  202. ];
  203. /** @nocollapse */
  204. CollapseDirective.ctorParameters = () => [
  205. { type: ElementRef },
  206. { type: Renderer2 },
  207. { type: AnimationBuilder }
  208. ];
  209. CollapseDirective.propDecorators = {
  210. collapsed: [{ type: Output }],
  211. collapses: [{ type: Output }],
  212. expanded: [{ type: Output }],
  213. expands: [{ type: Output }],
  214. isExpanded: [{ type: HostBinding, args: ['class.in',] }, { type: HostBinding, args: ['class.show',] }, { type: HostBinding, args: ['attr.aria-expanded',] }],
  215. isCollapsed: [{ type: HostBinding, args: ['attr.aria-hidden',] }],
  216. isCollapse: [{ type: HostBinding, args: ['class.collapse',] }],
  217. isCollapsing: [{ type: HostBinding, args: ['class.collapsing',] }],
  218. display: [{ type: Input }],
  219. isAnimated: [{ type: Input }],
  220. collapse: [{ type: Input }]
  221. };
  222. /**
  223. * @fileoverview added by tsickle
  224. * @suppress {checkTypes,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc
  225. */
  226. class CollapseModule {
  227. /**
  228. * @return {?}
  229. */
  230. static forRoot() {
  231. return { ngModule: CollapseModule, providers: [] };
  232. }
  233. }
  234. CollapseModule.decorators = [
  235. { type: NgModule, args: [{
  236. declarations: [CollapseDirective],
  237. exports: [CollapseDirective]
  238. },] }
  239. ];
  240. export { CollapseDirective, CollapseModule };
  241. //# sourceMappingURL=ngx-bootstrap-collapse.js.map