@@ -10,7 +10,20 @@ export interface LazyResult {
10
10
error ?: NzSafeAny ;
11
11
}
12
12
13
+ export interface LazyLoadItem {
14
+ path : string ;
15
+ options ?: LazyLoadOptions ;
16
+ }
17
+
18
+ export interface LazyLoadOptions {
19
+ innerContent ?: string ;
20
+ attributes ?: { [ qualifiedName : string ] : string } ;
21
+ rel ?: string ;
22
+ }
23
+
13
24
/**
25
+ * `LazyService` delay loading JS or CSS files.
26
+ *
14
27
* 延迟加载资源(js 或 css)服务
15
28
*/
16
29
@Injectable ( { providedIn : 'root' } )
@@ -33,27 +46,59 @@ export class LazyService {
33
46
this . cached = { } ;
34
47
}
35
48
36
- load ( paths : string | string [ ] ) : Promise < LazyResult [ ] > {
49
+ private attachAttributes ( el : HTMLElement , attributes ?: { [ qualifiedName : string ] : string } ) : void {
50
+ if ( attributes == null ) return ;
51
+
52
+ Object . entries ( attributes ) . forEach ( ( [ key , value ] ) => {
53
+ el . setAttribute ( key , value ) ;
54
+ } ) ;
55
+ }
56
+
57
+ /**
58
+ * Load script or style files
59
+ */
60
+ load ( paths : string | LazyLoadItem | Array < string | LazyLoadItem > ) : Promise < LazyResult [ ] > {
37
61
if ( ! Array . isArray ( paths ) ) {
38
62
paths = [ paths ] ;
39
63
}
40
64
41
65
const promises : Array < Promise < LazyResult > > = [ ] ;
42
- paths . forEach ( path => {
43
- if ( path . endsWith ( '.js' ) ) {
44
- promises . push ( this . loadScript ( path ) ) ;
45
- } else {
46
- promises . push ( this . loadStyle ( path ) ) ;
47
- }
48
- } ) ;
66
+ paths
67
+ . map ( v => ( typeof v !== 'object' ? ( { path : v } as LazyLoadItem ) : v ) )
68
+ . forEach ( item => {
69
+ if ( item . path . endsWith ( '.js' ) ) {
70
+ promises . push ( this . loadScript ( item . path , item . options ) ) ;
71
+ } else {
72
+ promises . push ( this . loadStyle ( item . path , item . options ) ) ;
73
+ }
74
+ } ) ;
49
75
50
76
return Promise . all ( promises ) . then ( res => {
51
77
this . _notify . next ( res ) ;
52
78
return Promise . resolve ( res ) ;
53
79
} ) ;
54
80
}
55
81
56
- loadScript ( path : string , innerContent ?: string ) : Promise < LazyResult > {
82
+ /**
83
+ * @deprecated Will be removed in 15.0.0, Please use `loadScript(path, options)` instead
84
+ */
85
+ loadScript ( path : string , innerContent : string , attributes ?: { [ qualifiedName : string ] : string } ) : Promise < LazyResult > ;
86
+ /**
87
+ * Load a script file
88
+ */
89
+ loadScript ( path : string , options ?: LazyLoadOptions ) : Promise < LazyResult > ;
90
+ loadScript (
91
+ path : string ,
92
+ innerContent ?: string | LazyLoadOptions ,
93
+ attributes ?: { [ qualifiedName : string ] : string }
94
+ ) : Promise < LazyResult > {
95
+ const options : LazyLoadOptions =
96
+ typeof innerContent === 'object'
97
+ ? innerContent
98
+ : {
99
+ innerContent,
100
+ attributes
101
+ } ;
57
102
return new Promise ( resolve => {
58
103
if ( this . list [ path ] === true ) {
59
104
resolve ( { ...this . cached [ path ] , status : 'loading' } ) ;
@@ -70,8 +115,9 @@ export class LazyService {
70
115
const node = this . doc . createElement ( 'script' ) as HTMLScriptElement ;
71
116
node . type = 'text/javascript' ;
72
117
node . src = path ;
73
- if ( innerContent ) {
74
- node . innerHTML = innerContent ;
118
+ this . attachAttributes ( node , options . attributes ) ;
119
+ if ( options . innerContent ) {
120
+ node . innerHTML = options . innerContent ;
75
121
}
76
122
node . onload = ( ) =>
77
123
onSuccess ( {
@@ -88,7 +134,33 @@ export class LazyService {
88
134
} ) ;
89
135
}
90
136
91
- loadStyle ( path : string , rel : string = 'stylesheet' , innerContent ?: string ) : Promise < LazyResult > {
137
+ /**
138
+ * @deprecated Will be removed in 15.0.0, Please use `loadStyle(path, options)` instead
139
+ */
140
+ loadStyle (
141
+ path : string ,
142
+ rel : string ,
143
+ innerContent ?: string ,
144
+ attributes ?: { [ qualifiedName : string ] : string }
145
+ ) : Promise < LazyResult > ;
146
+ /**
147
+ * Load a style file
148
+ */
149
+ loadStyle ( path : string , options ?: LazyLoadOptions ) : Promise < LazyResult > ;
150
+ loadStyle (
151
+ path : string ,
152
+ rel ?: string | LazyLoadOptions ,
153
+ innerContent ?: string ,
154
+ attributes ?: { [ qualifiedName : string ] : string }
155
+ ) : Promise < LazyResult > {
156
+ const options : LazyLoadOptions =
157
+ typeof rel === 'object'
158
+ ? rel
159
+ : {
160
+ rel,
161
+ innerContent,
162
+ attributes
163
+ } ;
92
164
return new Promise ( resolve => {
93
165
if ( this . list [ path ] === true ) {
94
166
resolve ( this . cached [ path ] ) ;
@@ -98,11 +170,12 @@ export class LazyService {
98
170
this . list [ path ] = true ;
99
171
100
172
const node = this . doc . createElement ( 'link' ) as HTMLLinkElement ;
101
- node . rel = rel ;
173
+ node . rel = options . rel ?? 'stylesheet' ;
102
174
node . type = 'text/css' ;
103
175
node . href = path ;
104
- if ( innerContent ) {
105
- node . innerHTML = innerContent ;
176
+ this . attachAttributes ( node , options . attributes ) ;
177
+ if ( options . innerContent ) {
178
+ node . innerHTML = options . innerContent ;
106
179
}
107
180
this . doc . getElementsByTagName ( 'head' ) [ 0 ] . appendChild ( node ) ;
108
181
const item : LazyResult = {
0 commit comments