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