Skip to content

Commit 2c1b10d

Browse files
committed
added lazy script loading service
1 parent 494d733 commit 2c1b10d

File tree

4 files changed

+66
-2
lines changed

4 files changed

+66
-2
lines changed

src/app/pages/home/timeline/timeline.component.scss

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
left: 50%;
1010
width: 1px;
1111
height: 100%;
12-
background: red;
12+
background: var(--app-grey);
1313
}
1414

1515
.timeline-item-wrapper {
@@ -44,6 +44,10 @@
4444
}
4545
}
4646

47+
&.position {
48+
border-top: 2px solid red;
49+
}
50+
4751
.item-head {
4852
border-bottom: 1px solid var(--app-grey);
4953
padding-bottom: 2px;

src/app/pages/home/timeline/timeline.component.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@ import { Component, OnInit } from '@angular/core';
66
styleUrls: ['./timeline.component.scss']
77
})
88
export class TimelineComponent implements OnInit {
9+
timelineItems = [{
910

11+
}];
1012
constructor() { }
1113

1214
ngOnInit() {
Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { Component, OnInit } from '@angular/core';
2+
import { LazyLoadingServiceService } from 'src/app/services/LazyLoadingService/lazy-loading-service.service';
23

34
@Component({
45
selector: 'app-nj-timepicker',
@@ -7,9 +8,13 @@ import { Component, OnInit } from '@angular/core';
78
})
89
export class NjTimepickerComponent implements OnInit {
910

10-
constructor() { }
11+
constructor(private lazyLoadService: LazyLoadingServiceService) { }
1112

1213
ngOnInit() {
14+
/* Usage */
15+
this.lazyLoadService.loadScript('https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.24.0/moment.min.js').subscribe(() => {
16+
// code
17+
});
1318
}
1419

1520
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
import { Injectable, Inject } from '@angular/core';
2+
import { ReplaySubject, Observable } from 'rxjs';
3+
import { DOCUMENT } from '@angular/common';
4+
5+
@Injectable({
6+
providedIn: 'root'
7+
})
8+
export class LazyLoadingServiceService {
9+
10+
_loadedLibraries: { [url: string]: ReplaySubject<any> } = {};
11+
12+
constructor(@Inject(DOCUMENT) private readonly document: any) { }
13+
14+
loadScript(url: string): Observable<any> {
15+
if (this._loadedLibraries[url]) {
16+
return this._loadedLibraries[url].asObservable();
17+
}
18+
19+
this._loadedLibraries[url] = new ReplaySubject();
20+
21+
const script = this.document.createElement('script');
22+
script.type = 'text/javascript';
23+
script.src = url;
24+
script.onload = () => {
25+
this._loadedLibraries[url].next();
26+
this._loadedLibraries[url].complete();
27+
};
28+
29+
this.document.body.appendChild(script);
30+
31+
return this._loadedLibraries[url].asObservable();
32+
}
33+
34+
loadStyle(url: string): Observable<any> {
35+
if (this._loadedLibraries[url]) {
36+
return this._loadedLibraries[url].asObservable();
37+
}
38+
39+
this._loadedLibraries[url] = new ReplaySubject();
40+
41+
const script = this.document.createElement('link');
42+
script.rel = 'stylesheet';
43+
script.href = url;
44+
script.onload = () => {
45+
this._loadedLibraries[url].next();
46+
this._loadedLibraries[url].complete();
47+
};
48+
49+
this.document.body.appendChild(script);
50+
51+
return this._loadedLibraries[url].asObservable();
52+
}
53+
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy