X-Git-Url: https://git.immae.eu/?a=blobdiff_plain;f=client%2Fsrc%2Fapp%2Fshared%2Fmisc%2Futils.ts;h=8381745f5fd4ad8394ea49949e64b269ab9d42d9;hb=0f7fedc39857ebc0eb29182c1588a92b9adfb75a;hp=d520b1a7b8f789a145c2166ef0bd9c61e04b5a7b;hpb=a9ca764e7e23c0c14949c308a9ce54d4fdb9a361;p=github%2FChocobozzz%2FPeerTube.git diff --git a/client/src/app/shared/misc/utils.ts b/client/src/app/shared/misc/utils.ts index d520b1a7b..8381745f5 100644 --- a/client/src/app/shared/misc/utils.ts +++ b/client/src/app/shared/misc/utils.ts @@ -17,11 +17,7 @@ function getParameterByName (name: string, url: string) { return decodeURIComponent(results[2].replace(/\+/g, ' ')) } -function viewportHeight () { - return Math.max(document.documentElement.clientHeight, window.innerHeight || 0) -} - -function populateAsyncUserVideoChannels (authService: AuthService, channel: any[]) { +function populateAsyncUserVideoChannels (authService: AuthService, channel: { id: number, label: string, support: string }[]) { return new Promise(res => { authService.userInformationLoaded .subscribe( @@ -32,7 +28,7 @@ function populateAsyncUserVideoChannels (authService: AuthService, channel: any[ const videoChannels = user.videoChannels if (Array.isArray(videoChannels) === false) return - videoChannels.forEach(c => channel.push({ id: c.id, label: c.displayName })) + videoChannels.forEach(c => channel.push({ id: c.id, label: c.displayName, support: c.support })) return res() } @@ -59,6 +55,15 @@ function immutableAssign (target: A, source: B) { return Object.assign({}, target, source) } +function objectToUrlEncoded (obj: any) { + const str: string[] = [] + for (const key of Object.keys(obj)) { + str.push(encodeURIComponent(key) + '=' + encodeURIComponent(obj[key])) + } + + return str.join('&') +} + // Thanks: https://gist.github.com/ghinda/8442a57f22099bdb2e34 function objectToFormData (obj: any, form?: FormData, namespace?: string) { let fd = form || new FormData() @@ -70,8 +75,13 @@ function objectToFormData (obj: any, form?: FormData, namespace?: string) { if (obj[key] === undefined) continue - if (typeof obj[ key ] === 'object' && !(obj[ key ] instanceof File)) { - objectToFormData(obj[ key ], fd, key) + if (Array.isArray(obj[key]) && obj[key].length === 0) { + fd.append(key, null) + continue + } + + if (obj[key] !== null && typeof obj[ key ] === 'object' && !(obj[ key ] instanceof File)) { + objectToFormData(obj[ key ], fd, formKey) } else { fd.append(formKey, obj[ key ]) } @@ -86,27 +96,19 @@ function lineFeedToHtml (obj: object, keyToNormalize: string) { }) } -// Try to cache a little bit window.innerWidth -let windowInnerWidth = window.innerWidth -setInterval(() => windowInnerWidth = window.innerWidth, 500) - -function isInSmallView () { - return windowInnerWidth < 600 -} - -function isInMobileView () { - return windowInnerWidth < 500 +function removeElementFromArray (arr: T[], elem: T) { + const index = arr.indexOf(elem) + if (index !== -1) arr.splice(index, 1) } export { - viewportHeight, + objectToUrlEncoded, getParameterByName, populateAsyncUserVideoChannels, getAbsoluteAPIUrl, dateToHuman, - isInSmallView, - isInMobileView, immutableAssign, objectToFormData, - lineFeedToHtml + lineFeedToHtml, + removeElementFromArray }