]>
Commit | Line | Data |
---|---|---|
1 | import { Video, VideoPlaylist } from '../../models' | |
2 | import { secondsToTime } from './date' | |
3 | ||
4 | function addQueryParams (url: string, params: { [ id: string ]: string }) { | |
5 | const objUrl = new URL(url) | |
6 | ||
7 | for (const key of Object.keys(params)) { | |
8 | objUrl.searchParams.append(key, params[key]) | |
9 | } | |
10 | ||
11 | return objUrl.toString() | |
12 | } | |
13 | ||
14 | function removeQueryParams (url: string) { | |
15 | const objUrl = new URL(url) | |
16 | ||
17 | objUrl.searchParams.forEach((_v, k) => objUrl.searchParams.delete(k)) | |
18 | ||
19 | return objUrl.toString() | |
20 | } | |
21 | ||
22 | function buildPlaylistLink (playlist: Pick<VideoPlaylist, 'shortUUID'>, base?: string) { | |
23 | return (base ?? window.location.origin) + buildPlaylistWatchPath(playlist) | |
24 | } | |
25 | ||
26 | function buildPlaylistWatchPath (playlist: Pick<VideoPlaylist, 'shortUUID'>) { | |
27 | return '/w/p/' + playlist.shortUUID | |
28 | } | |
29 | ||
30 | function buildVideoWatchPath (video: Pick<Video, 'shortUUID'>) { | |
31 | return '/w/' + video.shortUUID | |
32 | } | |
33 | ||
34 | function buildVideoLink (video: Pick<Video, 'shortUUID'>, base?: string) { | |
35 | return (base ?? window.location.origin) + buildVideoWatchPath(video) | |
36 | } | |
37 | ||
38 | function buildPlaylistEmbedPath (playlist: Pick<VideoPlaylist, 'uuid'>) { | |
39 | return '/video-playlists/embed/' + playlist.uuid | |
40 | } | |
41 | ||
42 | function buildPlaylistEmbedLink (playlist: Pick<VideoPlaylist, 'uuid'>, base?: string) { | |
43 | return (base ?? window.location.origin) + buildPlaylistEmbedPath(playlist) | |
44 | } | |
45 | ||
46 | function buildVideoEmbedPath (video: Pick<Video, 'uuid'>) { | |
47 | return '/videos/embed/' + video.uuid | |
48 | } | |
49 | ||
50 | function buildVideoEmbedLink (video: Pick<Video, 'uuid'>, base?: string) { | |
51 | return (base ?? window.location.origin) + buildVideoEmbedPath(video) | |
52 | } | |
53 | ||
54 | function decorateVideoLink (options: { | |
55 | url: string | |
56 | ||
57 | startTime?: number | |
58 | stopTime?: number | |
59 | ||
60 | subtitle?: string | |
61 | ||
62 | loop?: boolean | |
63 | autoplay?: boolean | |
64 | muted?: boolean | |
65 | ||
66 | // Embed options | |
67 | title?: boolean | |
68 | warningTitle?: boolean | |
69 | ||
70 | controls?: boolean | |
71 | controlBar?: boolean | |
72 | ||
73 | peertubeLink?: boolean | |
74 | p2p?: boolean | |
75 | }) { | |
76 | const { url } = options | |
77 | ||
78 | const params = new URLSearchParams() | |
79 | ||
80 | if (options.startTime !== undefined && options.startTime !== null) { | |
81 | const startTimeInt = Math.floor(options.startTime) | |
82 | params.set('start', secondsToTime(startTimeInt)) | |
83 | } | |
84 | ||
85 | if (options.stopTime) { | |
86 | const stopTimeInt = Math.floor(options.stopTime) | |
87 | params.set('stop', secondsToTime(stopTimeInt)) | |
88 | } | |
89 | ||
90 | if (options.subtitle) params.set('subtitle', options.subtitle) | |
91 | ||
92 | if (options.loop === true) params.set('loop', '1') | |
93 | if (options.autoplay === true) params.set('autoplay', '1') | |
94 | if (options.muted === true) params.set('muted', '1') | |
95 | if (options.title === false) params.set('title', '0') | |
96 | if (options.warningTitle === false) params.set('warningTitle', '0') | |
97 | ||
98 | if (options.controls === false) params.set('controls', '0') | |
99 | if (options.controlBar === false) params.set('controlBar', '0') | |
100 | ||
101 | if (options.peertubeLink === false) params.set('peertubeLink', '0') | |
102 | if (options.p2p !== undefined) params.set('p2p', options.p2p ? '1' : '0') | |
103 | ||
104 | return buildUrl(url, params) | |
105 | } | |
106 | ||
107 | function decoratePlaylistLink (options: { | |
108 | url: string | |
109 | ||
110 | playlistPosition?: number | |
111 | }) { | |
112 | const { url } = options | |
113 | ||
114 | const params = new URLSearchParams() | |
115 | ||
116 | if (options.playlistPosition) params.set('playlistPosition', '' + options.playlistPosition) | |
117 | ||
118 | return buildUrl(url, params) | |
119 | } | |
120 | ||
121 | // --------------------------------------------------------------------------- | |
122 | ||
123 | export { | |
124 | addQueryParams, | |
125 | removeQueryParams, | |
126 | ||
127 | buildPlaylistLink, | |
128 | buildVideoLink, | |
129 | ||
130 | buildVideoWatchPath, | |
131 | buildPlaylistWatchPath, | |
132 | ||
133 | buildPlaylistEmbedPath, | |
134 | buildVideoEmbedPath, | |
135 | ||
136 | buildPlaylistEmbedLink, | |
137 | buildVideoEmbedLink, | |
138 | ||
139 | decorateVideoLink, | |
140 | decoratePlaylistLink | |
141 | } | |
142 | ||
143 | function buildUrl (url: string, params: URLSearchParams) { | |
144 | let hasParams = false | |
145 | params.forEach(() => { hasParams = true }) | |
146 | ||
147 | if (hasParams) return url + '?' + params.toString() | |
148 | ||
149 | return url | |
150 | } |