-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
075e892
commit 69264d9
Showing
17 changed files
with
254 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
module.exports = { | ||
name: 'http-script', | ||
preset: '../../../jest.config.js', | ||
coverageDirectory: '../../../coverage/libs/http/script', | ||
snapshotSerializers: [ | ||
'jest-preset-angular/AngularSnapshotSerializer.js', | ||
'jest-preset-angular/HTMLCommentSerializer.js' | ||
] | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
{ | ||
"$schema": "../../node_modules/ng-packagr/ng-package.schema.json", | ||
"dest": "../../../dist/libs/http/script", | ||
"lib": { | ||
"entryFile": "src/index.ts" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
{ | ||
"name": "@ng-lab/http-script", | ||
"version": "0.0.1", | ||
"peerDependencies": { | ||
"@angular/common": "^7.2.0", | ||
"@angular/core": "^7.2.0" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
export * from './lib/http-script.module'; | ||
|
||
export { SCRIPTS_LOADER } from './lib/http-script.token'; | ||
export { HttpScriptLoaderService } from './lib/http-script.service'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import { async, TestBed } from '@angular/core/testing'; | ||
import { HttpScriptModule } from './http-script.module'; | ||
|
||
describe('HttpScriptModule', () => { | ||
beforeEach(async(() => { | ||
TestBed.configureTestingModule({ | ||
imports: [HttpScriptModule] | ||
}).compileComponents(); | ||
})); | ||
|
||
it('should create', () => { | ||
expect(HttpScriptModule).toBeDefined(); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import { NgModule } from '@angular/core'; | ||
import { CommonModule, DOCUMENT } from '@angular/common'; | ||
|
||
import { SCRIPTS_LOADER } from './http-script.token'; | ||
import { HttpScriptLoaderService } from './http-script.service'; | ||
|
||
@NgModule({ | ||
imports: [CommonModule], | ||
providers: [ | ||
{ | ||
provide: SCRIPTS_LOADER, | ||
useValue: [] | ||
}, | ||
{ | ||
provide: HttpScriptLoaderService, | ||
useClass: HttpScriptLoaderService, | ||
deps: [DOCUMENT, SCRIPTS_LOADER] | ||
} | ||
] | ||
}) | ||
export class HttpScriptModule {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
/** | ||
* @license | ||
* Copyright NgLab All Rights Reserved. | ||
* | ||
* Use of this source code is governed by an MIT-style license that can be | ||
* found in the LICENSE file at https://www.ng-lab.com/license | ||
*/ | ||
import { DOCUMENT } from '@angular/common'; | ||
import { Injectable, Inject } from '@angular/core'; | ||
|
||
import { Observable, Observer } from 'rxjs'; | ||
|
||
import { SCRIPTS_LOADER } from './http-script.token'; | ||
import { ScriptInterface } from './http-script.typing'; | ||
|
||
@Injectable() | ||
export class HttpScriptLoaderService { | ||
private _scripts: ScriptInterface[] = []; | ||
|
||
constructor( | ||
@Inject(DOCUMENT) private _doc: HTMLDocument, | ||
@Inject(SCRIPTS_LOADER) private _loadScripts: ScriptInterface[] | ||
) { | ||
this._initLoader(); | ||
} | ||
|
||
public load(script: ScriptInterface): Observable<ScriptInterface> { | ||
return new Observable<ScriptInterface>( | ||
(observer: Observer<ScriptInterface>) => { | ||
const scriptExist = this._scripts.find(js => js.name === script.name); | ||
|
||
if (scriptExist && scriptExist.loaded) { | ||
observer.next(scriptExist); | ||
observer.complete(); | ||
} else { | ||
this._scripts.push(script); | ||
this._createHeaderScriptTag(script, observer); | ||
} | ||
} | ||
); | ||
} | ||
|
||
public hasScript(name: string): boolean { | ||
return !!this._scripts.find(script => script.name === name); | ||
} | ||
|
||
public isLoaded(name: string): boolean { | ||
const script = this._scripts.find(js => js.name === name); | ||
|
||
return script ? script.loaded : false; | ||
} | ||
|
||
private _initLoader(): void { | ||
this._loadScripts.map(script => this.load(script).subscribe()); | ||
} | ||
|
||
private _createHeaderScriptTag( | ||
script: ScriptInterface, | ||
observer: Observer<ScriptInterface> | ||
): void { | ||
const scriptTag = this._doc.createElement('script'); | ||
scriptTag.type = 'text/javascript'; | ||
scriptTag.src = script.src; | ||
scriptTag.async = true; | ||
|
||
scriptTag.addEventListener('load', () => { | ||
script.loaded = true; | ||
observer.next(script); | ||
observer.complete(); | ||
}); | ||
|
||
scriptTag.addEventListener('error', () => { | ||
observer.error(`Cannot load script ${script.name}.`); | ||
}); | ||
|
||
this._doc.getElementsByTagName('body')[0].appendChild(scriptTag); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import { InjectionToken } from '@angular/core'; | ||
|
||
import { ScriptInterface } from './http-script.typing'; | ||
|
||
export const SCRIPTS_LOADER = new InjectionToken<ScriptInterface>( | ||
'[SCRIPTS] Scripts loader token', | ||
{ | ||
providedIn: 'root', | ||
factory: () => [] as any | ||
} | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
export interface ScriptInterface { | ||
name: string; | ||
src: string; | ||
loaded: boolean; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
import 'jest-preset-angular'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
{ | ||
"extends": "../../../tsconfig.json", | ||
"compilerOptions": { | ||
"types": ["node", "jest"] | ||
}, | ||
"include": ["**/*.ts"] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
{ | ||
"extends": "./tsconfig.json", | ||
"compilerOptions": { | ||
"outDir": "../../../dist/out-tsc/libs/http/script", | ||
"target": "es2015", | ||
"module": "es2015", | ||
"moduleResolution": "node", | ||
"declaration": true, | ||
"sourceMap": true, | ||
"inlineSources": true, | ||
"emitDecoratorMetadata": true, | ||
"experimentalDecorators": true, | ||
"importHelpers": true, | ||
"types": [], | ||
"lib": ["dom", "es2018"], | ||
"paths": { | ||
"@ng-lab/*": ["dist/libs/*"] | ||
} | ||
}, | ||
"angularCompilerOptions": { | ||
"annotateForClosureCompiler": true, | ||
"skipTemplateCodegen": true, | ||
"strictMetadataEmit": true, | ||
"fullTemplateTypeCheck": true, | ||
"strictInjectionParameters": true, | ||
"enableResourceInlining": true | ||
}, | ||
"exclude": ["src/test.ts", "**/*.spec.ts"] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
{ | ||
"extends": "./tsconfig.json", | ||
"compilerOptions": { | ||
"outDir": "../../../dist/out-tsc/libs/http/script", | ||
"module": "commonjs", | ||
"types": ["jest", "node"] | ||
}, | ||
"files": ["src/test-setup.ts"], | ||
"include": ["**/*.spec.ts", "**/*.d.ts"] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
{ | ||
"extends": "../../../tslint.json", | ||
"rules": { | ||
"directive-selector": [true, "attribute", "ng-lab", "camelCase"], | ||
"component-selector": [true, "element", "ng-lab", "kebab-case"] | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters