-
-
Notifications
You must be signed in to change notification settings - Fork 70
Added custom multi language support #120
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
b9cc289
a2924e3
081c0f6
d216b26
966b1a8
1e812e5
a180a42
d6a8d12
1431f29
d726628
3f49812
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
import { DOCUMENT } from '@angular/common'; | ||
import { Inject, Injectable } from '@angular/core'; | ||
|
||
export interface Resource { | ||
name: string; | ||
type: "css" | "js"; | ||
src: string; | ||
} | ||
|
||
export interface LoadedResource { | ||
[name: string]: { | ||
loaded: boolean, | ||
type: "css" | "js", | ||
src: string | ||
} | ||
} | ||
|
||
@Injectable() | ||
export class DynamicLoaderService { | ||
|
||
private static ResourcesStore: Resource[] = []; | ||
private static LoadedResources: LoadedResource = {}; | ||
|
||
private _document: Document | undefined = undefined; | ||
|
||
constructor(@Inject(DOCUMENT) _document?: any) { | ||
this._document = _document; | ||
|
||
DynamicLoaderService.ResourcesStore.forEach((res) => { | ||
DynamicLoaderService.LoadedResources[res.name] = { | ||
loaded: false, | ||
type: res.type, | ||
src: res.src | ||
}; | ||
}); | ||
} | ||
|
||
/** | ||
* Loads a series of previously registered Script(s) | ||
* @param resNames The list of resources to load | ||
*/ | ||
load(...resNames: string[]) { | ||
let promises: any[] = []; | ||
promises = resNames.map((name) => this.loadResource(name)); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This can be written as a shorthand form. No need for initializing the array first. Additionally, the array should be correctly typed - not with |
||
return Promise.all(promises); | ||
} | ||
|
||
/** | ||
* Loads a script given it's name. | ||
* @param name Name of the script to laod. | ||
*/ | ||
loadResource(name: string) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Add return type |
||
return new Promise((resolve, reject) => { | ||
const resourceRef = DynamicLoaderService.LoadedResources[name]; | ||
|
||
if (!resourceRef) { | ||
return reject({ resource: name, loaded: false, statusText: 'Resource not registered' }); | ||
} | ||
|
||
if (resourceRef.loaded) { | ||
return resolve({ resource: name, loaded: true, statusText: 'Already Loaded' }); | ||
} | ||
|
||
const tag = resourceRef.type === "js" ? this._document.createElement("script") : this._document.createElement("link"); | ||
|
||
tag.onload = (e) => { | ||
resourceRef.loaded = true; | ||
return resolve({ resource: name, loaded: true, statusText: "Loaded", status: e }); | ||
}; | ||
|
||
tag.onerror = (error) => { | ||
return reject({ resource: name, loaded: false, statusText: 'Error', error: error }); | ||
} | ||
|
||
if (tag instanceof HTMLScriptElement) { | ||
tag.type = "text/javascript"; | ||
tag.src = resourceRef.src; | ||
} | ||
if (tag instanceof HTMLLinkElement) { | ||
tag.type = "text/css"; | ||
tag.href = resourceRef.src; | ||
tag.rel = "stylesheet"; | ||
} | ||
|
||
this._document.head.appendChild(tag); | ||
}); | ||
} | ||
|
||
/** | ||
* Registers a series of Resource(s), without loading them | ||
* @param resources A list of Resource(s) | ||
*/ | ||
register(...resources: Resource[]) { | ||
resources.forEach(res => { | ||
DynamicLoaderService.ResourcesStore.push(res); | ||
DynamicLoaderService.LoadedResources[res.name] = { | ||
loaded: false, | ||
type: res.type, | ||
src: res.src | ||
}; | ||
}); | ||
} | ||
|
||
/** | ||
* Registers and then loads a list of Resource(s) | ||
* @param resource The list of resources | ||
*/ | ||
registerAndLoad(...resource: Resource[]) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Add return type |
||
const resName = resource.map(s => s.name); | ||
this.register(...resource); | ||
return this.load(...resName); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please add a return type