snappymail/dev/Common/Links.js

140 lines
3.3 KiB
JavaScript
Raw Normal View History

2022-10-10 19:52:56 +08:00
import { pInt } from 'Common/Utils';
2023-01-19 23:46:39 +08:00
import { doc, Settings } from 'Common/Globals';
2015-11-15 08:23:16 +08:00
const
2023-01-19 23:46:39 +08:00
BASE = doc.location.pathname.replace(/\/+$/,'') + '/',
2016-06-30 08:02:45 +08:00
HASH_PREFIX = '#/',
adminPath = () => rl.adminArea() && !Settings.app('adminHostUse'),
2023-01-19 23:46:39 +08:00
prefix = () => BASE + '?' + (adminPath() ? Settings.app('adminPath') : '');
2015-11-15 08:23:16 +08:00
2021-08-20 21:40:07 +08:00
export const
SUB_QUERY_PREFIX = '&q[]=',
/**
* @param {string=} startupUrl
* @returns {string}
*/
2022-02-25 18:33:08 +08:00
root = () => HASH_PREFIX,
2021-08-20 21:40:07 +08:00
/**
* @returns {string}
*/
2023-01-19 23:46:39 +08:00
logoutLink = () => adminPath() ? prefix() : BASE,
2021-08-20 21:40:07 +08:00
/**
* @param {string} type
* @param {string} hash
* @param {string=} customSpecSuffix
* @returns {string}
*/
2021-08-23 22:40:28 +08:00
serverRequestRaw = (type, hash) =>
2023-01-19 23:46:39 +08:00
BASE + '?/Raw/' + SUB_QUERY_PREFIX + '/'
2022-11-01 00:10:04 +08:00
+ '0/' // Settings.get('AccountHash') ?
+ (type
? type + '/' + (hash ? SUB_QUERY_PREFIX + '/' + hash : '')
2021-08-20 21:40:07 +08:00
: ''),
/**
* @param {string} download
* @param {string=} customSpecSuffix
* @returns {string}
*/
attachmentDownload = (download, customSpecSuffix) =>
serverRequestRaw('Download', download, customSpecSuffix),
proxy = url =>
2023-01-19 23:46:39 +08:00
BASE + '?/ProxyExternal/'
2022-04-19 16:33:21 +08:00
+ btoa(url.replace(/ /g, '%20')).replace(/\+/g, '-').replace(/\//g, '_').replace(/=+$/, ''),
2022-12-28 17:59:47 +08:00
// + b64EncodeJSONSafe(url.replace(/ /g, '%20')),
2021-08-20 21:40:07 +08:00
/**
* @param {string} type
* @returns {string}
*/
serverRequest = type => prefix() + '/' + type + '/' + SUB_QUERY_PREFIX + '/0/',
2021-08-20 21:40:07 +08:00
2023-01-19 23:46:39 +08:00
// Is '?/Css/0/Admin' needed?
cssLink = theme => BASE + '?/Css/0/User/-/' + encodeURI(theme) + '/-/' + Date.now() + '/Hash/-/Json/',
2021-08-20 21:40:07 +08:00
/**
* @param {string} lang
* @param {boolean} isAdmin
* @returns {string}
*/
langLink = (lang, isAdmin) =>
2023-01-19 23:46:39 +08:00
BASE + '?/Lang/0/' + (isAdmin ? 'Admin' : 'App')
+ '/' + encodeURI(lang)
+ '/' + Settings.app('version') + '/',
2021-08-20 21:40:07 +08:00
/**
* @param {string} path
* @returns {string}
*/
staticLink = path => Settings.app('webVersionPath') + 'static/' + path,
2021-08-20 21:40:07 +08:00
/**
* @param {string} theme
* @returns {string}
*/
themePreviewLink = theme => {
let path = 'webVersionPath';
if (theme.endsWith('@custom')) {
theme = theme.slice(0, theme.length - 7).trim();
path = 'webPath';
2021-08-20 21:40:07 +08:00
}
return Settings.app(path) + 'themes/' + encodeURI(theme) + '/images/preview.png';
2021-08-20 21:40:07 +08:00
},
/**
* @param {string} inboxFolderName = 'INBOX'
* @returns {string}
*/
mailbox = (inboxFolderName = 'INBOX') => HASH_PREFIX + 'mailbox/' + inboxFolderName,
/**
* @param {string=} screenName = ''
* @returns {string}
*/
settings = (screenName = '') => HASH_PREFIX + 'settings' + (screenName ? '/' + screenName : ''),
/**
* @param {string=} screenName
* @returns {string}
*/
admin = screenName => HASH_PREFIX + (
'AdminDomains' == screenName ? 'domains'
: 'AdminSecurity' == screenName ? 'security'
: ''
),
/**
* @param {string} folder
* @param {number=} page = 1
* @param {string=} search = ''
2021-09-10 22:28:29 +08:00
* @param {number=} threadUid = 0
2021-08-20 21:40:07 +08:00
* @returns {string}
*/
mailBox = (folder, page, search, threadUid, messageUid) => {
2021-12-07 23:52:05 +08:00
let result = [HASH_PREFIX + 'mailbox'];
2021-08-20 21:40:07 +08:00
if (folder) {
2021-12-07 23:52:05 +08:00
result.push(folder + (threadUid ? '~' + threadUid : ''));
2021-08-20 21:40:07 +08:00
}
if (messageUid) {
result.push('m' + messageUid);
} else {
page = pInt(page, 1);
if (1 < page) {
result.push('p' + page);
}
search && result.push(encodeURI(search));
2021-08-20 21:40:07 +08:00
}
2021-12-07 23:52:05 +08:00
return result.join('/');
},
mailBoxMessage = (folder, messageUid) => mailBox(folder, 1, '', 0, pInt(messageUid));