snappymail/dev/Common/Links.js

211 lines
4.3 KiB
JavaScript
Raw Normal View History

import { pString, pInt } from 'Common/Utils';
import { Settings } from 'Common/Globals';
2015-11-15 08:23:16 +08:00
const
ROOT = './',
2016-06-30 08:02:45 +08:00
HASH_PREFIX = '#/',
SERVER_PREFIX = './?',
VERSION = Settings.app('version'),
VERSION_PREFIX = Settings.app('webVersionPath') || 'snappymail/v/' + VERSION + '/',
2016-06-16 07:36:44 +08:00
getHash = () => Settings.get('AuthAccountHash') || '0';
2015-11-15 08:23:16 +08:00
2016-06-16 07:36:44 +08:00
/**
2016-06-30 08:02:45 +08:00
* @returns {string}
2016-06-16 07:36:44 +08:00
*/
2021-01-27 19:02:37 +08:00
export const SUB_QUERY_PREFIX = '&q[]=';
2015-11-15 08:23:16 +08:00
2016-06-16 07:36:44 +08:00
/**
* @param {string=} startupUrl
2016-06-30 08:02:45 +08:00
* @returns {string}
2016-06-16 07:36:44 +08:00
*/
2019-07-05 03:19:24 +08:00
export function root(startupUrl = '') {
2016-06-16 07:36:44 +08:00
return HASH_PREFIX + pString(startupUrl);
}
2016-04-30 07:42:18 +08:00
2016-06-16 07:36:44 +08:00
/**
2016-06-30 08:02:45 +08:00
* @returns {string}
2016-06-16 07:36:44 +08:00
*/
2019-07-05 03:19:24 +08:00
export function rootAdmin() {
2020-10-02 18:40:33 +08:00
return Settings.app('adminHostUse') ? ROOT : SERVER_PREFIX + (Settings.app('adminPath') || 'admin');
2016-06-16 07:36:44 +08:00
}
2016-04-30 07:42:18 +08:00
2016-06-16 07:36:44 +08:00
/**
2016-06-30 08:02:45 +08:00
* @returns {string}
2016-06-16 07:36:44 +08:00
*/
2021-01-27 19:02:37 +08:00
export function logoutLink() {
return rl.adminArea() ? rootAdmin() : ROOT;
2016-06-16 07:36:44 +08:00
}
2015-11-15 08:23:16 +08:00
2016-06-16 07:36:44 +08:00
/**
* @param {string} type
* @param {string} hash
2016-06-16 07:36:44 +08:00
* @param {string=} customSpecSuffix
2016-06-30 08:02:45 +08:00
* @returns {string}
2016-06-16 07:36:44 +08:00
*/
export function serverRequestRaw(type, hash, customSpecSuffix) {
return SERVER_PREFIX + '/Raw/' + SUB_QUERY_PREFIX + '/'
+ (null == customSpecSuffix ? getHash() : customSpecSuffix) + '/'
+ (type
? type + '/' + (hash ? SUB_QUERY_PREFIX + '/' + hash : '')
: '')
;
2016-06-16 07:36:44 +08:00
}
2015-11-15 08:23:16 +08:00
2016-06-16 07:36:44 +08:00
/**
* @param {string} download
* @param {string=} customSpecSuffix
2016-06-30 08:02:45 +08:00
* @returns {string}
2016-06-16 07:36:44 +08:00
*/
2019-07-05 03:19:24 +08:00
export function attachmentDownload(download, customSpecSuffix) {
return serverRequestRaw('Download', download, customSpecSuffix);
2016-06-16 07:36:44 +08:00
}
2015-11-15 08:23:16 +08:00
2016-06-16 07:36:44 +08:00
/**
* @param {string} type
2016-06-30 08:02:45 +08:00
* @returns {string}
2016-06-16 07:36:44 +08:00
*/
2019-07-05 03:19:24 +08:00
export function serverRequest(type) {
return SERVER_PREFIX + '/' + type + '/' + SUB_QUERY_PREFIX + '/' + getHash() + '/';
2016-06-16 07:36:44 +08:00
}
2015-11-15 08:23:16 +08:00
2016-06-16 07:36:44 +08:00
/**
* @param {string} email
2016-06-30 08:02:45 +08:00
* @returns {string}
2016-06-16 07:36:44 +08:00
*/
2019-07-05 03:19:24 +08:00
export function change(email) {
return serverRequest('Change') + encodeURIComponent(email) + '/';
2016-06-16 07:36:44 +08:00
}
2015-11-15 08:23:16 +08:00
2016-06-16 07:36:44 +08:00
/**
* @param {string} hash
2016-06-30 08:02:45 +08:00
* @returns {string}
2016-06-16 07:36:44 +08:00
*/
2019-07-05 03:19:24 +08:00
export function userBackground(hash) {
return serverRequestRaw('UserBackground', hash);
2016-06-16 07:36:44 +08:00
}
2015-11-15 08:23:16 +08:00
2016-06-16 07:36:44 +08:00
/**
* @param {string} lang
* @param {boolean} isAdmin
2016-06-30 08:02:45 +08:00
* @returns {string}
2016-06-16 07:36:44 +08:00
*/
2019-07-05 03:19:24 +08:00
export function langLink(lang, isAdmin) {
return SERVER_PREFIX + '/Lang/0/' + (isAdmin ? 'Admin' : 'App') + '/' + encodeURI(lang) + '/' + VERSION + '/';
2016-06-16 07:36:44 +08:00
}
2015-11-15 08:23:16 +08:00
2016-06-16 07:36:44 +08:00
/**
* @param {string} path
2016-06-30 08:02:45 +08:00
* @returns {string}
2016-06-16 07:36:44 +08:00
*/
2019-07-05 03:19:24 +08:00
export function staticPrefix(path) {
2020-10-02 18:40:33 +08:00
return VERSION_PREFIX + 'static/' + path;
2016-06-16 07:36:44 +08:00
}
2015-11-15 08:23:16 +08:00
2016-06-16 07:36:44 +08:00
/**
* @param {string} fileName
2016-06-30 08:02:45 +08:00
* @returns {string}
2016-06-16 07:36:44 +08:00
*/
2019-07-05 03:19:24 +08:00
export function sound(fileName) {
2016-06-16 07:36:44 +08:00
return staticPrefix('sounds/' + fileName);
}
2015-11-15 08:23:16 +08:00
2016-06-16 07:36:44 +08:00
/**
2016-06-30 08:02:45 +08:00
* @returns {string}
2016-06-16 07:36:44 +08:00
*/
2019-07-05 03:19:24 +08:00
export function notificationMailIcon() {
return staticPrefix('css/images/icon-message-notification.png');
2016-06-16 07:36:44 +08:00
}
2015-11-15 08:23:16 +08:00
2016-06-16 07:36:44 +08:00
/**
2016-06-30 08:02:45 +08:00
* @returns {string}
2016-06-16 07:36:44 +08:00
*/
2019-07-05 03:19:24 +08:00
export function openPgpJs() {
2016-06-16 07:36:44 +08:00
return staticPrefix('js/min/openpgp.min.js');
}
2015-11-15 08:23:16 +08:00
2016-06-16 07:36:44 +08:00
/**
2016-06-30 08:02:45 +08:00
* @returns {string}
2016-06-16 07:36:44 +08:00
*/
2019-07-05 03:19:24 +08:00
export function openPgpWorkerJs() {
2016-06-16 07:36:44 +08:00
return staticPrefix('js/min/openpgp.worker.min.js');
}
2015-11-15 08:23:16 +08:00
2016-06-16 07:36:44 +08:00
/**
* @param {string} theme
2016-06-30 08:02:45 +08:00
* @returns {string}
2016-06-16 07:36:44 +08:00
*/
2019-07-05 03:19:24 +08:00
export function themePreviewLink(theme) {
2016-06-16 07:36:44 +08:00
let prefix = VERSION_PREFIX;
2019-07-05 03:19:24 +08:00
if ('@custom' === theme.substr(-7)) {
theme = theme.substr(0, theme.length - 7).trim();
2020-10-02 18:40:33 +08:00
prefix = Settings.app('webPath') || '';
2015-11-15 08:23:16 +08:00
}
return prefix + 'themes/' + encodeURI(theme) + '/images/preview.png';
2016-06-16 07:36:44 +08:00
}
2015-11-15 08:23:16 +08:00
2016-06-16 07:36:44 +08:00
/**
* @param {string} inboxFolderName = 'INBOX'
2016-06-30 08:02:45 +08:00
* @returns {string}
2016-06-16 07:36:44 +08:00
*/
export function mailbox(inboxFolderName = 'INBOX') {
2016-06-16 07:36:44 +08:00
return HASH_PREFIX + 'mailbox/' + inboxFolderName;
}
2015-11-15 08:23:16 +08:00
2016-06-16 07:36:44 +08:00
/**
* @param {string=} screenName = ''
2016-06-30 08:02:45 +08:00
* @returns {string}
2016-06-16 07:36:44 +08:00
*/
2019-07-05 03:19:24 +08:00
export function settings(screenName = '') {
2016-06-16 07:36:44 +08:00
return HASH_PREFIX + 'settings' + (screenName ? '/' + screenName : '');
}
2015-11-15 08:23:16 +08:00
2016-06-16 07:36:44 +08:00
/**
* @param {string} screenName
2016-06-30 08:02:45 +08:00
* @returns {string}
2016-06-16 07:36:44 +08:00
*/
2019-07-05 03:19:24 +08:00
export function admin(screenName) {
2016-06-16 07:36:44 +08:00
let result = HASH_PREFIX;
2019-07-05 03:19:24 +08:00
switch (screenName) {
2016-06-16 07:36:44 +08:00
case 'AdminDomains':
result += 'domains';
break;
case 'AdminSecurity':
result += 'security';
break;
2016-06-30 08:02:45 +08:00
// no default
2016-06-16 07:36:44 +08:00
}
return result;
}
/**
* @param {string} folder
* @param {number=} page = 1
* @param {string=} search = ''
* @param {string=} threadUid = ''
2016-06-30 08:02:45 +08:00
* @returns {string}
2016-06-16 07:36:44 +08:00
*/
2019-07-05 03:19:24 +08:00
export function mailBox(folder, page = 1, search = '', threadUid = '') {
page = pInt(page, 1);
2016-06-16 07:36:44 +08:00
search = pString(search);
let result = HASH_PREFIX + 'mailbox/';
2015-11-15 08:23:16 +08:00
if (folder) {
2016-06-16 07:36:44 +08:00
const resultThreadUid = pInt(threadUid);
result += encodeURI(folder) + (0 < resultThreadUid ? '~' + resultThreadUid : '');
2015-11-15 08:23:16 +08:00
}
2019-07-05 03:19:24 +08:00
if (1 < page) {
2021-02-04 18:25:00 +08:00
result = result.replace(/[/]+$/, '') + '/p' + page;
2015-11-15 08:23:16 +08:00
}
if (search) {
2021-02-04 18:25:00 +08:00
result = result.replace(/[/]+$/, '') + '/' + encodeURI(search);
2015-11-15 08:23:16 +08:00
}
2016-06-16 07:36:44 +08:00
return result;
}