|
| 1 | +import { Directive, Input, ElementRef, Renderer2, OnInit, OnChanges, SimpleChanges } from '@angular/core'; |
| 2 | + |
| 3 | +@Directive({ |
| 4 | + selector: '[ngBusy]', |
| 5 | + standalone: true |
| 6 | +}) |
| 7 | +export class NgBusyDirective implements OnInit, OnChanges { |
| 8 | + @Input('ngBusy') isBusy = false; |
| 9 | + @Input() busyText = ''; |
| 10 | + @Input() busyClass = 'busy-loading'; |
| 11 | + |
| 12 | + private originalContent: string | null = null; |
| 13 | + private originalDisabled: boolean | null = null; |
| 14 | + private spinnerElement: HTMLElement | null = null; |
| 15 | + private styleElement: HTMLStyleElement | null = null; |
| 16 | + |
| 17 | + constructor( |
| 18 | + private el: ElementRef, |
| 19 | + private renderer: Renderer2 |
| 20 | + ) {} |
| 21 | + |
| 22 | + ngOnInit() { |
| 23 | + // Store original content and state |
| 24 | + this.originalContent = this.el.nativeElement.innerHTML; |
| 25 | + this.originalDisabled = this.el.nativeElement.disabled; |
| 26 | + |
| 27 | + // Add global styles for animation |
| 28 | + this.addGlobalStyles(); |
| 29 | + } |
| 30 | + |
| 31 | + ngOnChanges(changes: SimpleChanges) { |
| 32 | + if (changes['isBusy']) { |
| 33 | + this.updateBusyState(); |
| 34 | + } |
| 35 | + } |
| 36 | + |
| 37 | + private updateBusyState() { |
| 38 | + if (this.isBusy) { |
| 39 | + this.showBusyState(); |
| 40 | + } else { |
| 41 | + this.hideBusyState(); |
| 42 | + } |
| 43 | + } |
| 44 | + |
| 45 | + private showBusyState() { |
| 46 | + // Add busy class |
| 47 | + this.renderer.addClass(this.el.nativeElement, this.busyClass); |
| 48 | + |
| 49 | + // Disable element |
| 50 | + this.renderer.setAttribute(this.el.nativeElement, 'disabled', 'true'); |
| 51 | + |
| 52 | + // Create busy content |
| 53 | + const busyContent = this.createBusyContent(); |
| 54 | + |
| 55 | + // Clear and set busy content |
| 56 | + this.el.nativeElement.innerHTML = ''; |
| 57 | + this.el.nativeElement.appendChild(busyContent); |
| 58 | + } |
| 59 | + |
| 60 | + private hideBusyState() { |
| 61 | + // Remove busy class |
| 62 | + this.renderer.removeClass(this.el.nativeElement, this.busyClass); |
| 63 | + |
| 64 | + // Restore original disabled state |
| 65 | + if (this.originalDisabled) { |
| 66 | + this.renderer.setAttribute(this.el.nativeElement, 'disabled', 'true'); |
| 67 | + } else { |
| 68 | + this.renderer.removeAttribute(this.el.nativeElement, 'disabled'); |
| 69 | + } |
| 70 | + |
| 71 | + // Restore original content |
| 72 | + if (this.originalContent) { |
| 73 | + this.el.nativeElement.innerHTML = this.originalContent; |
| 74 | + } |
| 75 | + } |
| 76 | + |
| 77 | + private createBusyContent(): HTMLElement { |
| 78 | + const container = this.renderer.createElement('span'); |
| 79 | + this.renderer.setStyle(container, 'display', 'inline-flex'); |
| 80 | + this.renderer.setStyle(container, 'align-items', 'center'); |
| 81 | + this.renderer.setStyle(container, 'gap', '8px'); |
| 82 | + |
| 83 | + // Create CSS spinner with proper rotation |
| 84 | + this.spinnerElement = this.renderer.createElement('span'); |
| 85 | + this.renderer.addClass(this.spinnerElement, 'css-spinner'); |
| 86 | + |
| 87 | + // Create spinner using CSS border technique |
| 88 | + this.renderer.setStyle(this.spinnerElement, 'display', 'inline-block'); |
| 89 | + this.renderer.setStyle(this.spinnerElement, 'width', '16px'); |
| 90 | + this.renderer.setStyle(this.spinnerElement, 'height', '16px'); |
| 91 | + this.renderer.setStyle(this.spinnerElement, 'border', '2px solid rgba(255, 255, 255, 0.3)'); |
| 92 | + this.renderer.setStyle(this.spinnerElement, 'border-top', '2px solid #ffffff'); |
| 93 | + this.renderer.setStyle(this.spinnerElement, 'border-radius', '50%'); |
| 94 | + this.renderer.setStyle(this.spinnerElement, 'animation', 'spin 1s linear infinite'); |
| 95 | + this.renderer.setStyle(this.spinnerElement, 'vertical-align', 'middle'); |
| 96 | + |
| 97 | + // Create text |
| 98 | + const text = this.busyText || 'Loading...'; |
| 99 | + const textNode = this.renderer.createText(text); |
| 100 | + |
| 101 | + // Assemble |
| 102 | + container.appendChild(this.spinnerElement); |
| 103 | + container.appendChild(textNode); |
| 104 | + |
| 105 | + return container; |
| 106 | + } |
| 107 | + |
| 108 | + private addGlobalStyles() { |
| 109 | + // Add proper rotation animation |
| 110 | + if (!this.styleElement) { |
| 111 | + this.styleElement = this.renderer.createElement('style'); |
| 112 | + this.renderer.setProperty(this.styleElement, 'innerHTML', ` |
| 113 | + @keyframes spin { |
| 114 | + 0% { transform: rotate(0deg); } |
| 115 | + 100% { transform: rotate(360deg); } |
| 116 | + } |
| 117 | + |
| 118 | + .css-spinner { |
| 119 | + border: 2px solid rgba(255, 255, 255, 0.3) !important; |
| 120 | + border-top: 2px solid #ffffff !important; |
| 121 | + } |
| 122 | + |
| 123 | + button .css-spinner { |
| 124 | + border: 2px solid rgba(255, 255, 255, 0.3) !important; |
| 125 | + border-top: 2px solid #ffffff !important; |
| 126 | + } |
| 127 | + `); |
| 128 | + this.renderer.appendChild(document.head, this.styleElement); |
| 129 | + } |
| 130 | + } |
| 131 | +} |
0 commit comments