2020-10-15 01:16:37 +08:00
|
|
|
import { SaveSettingsStep } from 'Common/Enums';
|
2021-04-28 18:08:07 +08:00
|
|
|
import { doc, elementById } from 'Common/Globals';
|
2016-06-07 05:57:52 +08:00
|
|
|
|
2021-01-27 07:26:31 +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
|
2020-08-18 03:57:56 +08:00
|
|
|
* @param {number=} defaultValue = 0
|
2016-06-30 08:02:45 +08:00
|
|
|
* @returns {number}
|
2016-06-07 05:57:52 +08:00
|
|
|
*/
|
2020-08-18 03:57:56 +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) {
|
2020-08-18 03:57:56 +08:00
|
|
|
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() {
|
2016-07-30 03:14:51 +08:00
|
|
|
try {
|
2020-10-15 01:16:37 +08:00
|
|
|
return doc.activeElement && doc.activeElement.matches(
|
2021-08-17 00:09:54 +08:00
|
|
|
'input,textarea,[contenteditable]'
|
2020-10-15 01:16:37 +08:00
|
|
|
);
|
|
|
|
} 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
|
|
|
*/
|
2020-07-23 22:06:16 +08:00
|
|
|
export const convertThemeName = theme => {
|
2019-07-05 03:19:24 +08:00
|
|
|
if ('@custom' === theme.substr(-7)) {
|
2020-09-03 18:51:15 +08:00
|
|
|
theme = theme.substr(0, theme.length - 7).trim();
|
2016-06-07 05:57:52 +08:00
|
|
|
}
|
|
|
|
|
2020-08-07 00:24:46 +08:00
|
|
|
return theme
|
2020-09-03 18:51:15 +08:00
|
|
|
.replace(/([A-Z])/g, ' $1')
|
2020-10-15 01:16:37 +08:00
|
|
|
.replace(/[^a-zA-Z0-9]+/g, ' ')
|
2020-09-03 18:51:15 +08:00
|
|
|
.trim();
|
2020-07-23 22:06:16 +08:00
|
|
|
};
|
2016-06-07 05:57:52 +08:00
|
|
|
|
2016-06-30 08:02:45 +08:00
|
|
|
/**
|
|
|
|
* @param {object} domOption
|
|
|
|
* @param {object} item
|
|
|
|
* @returns {void}
|
|
|
|
*/
|
2020-10-15 01:16:37 +08:00
|
|
|
export function defaultOptionsAfterRender(domItem, item) {
|
2020-07-30 03:49:41 +08:00
|
|
|
if (item && undefined !== item.disabled && domItem) {
|
2020-08-19 22:47:33 +08:00
|
|
|
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);
|
2020-10-03 05:54:15 +08:00
|
|
|
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,
|
2020-12-30 22:50:47 +08:00
|
|
|
__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) {
|
2021-04-28 18:08:07 +08:00
|
|
|
const themeStyle = elementById('app-theme-style'),
|
2016-06-21 01:22:51 +08:00
|
|
|
clearTimer = () => {
|
2020-08-12 06:25:36 +08:00
|
|
|
__themeTimer = setTimeout(() => themeTrigger(SaveSettingsStep.Idle), 1000);
|
2020-12-30 22:50:47 +08:00
|
|
|
__themeJson = null;
|
2016-06-30 08:02:45 +08:00
|
|
|
};
|
|
|
|
|
2021-04-28 18:08:07 +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) {
|
2020-08-19 22:47:33 +08:00
|
|
|
url = url.toString()
|
|
|
|
.replace(/\/-\/[^/]+\/-\//, '/-/' + value + '/-/')
|
|
|
|
.replace(/\/Css\/[^/]+\/User\//, '/Css/0/User/')
|
|
|
|
.replace(/\/Hash\/[^/]+\//, '/Hash/-/');
|
2016-06-07 05:57:52 +08:00
|
|
|
|
2020-09-03 18:51:15 +08:00
|
|
|
if ('Json/' !== url.substr(-5)) {
|
2016-06-07 05:57:52 +08:00
|
|
|
url += 'Json/';
|
|
|
|
}
|
|
|
|
|
2020-08-12 06:25:36 +08:00
|
|
|
clearTimeout(__themeTimer);
|
2016-08-22 05:30:34 +08:00
|
|
|
|
2016-06-07 05:57:52 +08:00
|
|
|
themeTrigger(SaveSettingsStep.Animate);
|
|
|
|
|
2020-12-30 22:50:47 +08:00
|
|
|
if (__themeJson) {
|
|
|
|
__themeJson.abort();
|
2016-06-07 05:57:52 +08:00
|
|
|
}
|
2020-07-24 02:35:37 +08:00
|
|
|
let init = {};
|
|
|
|
if (window.AbortController) {
|
2020-12-30 22:50:47 +08:00
|
|
|
__themeJson = new AbortController();
|
|
|
|
init.signal = __themeJson.signal;
|
2020-07-24 02:35:37 +08:00
|
|
|
}
|
2020-09-14 18:39:15 +08:00
|
|
|
rl.fetchJSON(url, init)
|
2020-07-24 02:35:37 +08:00
|
|
|
.then(data => {
|
2021-07-22 03:34:17 +08:00
|
|
|
if (2 === arrayLength(data)) {
|
2021-04-28 18:08:07 +08:00
|
|
|
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
|
|
|
}
|
|
|
|
}
|
2021-03-16 17:59:47 +08:00
|
|
|
|
|
|
|
export function addObservablesTo(target, observables) {
|
|
|
|
Object.entries(observables).forEach(([key, value]) =>
|
2021-03-16 23:49:14 +08:00
|
|
|
target[key] = /*isArray(value) ? ko.observableArray(value) :*/ ko.observable(value) );
|
2021-03-16 17:59:47 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
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));
|
|
|
|
}
|