ng-zorro-antd-core-wave.mjs 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. import { Platform } from '@angular/cdk/platform';
  2. import * as i0 from '@angular/core';
  3. import { InjectionToken, makeEnvironmentProviders, inject, CSP_NONCE, Input, Directive, NgModule } from '@angular/core';
  4. import { ANIMATION_MODULE_TYPE } from '@angular/platform-browser/animations';
  5. /**
  6. * Use of this source code is governed by an MIT-style license that can be
  7. * found in the LICENSE file at https://github.com/NG-ZORRO/ng-zorro-antd/blob/master/LICENSE
  8. */
  9. class NzWaveRenderer {
  10. triggerElement;
  11. ngZone;
  12. insertExtraNode;
  13. platform;
  14. cspNonce;
  15. waveTransitionDuration = 400;
  16. styleForPseudo = null;
  17. extraNode = null;
  18. lastTime = 0;
  19. clickHandler;
  20. get waveAttributeName() {
  21. return this.insertExtraNode ? 'ant-click-animating' : 'ant-click-animating-without-extra-node';
  22. }
  23. constructor(triggerElement, ngZone, insertExtraNode, platform, cspNonce) {
  24. this.triggerElement = triggerElement;
  25. this.ngZone = ngZone;
  26. this.insertExtraNode = insertExtraNode;
  27. this.platform = platform;
  28. this.cspNonce = cspNonce;
  29. this.clickHandler = this.onClick.bind(this);
  30. this.bindTriggerEvent();
  31. }
  32. onClick = (event) => {
  33. if (!this.triggerElement ||
  34. !this.triggerElement.getAttribute ||
  35. this.triggerElement.getAttribute('disabled') ||
  36. event.target.tagName === 'INPUT' ||
  37. this.triggerElement.className.indexOf('disabled') >= 0) {
  38. return;
  39. }
  40. this.fadeOutWave();
  41. };
  42. bindTriggerEvent() {
  43. if (this.platform.isBrowser) {
  44. this.ngZone.runOutsideAngular(() => {
  45. this.removeTriggerEvent();
  46. if (this.triggerElement) {
  47. this.triggerElement.addEventListener('click', this.clickHandler, true);
  48. }
  49. });
  50. }
  51. }
  52. removeTriggerEvent() {
  53. if (this.triggerElement) {
  54. this.triggerElement.removeEventListener('click', this.clickHandler, true);
  55. }
  56. }
  57. removeStyleAndExtraNode() {
  58. if (this.styleForPseudo && document.body.contains(this.styleForPseudo)) {
  59. document.body.removeChild(this.styleForPseudo);
  60. this.styleForPseudo = null;
  61. }
  62. if (this.insertExtraNode && this.triggerElement.contains(this.extraNode)) {
  63. this.triggerElement.removeChild(this.extraNode);
  64. }
  65. }
  66. destroy() {
  67. this.removeTriggerEvent();
  68. this.removeStyleAndExtraNode();
  69. }
  70. fadeOutWave() {
  71. const node = this.triggerElement;
  72. const waveColor = this.getWaveColor(node);
  73. node.setAttribute(this.waveAttributeName, 'true');
  74. if (Date.now() < this.lastTime + this.waveTransitionDuration) {
  75. return;
  76. }
  77. if (this.isValidColor(waveColor)) {
  78. if (!this.styleForPseudo) {
  79. this.styleForPseudo = document.createElement('style');
  80. if (this.cspNonce) {
  81. this.styleForPseudo.nonce = this.cspNonce;
  82. }
  83. }
  84. this.styleForPseudo.innerHTML = `
  85. [ant-click-animating-without-extra-node='true']::after, .ant-click-animating-node {
  86. --antd-wave-shadow-color: ${waveColor};
  87. }`;
  88. document.body.appendChild(this.styleForPseudo);
  89. }
  90. if (this.insertExtraNode) {
  91. if (!this.extraNode) {
  92. this.extraNode = document.createElement('div');
  93. }
  94. this.extraNode.className = 'ant-click-animating-node';
  95. node.appendChild(this.extraNode);
  96. }
  97. this.lastTime = Date.now();
  98. this.runTimeoutOutsideZone(() => {
  99. node.removeAttribute(this.waveAttributeName);
  100. this.removeStyleAndExtraNode();
  101. }, this.waveTransitionDuration);
  102. }
  103. isValidColor(color) {
  104. return (!!color &&
  105. color !== '#ffffff' &&
  106. color !== 'rgb(255, 255, 255)' &&
  107. this.isNotGrey(color) &&
  108. !/rgba\(\d*, \d*, \d*, 0\)/.test(color) &&
  109. color !== 'transparent');
  110. }
  111. isNotGrey(color) {
  112. const match = color.match(/rgba?\((\d*), (\d*), (\d*)(, [.\d]*)?\)/);
  113. if (match && match[1] && match[2] && match[3]) {
  114. return !(match[1] === match[2] && match[2] === match[3]);
  115. }
  116. return true;
  117. }
  118. getWaveColor(node) {
  119. const nodeStyle = getComputedStyle(node);
  120. return (nodeStyle.getPropertyValue('border-top-color') || // Firefox Compatible
  121. nodeStyle.getPropertyValue('border-color') ||
  122. nodeStyle.getPropertyValue('background-color'));
  123. }
  124. runTimeoutOutsideZone(fn, delay) {
  125. this.ngZone.runOutsideAngular(() => setTimeout(fn, delay));
  126. }
  127. }
  128. /**
  129. * Use of this source code is governed by an MIT-style license that can be
  130. * found in the LICENSE file at https://github.com/NG-ZORRO/ng-zorro-antd/blob/master/LICENSE
  131. */
  132. const NZ_WAVE_GLOBAL_DEFAULT_CONFIG = {
  133. disabled: false
  134. };
  135. const NZ_WAVE_GLOBAL_CONFIG = new InjectionToken('nz-wave-global-options');
  136. function provideNzWave(config) {
  137. return makeEnvironmentProviders([{ provide: NZ_WAVE_GLOBAL_CONFIG, useValue: config }]);
  138. }
  139. class NzWaveDirective {
  140. ngZone;
  141. elementRef;
  142. nzWaveExtraNode = false;
  143. waveRenderer;
  144. waveDisabled = false;
  145. get disabled() {
  146. return this.waveDisabled;
  147. }
  148. get rendererRef() {
  149. return this.waveRenderer;
  150. }
  151. cspNonce = inject(CSP_NONCE, { optional: true });
  152. platform = inject(Platform);
  153. config = inject(NZ_WAVE_GLOBAL_CONFIG, { optional: true });
  154. animationType = inject(ANIMATION_MODULE_TYPE, { optional: true });
  155. constructor(ngZone, elementRef) {
  156. this.ngZone = ngZone;
  157. this.elementRef = elementRef;
  158. this.waveDisabled = this.isConfigDisabled();
  159. }
  160. isConfigDisabled() {
  161. let disabled = false;
  162. if (this.config && typeof this.config.disabled === 'boolean') {
  163. disabled = this.config.disabled;
  164. }
  165. if (this.animationType === 'NoopAnimations') {
  166. disabled = true;
  167. }
  168. return disabled;
  169. }
  170. ngOnDestroy() {
  171. if (this.waveRenderer) {
  172. this.waveRenderer.destroy();
  173. }
  174. }
  175. ngOnInit() {
  176. this.renderWaveIfEnabled();
  177. }
  178. renderWaveIfEnabled() {
  179. if (!this.waveDisabled && this.elementRef.nativeElement) {
  180. this.waveRenderer = new NzWaveRenderer(this.elementRef.nativeElement, this.ngZone, this.nzWaveExtraNode, this.platform, this.cspNonce);
  181. }
  182. }
  183. disable() {
  184. this.waveDisabled = true;
  185. if (this.waveRenderer) {
  186. this.waveRenderer.removeTriggerEvent();
  187. this.waveRenderer.removeStyleAndExtraNode();
  188. }
  189. }
  190. enable() {
  191. // config priority
  192. this.waveDisabled = this.isConfigDisabled() || false;
  193. if (this.waveRenderer) {
  194. this.waveRenderer.bindTriggerEvent();
  195. }
  196. }
  197. static ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "19.2.2", ngImport: i0, type: NzWaveDirective, deps: [{ token: i0.NgZone }, { token: i0.ElementRef }], target: i0.ɵɵFactoryTarget.Directive });
  198. static ɵdir = i0.ɵɵngDeclareDirective({ minVersion: "14.0.0", version: "19.2.2", type: NzWaveDirective, isStandalone: true, selector: "[nz-wave],button[nz-button]:not([nzType=\"link\"]):not([nzType=\"text\"])", inputs: { nzWaveExtraNode: "nzWaveExtraNode" }, exportAs: ["nzWave"], ngImport: i0 });
  199. }
  200. i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "19.2.2", ngImport: i0, type: NzWaveDirective, decorators: [{
  201. type: Directive,
  202. args: [{
  203. selector: '[nz-wave],button[nz-button]:not([nzType="link"]):not([nzType="text"])',
  204. exportAs: 'nzWave'
  205. }]
  206. }], ctorParameters: () => [{ type: i0.NgZone }, { type: i0.ElementRef }], propDecorators: { nzWaveExtraNode: [{
  207. type: Input
  208. }] } });
  209. /**
  210. * Use of this source code is governed by an MIT-style license that can be
  211. * found in the LICENSE file at https://github.com/NG-ZORRO/ng-zorro-antd/blob/master/LICENSE
  212. */
  213. class NzWaveModule {
  214. static ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "19.2.2", ngImport: i0, type: NzWaveModule, deps: [], target: i0.ɵɵFactoryTarget.NgModule });
  215. static ɵmod = i0.ɵɵngDeclareNgModule({ minVersion: "14.0.0", version: "19.2.2", ngImport: i0, type: NzWaveModule, imports: [NzWaveDirective], exports: [NzWaveDirective] });
  216. static ɵinj = i0.ɵɵngDeclareInjector({ minVersion: "12.0.0", version: "19.2.2", ngImport: i0, type: NzWaveModule, providers: [provideNzWave(NZ_WAVE_GLOBAL_DEFAULT_CONFIG)] });
  217. }
  218. i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "19.2.2", ngImport: i0, type: NzWaveModule, decorators: [{
  219. type: NgModule,
  220. args: [{
  221. imports: [NzWaveDirective],
  222. exports: [NzWaveDirective],
  223. providers: [provideNzWave(NZ_WAVE_GLOBAL_DEFAULT_CONFIG)]
  224. }]
  225. }] });
  226. /**
  227. * Use of this source code is governed by an MIT-style license that can be
  228. * found in the LICENSE file at https://github.com/NG-ZORRO/ng-zorro-antd/blob/master/LICENSE
  229. */
  230. /**
  231. * Generated bundle index. Do not edit.
  232. */
  233. export { NZ_WAVE_GLOBAL_CONFIG, NZ_WAVE_GLOBAL_DEFAULT_CONFIG, NzWaveDirective, NzWaveModule, NzWaveRenderer, provideNzWave };
  234. //# sourceMappingURL=ng-zorro-antd-core-wave.mjs.map