-
Notifications
You must be signed in to change notification settings - Fork 460
/
dot-utils.ts
96 lines (82 loc) · 2.75 KB
/
dot-utils.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
import {
DotCMSBaseTypesContentTypes,
DotCMSContentlet,
DotPageToolUrlParams
} from '@dotcms/dotcms-models';
/**
* Generate an anchor element with a Blob file to eventually be click to force a download
* This approach is needed because FF do not hear WS events while waiting for a request.
*/
export function getDownloadLink(blob: Blob, fileName: string): HTMLAnchorElement {
const anchor = document.createElement('a');
anchor.download = fileName;
anchor.href = window.URL.createObjectURL(blob);
return anchor;
}
// Replace {n} in the string with the strings in the args array
export function formatMessage(message: string, args: string[]): string {
return message.replace(/{(\d+)}/g, (match, number) => {
return typeof args[number] !== 'undefined' ? args[number] : match;
});
}
// Generates an unique Url with host, language and device Ids
export function generateDotFavoritePageUrl(params: {
languageId: number;
pageURI: string;
deviceInode?: string;
siteId?: string;
}): string {
const { deviceInode, languageId, pageURI, siteId } = params;
return (
`${pageURI}?` +
(siteId ? `host_id=${siteId}` : '') +
`&language_id=${languageId}` +
(deviceInode ? `&device_inode=${deviceInode}` : '&device_inode=')
);
}
/**
* Get the query parameter separator
* @param url
* @returns
*/
export function getQueryParameterSeparator(url: string): string {
const regex = /[?&]/;
return url.match(regex) ? '&' : '?';
}
/**
* This method is used to get the runnable link for the tool
* @param url
* @returns
*/
export function getRunnableLink(url: string, currentPageUrlParams: DotPageToolUrlParams): string {
const { currentUrl, requestHostName, siteId, languageId } = currentPageUrlParams;
url = url
.replace('{requestHostName}', requestHostName ?? '')
.replace('{currentUrl}', currentUrl ?? '')
.replace('{siteId}', siteId ? `${getQueryParameterSeparator(url)}host_id=${siteId}` : '')
.replace(
'{languageId}',
languageId ? `${getQueryParameterSeparator(url)}language_id=${String(languageId)}` : ''
);
return url;
}
/**
* Get the image asset URL
*
* @export
* @param {DotCMSContentlet} asset
* @return {*} {string}
*/
export function getImageAssetUrl(contentlet: DotCMSContentlet): string {
if (!contentlet?.baseType) {
return contentlet.asset;
}
switch (contentlet?.baseType) {
case DotCMSBaseTypesContentTypes.FILEASSET:
return contentlet.fileAssetVersion || contentlet.fileAsset;
case DotCMSBaseTypesContentTypes.DOTASSET:
return contentlet.assetVersion || contentlet.asset;
default:
return contentlet?.asset || '';
}
}