Skip to content

Commit 749f3d2

Browse files
authored
Merge pull request #63 from t-ho/dev
Support custom template
2 parents ee6934b + dba2119 commit 749f3d2

30 files changed

+480
-329
lines changed

README.md

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
[![Build Status](https://travis-ci.org/t-ho/ngx-ui-loader.svg?branch=master)](https://travis-ci.org/t-ho/ngx-ui-loader)
44
[![codecov](https://codecov.io/gh/t-ho/ngx-ui-loader/branch/master/graph/badge.svg)](https://codecov.io/gh/t-ho/ngx-ui-loader)
55
[![npm](https://img.shields.io/npm/dw/ngx-ui-loader.svg)](https://www.npmjs.com/package/ngx-ui-loader)
6-
[![](https://data.jsdelivr.com/v1/package/npm/ngx-ui-loader/badge?style=rounded)](https://www.jsdelivr.com/package/npm/ngx-ui-loader)
76
[![npm bundle size (minified + gzip)](https://img.shields.io/bundlephobia/minzip/ngx-ui-loader.svg)](https://bundlephobia.com/result?p=ngx-ui-loader)
87
[![code style: prettier](https://img.shields.io/badge/code_style-prettier-ff69b4.svg)](https://github.com/prettier/prettier)
98
[![license](https://img.shields.io/npm/l/ngx-ui-loader.svg)](https://github.com/t-ho/ngx-ui-loader/wiki/License)
@@ -16,14 +15,10 @@ An all-in-one and fully customizable loader/spinner for Angular applications. It
1615

1716
## Features
1817

19-
- Support **multiple loaders** (>= ngx-ui-loader@7.1.0). See [Multiple loaders](https://tdev.app/ngx-ui-loader#multiple-loaders) for more details and [demo here](https://tdev.app/ngx-ui-loader/demo)
18+
- Support [custom template](https://tdev.app/ngx-ui-loader#custom-template) (>= ngx-ui-loader@9.1.0).
19+
- Support [multiple loaders](https://tdev.app/ngx-ui-loader#multiple-loaders) (>= ngx-ui-loader@7.1.0).
2020
- Show foreground loader with **progress bar**
2121
- The page content can be **blurred/frosted** while showing foreground loader. See [NgxUiLoaderBlurred](https://tdev.app/ngx-ui-loader#ngxuiloaderblurred-directive) directive for more details
22-
- Show loader with different id for different tasks
23-
- Be able to add **logo** and **loading text**
24-
- Be able to change position of spinners, logo and loading text
25-
- Be able to change color and size of logo, spinners and progress bar
26-
- Be able to change the direction of progress bar
2722
- Automatically show loader for router events. See [NgxUiLoaderRouterModule](https://tdev.app/ngx-ui-loader#automatically-show-loader-for-router-events) for more details
2823
- Automatically show loader for http requests. See [NgxUiLoaderHttpModule](https://tdev.app/ngx-ui-loader#automatically-show-loader-for-http-requests) for more details
2924

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
"prettier": "prettier --write \"**/*.{scss,ts,component.html}\"",
77
"start": "ng serve",
88
"build": "ng build ngx-ui-loader --prod && ng build --prod",
9-
"build:lib": "ng build ngx-ui-loader --prod && cp README.md CHANGELOG.md LICENSE ./dist/ngx-ui-loader/",
9+
"build:lib": "ng build ngx-ui-loader --prod && cp README.md CHANGELOG.md ./dist/ngx-ui-loader/",
1010
"build:schematics": "cd projects/ngx-ui-loader && npm run build && cd ../..",
1111
"test": "ng test ngx-ui-loader --code-coverage",
1212
"lint": "ng lint",

projects/ngx-ui-loader/src/lib/core/ngx-ui-loader-blurred.directive.ts

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,30 +3,19 @@ import { Subscription } from 'rxjs';
33
import { filter } from 'rxjs/operators';
44

55
import { NgxUiLoaderService } from './ngx-ui-loader.service';
6-
import { coerceNumber } from '../utils/functions';
76
import { ShowEvent } from '../utils/interfaces';
87
import { FOREGROUND, OVERLAY_DISAPPEAR_TIME, FAST_OVERLAY_DISAPPEAR_TIME } from '../utils/constants';
98

109
@Directive({ selector: '[ngxUiLoaderBlurred]' })
1110
export class NgxUiLoaderBlurredDirective implements OnInit, OnDestroy {
12-
private blurNumber: number;
13-
14-
@Input()
15-
get blur(): number {
16-
return this.blurNumber;
17-
}
18-
19-
set blur(value: number) {
20-
this.blurNumber = coerceNumber(value, this.loader.getDefaultConfig().blur);
21-
}
22-
11+
@Input() blur: number;
2312
@Input() loaderId: string;
2413

2514
showForegroundWatcher: Subscription;
2615
fastFadeOut: boolean;
2716

2817
constructor(private elementRef: ElementRef, private renderer: Renderer2, private loader: NgxUiLoaderService) {
29-
this.blurNumber = this.loader.getDefaultConfig().blur;
18+
this.blur = this.loader.getDefaultConfig().blur;
3019
this.loaderId = this.loader.getDefaultConfig().masterLoaderId;
3120
this.fastFadeOut = this.loader.getDefaultConfig().fastFadeOut;
3221
}
@@ -39,7 +28,7 @@ export class NgxUiLoaderBlurredDirective implements OnInit, OnDestroy {
3928
.pipe(filter((showEvent: ShowEvent) => this.loaderId === showEvent.loaderId))
4029
.subscribe(data => {
4130
if (data.isShow) {
42-
const filterValue = `blur(${this.blurNumber}px)`;
31+
const filterValue = `blur(${this.blur}px)`;
4332
this.renderer.setStyle(this.elementRef.nativeElement, '-webkit-filter', filterValue);
4433
this.renderer.setStyle(this.elementRef.nativeElement, 'filter', filterValue);
4534
} else {

projects/ngx-ui-loader/src/lib/core/ngx-ui-loader.component.html

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,12 @@
4343
[style.height.px]="fgsSize"
4444
[style.top]="spinnerTop"
4545
>
46-
<div [class]="fgSpinnerClass">
46+
<div *ngIf="!fgsTemplate; else foregroundTemplate" [class]="fgSpinnerClass">
4747
<div *ngFor="let div of fgDivs"></div>
4848
</div>
49+
<ng-template #foregroundTemplate>
50+
<ng-container *ngTemplateOutlet="fgsTemplate"></ng-container>
51+
</ng-template>
4952
</div>
5053
<!-- Foreground spinner }}} -->
5154

@@ -68,8 +71,11 @@
6871
[style.color]="bgsColor"
6972
[style.opacity]="bgsOpacity"
7073
>
71-
<div [class]="bgSpinnerClass">
74+
<div *ngIf="!bgsTemplate; else backgroundTemplate" [class]="bgSpinnerClass">
7275
<div *ngFor="let div of bgDivs"></div>
7376
</div>
77+
<ng-template #backgroundTemplate>
78+
<ng-container *ngTemplateOutlet="bgsTemplate"></ng-container>
79+
</ng-template>
7480
</div>
7581
<!-- Background spinner }}} -->

projects/ngx-ui-loader/src/lib/core/ngx-ui-loader.component.spec.ts

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -327,19 +327,6 @@ describe('NgxUiLoaderComponent', () => {
327327
expect(textEl.style.top).toBe('initial');
328328
});
329329

330-
it('#validate', () => {
331-
spyOn(console, 'error');
332-
component.bgsPosition = 'invalid-position' as PositionType;
333-
component.ngOnChanges({
334-
bgsPosition: new SimpleChange(null, component.bgsPosition, true)
335-
});
336-
fixture.detectChanges();
337-
expect(console.error).toHaveBeenCalledWith(
338-
`[ngx-ui-loader] - bgsPosition ("invalid-position") is invalid. ` + `Default value "${component.defaultConfig.bgsPosition}" is used.`
339-
);
340-
expect(component.bgsPosition).toBe(component.defaultConfig.bgsPosition);
341-
});
342-
343330
it(`start() - 1 - condition: 0 background and 0 foreground`, () => {
344331
loaderService.start();
345332
fixture.detectChanges();

projects/ngx-ui-loader/src/lib/core/ngx-ui-loader.component.ts

Lines changed: 5 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@ import {
77
SimpleChange,
88
OnDestroy,
99
ChangeDetectionStrategy,
10-
ChangeDetectorRef
10+
ChangeDetectorRef,
11+
TemplateRef
1112
} from '@angular/core';
1213
import { DomSanitizer, SafeResourceUrl, SafeStyle } from '@angular/platform-browser';
1314
import { NgxUiLoaderService } from './ngx-ui-loader.service';
@@ -16,10 +17,9 @@ import { filter } from 'rxjs/operators';
1617

1718
import { NgxUiLoaderConfig } from '../utils/interfaces';
1819
import { DirectionType, PositionType, SpinnerType } from '../utils/types';
19-
import { POSITION, PB_DIRECTION, SPINNER } from '../utils/enums';
20+
import { POSITION } from '../utils/enums';
2021
import { SPINNER_CONFIG } from '../utils/constants';
2122
import { ShowEvent } from '../utils/interfaces';
22-
import { coerceNumber } from '../utils/functions';
2323

2424
@Component({
2525
selector: 'ngx-ui-loader',
@@ -32,10 +32,12 @@ export class NgxUiLoaderComponent implements OnChanges, OnDestroy, OnInit {
3232
@Input() bgsOpacity: number;
3333
@Input() bgsPosition: PositionType;
3434
@Input() bgsSize: number;
35+
@Input() bgsTemplate: TemplateRef<any>;
3536
@Input() bgsType: SpinnerType;
3637
@Input() fgsColor: string;
3738
@Input() fgsPosition: PositionType;
3839
@Input() fgsSize: number;
40+
@Input() fgsTemplate: TemplateRef<any>;
3941
@Input() fgsType: SpinnerType;
4042
@Input() gap: number;
4143
@Input() loaderId: string;
@@ -116,12 +118,8 @@ export class NgxUiLoaderComponent implements OnChanges, OnDestroy, OnInit {
116118
this.ngxService.bindLoaderData(this.loaderId);
117119
this.determinePositions();
118120

119-
this.bgsPosition = this.validate('bgsPosition', this.bgsPosition, POSITION, this.defaultConfig.bgsPosition) as PositionType;
120-
121121
this.trustedLogoUrl = this.domSanitizer.bypassSecurityTrustResourceUrl(this.logoUrl);
122122

123-
this.pbDirection = this.validate('pbDirection', this.pbDirection, PB_DIRECTION, this.defaultConfig.pbDirection) as DirectionType;
124-
125123
this.showForegroundWatcher = this.ngxService.showForeground$
126124
.pipe(filter((showEvent: ShowEvent) => this.loaderId === showEvent.loaderId))
127125
.subscribe(data => {
@@ -161,42 +159,24 @@ export class NgxUiLoaderComponent implements OnChanges, OnDestroy, OnInit {
161159
}
162160

163161
const bgsTypeChange: SimpleChange = changes.bgsType;
164-
const bgsPositionChange: SimpleChange = changes.bgsPosition;
165162
const fgsTypeChange: SimpleChange = changes.fgsType;
166-
const loaderIdChange: SimpleChange = changes.loaderId;
167163
const logoUrlChange: SimpleChange = changes.logoUrl;
168-
const pbDirectionChange: SimpleChange = changes.pbDirection;
169164

170165
if (fgsTypeChange || bgsTypeChange) {
171166
this.initializeSpinners();
172167
}
173168

174-
if (loaderIdChange) {
175-
this.ngxService.updateLoaderId(loaderIdChange.previousValue, this.loaderId);
176-
}
177-
178169
this.determinePositions();
179170

180-
if (bgsPositionChange) {
181-
this.bgsPosition = this.validate('bgsPosition', this.bgsPosition, POSITION, this.defaultConfig.bgsPosition) as PositionType;
182-
}
183-
184171
if (logoUrlChange) {
185172
this.trustedLogoUrl = this.domSanitizer.bypassSecurityTrustResourceUrl(this.logoUrl);
186173
}
187-
188-
if (pbDirectionChange) {
189-
this.pbDirection = this.validate('pbDirection', this.pbDirection, PB_DIRECTION, this.defaultConfig.pbDirection) as DirectionType;
190-
}
191174
}
192175

193176
/**
194177
* Initialize spinners
195178
*/
196179
private initializeSpinners(): void {
197-
this.fgsType = this.validate('fgsType', this.fgsType, SPINNER, this.defaultConfig.fgsType) as SpinnerType;
198-
this.bgsType = this.validate('bgsType', this.bgsType, SPINNER, this.defaultConfig.bgsType) as SpinnerType;
199-
200180
this.fgDivs = Array(SPINNER_CONFIG[this.fgsType].divs).fill(1);
201181
this.fgSpinnerClass = SPINNER_CONFIG[this.fgsType].class;
202182
this.bgDivs = Array(SPINNER_CONFIG[this.bgsType].divs).fill(1);
@@ -207,11 +187,6 @@ export class NgxUiLoaderComponent implements OnChanges, OnDestroy, OnInit {
207187
* Determine the positions of spinner, logo and text
208188
*/
209189
private determinePositions(): void {
210-
this.fgsPosition = this.validate('fgsPosition', this.fgsPosition, POSITION, this.defaultConfig.fgsPosition) as PositionType;
211-
this.logoPosition = this.validate('logoPosition', this.logoPosition, POSITION, this.defaultConfig.logoPosition) as PositionType;
212-
this.textPosition = this.validate('textPosition', this.textPosition, POSITION, this.defaultConfig.textPosition) as PositionType;
213-
this.gap = coerceNumber(this.gap, this.defaultConfig.gap);
214-
215190
this.logoTop = 'initial';
216191
this.spinnerTop = 'initial';
217192
this.textTop = 'initial';
@@ -267,18 +242,6 @@ export class NgxUiLoaderComponent implements OnChanges, OnDestroy, OnInit {
267242
}
268243
}
269244

270-
private validate(inputName: string, value: string, validTypeObj: {}, fallbackValue: string): string {
271-
if (
272-
Object.keys(validTypeObj)
273-
.map(k => validTypeObj[k])
274-
.findIndex(v => v === value) === -1
275-
) {
276-
console.error(`[ngx-ui-loader] - ${inputName} ("${value}") is invalid. ` + `Default value "${fallbackValue}" is used.`);
277-
return fallbackValue;
278-
}
279-
return value;
280-
}
281-
282245
/**
283246
* On destroy event
284247
*/

0 commit comments

Comments
 (0)