X-Git-Url: https://git.immae.eu/?a=blobdiff_plain;f=client%2Fsrc%2Fapp%2Fshared%2Fmisc%2Futils.ts;h=f26240d216eee3461aab8d85eb20f12326d386b0;hb=41f8f6207a25b75c69120cabe9b0571ac055f2ec;hp=78e8e968250951bec4c61f5c291e2aa37328628f;hpb=7373507fa830b0f18cb4cd95dfd923b1600e501d;p=github%2FChocobozzz%2FPeerTube.git
diff --git a/client/src/app/shared/misc/utils.ts b/client/src/app/shared/misc/utils.ts
index 78e8e9682..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
@@ -102,12 +102,18 @@ function objectToFormData (obj: any, form?: FormData, namespace?: string) {
return fd
}
-function lineFeedToHtml (obj: any, keyToNormalize: string) {
+function objectLineFeedToHtml (obj: any, keyToNormalize: string) {
return immutableAssign(obj, {
- [keyToNormalize]: obj[keyToNormalize].replace(/\r?\n|\r/g, '
')
+ [keyToNormalize]: lineFeedToHtml(obj[keyToNormalize])
})
}
+function lineFeedToHtml (text: string) {
+ if (!text) return text
+
+ return text.replace(/\r?\n|\r/g, '
')
+}
+
function removeElementFromArray (arr: T[], elem: T) {
const index = arr.indexOf(elem)
if (index !== -1) arr.splice(index, 1)
@@ -128,9 +134,45 @@ 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,
+ lineFeedToHtml,
objectToUrlEncoded,
getParameterByName,
populateAsyncUserVideoChannels,
@@ -138,7 +180,8 @@ export {
dateToHuman,
immutableAssign,
objectToFormData,
- lineFeedToHtml,
+ objectLineFeedToHtml,
removeElementFromArray,
+ importModule,
scrollToTop
}