snappymail/dev/Common/Utils.js

1476 lines
32 KiB
JavaScript
Raw Normal View History

2016-06-07 05:57:52 +08:00
import window from 'window';
import $ from '$';
import _ from '_';
import ko from 'ko';
2016-09-07 05:06:38 +08:00
import Autolinker from 'Autolinker';
2019-07-05 03:19:24 +08:00
import { $win, $div, $hcont, dropdownVisibility, data as GlobalsData } from 'Common/Globals';
import { ComposeType, EventKeyCode, SaveSettingsStep, FolderType } from 'Common/Enums';
import { Mime } from 'Common/Mime';
import { jassl } from 'Common/Jassl';
2016-06-07 05:57:52 +08:00
const trim = $.trim;
const inArray = $.inArray;
const isArray = _.isArray;
const isObject = _.isObject;
const isFunc = _.isFunction;
const isUnd = _.isUndefined;
const isNull = _.isNull;
2016-06-16 07:36:44 +08:00
const has = _.has;
const bind = _.bind;
2016-07-02 06:49:59 +08:00
const noop = () => {}; // eslint-disable-line no-empty-function
2016-06-17 07:23:49 +08:00
const noopTrue = () => true;
const noopFalse = () => false;
2016-06-07 05:57:52 +08:00
2019-07-05 03:19:24 +08:00
export { trim, inArray, isArray, isObject, isFunc, isUnd, isNull, has, bind, noop, noopTrue, noopFalse, jassl };
2016-06-07 05:57:52 +08:00
/**
2016-07-02 06:49:59 +08:00
* @param {Function} func
2016-06-07 05:57:52 +08:00
*/
2019-07-05 03:19:24 +08:00
export function silentTryCatch(func) {
2016-06-16 07:36:44 +08:00
try {
2016-07-02 06:49:59 +08:00
func();
2019-07-05 03:19:24 +08:00
} catch (e) {} // eslint-disable-line no-empty
2016-06-07 05:57:52 +08:00
}
/**
* @param {*} value
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 isNormal(value) {
2016-06-07 05:57:52 +08:00
return !isUnd(value) && !isNull(value);
}
/**
* @param {(string|number)} value
* @param {boolean=} includeZero = true
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 isPosNumeric(value, includeZero = true) {
return !isNormal(value)
? false
: includeZero
? /^[0-9]*$/.test(value.toString())
: /^[1-9]+[0-9]*$/.test(value.toString());
2016-06-07 05:57:52 +08:00
}
/**
* @param {*} value
* @param {number=} defaultValur = 0
2016-06-30 08:02:45 +08:00
* @returns {number}
2016-06-07 05:57:52 +08:00
*/
2019-07-05 03:19:24 +08:00
export function pInt(value, defaultValur = 0) {
2016-06-07 05:57:52 +08:00
const result = isNormal(value) && '' !== value ? window.parseInt(value, 10) : defaultValur;
return window.isNaN(result) ? defaultValur : result;
}
/**
* @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) {
2016-06-07 05:57:52 +08:00
return isNormal(value) ? '' + value : '';
}
/**
* @param {*} value
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 pBool(value) {
2016-06-07 05:57:52 +08:00
return !!value;
}
2016-07-16 03:54:37 +08:00
/**
* @param {*} value
* @returns {string}
*/
2019-07-05 03:19:24 +08:00
export function boolToAjax(value) {
2016-07-16 03:54:37 +08:00
return value ? '1' : '0';
}
2016-06-07 05:57:52 +08:00
/**
* @param {*} values
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 isNonEmptyArray(values) {
2016-06-07 05:57:52 +08:00
return isArray(values) && 0 < values.length;
}
/**
* @param {string} component
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 encodeURIComponent(component) {
2016-06-07 05:57:52 +08:00
return window.encodeURIComponent(component);
}
/**
* @param {string} component
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 decodeURIComponent(component) {
2016-06-07 05:57:52 +08:00
return window.decodeURIComponent(component);
}
2016-07-07 07:11:13 +08:00
/**
* @param {string} url
* @returns {string}
*/
2019-07-05 03:19:24 +08:00
export function decodeURI(url) {
2016-07-07 07:11:13 +08:00
return window.decodeURI(url);
}
/**
* @param {string} url
* @returns {string}
*/
2019-07-05 03:19:24 +08:00
export function encodeURI(url) {
2016-07-07 07:11:13 +08:00
return window.encodeURI(url);
}
2016-06-07 05:57:52 +08:00
/**
* @param {string} queryString
2016-06-30 08:02:45 +08:00
* @returns {Object}
2016-06-07 05:57:52 +08:00
*/
2019-07-05 03:19:24 +08:00
export function simpleQueryParser(queryString) {
let index = 0,
2016-07-16 05:29:42 +08:00
len = 0,
temp = null;
2016-06-30 08:02:45 +08:00
2019-07-05 03:19:24 +08:00
const queries = queryString.split('&'),
2016-06-30 08:02:45 +08:00
params = {};
2019-07-05 03:19:24 +08:00
for (len = queries.length; index < len; index++) {
2016-06-07 05:57:52 +08:00
temp = queries[index].split('=');
params[decodeURIComponent(temp[0])] = decodeURIComponent(temp[1]);
}
return params;
}
/**
* @param {number=} len = 32
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 fakeMd5(len = 32) {
const line = '0123456789abcdefghijklmnopqrstuvwxyz',
2016-06-30 08:02:45 +08:00
lineLen = line.length;
2016-06-07 05:57:52 +08:00
len = pInt(len);
let result = '';
2019-07-05 03:19:24 +08:00
while (result.length < len) {
2016-06-07 05:57:52 +08:00
result += line.substr(window.Math.round(window.Math.random() * lineLen), 1);
}
return result;
}
/**
* @param {string} text
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 encodeHtml(text) {
2016-06-07 05:57:52 +08:00
return isNormal(text) ? _.escape(text.toString()) : '';
}
/**
* @param {string} text
* @param {number=} len = 100
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 splitPlainText(text, len = 100) {
let prefix = '',
2016-06-07 05:57:52 +08:00
subText = '',
result = text,
spacePos = 0,
2016-06-30 08:02:45 +08:00
newLinePos = 0;
2016-06-07 05:57:52 +08:00
2019-07-05 03:19:24 +08:00
while (result.length > len) {
2016-06-07 05:57:52 +08:00
subText = result.substring(0, len);
spacePos = subText.lastIndexOf(' ');
newLinePos = subText.lastIndexOf('\n');
2019-07-05 03:19:24 +08:00
if (-1 !== newLinePos) {
2016-06-07 05:57:52 +08:00
spacePos = newLinePos;
}
2019-07-05 03:19:24 +08:00
if (-1 === spacePos) {
2016-06-07 05:57:52 +08:00
spacePos = len;
}
prefix += subText.substring(0, spacePos) + '\n';
result = result.substring(spacePos + 1);
}
return prefix + result;
}
2016-06-30 08:02:45 +08:00
const timeOutAction = (function() {
const timeOuts = {};
2016-06-07 05:57:52 +08:00
return (action, fFunction, timeOut) => {
timeOuts[action] = isUnd(timeOuts[action]) ? 0 : timeOuts[action];
window.clearTimeout(timeOuts[action]);
timeOuts[action] = window.setTimeout(fFunction, timeOut);
};
2019-07-05 03:19:24 +08:00
})();
2016-06-07 05:57:52 +08:00
2016-06-30 08:02:45 +08:00
const timeOutActionSecond = (function() {
const timeOuts = {};
2016-06-07 05:57:52 +08:00
return (action, fFunction, timeOut) => {
2019-07-05 03:19:24 +08:00
if (!timeOuts[action]) {
2016-06-07 05:57:52 +08:00
timeOuts[action] = window.setTimeout(() => {
fFunction();
timeOuts[action] = 0;
}, timeOut);
}
};
2019-07-05 03:19:24 +08:00
})();
2016-06-07 05:57:52 +08:00
2019-07-05 03:19:24 +08:00
export { timeOutAction, timeOutActionSecond };
2016-06-07 05:57:52 +08:00
2019-03-28 06:01:26 +08:00
/**
* @param {any} m
* @returns {any}
*/
export function deModule(m) {
return (m && m.default ? m.default : m) || '';
}
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 {
2019-07-05 03:19:24 +08:00
if (window.document.activeElement) {
if (isUnd(window.document.activeElement.__inFocusCache)) {
window.document.activeElement.__inFocusCache = $(window.document.activeElement).is(
'input,textarea,iframe,.cke_editable'
);
}
2016-06-07 05:57:52 +08:00
return !!window.document.activeElement.__inFocusCache;
}
2019-07-05 03:19:24 +08:00
} catch (e) {} // eslint-disable-line no-empty
2016-06-07 05:57:52 +08:00
return false;
}
2016-06-30 08:02:45 +08:00
/**
* @param {boolean} force
* @returns {void}
*/
2019-07-05 03:19:24 +08:00
export function removeInFocus(force) {
if (window.document && window.document.activeElement && window.document.activeElement.blur) {
2016-06-07 05:57:52 +08:00
try {
const activeEl = $(window.document.activeElement);
2019-07-05 03:19:24 +08:00
if (activeEl && activeEl.is('input,textarea')) {
2016-06-07 05:57:52 +08:00
window.document.activeElement.blur();
2019-07-05 03:19:24 +08:00
} else if (force) {
2016-06-30 08:02:45 +08:00
window.document.activeElement.blur();
2016-06-07 05:57:52 +08:00
}
2019-07-05 03:19:24 +08:00
} catch (e) {} // eslint-disable-line no-empty
2016-06-07 05:57:52 +08:00
}
}
2016-06-30 08:02:45 +08:00
/**
* @returns {void}
*/
2019-07-05 03:19:24 +08:00
export function removeSelection() {
2016-06-07 05:57:52 +08:00
try {
2019-07-05 03:19:24 +08:00
if (window && window.getSelection) {
2016-06-07 05:57:52 +08:00
const sel = window.getSelection();
2019-07-05 03:19:24 +08:00
if (sel && sel.removeAllRanges) {
2016-06-07 05:57:52 +08:00
sel.removeAllRanges();
}
2019-07-05 03:19:24 +08:00
} else if (window.document && window.document.selection && window.document.selection.empty) {
2016-06-07 05:57:52 +08:00
window.document.selection.empty();
}
2019-07-05 03:19:24 +08:00
} catch (e) {} // eslint-disable-line no-empty
2016-06-07 05:57:52 +08:00
}
/**
* @param {string} prefix
* @param {string} subject
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 replySubjectAdd(prefix, subject) {
2016-06-07 05:57:52 +08:00
prefix = trim(prefix.toUpperCase());
subject = trim(subject.replace(/[\s]+/g, ' '));
2016-06-30 08:02:45 +08:00
let drop = false,
2016-06-07 05:57:52 +08:00
re = 'RE' === prefix,
2016-06-30 08:02:45 +08:00
fwd = 'FWD' === prefix;
2019-07-05 03:19:24 +08:00
const parts = [],
2016-06-30 08:02:45 +08:00
prefixIsRe = !fwd;
2016-06-07 05:57:52 +08:00
2019-07-05 03:19:24 +08:00
if ('' !== subject) {
2016-06-07 05:57:52 +08:00
_.each(subject.split(':'), (part) => {
const trimmedPart = trim(part);
2019-07-05 03:19:24 +08:00
if (!drop && (/^(RE|FWD)$/i.test(trimmedPart) || /^(RE|FWD)[[(][\d]+[\])]$/i.test(trimmedPart))) {
if (!re) {
re = !!/^RE/i.test(trimmedPart);
2016-06-07 05:57:52 +08:00
}
2019-07-05 03:19:24 +08:00
if (!fwd) {
fwd = !!/^FWD/i.test(trimmedPart);
2016-06-07 05:57:52 +08:00
}
2019-07-05 03:19:24 +08:00
} else {
2016-06-07 05:57:52 +08:00
parts.push(part);
drop = true;
}
});
}
2019-07-05 03:19:24 +08:00
if (prefixIsRe) {
2016-06-07 05:57:52 +08:00
re = false;
2019-07-05 03:19:24 +08:00
} else {
2016-06-07 05:57:52 +08:00
fwd = false;
}
2019-07-05 03:19:24 +08:00
return trim((prefixIsRe ? 'Re: ' : 'Fwd: ') + (re ? 'Re: ' : '') + (fwd ? 'Fwd: ' : '') + trim(parts.join(':')));
2016-06-07 05:57:52 +08:00
}
/**
* @param {number} num
* @param {number} dec
2016-06-30 08:02:45 +08:00
* @returns {number}
2016-06-07 05:57:52 +08:00
*/
2019-07-05 03:19:24 +08:00
export function roundNumber(num, dec) {
2016-06-07 05:57:52 +08:00
return window.Math.round(num * window.Math.pow(10, dec)) / window.Math.pow(10, dec);
}
/**
* @param {(number|string)} sizeInBytes
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 friendlySize(sizeInBytes) {
2016-06-07 05:57:52 +08:00
sizeInBytes = pInt(sizeInBytes);
2019-07-05 03:19:24 +08:00
switch (true) {
2016-06-30 08:02:45 +08:00
case 1073741824 <= sizeInBytes:
return roundNumber(sizeInBytes / 1073741824, 1) + 'GB';
case 1048576 <= sizeInBytes:
return roundNumber(sizeInBytes / 1048576, 1) + 'MB';
case 1024 <= sizeInBytes:
return roundNumber(sizeInBytes / 1024, 0) + 'KB';
// no default
2016-06-07 05:57:52 +08:00
}
return sizeInBytes + 'B';
}
/**
* @param {string} desc
*/
2019-07-05 03:19:24 +08:00
export function log(desc) {
if (window.console && window.console.log) {
2016-06-07 05:57:52 +08:00
window.console.log(desc);
}
}
/**
* @param {?} object
* @param {string} methodName
* @param {Array=} params
* @param {number=} delay = 0
*/
2019-07-05 03:19:24 +08:00
export function delegateRun(object, methodName, params, delay = 0) {
if (object && object[methodName]) {
2016-06-07 05:57:52 +08:00
delay = pInt(delay);
2016-07-02 06:49:59 +08:00
params = isArray(params) ? params : [];
2019-07-05 03:19:24 +08:00
if (0 >= delay) {
2016-07-02 06:49:59 +08:00
object[methodName](...params);
2019-07-05 03:19:24 +08:00
} else {
2016-06-07 05:57:52 +08:00
_.delay(() => {
2016-07-02 06:49:59 +08:00
object[methodName](...params);
2016-06-07 05:57:52 +08:00
}, delay);
}
}
}
/**
* @param {?} event
*/
2019-07-05 03:19:24 +08:00
export function killCtrlACtrlS(event) {
2016-06-07 05:57:52 +08:00
event = event || window.event;
2019-07-05 03:19:24 +08:00
if (event && event.ctrlKey && !event.shiftKey && !event.altKey) {
2016-06-07 05:57:52 +08:00
const key = event.keyCode || event.which;
2019-07-05 03:19:24 +08:00
if (key === EventKeyCode.S) {
2016-06-07 05:57:52 +08:00
event.preventDefault();
return;
2019-07-05 03:19:24 +08:00
} else if (key === EventKeyCode.A) {
2016-06-07 05:57:52 +08:00
const sender = event.target || event.srcElement;
2019-07-05 03:19:24 +08:00
if (
sender &&
('true' === '' + sender.contentEditable || (sender.tagName && sender.tagName.match(/INPUT|TEXTAREA/i)))
) {
2016-06-07 05:57:52 +08:00
return;
}
2019-07-05 03:19:24 +08:00
if (window.getSelection) {
2016-06-07 05:57:52 +08:00
window.getSelection().removeAllRanges();
2019-07-05 03:19:24 +08:00
} else if (window.document.selection && window.document.selection.clear) {
2016-06-07 05:57:52 +08:00
window.document.selection.clear();
}
event.preventDefault();
}
}
}
/**
* @param {(Object|null|undefined)} context
* @param {Function} fExecute
* @param {(Function|boolean|null)=} fCanExecute = true
2016-06-30 08:02:45 +08:00
* @returns {Function}
2016-06-07 05:57:52 +08:00
*/
2019-07-05 03:19:24 +08:00
export function createCommandLegacy(context, fExecute, fCanExecute = true) {
2016-06-30 08:02:45 +08:00
let fResult = null;
const fNonEmpty = (...args) => {
2019-07-05 03:19:24 +08:00
if (fResult && fResult.canExecute && fResult.canExecute()) {
2016-06-30 08:02:45 +08:00
fExecute.apply(context, args);
2016-06-07 05:57:52 +08:00
}
2016-06-30 08:02:45 +08:00
return false;
};
2016-06-07 05:57:52 +08:00
fResult = fExecute ? fNonEmpty : noop;
fResult.enabled = ko.observable(true);
fResult.isCommand = true;
2016-06-07 05:57:52 +08:00
2019-07-05 03:19:24 +08:00
if (isFunc(fCanExecute)) {
2016-09-10 06:38:16 +08:00
fResult.canExecute = ko.computed(() => fResult && fResult.enabled() && fCanExecute.call(context));
2019-07-05 03:19:24 +08:00
} else {
2016-09-10 06:38:16 +08:00
fResult.canExecute = ko.computed(() => fResult && fResult.enabled() && !!fCanExecute);
2016-06-07 05:57:52 +08:00
}
return fResult;
}
/**
* @param {string} theme
2016-06-30 08:02:45 +08:00
* @returns {string}
2016-06-07 05:57:52 +08:00
*/
export const convertThemeName = _.memoize((theme) => {
2019-07-05 03:19:24 +08:00
if ('@custom' === theme.substr(-7)) {
2016-06-07 05:57:52 +08:00
theme = trim(theme.substring(0, theme.length - 7));
}
2019-07-05 03:19:24 +08:00
return trim(
theme
.replace(/[^a-zA-Z0-9]+/g, ' ')
.replace(/([A-Z])/g, ' $1')
.replace(/[\s]+/g, ' ')
);
2016-06-07 05:57:52 +08:00
});
/**
* @param {string} name
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 quoteName(name) {
2016-06-07 05:57:52 +08:00
return name.replace(/["]/g, '\\"');
}
/**
2016-06-30 08:02:45 +08:00
* @returns {number}
2016-06-07 05:57:52 +08:00
*/
2019-07-05 03:19:24 +08:00
export function microtime() {
return new window.Date().getTime();
2016-06-07 05:57:52 +08:00
}
/**
2016-06-30 08:02:45 +08:00
* @returns {number}
2016-06-07 05:57:52 +08:00
*/
2019-07-05 03:19:24 +08:00
export function timestamp() {
2016-06-07 05:57:52 +08:00
return window.Math.round(microtime() / 1000);
}
/**
*
* @param {string} language
* @param {boolean=} isEng = false
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 convertLangName(language, isEng = false) {
return require('Common/Translator').i18n(
'LANGS_NAMES' + (true === isEng ? '_EN' : '') + '/LANG_' + language.toUpperCase().replace(/[^a-zA-Z0-9]+/g, '_'),
null,
language
);
2016-06-07 05:57:52 +08:00
}
2016-06-30 08:02:45 +08:00
/**
* @returns {object}
*/
2019-07-05 03:19:24 +08:00
export function draggablePlace() {
return $(
'<div class="draggablePlace">' +
'<span class="text"></span>&nbsp;' +
'<i class="icon-copy icon-white visible-on-ctrl"></i>' +
'<i class="icon-mail icon-white hidden-on-ctrl"></i>' +
'</div>'
).appendTo('#rl-hidden');
2016-06-07 05:57:52 +08:00
}
2016-06-30 08:02:45 +08:00
/**
* @param {object} domOption
* @param {object} item
* @returns {void}
*/
2019-07-05 03:19:24 +08:00
export function defautOptionsAfterRender(domItem, item) {
if (item && !isUnd(item.disabled) && domItem) {
2016-06-30 08:02:45 +08:00
$(domItem)
2016-06-07 05:57:52 +08:00
.toggleClass('disabled', item.disabled)
2016-06-30 08:02:45 +08:00
.prop('disabled', item.disabled);
2016-06-07 05:57:52 +08:00
}
}
2016-06-16 07:36:44 +08:00
/**
* @param {string} title
* @param {Object} body
* @param {boolean} isHtml
* @param {boolean} print
*/
2019-07-05 03:19:24 +08:00
export function clearBqSwitcher(body) {
2016-06-16 07:36:44 +08:00
body.find('blockquote.rl-bq-switcher').removeClass('rl-bq-switcher hidden-bq');
2019-07-05 03:19:24 +08:00
body
.find('.rlBlockquoteSwitcher')
.off('.rlBlockquoteSwitcher')
.remove();
2016-06-16 07:36:44 +08:00
body.find('[data-html-editor-font-wrapper]').removeAttr('data-html-editor-font-wrapper');
}
/**
* @param {object} messageData
2016-06-16 07:36:44 +08:00
* @param {Object} body
* @param {boolean} isHtml
* @param {boolean} print
* @returns {void}
2016-06-16 07:36:44 +08:00
*/
2019-07-05 03:19:24 +08:00
export function previewMessage(
{ title, subject, date, fromCreds, toCreds, toLabel, ccClass, ccCreds, ccLabel },
body,
isHtml,
print
) {
const win = window.open(''),
2016-06-16 07:36:44 +08:00
doc = win.document,
bodyClone = body.clone(),
2016-06-30 08:02:45 +08:00
bodyClass = isHtml ? 'html' : 'plain';
2016-06-16 07:36:44 +08:00
clearBqSwitcher(bodyClone);
const html = bodyClone ? bodyClone.html() : '';
2019-07-05 03:19:24 +08:00
doc.write(
deModule(require('Html/PreviewMessage.html'))
.replace('{{title}}', encodeHtml(title))
.replace('{{subject}}', encodeHtml(subject))
.replace('{{date}}', encodeHtml(date))
.replace('{{fromCreds}}', encodeHtml(fromCreds))
.replace('{{toCreds}}', encodeHtml(toCreds))
.replace('{{toLabel}}', encodeHtml(toLabel))
.replace('{{ccClass}}', encodeHtml(ccClass))
.replace('{{ccCreds}}', encodeHtml(ccCreds))
.replace('{{ccLabel}}', encodeHtml(ccLabel))
.replace('{{bodyClass}}', bodyClass)
.replace('{{html}}', html)
);
2016-06-16 07:36:44 +08:00
doc.close();
2019-07-05 03:19:24 +08:00
if (print) {
2016-06-16 07:36:44 +08:00
window.setTimeout(() => win.print(), 100);
}
}
2016-06-07 05:57:52 +08:00
/**
* @param {Function} fCallback
* @param {?} koTrigger
* @param {?} context = null
* @param {number=} timer = 1000
2016-06-30 08:02:45 +08:00
* @returns {Function}
2016-06-07 05:57:52 +08:00
*/
2019-07-05 03:19:24 +08:00
export function settingsSaveHelperFunction(fCallback, koTrigger, context = null, timer = 1000) {
2016-06-07 05:57:52 +08:00
timer = pInt(timer);
return (type, data, cached, requestAction, requestParameters) => {
koTrigger.call(context, data && data.Result ? SaveSettingsStep.TrueResult : SaveSettingsStep.FalseResult);
2019-07-05 03:19:24 +08:00
if (fCallback) {
2016-06-07 05:57:52 +08:00
fCallback.call(context, type, data, cached, requestAction, requestParameters);
}
_.delay(() => {
koTrigger.call(context, SaveSettingsStep.Idle);
}, timer);
};
}
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) {
2016-06-07 05:57:52 +08:00
return settingsSaveHelperFunction(null, koTrigger, context, 1000);
}
2016-06-30 08:02:45 +08:00
/**
* @param {object} remote
* @param {string} settingName
* @param {string} type
* @param {function} fTriggerFunction
* @returns {function}
*/
2019-07-05 03:19:24 +08:00
export function settingsSaveHelperSubscribeFunction(remote, settingName, type, fTriggerFunction) {
2016-06-07 05:57:52 +08:00
return (value) => {
2019-07-05 03:19:24 +08:00
if (remote) {
switch (type) {
2016-06-07 05:57:52 +08:00
case 'bool':
case 'boolean':
value = value ? '1' : '0';
break;
case 'int':
case 'integer':
case 'number':
value = pInt(value);
break;
case 'trim':
value = trim(value);
break;
2016-06-30 08:02:45 +08:00
default:
value = pString(value);
break;
2016-06-07 05:57:52 +08:00
}
2016-06-30 08:02:45 +08:00
const data = {};
2016-06-07 05:57:52 +08:00
data[settingName] = value;
2019-07-05 03:19:24 +08:00
if (remote.saveAdminConfig) {
2016-06-07 05:57:52 +08:00
remote.saveAdminConfig(fTriggerFunction || null, data);
2019-07-05 03:19:24 +08:00
} else if (remote.saveSettings) {
2016-06-07 05:57:52 +08:00
remote.saveSettings(fTriggerFunction || null, data);
}
}
};
}
/**
* @param {string} html
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 findEmailAndLinks(html) {
return Autolinker
? Autolinker.link(html, {
newWindow: true,
stripPrefix: false,
urls: true,
email: true,
mention: false,
phone: false,
hashtag: false,
replaceFn: function(match) {
return !(match && 'url' === match.getType() && match.matchedText && 0 !== match.matchedText.indexOf('http'));
}
})
: html;
2016-06-07 05:57:52 +08:00
}
/**
* @param {string} html
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 htmlToPlain(html) {
let pos = 0,
limit = 0,
2016-06-07 05:57:52 +08:00
iP1 = 0,
iP2 = 0,
iP3 = 0,
2016-06-30 08:02:45 +08:00
text = '';
const convertBlockquote = (blockquoteText) => {
blockquoteText = '> ' + trim(blockquoteText).replace(/\n/gm, '\n> ');
2019-07-05 03:19:24 +08:00
return blockquoteText.replace(/(^|\n)([> ]+)/gm, (...args) =>
args && 2 < args.length ? args[1] + trim(args[2].replace(/[\s]/g, '')) + ' ' : ''
);
};
2016-06-07 05:57:52 +08:00
const convertDivs = (...args) => {
2019-07-05 03:19:24 +08:00
if (args && 1 < args.length) {
let divText = trim(args[1]);
2019-07-05 03:19:24 +08:00
if (0 < divText.length) {
divText = divText.replace(/<div[^>]*>([\s\S\r\n]*)<\/div>/gim, convertDivs);
divText = '\n' + trim(divText) + '\n';
2016-06-07 05:57:52 +08:00
}
return divText;
}
return '';
};
2019-07-05 03:19:24 +08:00
const convertPre = (...args) =>
args && 1 < args.length
? args[1]
.toString()
.replace(/[\n]/gm, '<br />')
.replace(/[\r]/gm, '')
: '',
2016-07-02 06:49:59 +08:00
fixAttibuteValue = (...args) => (args && 1 < args.length ? '' + args[1] + _.escape(args[2]) : ''),
convertLinks = (...args) => (args && 1 < args.length ? trim(args[1]) : '');
2016-06-07 05:57:52 +08:00
text = html
.replace(/<p[^>]*><\/p>/gi, '')
2019-07-05 03:19:24 +08:00
.replace(/<pre[^>]*>([\s\S\r\n\t]*)<\/pre>/gim, convertPre)
2016-06-07 05:57:52 +08:00
.replace(/[\s]+/gm, ' ')
2019-07-05 03:19:24 +08:00
.replace(/((?:href|data)\s?=\s?)("[^"]+?"|'[^']+?')/gim, fixAttibuteValue)
.replace(/<br[^>]*>/gim, '\n')
2016-06-07 05:57:52 +08:00
.replace(/<\/h[\d]>/gi, '\n')
.replace(/<\/p>/gi, '\n\n')
2019-07-05 03:19:24 +08:00
.replace(/<ul[^>]*>/gim, '\n')
2016-06-07 05:57:52 +08:00
.replace(/<\/ul>/gi, '\n')
2019-07-05 03:19:24 +08:00
.replace(/<li[^>]*>/gim, ' * ')
2016-06-07 05:57:52 +08:00
.replace(/<\/li>/gi, '\n')
.replace(/<\/td>/gi, '\n')
.replace(/<\/tr>/gi, '\n')
2019-07-05 03:19:24 +08:00
.replace(/<hr[^>]*>/gim, '\n_______________________________\n\n')
.replace(/<div[^>]*>([\s\S\r\n]*)<\/div>/gim, convertDivs)
.replace(/<blockquote[^>]*>/gim, '\n__bq__start__\n')
.replace(/<\/blockquote>/gim, '\n__bq__end__\n')
.replace(/<a [^>]*>([\s\S\r\n]*?)<\/a>/gim, convertLinks)
2016-06-07 05:57:52 +08:00
.replace(/<\/div>/gi, '\n')
.replace(/&nbsp;/gi, ' ')
.replace(/&quot;/gi, '"')
2016-06-30 08:02:45 +08:00
.replace(/<[^>]*>/gm, '');
2016-06-07 05:57:52 +08:00
text = $div.html(text).text();
text = text
.replace(/\n[ \t]+/gm, '\n')
.replace(/[\n]{3,}/gm, '\n\n')
.replace(/&gt;/gi, '>')
.replace(/&lt;/gi, '<')
2016-06-30 08:02:45 +08:00
.replace(/&amp;/gi, '&');
2016-06-07 05:57:52 +08:00
2017-07-06 06:31:41 +08:00
text = splitPlainText(text);
2016-06-07 05:57:52 +08:00
pos = 0;
limit = 800;
2016-06-07 05:57:52 +08:00
2019-07-05 03:19:24 +08:00
while (0 < limit) {
2016-06-30 08:02:45 +08:00
limit -= 1;
iP1 = text.indexOf('__bq__start__', pos);
2019-07-05 03:19:24 +08:00
if (-1 < iP1) {
2016-06-07 05:57:52 +08:00
iP2 = text.indexOf('__bq__start__', iP1 + 5);
iP3 = text.indexOf('__bq__end__', iP1 + 5);
2019-07-05 03:19:24 +08:00
if ((-1 === iP2 || iP3 < iP2) && iP1 < iP3) {
text = text.substring(0, iP1) + convertBlockquote(text.substring(iP1 + 13, iP3)) + text.substring(iP3 + 11);
2016-06-07 05:57:52 +08:00
pos = 0;
2019-07-05 03:19:24 +08:00
} else if (-1 < iP2 && iP2 < iP3) {
pos = iP2 - 1;
2019-07-05 03:19:24 +08:00
} else {
pos = 0;
2016-06-07 05:57:52 +08:00
}
2019-07-05 03:19:24 +08:00
} else {
2016-06-07 05:57:52 +08:00
break;
}
}
2019-07-05 03:19:24 +08:00
text = text.replace(/__bq__start__/gm, '').replace(/__bq__end__/gm, '');
2016-06-07 05:57:52 +08:00
return text;
}
/**
* @param {string} plain
* @param {boolean} findEmailAndLinksInText = false
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 plainToHtml(plain, findEmailAndLinksInText = false) {
2016-06-07 05:57:52 +08:00
plain = plain.toString().replace(/\r/g, '');
2016-07-02 06:49:59 +08:00
plain = plain.replace(/^>[> ]>+/gm, ([match]) => (match ? match.replace(/[ ]+/g, '') : match));
2019-07-05 03:19:24 +08:00
let bIn = false,
2016-06-07 05:57:52 +08:00
bDo = true,
bStart = true,
aNextText = [],
sLine = '',
iIndex = 0,
2016-06-30 08:02:45 +08:00
aText = plain.split('\n');
2016-06-07 05:57:52 +08:00
2019-07-05 03:19:24 +08:00
do {
2016-06-07 05:57:52 +08:00
bDo = false;
aNextText = [];
2019-07-05 03:19:24 +08:00
for (iIndex = 0; iIndex < aText.length; iIndex++) {
2016-06-07 05:57:52 +08:00
sLine = aText[iIndex];
bStart = '>' === sLine.substr(0, 1);
2019-07-05 03:19:24 +08:00
if (bStart && !bIn) {
2016-06-07 05:57:52 +08:00
bDo = true;
bIn = true;
aNextText.push('~~~blockquote~~~');
aNextText.push(sLine.substr(1));
2019-07-05 03:19:24 +08:00
} else if (!bStart && bIn) {
if ('' !== sLine) {
2016-06-07 05:57:52 +08:00
bIn = false;
aNextText.push('~~~/blockquote~~~');
aNextText.push(sLine);
2019-07-05 03:19:24 +08:00
} else {
2016-06-07 05:57:52 +08:00
aNextText.push(sLine);
}
2019-07-05 03:19:24 +08:00
} else if (bStart && bIn) {
2016-06-07 05:57:52 +08:00
aNextText.push(sLine.substr(1));
2019-07-05 03:19:24 +08:00
} else {
2016-06-07 05:57:52 +08:00
aNextText.push(sLine);
}
}
2019-07-05 03:19:24 +08:00
if (bIn) {
2016-06-07 05:57:52 +08:00
bIn = false;
aNextText.push('~~~/blockquote~~~');
}
aText = aNextText;
2019-07-05 03:19:24 +08:00
} while (bDo);
2016-06-07 05:57:52 +08:00
plain = aText.join('\n');
plain = plain
// .replace(/~~~\/blockquote~~~\n~~~blockquote~~~/g, '\n')
2016-06-07 05:57:52 +08:00
.replace(/&/g, '&amp;')
2019-07-05 03:19:24 +08:00
.replace(/>/g, '&gt;')
.replace(/</g, '&lt;')
2016-06-07 05:57:52 +08:00
.replace(/~~~blockquote~~~[\s]*/g, '<blockquote>')
.replace(/[\s]*~~~\/blockquote~~~/g, '</blockquote>')
2016-06-30 08:02:45 +08:00
.replace(/\n/g, '<br />');
2016-06-07 05:57:52 +08:00
return findEmailAndLinksInText ? findEmailAndLinks(plain) : plain;
}
2016-07-16 05:29:42 +08:00
window['rainloop_Utils_htmlToPlain'] = htmlToPlain; // eslint-disable-line dot-notation
window['rainloop_Utils_plainToHtml'] = plainToHtml; // eslint-disable-line dot-notation
2016-06-07 05:57:52 +08:00
/**
* @param {Array} aSystem
* @param {Array} aList
* @param {Array=} aDisabled
* @param {Array=} aHeaderLines
* @param {?number=} iUnDeep
* @param {Function=} fDisableCallback
* @param {Function=} fVisibleCallback
* @param {Function=} fRenameCallback
* @param {boolean=} bSystem
* @param {boolean=} bBuildUnvisible
2016-06-30 08:02:45 +08:00
* @returns {Array}
2016-06-07 05:57:52 +08:00
*/
2019-07-05 03:19:24 +08:00
export function folderListOptionsBuilder(
aSystem,
aList,
aDisabled,
aHeaderLines,
iUnDeep,
fDisableCallback,
fVisibleCallback,
fRenameCallback,
bSystem,
bBuildUnvisible
) {
let /**
2016-06-07 05:57:52 +08:00
* @type {?FolderModel}
*/
oItem = null,
bSep = false,
iIndex = 0,
iLen = 0,
2016-06-30 08:02:45 +08:00
aResult = [];
const sDeepPrefix = '\u00A0\u00A0\u00A0';
2016-06-07 05:57:52 +08:00
bBuildUnvisible = isUnd(bBuildUnvisible) ? false : !!bBuildUnvisible;
bSystem = !isNormal(bSystem) ? 0 < aSystem.length : bSystem;
iUnDeep = !isNormal(iUnDeep) ? 0 : iUnDeep;
fDisableCallback = isNormal(fDisableCallback) ? fDisableCallback : null;
fVisibleCallback = isNormal(fVisibleCallback) ? fVisibleCallback : null;
fRenameCallback = isNormal(fRenameCallback) ? fRenameCallback : null;
2019-07-05 03:19:24 +08:00
if (!isArray(aDisabled)) {
2016-06-07 05:57:52 +08:00
aDisabled = [];
}
2019-07-05 03:19:24 +08:00
if (!isArray(aHeaderLines)) {
2016-06-07 05:57:52 +08:00
aHeaderLines = [];
}
2019-07-05 03:19:24 +08:00
for (iIndex = 0, iLen = aHeaderLines.length; iIndex < iLen; iIndex++) {
2016-06-07 05:57:52 +08:00
aResult.push({
id: aHeaderLines[iIndex][0],
name: aHeaderLines[iIndex][1],
system: false,
seporator: false,
disabled: false
});
}
bSep = true;
2019-07-05 03:19:24 +08:00
for (iIndex = 0, iLen = aSystem.length; iIndex < iLen; iIndex++) {
2016-06-07 05:57:52 +08:00
oItem = aSystem[iIndex];
2019-07-05 03:19:24 +08:00
if (fVisibleCallback ? fVisibleCallback(oItem) : true) {
if (bSep && 0 < aResult.length) {
2016-06-07 05:57:52 +08:00
aResult.push({
id: '---',
name: '---',
system: false,
seporator: true,
disabled: true
});
}
bSep = false;
aResult.push({
id: oItem.fullNameRaw,
2016-07-02 06:49:59 +08:00
name: fRenameCallback ? fRenameCallback(oItem) : oItem.name(),
2016-06-07 05:57:52 +08:00
system: true,
seporator: false,
2019-07-05 03:19:24 +08:00
disabled:
!oItem.selectable ||
-1 < inArray(oItem.fullNameRaw, aDisabled) ||
2016-07-02 06:49:59 +08:00
(fDisableCallback ? fDisableCallback(oItem) : false)
2016-06-07 05:57:52 +08:00
});
}
}
bSep = true;
2019-07-05 03:19:24 +08:00
for (iIndex = 0, iLen = aList.length; iIndex < iLen; iIndex++) {
2016-06-07 05:57:52 +08:00
oItem = aList[iIndex];
// if (oItem.subScribed() || !oItem.existen || bBuildUnvisible)
2019-07-05 03:19:24 +08:00
if (
(oItem.subScribed() || !oItem.existen || bBuildUnvisible) &&
(oItem.selectable || oItem.hasSubScribedSubfolders())
) {
if (fVisibleCallback ? fVisibleCallback(oItem) : true) {
if (FolderType.User === oItem.type() || !bSystem || oItem.hasSubScribedSubfolders()) {
if (bSep && 0 < aResult.length) {
2016-06-07 05:57:52 +08:00
aResult.push({
id: '---',
name: '---',
system: false,
seporator: true,
disabled: true
});
}
bSep = false;
aResult.push({
id: oItem.fullNameRaw,
2019-07-05 03:19:24 +08:00
name:
new window.Array(oItem.deep + 1 - iUnDeep).join(sDeepPrefix) +
2016-07-02 06:49:59 +08:00
(fRenameCallback ? fRenameCallback(oItem) : oItem.name()),
2016-06-07 05:57:52 +08:00
system: false,
seporator: false,
2019-07-05 03:19:24 +08:00
disabled:
!oItem.selectable ||
-1 < inArray(oItem.fullNameRaw, aDisabled) ||
2016-07-02 06:49:59 +08:00
(fDisableCallback ? fDisableCallback(oItem) : false)
2016-06-07 05:57:52 +08:00
});
}
}
}
2019-07-05 03:19:24 +08:00
if (oItem.subScribed() && 0 < oItem.subFolders().length) {
aResult = aResult.concat(
folderListOptionsBuilder(
[],
oItem.subFolders(),
aDisabled,
[],
iUnDeep,
fDisableCallback,
fVisibleCallback,
fRenameCallback,
bSystem,
bBuildUnvisible
)
);
2016-06-07 05:57:52 +08:00
}
}
return aResult;
}
2016-06-30 08:02:45 +08:00
/**
* @param {object} element
* @returns {void}
*/
2019-07-05 03:19:24 +08:00
export function selectElement(element) {
let sel = null,
2016-07-16 05:29:42 +08:00
range = null;
2019-07-05 03:19:24 +08:00
if (window.getSelection) {
2016-06-07 05:57:52 +08:00
sel = window.getSelection();
sel.removeAllRanges();
range = window.document.createRange();
range.selectNodeContents(element);
sel.addRange(range);
2019-07-05 03:19:24 +08:00
} else if (window.document.selection) {
2016-06-07 05:57:52 +08:00
range = window.document.body.createTextRange();
range.moveToElementText(element);
range.select();
}
}
export const detectDropdownVisibility = _.debounce(() => {
2016-07-02 06:49:59 +08:00
dropdownVisibility(!!_.find(GlobalsData.aBootstrapDropdowns, (item) => item.hasClass('open')));
2016-06-07 05:57:52 +08:00
}, 50);
/**
* @param {boolean=} delay = false
*/
export function triggerAutocompleteInputChange(delay = false) {
const fFunc = () => {
$('.checkAutocomplete').trigger('change');
};
2019-07-05 03:19:24 +08:00
if (delay) {
2016-06-07 05:57:52 +08:00
_.delay(fFunc, 100);
2019-07-05 03:19:24 +08:00
} else {
2016-06-07 05:57:52 +08:00
fFunc();
}
}
2016-06-30 08:02:45 +08:00
const configurationScriptTagCache = {};
2016-06-07 05:57:52 +08:00
/**
* @param {string} configuration
2016-06-30 08:02:45 +08:00
* @returns {object}
2016-06-07 05:57:52 +08:00
*/
2019-07-05 03:19:24 +08:00
export function getConfigurationFromScriptTag(configuration) {
if (!configurationScriptTagCache[configuration]) {
configurationScriptTagCache[configuration] = $(
'script[type="application/json"][data-configuration="' + configuration + '"]'
);
2016-06-07 05:57:52 +08:00
}
2019-07-05 03:19:24 +08:00
try {
2016-06-30 08:02:45 +08:00
return JSON.parse(configurationScriptTagCache[configuration].text());
2019-07-05 03:19:24 +08:00
} catch (e) {} // eslint-disable-line no-empty
2016-06-07 05:57:52 +08:00
2016-06-30 08:02:45 +08:00
return {};
2016-06-07 05:57:52 +08:00
}
/**
* @param {mixed} mPropOrValue
* @param {mixed} value
*/
2019-07-05 03:19:24 +08:00
export function disposeOne(propOrValue, value) {
2016-06-07 05:57:52 +08:00
const disposable = value || propOrValue;
2019-07-05 03:19:24 +08:00
if (disposable && 'function' === typeof disposable.dispose) {
2016-06-07 05:57:52 +08:00
disposable.dispose();
}
}
/**
* @param {Object} object
*/
2019-07-05 03:19:24 +08:00
export function disposeObject(object) {
if (object) {
if (isArray(object.disposables)) {
2016-06-07 05:57:52 +08:00
_.each(object.disposables, disposeOne);
}
ko.utils.objectForEach(object, disposeOne);
}
}
/**
* @param {Object|Array} objectOrObjects
2016-06-30 08:02:45 +08:00
* @returns {void}
2016-06-07 05:57:52 +08:00
*/
2019-07-05 03:19:24 +08:00
export function delegateRunOnDestroy(objectOrObjects) {
if (objectOrObjects) {
if (isArray(objectOrObjects)) {
2016-06-10 00:46:03 +08:00
_.each(objectOrObjects, (item) => {
2016-06-07 05:57:52 +08:00
delegateRunOnDestroy(item);
});
2019-07-05 03:19:24 +08:00
} else if (objectOrObjects && objectOrObjects.onDestroy) {
2016-06-07 05:57:52 +08:00
objectOrObjects.onDestroy();
}
}
}
2016-06-30 08:02:45 +08:00
/**
* @param {object} $styleTag
* @param {string} css
* @returns {boolean}
*/
2019-07-05 03:19:24 +08:00
export function appendStyles($styleTag, css) {
if ($styleTag && $styleTag[0]) {
if ($styleTag[0].styleSheet && !isUnd($styleTag[0].styleSheet.cssText)) {
2016-06-07 05:57:52 +08:00
$styleTag[0].styleSheet.cssText = css;
2019-07-05 03:19:24 +08:00
} else {
2016-06-07 05:57:52 +08:00
$styleTag.text(css);
}
return true;
}
return false;
}
2019-07-05 03:19:24 +08:00
let __themeTimer = 0,
2016-06-30 08:02:45 +08:00
__themeAjax = 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}
*/
2019-07-05 03:19:24 +08:00
export function changeTheme(value, themeTrigger = noop) {
const themeLink = $('#app-theme-link'),
clearTimer = () => {
__themeTimer = window.setTimeout(() => themeTrigger(SaveSettingsStep.Idle), 1000);
__themeAjax = null;
2016-06-30 08:02:45 +08:00
};
2019-07-05 03:19:24 +08:00
let themeStyle = $('#app-theme-style'),
2016-06-30 08:02:45 +08:00
url = themeLink.attr('href');
2016-06-07 05:57:52 +08:00
2019-07-05 03:19:24 +08:00
if (!url) {
2016-06-07 05:57:52 +08:00
url = themeStyle.attr('data-href');
}
2019-07-05 03:19:24 +08:00
if (url) {
url = url.toString().replace(/\/-\/[^/]+\/-\//, '/-/' + value + '/-/');
url = url.replace(/\/Css\/[^/]+\/User\//, '/Css/0/User/');
url = url.replace(/\/Hash\/[^/]+\//, '/Hash/-/');
2016-06-07 05:57:52 +08:00
2019-07-05 03:19:24 +08:00
if ('Json/' !== url.substring(url.length - 5, url.length)) {
2016-06-07 05:57:52 +08:00
url += 'Json/';
}
window.clearTimeout(__themeTimer);
2016-08-22 05:30:34 +08:00
2016-06-07 05:57:52 +08:00
themeTrigger(SaveSettingsStep.Animate);
2019-07-05 03:19:24 +08:00
if (__themeAjax && __themeAjax.abort) {
2016-06-07 05:57:52 +08:00
__themeAjax.abort();
}
__themeAjax = $.ajax({
url: url,
dataType: 'json'
2019-07-05 03:19:24 +08:00
})
.then((data) => {
if (data && isArray(data) && 2 === data.length) {
if (themeLink && themeLink[0] && (!themeStyle || !themeStyle[0])) {
themeStyle = $('<style id="app-theme-style"></style>');
themeLink.after(themeStyle);
themeLink.remove();
2016-06-07 05:57:52 +08:00
}
2019-07-05 03:19:24 +08:00
if (themeStyle && themeStyle[0]) {
if (appendStyles(themeStyle, data[1])) {
themeStyle.attr('data-href', url).attr('data-theme', data[0]);
}
}
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
}
}
2016-06-30 08:02:45 +08:00
/**
* @returns {function}
*/
2019-07-05 03:19:24 +08:00
export function computedPagenatorHelper(koCurrentPage, koPageCount) {
2016-06-28 02:14:33 +08:00
return () => {
2019-07-05 03:19:24 +08:00
const currentPage = koCurrentPage(),
2016-06-28 02:14:33 +08:00
pageCount = koPageCount(),
result = [],
fAdd = (index, push = true, customName = '') => {
const data = {
current: index === currentPage,
name: '' === customName ? index.toString() : customName.toString(),
2016-06-30 08:02:45 +08:00
custom: '' !== customName,
2016-06-28 02:14:33 +08:00
title: '' === customName ? '' : index.toString(),
value: index.toString()
};
2019-07-05 03:19:24 +08:00
if (push) {
2016-06-28 02:14:33 +08:00
result.push(data);
2019-07-05 03:19:24 +08:00
} else {
2016-06-28 02:14:33 +08:00
result.unshift(data);
}
2016-06-30 08:02:45 +08:00
};
2016-06-28 02:14:33 +08:00
2019-07-05 03:19:24 +08:00
let prev = 0,
2016-06-28 02:14:33 +08:00
next = 0,
2016-06-30 08:02:45 +08:00
limit = 2;
2016-06-28 02:14:33 +08:00
2019-07-05 03:19:24 +08:00
if (1 < pageCount || (0 < pageCount && pageCount < currentPage)) {
if (pageCount < currentPage) {
2016-06-28 02:14:33 +08:00
fAdd(pageCount);
prev = pageCount;
next = pageCount;
2019-07-05 03:19:24 +08:00
} else {
if (3 >= currentPage || pageCount - 2 <= currentPage) {
2016-06-28 02:14:33 +08:00
limit += 2;
}
fAdd(currentPage);
prev = currentPage;
next = currentPage;
}
while (0 < limit) {
prev -= 1;
next += 1;
2019-07-05 03:19:24 +08:00
if (0 < prev) {
2016-06-28 02:14:33 +08:00
fAdd(prev, false);
2016-06-30 08:02:45 +08:00
limit -= 1;
2016-06-28 02:14:33 +08:00
}
2019-07-05 03:19:24 +08:00
if (pageCount >= next) {
2016-06-28 02:14:33 +08:00
fAdd(next, true);
2016-06-30 08:02:45 +08:00
limit -= 1;
2019-07-05 03:19:24 +08:00
} else if (0 >= prev) {
2016-06-28 02:14:33 +08:00
break;
}
}
2019-07-05 03:19:24 +08:00
if (3 === prev) {
2016-06-28 02:14:33 +08:00
fAdd(2, false);
2019-07-05 03:19:24 +08:00
} else if (3 < prev) {
2016-06-28 02:14:33 +08:00
fAdd(Math.round((prev - 1) / 2), false, '...');
}
2019-07-05 03:19:24 +08:00
if (pageCount - 2 === next) {
2016-06-28 02:14:33 +08:00
fAdd(pageCount - 1, true);
2019-07-05 03:19:24 +08:00
} else if (pageCount - 2 > next) {
2016-06-28 02:14:33 +08:00
fAdd(Math.round((pageCount + next) / 2), true, '...');
}
// first and last
2019-07-05 03:19:24 +08:00
if (1 < prev) {
2016-06-28 02:14:33 +08:00
fAdd(1, false);
}
2019-07-05 03:19:24 +08:00
if (pageCount > next) {
2016-06-28 02:14:33 +08:00
fAdd(pageCount, true);
}
}
return result;
};
}
/**
* @param {string} fileName
2016-06-30 08:02:45 +08:00
* @returns {string}
2016-06-28 02:14:33 +08:00
*/
2019-07-05 03:19:24 +08:00
export function getFileExtension(fileName) {
2016-06-28 02:14:33 +08:00
fileName = trim(fileName).toLowerCase();
const result = fileName.split('.').pop();
return result === fileName ? '' : result;
}
/**
* @param {string} fileName
2016-06-30 08:02:45 +08:00
* @returns {string}
2016-06-28 02:14:33 +08:00
*/
2019-07-05 03:19:24 +08:00
export function mimeContentType(fileName) {
let ext = '',
2016-06-30 08:02:45 +08:00
result = 'application/octet-stream';
2016-06-28 02:14:33 +08:00
fileName = trim(fileName).toLowerCase();
2019-07-05 03:19:24 +08:00
if ('winmail.dat' === fileName) {
2016-06-28 02:14:33 +08:00
return 'application/ms-tnef';
}
ext = getFileExtension(fileName);
2019-07-05 03:19:24 +08:00
if (ext && 0 < ext.length && !isUnd(Mime[ext])) {
2016-06-28 02:14:33 +08:00
result = Mime[ext];
}
return result;
}
2017-08-07 23:09:14 +08:00
/**
* @param {string} color
* @returns {boolean}
*/
2019-07-05 03:19:24 +08:00
export function isTransparent(color) {
2017-08-07 23:09:14 +08:00
return 'rgba(0, 0, 0, 0)' === color || 'transparent' === color;
}
2016-10-26 06:10:36 +08:00
/**
* @param {Object} $el
* @returns {number}
*/
2019-07-05 03:19:24 +08:00
export function getRealHeight($el) {
$el
.clone()
.show()
.appendTo($hcont);
2016-10-26 06:10:36 +08:00
const result = $hcont.height();
$hcont.empty();
return result;
}
2016-06-28 02:14:33 +08:00
/**
* @param {string} url
* @param {number} value
* @param {Function} fCallback
*/
2019-07-05 03:19:24 +08:00
export function resizeAndCrop(url, value, fCallback) {
const img = new window.Image();
2016-06-28 02:14:33 +08:00
img.onload = function() {
2019-07-05 03:19:24 +08:00
let diff = [0, 0];
2016-06-28 02:14:33 +08:00
2019-07-05 03:19:24 +08:00
const canvas = window.document.createElement('canvas'),
2016-06-30 08:02:45 +08:00
ctx = canvas.getContext('2d');
2016-06-28 02:14:33 +08:00
canvas.width = value;
canvas.height = value;
2019-07-05 03:19:24 +08:00
if (this.width > this.height) {
2016-06-28 02:14:33 +08:00
diff = [this.width - this.height, 0];
2019-07-05 03:19:24 +08:00
} else {
2016-06-28 02:14:33 +08:00
diff = [0, this.height - this.width];
}
ctx.fillStyle = '#fff';
ctx.fillRect(0, 0, value, value);
ctx.drawImage(this, diff[0] / 2, diff[1] / 2, this.width - diff[0], this.height - diff[1], 0, 0, value, value);
fCallback(canvas.toDataURL('image/jpeg'));
2016-06-07 05:57:52 +08:00
};
2016-06-28 02:14:33 +08:00
img.src = url;
2016-06-25 06:18:59 +08:00
}
2016-06-07 05:57:52 +08:00
/**
* @param {string} mailToUrl
2017-09-28 01:58:15 +08:00
* @param {Function} PopupComposeViewModel
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 mailToHelper(mailToUrl, PopupComposeViewModel) {
if (
mailToUrl &&
'mailto:' ===
mailToUrl
.toString()
.substr(0, 7)
.toLowerCase()
) {
if (!PopupComposeViewModel) {
2016-06-07 05:57:52 +08:00
return true;
}
mailToUrl = mailToUrl.toString().substr(7);
2019-07-05 03:19:24 +08:00
let to = [],
2016-06-07 05:57:52 +08:00
cc = null,
bcc = null,
2016-06-30 08:02:45 +08:00
params = {};
2019-07-05 03:19:24 +08:00
const email = mailToUrl.replace(/\?.+$/, ''),
query = mailToUrl.replace(/^[^?]*\?/, ''),
2017-09-28 01:58:15 +08:00
EmailModel = require('Model/Email').default;
2016-06-30 08:02:45 +08:00
params = simpleQueryParser(query);
2016-06-07 05:57:52 +08:00
2019-07-05 03:19:24 +08:00
if (!isUnd(params.to)) {
2017-10-07 02:52:00 +08:00
to = EmailModel.parseEmailLine(decodeURIComponent(email + ',' + params.to));
2019-07-05 03:19:24 +08:00
to = _.values(
to.reduce((result, value) => {
if (value) {
if (result[value.email]) {
if (!result[value.email].name) {
result[value.email] = value;
}
} else {
2017-10-07 02:52:00 +08:00
result[value.email] = value;
}
}
2019-07-05 03:19:24 +08:00
return result;
}, {})
);
} else {
2017-10-07 02:52:00 +08:00
to = EmailModel.parseEmailLine(email);
}
2019-07-05 03:19:24 +08:00
if (!isUnd(params.cc)) {
2017-09-28 01:58:15 +08:00
cc = EmailModel.parseEmailLine(decodeURIComponent(params.cc));
2016-06-07 05:57:52 +08:00
}
2019-07-05 03:19:24 +08:00
if (!isUnd(params.bcc)) {
2017-09-28 01:58:15 +08:00
bcc = EmailModel.parseEmailLine(decodeURIComponent(params.bcc));
2016-06-07 05:57:52 +08:00
}
2017-09-28 01:58:15 +08:00
require('Knoin/Knoin').showScreenPopup(PopupComposeViewModel, [
2019-07-05 03:19:24 +08:00
ComposeType.Empty,
null,
to,
cc,
bcc,
2016-06-07 05:57:52 +08:00
isUnd(params.subject) ? null : pString(decodeURIComponent(params.subject)),
isUnd(params.body) ? null : plainToHtml(pString(decodeURIComponent(params.body)))
]);
return true;
}
return false;
}
2016-08-30 06:10:24 +08:00
/**
* @param {Function} fn
* @returns {void}
*/
2019-07-05 03:19:24 +08:00
export function domReady(fn) {
2016-08-30 06:10:24 +08:00
$(() => fn());
2019-07-05 03:19:24 +08:00
//
// if ('loading' !== window.document.readyState)
// {
// fn();
// }
// else
// {
// window.document.addEventListener('DOMContentLoaded', fn);
// }
2016-08-30 06:10:24 +08:00
}
2016-06-07 05:57:52 +08:00
export const windowResize = _.debounce((timeout) => {
2019-07-05 03:19:24 +08:00
if (isUnd(timeout) || isNull(timeout)) {
$win.trigger('resize');
2019-07-05 03:19:24 +08:00
} else {
2016-06-10 00:46:03 +08:00
window.setTimeout(() => {
$win.trigger('resize');
2016-06-07 05:57:52 +08:00
}, timeout);
}
}, 50);
2016-06-30 08:02:45 +08:00
/**
* @returns {void}
*/
2019-07-05 03:19:24 +08:00
export function windowResizeCallback() {
2016-06-07 05:57:52 +08:00
windowResize();
}
2016-06-28 02:14:33 +08:00
let substr = window.String.substr;
2019-07-05 03:19:24 +08:00
if ('b' !== 'ab'.substr(-1)) {
2016-06-28 02:14:33 +08:00
substr = (str, start, length) => {
2016-06-30 08:02:45 +08:00
start = 0 > start ? str.length + start : start;
2016-06-28 02:14:33 +08:00
return str.substr(start, length);
};
window.String.substr = substr;
}