X-Git-Url: https://git.immae.eu/?a=blobdiff_plain;f=client%2Fsrc%2Fapp%2Fshared%2Fmisc%2Futils.ts;h=f26240d216eee3461aab8d85eb20f12326d386b0;hb=41f8f6207a25b75c69120cabe9b0571ac055f2ec;hp=7cc6055c2ae6c3b355ef7ba0940b77988332232a;hpb=b718fd22374d64534bcfe69932cf562894abed6a;p=github%2FChocobozzz%2FPeerTube.git diff --git a/client/src/app/shared/misc/utils.ts b/client/src/app/shared/misc/utils.ts index 7cc6055c2..f26240d21 100644 --- a/client/src/app/shared/misc/utils.ts +++ b/client/src/app/shared/misc/utils.ts @@ -17,7 +17,7 @@ function getParameterByName (name: string, url: string) { return decodeURIComponent(results[2].replace(/\+/g, ' ')) } -function populateAsyncUserVideoChannels (authService: AuthService, channel: { id: number, label: string, support: string }[]) { +function populateAsyncUserVideoChannels (authService: AuthService, channel: { id: number, label: string, support?: string }[]) { return new Promise(res => { authService.userInformationLoaded .subscribe( @@ -78,10 +78,10 @@ function objectToUrlEncoded (obj: any) { // Thanks: https://gist.github.com/ghinda/8442a57f22099bdb2e34 function objectToFormData (obj: any, form?: FormData, namespace?: string) { - let fd = form || new FormData() + const fd = form || new FormData() let formKey - for (let key of Object.keys(obj)) { + for (const key of Object.keys(obj)) { if (namespace) formKey = `${namespace}[${key}]` else formKey = key @@ -134,6 +134,41 @@ function scrollToTop () { window.scroll(0, 0) } +// Thanks: https://github.com/uupaa/dynamic-import-polyfill +function importModule (path: string) { + return new Promise((resolve, reject) => { + const vector = '$importModule$' + Math.random().toString(32).slice(2) + const script = document.createElement('script') + + const destructor = () => { + delete window[ vector ] + script.onerror = null + script.onload = null + script.remove() + URL.revokeObjectURL(script.src) + script.src = '' + } + + script.defer = true + script.type = 'module' + + script.onerror = () => { + reject(new Error(`Failed to import: ${path}`)) + destructor() + } + script.onload = () => { + resolve(window[ vector ]) + destructor() + } + const absURL = (environment.apiUrl || window.location.origin) + path + const loader = `import * as m from "${absURL}"; window.${vector} = m;` // export Module + const blob = new Blob([ loader ], { type: 'text/javascript' }) + script.src = URL.createObjectURL(blob) + + document.head.appendChild(script) + }) +} + export { sortBy, durationToString, @@ -147,5 +182,6 @@ export { objectToFormData, objectLineFeedToHtml, removeElementFromArray, + importModule, scrollToTop }