diff options
Diffstat (limited to 'shared/core-utils/common/url.ts')
-rw-r--r-- | shared/core-utils/common/url.ts | 130 |
1 files changed, 130 insertions, 0 deletions
diff --git a/shared/core-utils/common/url.ts b/shared/core-utils/common/url.ts index e69de29bb..52ed247c4 100644 --- a/shared/core-utils/common/url.ts +++ b/shared/core-utils/common/url.ts | |||
@@ -0,0 +1,130 @@ | |||
1 | import { Video, VideoPlaylist } from '../../models' | ||
2 | import { secondsToTime } from './date' | ||
3 | |||
4 | function buildPlaylistLink (playlist: Pick<VideoPlaylist, 'shortUUID'>, base?: string) { | ||
5 | return (base ?? window.location.origin) + buildPlaylistWatchPath(playlist) | ||
6 | } | ||
7 | |||
8 | function buildPlaylistWatchPath (playlist: Pick<VideoPlaylist, 'shortUUID'>) { | ||
9 | return '/w/p/' + playlist.shortUUID | ||
10 | } | ||
11 | |||
12 | function buildVideoWatchPath (video: Pick<Video, 'shortUUID'>) { | ||
13 | return '/w/' + video.shortUUID | ||
14 | } | ||
15 | |||
16 | function buildVideoLink (video: Pick<Video, 'shortUUID'>, base?: string) { | ||
17 | return (base ?? window.location.origin) + buildVideoWatchPath(video) | ||
18 | } | ||
19 | |||
20 | function buildPlaylistEmbedPath (playlist: Pick<VideoPlaylist, 'uuid'>) { | ||
21 | return '/video-playlists/embed/' + playlist.uuid | ||
22 | } | ||
23 | |||
24 | function buildPlaylistEmbedLink (playlist: Pick<VideoPlaylist, 'uuid'>, base?: string) { | ||
25 | return (base ?? window.location.origin) + buildPlaylistEmbedPath(playlist) | ||
26 | } | ||
27 | |||
28 | function buildVideoEmbedPath (video: Pick<Video, 'uuid'>) { | ||
29 | return '/videos/embed/' + video.uuid | ||
30 | } | ||
31 | |||
32 | function buildVideoEmbedLink (video: Pick<Video, 'uuid'>, base?: string) { | ||
33 | return (base ?? window.location.origin) + buildVideoEmbedPath(video) | ||
34 | } | ||
35 | |||
36 | function decorateVideoLink (options: { | ||
37 | url: string | ||
38 | |||
39 | startTime?: number | ||
40 | stopTime?: number | ||
41 | |||
42 | subtitle?: string | ||
43 | |||
44 | loop?: boolean | ||
45 | autoplay?: boolean | ||
46 | muted?: boolean | ||
47 | |||
48 | // Embed options | ||
49 | title?: boolean | ||
50 | warningTitle?: boolean | ||
51 | controls?: boolean | ||
52 | peertubeLink?: boolean | ||
53 | }) { | ||
54 | const { url } = options | ||
55 | |||
56 | const params = generateParams(window.location.search) | ||
57 | |||
58 | if (options.startTime !== undefined && options.startTime !== null) { | ||
59 | const startTimeInt = Math.floor(options.startTime) | ||
60 | params.set('start', secondsToTime(startTimeInt)) | ||
61 | } | ||
62 | |||
63 | if (options.stopTime) { | ||
64 | const stopTimeInt = Math.floor(options.stopTime) | ||
65 | params.set('stop', secondsToTime(stopTimeInt)) | ||
66 | } | ||
67 | |||
68 | if (options.subtitle) params.set('subtitle', options.subtitle) | ||
69 | |||
70 | if (options.loop === true) params.set('loop', '1') | ||
71 | if (options.autoplay === true) params.set('autoplay', '1') | ||
72 | if (options.muted === true) params.set('muted', '1') | ||
73 | if (options.title === false) params.set('title', '0') | ||
74 | if (options.warningTitle === false) params.set('warningTitle', '0') | ||
75 | if (options.controls === false) params.set('controls', '0') | ||
76 | if (options.peertubeLink === false) params.set('peertubeLink', '0') | ||
77 | |||
78 | return buildUrl(url, params) | ||
79 | } | ||
80 | |||
81 | function decoratePlaylistLink (options: { | ||
82 | url: string | ||
83 | |||
84 | playlistPosition?: number | ||
85 | }) { | ||
86 | const { url } = options | ||
87 | |||
88 | const params = generateParams(window.location.search) | ||
89 | |||
90 | if (options.playlistPosition) params.set('playlistPosition', '' + options.playlistPosition) | ||
91 | |||
92 | return buildUrl(url, params) | ||
93 | } | ||
94 | |||
95 | // --------------------------------------------------------------------------- | ||
96 | |||
97 | export { | ||
98 | buildPlaylistLink, | ||
99 | buildVideoLink, | ||
100 | |||
101 | buildVideoWatchPath, | ||
102 | buildPlaylistWatchPath, | ||
103 | |||
104 | buildPlaylistEmbedPath, | ||
105 | buildVideoEmbedPath, | ||
106 | |||
107 | buildPlaylistEmbedLink, | ||
108 | buildVideoEmbedLink, | ||
109 | |||
110 | decorateVideoLink, | ||
111 | decoratePlaylistLink | ||
112 | } | ||
113 | |||
114 | function buildUrl (url: string, params: URLSearchParams) { | ||
115 | let hasParams = false | ||
116 | params.forEach(() => { hasParams = true }) | ||
117 | |||
118 | if (hasParams) return url + '?' + params.toString() | ||
119 | |||
120 | return url | ||
121 | } | ||
122 | |||
123 | function generateParams (url: string) { | ||
124 | const params = new URLSearchParams(window.location.search) | ||
125 | // Unused parameters in embed | ||
126 | params.delete('videoId') | ||
127 | params.delete('resume') | ||
128 | |||
129 | return params | ||
130 | } | ||