snappymail/dev/Common/Utils.js

142 lines
3.3 KiB
JavaScript
Raw Normal View History

import { SaveSettingsStep } from 'Common/Enums';
import { doc, elementById } from 'Common/Globals';
2016-06-07 05:57:52 +08:00
export const
isArray = Array.isArray,
2021-07-22 03:34:17 +08:00
arrayLength = array => isArray(array) && array.length,
2021-03-24 21:22:25 +08:00
isFunction = v => typeof v === 'function';
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,[contenteditable]'
);
} 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) {
2021-03-18 21:48:21 +08:00
return iError => {
koTrigger.call(context, iError ? SaveSettingsStep.FalseResult : SaveSettingsStep.TrueResult);
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}
*/
2021-08-13 15:58:43 +08:00
export function changeTheme(value, themeTrigger = ()=>0) {
const themeStyle = elementById('app-theme-style'),
clearTimer = () => {
__themeTimer = setTimeout(() => themeTrigger(SaveSettingsStep.Idle), 1000);
__themeJson = null;
2016-06-30 08:02:45 +08:00
};
let url = 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 => {
2021-07-22 03:34:17 +08:00
if (2 === arrayLength(data)) {
themeStyle.textContent = data[1];
themeStyle.dataset.href = url;
themeStyle.dataset.theme = data[0];
2019-07-05 03:19:24 +08:00
themeTrigger(SaveSettingsStep.TrueResult);
}
})
.then(clearTimer, clearTimer);
2016-06-07 05:57:52 +08:00
}
}
export function addObservablesTo(target, observables) {
Object.entries(observables).forEach(([key, value]) =>
target[key] = /*isArray(value) ? ko.observableArray(value) :*/ ko.observable(value) );
}
export function addComputablesTo(target, computables) {
Object.entries(computables).forEach(([key, fn]) => target[key] = ko.computed(fn));
}
export function addSubscribablesTo(target, subscribables) {
Object.entries(subscribables).forEach(([key, fn]) => target[key].subscribe(fn));
}