Skip to content
This repository was archived by the owner on Oct 14, 2024. It is now read-only.

Commit 5b7c652

Browse files
authored
Merge pull request #12 from benshabatnoam/master
Custom template component
2 parents 5076f10 + 9411544 commit 5b7c652

12 files changed

Lines changed: 96 additions & 37 deletions

File tree

.vscode/launch.json

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
{
2+
// Use IntelliSense to learn about possible attributes.
3+
// Hover to view descriptions of existing attributes.
4+
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
5+
"version": "0.2.0",
6+
"configurations": [
7+
{
8+
"type": "chrome",
9+
"request": "launch",
10+
"name": "Launch Chrome against localhost",
11+
"url": "http://localhost:4200",
12+
"webRoot": "${workspaceFolder}"
13+
}
14+
]
15+
}

.vscode/tasks.json

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
{
2+
// See https://go.microsoft.com/fwlink/?LinkId=733558
3+
// for the documentation about the tasks.json format
4+
"version": "2.0.0",
5+
"tasks": [
6+
{
7+
"type": "npm",
8+
"script": "start",
9+
"problemMatcher": []
10+
},
11+
{
12+
"type": "npm",
13+
"script": "build",
14+
"path": "projects/ng-busy/",
15+
"problemMatcher": []
16+
},
17+
{
18+
"type": "npm",
19+
"script": "build",
20+
"problemMatcher": []
21+
}
22+
]
23+
}

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,4 +52,4 @@
5252
"ts-node": "~5.0.1",
5353
"tslint": "~5.9.1"
5454
}
55-
}
55+
}

projects/ng-busy/README.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,7 @@ In other words, you may use flexible syntax:
136136
| minDuration | Optional | 0 | The amount of time to keep the indicator showing even if the busy thing was completed quicker. Specified in milliseconds.|
137137
| disableAnimation | Optional | false | Disable the animation when the spinner appear |
138138
| wrapperClass | Optional | 'ng-busy' | The name(s) of the CSS classes to be applied to the wrapper element of the indicator. |
139+
| templateNgStyle | Optional | { } | An object that will be assigned to the custom component assigned to `template` option, if one was configured (see example below in Overriding Defaults). |
139140

140141

