snappymail/dev/Common/Utils.js

137 lines
3.1 KiB
JavaScript
Raw Normal View History

import { SaveSettingsStep } from 'Common/Enums';
2016-06-07 05:57:52 +08:00
2020-08-07 22:28:30 +08:00
const
doc = document;
2016-06-07 05:57:52 +08:00
/**
* @param {*} value
* @param {number=} defaultValue = 0
2016-06-30 08:02:45 +08:00
* @returns {number}
2016-06-07 05:57:52 +08:00
*/
export function pInt(value, defaultValue = 0) {
value = parseInt(value, 10);
return isNaN(value) || !isFinite(value) ? defaultValue : value;
2016-06-07 05:57:52 +08:00
}
/**
* @param {*} value
2016-06-30 08:02:45 +08:00
* @returns {string}
2016-06-07 05:57:52 +08:00
*/
2019-07-05 03:19:24 +08:00
export function pString(value) {
return null != value ? '' + value : '';
2016-06-07 05:57:52 +08:00
}
/**
2016-06-30 08:02:45 +08:00
* @returns {boolean}
2016-06-07 05:57:52 +08:00
*/
2019-07-05 03:19:24 +08:00
export function inFocus() {
try {
return doc.activeElement && doc.activeElement.matches(
'input,textarea,iframe,.cke_editable'
);
} catch (e) {
return false;
}
2016-06-07 05:57:52 +08:00
}
/**
* @param {string} theme
2016-06-30 08:02:45 +08:00
* @returns {string}
2016-06-07 05:57:52 +08:00
*/
export const convertThemeName = theme => {
2019-07-05 03:19:24 +08:00
if ('@custom' === theme.substr(-7)) {
theme = theme.substr(0, theme.length - 7).trim();
2016-06-07 05:57:52 +08:00
}
return theme
.replace(/([A-Z])/g, ' $1')
.replace(/[^a-zA-Z0-9]+/g, ' ')
.trim();
};
2016-06-07 05:57:52 +08:00
2016-06-30 08:02:45 +08:00
/**
* @param {object} domOption
* @param {object} item
* @returns {void}
*/
export function defaultOptionsAfterRender(domItem, item) {
if (item && undefined !== item.disabled && domItem) {
domItem.classList.toggle('disabled', domItem.disabled = item.disabled);
2016-06-07 05:57:52 +08:00
}
}
2016-06-30 08:02:45 +08:00
/**
* @param {object} koTrigger
* @param {mixed} context
* @returns {mixed}
*/
2019-07-05 03:19:24 +08:00
export function settingsSaveHelperSimpleFunction(koTrigger, context) {
return (type, data) => {
koTrigger.call(context, data && data.Result ? SaveSettingsStep.TrueResult : SaveSettingsStep.FalseResult);
setTimeout(() => koTrigger.call(context, SaveSettingsStep.Idle), 1000);
2016-06-07 05:57:52 +08:00
};
}
2019-07-05 03:19:24 +08:00
let __themeTimer = 0,
__themeJson = null;
2016-06-07 05:57:52 +08:00
2016-06-30 08:02:45 +08:00
/**
* @param {string} value
2016-08-22 05:30:34 +08:00
* @param {function=} themeTrigger = noop
2016-06-30 08:02:45 +08:00
* @returns {void}
*/
export function changeTheme(value, themeTrigger = ()=>{}) {
const themeLink = doc.getElementById('app-theme-link'),
clearTimer = () => {
__themeTimer = setTimeout(() => themeTrigger(SaveSettingsStep.Idle), 1000);
__themeJson = null;
2016-06-30 08:02:45 +08:00
};
let themeStyle = doc.getElementById('app-theme-style'),
url = (themeLink && themeLink.href) || (themeStyle && themeStyle.dataset.href);
2016-06-07 05:57:52 +08:00
2019-07-05 03:19:24 +08:00
if (url) {
url = url.toString()
.replace(/\/-\/[^/]+\/-\//, '/-/' + value + '/-/')
.replace(/\/Css\/[^/]+\/User\//, '/Css/0/User/')
.replace(/\/Hash\/[^/]+\//, '/Hash/-/');
2016-06-07 05:57:52 +08:00
if ('Json/' !== url.substr(-5)) {
2016-06-07 05:57:52 +08:00
url += 'Json/';
}
clearTimeout(__themeTimer);
2016-08-22 05:30:34 +08:00
2016-06-07 05:57:52 +08:00
themeTrigger(SaveSettingsStep.Animate);
if (__themeJson) {
__themeJson.abort();
2016-06-07 05:57:52 +08:00
}
let init = {};
if (window.AbortController) {
__themeJson = new AbortController();
init.signal = __themeJson.signal;
}
rl.fetchJSON(url, init)
.then(data => {
if (data && Array.isArray(data) && 2 === data.length) {
if (themeLink && !themeStyle) {
themeStyle = doc.createElement('style');
themeStyle.id = 'app-theme-style';
2019-07-05 03:19:24 +08:00
themeLink.after(themeStyle);
themeLink.remove();
2016-06-07 05:57:52 +08:00
}
if (themeStyle) {
themeStyle.textContent = data[1];
themeStyle.dataset.href = url;
themeStyle.dataset.theme = data[0];
2019-07-05 03:19:24 +08:00
}
2016-06-07 05:57:52 +08:00
2019-07-05 03:19:24 +08:00
themeTrigger(SaveSettingsStep.TrueResult);
}
})
.then(clearTimer, clearTimer);
2016-06-07 05:57:52 +08:00
}
}