141142
## Overriding Defaults
@@ -157,7 +158,8 @@ import {CustomBusyComponent} from '...'
157158
backdrop: false,
158159
template: CustomBusyComponent,
159160
delay: 200,
160-
minDuration: 600
161+
minDuration: 600,
162+
templateNgStyle: { "background-color": "black" }
161163
}))
162164
],
163165
declarations: [
@@ -176,7 +178,7 @@ export class AppModule
176178
@Component({
177179
selector: 'default-busy',
178180
template: `
179-
<div class="ng-busy-default-wrapper">
181+
<div class="ng-busy-default-wrapper" [ngStyle]="templateNgStyle">
180182
<div class="ng-busy-default-sign">
181183
<div class="ng-busy-default-spinner">
182184
<div class="bar1"></div>

projects/ng-busy/src/lib/model/busy-config.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import {InstanceConfigHolderService} from '../service/instance-config-holder.ser
44

55
export class BusyConfig implements IBusyConfig {
66
template: TemplateRef<any> | Type<any>;
7+
templateNgStyle: {};
78
delay: number;
89
minDuration: number;
910
backdrop: boolean;
@@ -54,6 +55,7 @@ export class DefaultBusyComponent {
5455

5556
export interface IBusyConfig {
5657
template?: TemplateRef<any> | Type<any>;
58+
templateNgStyle?: {};
5759
delay?: number;
5860
minDuration?: number;
5961
backdrop?: boolean;
@@ -65,6 +67,7 @@ export interface IBusyConfig {
6567

6668
export const BUSY_CONFIG_DEFAULTS = {
6769
template: DefaultBusyComponent,
70+
templateNgStyle: {},
6871
delay: 0,
6972
minDuration: 0,
7073
backdrop: true,

projects/ng-busy/src/lib/ng-busy.directive.ts

Lines changed: 22 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,14 @@ import {
1010
TemplateRef, Type,
1111
ViewContainerRef
1212
} from '@angular/core';
13-
import {ViewRef} from '@angular/core/src/linker/view_ref';
14-
import {BusyTrackerService} from './service/busy-tracker.service';
15-
import {BusyConfigHolderService} from './service/busy-config-holder.service';
16-
import {Subscription} from 'rxjs';
17-
import {IBusyConfig} from './model/busy-config';
18-
import {NgBusyComponent} from './component/ng-busy/ng-busy.component';
19-
import {InstanceConfigHolderService} from './service/instance-config-holder.service';
20-
import {isPromise} from './util/isPromise';
13+
import { ViewRef } from '@angular/core/src/linker/view_ref';
14+
import { BusyTrackerService } from './service/busy-tracker.service';
15+
import { BusyConfigHolderService } from './service/busy-config-holder.service';
16+
import { Subscription } from 'rxjs';
17+
import { IBusyConfig } from './model/busy-config';
18+
import { NgBusyComponent } from './component/ng-busy/ng-busy.component';
19+
import { InstanceConfigHolderService } from './service/instance-config-holder.service';
20+
import { isPromise } from './util/isPromise';
2121

2222
@Directive({
2323
selector: '[ngBusy]',
@@ -43,17 +43,18 @@ export class NgBusyDirective implements DoCheck, OnDestroy {
4343
private isLoading = false;
4444
private busyEmitter: EventEmitter<boolean> = new EventEmitter<boolean>();
4545
public template: TemplateRef<any> | Type<any>;
46+
public templateNgStyle: {};
4647
private _option: any;
4748

4849
constructor(private configHolder: BusyConfigHolderService,
49-
private instanceConfigHolder: InstanceConfigHolderService,
50-
private resolver: ComponentFactoryResolver,
51-
private tracker: BusyTrackerService,
52-
private appRef: ApplicationRef,
53-
private vcr: ViewContainerRef,
54-
private element: ElementRef,
55-
private renderer: Renderer2,
56-
private injector: Injector) {
50+
private instanceConfigHolder: InstanceConfigHolderService,
51+
private resolver: ComponentFactoryResolver,
52+
private tracker: BusyTrackerService,
53+
private appRef: ApplicationRef,
54+
private vcr: ViewContainerRef,
55+
private element: ElementRef,
56+
private renderer: Renderer2,
57+
private injector: Injector) {
5758
this.onStartSubscription = tracker.onStartBusy.subscribe(() => {
5859
setTimeout(() => {
5960
this.recreateBusyIfNecessary();
@@ -88,22 +89,24 @@ export class NgBusyDirective implements DoCheck, OnDestroy {
8889
private recreateBusyIfNecessary() {
8990
if (!this.busyRef
9091
|| this.template !== this.optionsNorm.template
92+
|| this.templateNgStyle !== this.optionsNorm.templateNgStyle
9193
) {
9294
this.destroyComponents();
9395
this.template = this.optionsNorm.template;
96+
this.templateNgStyle = this.optionsNorm.templateNgStyle;
9497
this.createBusy();
9598
this.busyEmitter.emit(this.isLoading);
9699
}
97100
}
98101

99102
private normalizeOptions(options: any): IBusyConfig {
100103
if (!options) {
101-
options = {busy: []};
104+
options = { busy: [] };
102105
} else if (Array.isArray(options)
103106
|| isPromise(options)
104107
|| options instanceof Subscription
105108
) {
106-
options = {busy: options};
109+
options = { busy: options };
107110
}
108111
options = Object.assign({}, this.configHolder.config, options);
109112
if (!Array.isArray(options.busy)) {
@@ -151,6 +154,7 @@ export class NgBusyDirective implements DoCheck, OnDestroy {
151154
}
152155
const factory = this.resolver.resolveComponentFactory(this.template);
153156
const componentRef = factory.create(injector);
157+
componentRef.instance.templateNgStyle = this.options.templateNgStyle;
154158
this.componentViewRef = componentRef.hostView;
155159
this.appRef.attachView(this.componentViewRef);
156160
return [[componentRef.location.nativeElement]];

src/app/modules/ng-busy-demo/component/code-viewer/code-viewer.component.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ export class CodeViewerComponent implements OnChanges {
5858
options['backdrop'] = config.backdrop;
5959
options['message'] = config.message;
6060
options['wrapperClass'] = config.wrapperClass;
61+
options['templateNgStyle'] = config.templateNgStyle;
6162
let result = JSON.stringify(options);
6263
result = result.replace('{', `{template:${template},`);
6364
return result;

src/app/modules/ng-busy-demo/component/custom-busy-component/custom-busy-component.component.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
import {ChangeDetectorRef, Component, Inject, OnInit} from '@angular/core';
2-
import {BUSY_CONFIG_DEFAULTS} from 'ng-busy';
3-
import {InstanceConfigHolderService} from '../../../../../../projects/ng-busy/src/lib/service/instance-config-holder.service';
1+
import { Component, Inject } from '@angular/core';
2+
import { BUSY_CONFIG_DEFAULTS } from 'ng-busy';
3+
import { InstanceConfigHolderService } from '../../../../../../projects/ng-busy/src/lib/service/instance-config-holder.service';
44

55
@Component({
66
selector: 'default-busy',
77
template: `
8-
<div style="background: url('../../assets/img/du.gif') no-repeat center 20px; background-size: 72px;">
8+
<div style="background: url('../../assets/img/du.gif') no-repeat center 20px; background-size: 72px;" [ngStyle]="templateNgStyle">
99
<div style="margin-top: 110px; text-align: center; font-size: 18px; font-weight: 700; line-height: 110px;">
1010
{{message}}
1111
</div>

src/app/modules/ng-busy-demo/component/options/options.component.css

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,7 @@
55
.c-checkbox {
66
line-height: 36px;
77
}
8+
9+
#template-ng-style {
10+
height: 100px;
11+
}

src/app/modules/ng-busy-demo/component/options/options.component.html

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,7 @@
77
<div class="form-group row">
88
<label for="options-min-duration" class="col-sm-5 form-control-label">Min duration (ms)</label>
99
<div class="col-sm-7">
10-
<input type="number" class="form-control" id="options-min-duration" name="minDuration"
11-
[(ngModel)]="data.minDuration">
10+
<input type="number" class="form-control" id="options-min-duration" name="minDuration" [(ngModel)]="data.minDuration">
1211
</div>
1312
</div>
1413
<div class="form-group row">
@@ -25,12 +24,18 @@
2524
</select>
2625
</div>
2726
</div>
27+
<div class="form-group row" *ngIf="templateType === 'custom'">
28+
<label for="template-ng-style" class="col-sm-5 form-control-label">TemplateNgStyle</label>
29+
<div class="col-sm-7">
30+
<textarea type="text" class="form-control" id="template-ng-style" name="message" [(ngModel)]="templateNgStyle"></textarea>
31+
</div>
32+
</div>
2833
<div class="form-group row">
2934
<label class="col-sm-5 form-control-label">Backdrop</label>
3035
<div class="col-sm-7">
3136
<label class="c-input c-checkbox">
3237
<input type="checkbox" id="options-backdrop" #backdrop name="backdrop" (change)="changeBackdrop(backdrop)"
33-
checked>
38+
checked>
3439
<span class="c-indicator"></span>
3540
Show
3641
</label>
@@ -48,4 +53,4 @@
4853
</div>
4954
<div class="col-sm-offset-5 col-sm-7">
5055
<button type="button" class="btn btn-secondary" (click)="playDemo()">Demo</button>
51-
</div>
56+
</div>

0 commit comments

Comments
 (0)