snappymail/dev/Common/Utils.js

1599 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';
2016-06-07 05:57:52 +08:00
import {$win, $div, dropdownVisibility, data as GlobalsData} from 'Common/Globals';
import {ComposeType, EventKeyCode, SaveSettingsStep, FolderType} from 'Common/Enums';
2016-06-28 02:14:33 +08:00
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
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
*/
2016-07-02 06:49:59 +08:00
export function silentTryCatch(func)
2016-06-07 05:57:52 +08:00
{
2016-06-16 07:36:44 +08:00
try {
2016-07-02 06:49:59 +08:00
func();
2016-06-30 08:02:45 +08:00
}
2016-07-02 06:49:59 +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
*/
export function isNormal(value)
{
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
*/
export function isPosNumeric(value, includeZero = true)
{
return !isNormal(value) ? false :
(includeZero ? (/^[0-9]*$/).test(value.toString()) : (/^[1-9]+[0-9]*$/).test(value.toString()));
}
/**
* @param {*} value
* @param {number=} defaultValur = 0
2016-06-30 08:02:45 +08:00
* @returns {number}
2016-06-07 05:57:52 +08:00
*/
export function pInt(value, defaultValur = 0)
{
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
*/
export function pString(value)
{
return isNormal(value) ? '' + value : '';
}
/**
* @param {*} value
2016-06-30 08:02:45 +08:00
* @returns {boolean}
2016-06-07 05:57:52 +08:00
*/
export function pBool(value)
{
return !!value;
}
2016-07-16 03:54:37 +08:00
/**
* @param {*} value
* @returns {string}
*/
export function boolToAjax(value)
{
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
*/
export function isNonEmptyArray(values)
{
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
*/
export function encodeURIComponent(component)
{
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
*/
export function decodeURIComponent(component)
{
return window.decodeURIComponent(component);
}
2016-07-07 07:11:13 +08:00
/**
* @param {string} url
* @returns {string}
*/
export function decodeURI(url)
{
return window.decodeURI(url);
}
/**
* @param {string} url
* @returns {string}
*/
export function encodeURI(url)
{
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
*/
export function simpleQueryParser(queryString)
{
2016-07-16 05:29:42 +08:00
let
index = 0,
len = 0,
temp = null;
2016-06-30 08:02:45 +08:00
const
queries = queryString.split('&'),
params = {};
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
*/
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 = '';
while (result.length < len)
{
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
*/
export function encodeHtml(text)
{
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
*/
export function splitPlainText(text, len = 100)
{
let
prefix = '',
subText = '',
result = text,
spacePos = 0,
2016-06-30 08:02:45 +08:00
newLinePos = 0;
2016-06-07 05:57:52 +08:00
while (result.length > len)
{
subText = result.substring(0, len);
spacePos = subText.lastIndexOf(' ');
newLinePos = subText.lastIndexOf('\n');
if (-1 !== newLinePos)
{
spacePos = newLinePos;
}
if (-1 === spacePos)
{
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);
};
}());
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) => {
if (!timeOuts[action])
{
timeOuts[action] = window.setTimeout(() => {
fFunction();
timeOuts[action] = 0;
}, timeOut);
}
};
}());
export {timeOutAction, timeOutActionSecond};
/**
2016-06-30 08:02:45 +08:00
* @returns {boolean}
2016-06-07 05:57:52 +08:00
*/
export function inFocus()
{
try {
if (window.document.activeElement)
2016-06-07 05:57:52 +08:00
{
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;
}
2016-06-07 05:57:52 +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}
*/
2016-06-07 05:57:52 +08:00
export function removeInFocus(force)
{
if (window.document && window.document.activeElement && window.document.activeElement.blur)
{
try {
const activeEl = $(window.document.activeElement);
if (activeEl && activeEl.is('input,textarea'))
{
window.document.activeElement.blur();
}
else if (force)
{
2016-06-30 08:02:45 +08:00
window.document.activeElement.blur();
2016-06-07 05:57:52 +08:00
}
2016-06-30 08:02:45 +08:00
}
2016-07-02 06:49:59 +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}
*/
2016-06-07 05:57:52 +08:00
export function removeSelection()
{
try {
if (window && window.getSelection)
{
const sel = window.getSelection();
if (sel && sel.removeAllRanges)
{
sel.removeAllRanges();
}
}
else if (window.document && window.document.selection && window.document.selection.empty)
{
window.document.selection.empty();
}
2016-06-30 08:02:45 +08:00
}
2016-07-02 06:49:59 +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
*/
export function replySubjectAdd(prefix, subject)
{
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;
const
parts = [],
prefixIsRe = !fwd;
2016-06-07 05:57:52 +08:00
if ('' !== subject)
{
_.each(subject.split(':'), (part) => {
const trimmedPart = trim(part);
2016-06-30 08:02:45 +08:00
if (!drop && ((/^(RE|FWD)$/i).test(trimmedPart) || (/^(RE|FWD)[\[\(][\d]+[\]\)]$/i).test(trimmedPart)))
2016-06-07 05:57:52 +08:00
{
if (!re)
{
2016-06-30 08:02:45 +08:00
re = !!(/^RE/i).test(trimmedPart);
2016-06-07 05:57:52 +08:00
}
if (!fwd)
{
2016-06-30 08:02:45 +08:00
fwd = !!(/^FWD/i).test(trimmedPart);
2016-06-07 05:57:52 +08:00
}
}
else
{
parts.push(part);
drop = true;
}
});
}
if (prefixIsRe)
{
re = false;
}
else
{
fwd = false;
}
return trim(
(prefixIsRe ? 'Re: ' : 'Fwd: ') +
(re ? 'Re: ' : '') +
(fwd ? 'Fwd: ' : '') +
trim(parts.join(':'))
);
}
/**
* @param {number} num
* @param {number} dec
2016-06-30 08:02:45 +08:00
* @returns {number}
2016-06-07 05:57:52 +08:00
*/
export function roundNumber(num, dec)
{
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
*/
export function friendlySize(sizeInBytes)
{
sizeInBytes = pInt(sizeInBytes);
2016-06-30 08:02:45 +08:00
switch (true)
2016-06-07 05:57:52 +08:00
{
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
*/
export function log(desc)
{
if (window.console && window.console.log)
{
window.console.log(desc);
}
}
/**
* @param {?} object
* @param {string} methodName
* @param {Array=} params
* @param {number=} delay = 0
*/
export function delegateRun(object, methodName, params, delay = 0)
{
if (object && object[methodName])
{
delay = pInt(delay);
2016-07-02 06:49:59 +08:00
params = isArray(params) ? params : [];
2016-06-07 05:57:52 +08:00
if (0 >= delay)
{
2016-07-02 06:49:59 +08:00
object[methodName](...params);
2016-06-07 05:57:52 +08:00
}
else
{
_.delay(() => {
2016-07-02 06:49:59 +08:00
object[methodName](...params);
2016-06-07 05:57:52 +08:00
}, delay);
}
}
}
/**
* @param {?} event
*/
2016-07-16 05:29:42 +08:00
export function killCtrlACtrlS(event)
2016-06-07 05:57:52 +08:00
{
event = event || window.event;
if (event && event.ctrlKey && !event.shiftKey && !event.altKey)
{
const key = event.keyCode || event.which;
if (key === EventKeyCode.S)
{
event.preventDefault();
return;
}
else if (key === EventKeyCode.A)
{
const sender = event.target || event.srcElement;
if (sender && ('true' === '' + sender.contentEditable ||
(sender.tagName && sender.tagName.match(/INPUT|TEXTAREA/i))))
{
return;
}
if (window.getSelection)
{
window.getSelection().removeAllRanges();
}
else if (window.document.selection && window.document.selection.clear)
{
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
*/
export function createCommandLegacy(context, fExecute, fCanExecute = true)
2016-06-07 05:57:52 +08:00
{
2016-06-30 08:02:45 +08:00
let fResult = null;
const fNonEmpty = (...args) => {
if (fResult && fResult.canExecute && fResult.canExecute())
{
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
if (isFunc(fCanExecute))
{
2016-09-10 06:38:16 +08:00
fResult.canExecute = ko.computed(() => fResult && fResult.enabled() && fCanExecute.call(context));
2016-06-07 05:57:52 +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) => {
if ('@custom' === theme.substr(-7))
{
theme = trim(theme.substring(0, theme.length - 7));
}
return trim(theme.replace(/[^a-zA-Z0-9]+/g, ' ').replace(/([A-Z])/g, ' $1').replace(/[\s]+/g, ' '));
});
/**
* @param {string} name
2016-06-30 08:02:45 +08:00
* @returns {string}
2016-06-07 05:57:52 +08:00
*/
export function quoteName(name)
{
return name.replace(/["]/g, '\\"');
}
/**
2016-06-30 08:02:45 +08:00
* @returns {number}
2016-06-07 05:57:52 +08:00
*/
export function microtime()
{
return (new window.Date()).getTime();
}
/**
2016-06-30 08:02:45 +08:00
* @returns {number}
2016-06-07 05:57:52 +08:00
*/
export function timestamp()
{
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
*/
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-30 08:02:45 +08:00
/**
* @returns {object}
*/
2016-06-07 05:57:52 +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}
*/
export function defautOptionsAfterRender(domItem, item)
2016-06-07 05:57:52 +08:00
{
2016-06-30 08:02:45 +08:00
if (item && !isUnd(item.disabled) && domItem)
2016-06-07 05:57:52 +08:00
{
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
*/
export function clearBqSwitcher(body)
{
body.find('blockquote.rl-bq-switcher').removeClass('rl-bq-switcher hidden-bq');
body.find('.rlBlockquoteSwitcher').off('.rlBlockquoteSwitcher').remove();
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
*/
export function previewMessage({title, subject, date, fromCreds, toCreds, toLabel}, body, isHtml, print)
2016-06-16 07:36:44 +08:00
{
const
win = window.open(''),
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() : '';
doc.write(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('{{bodyClass}}', bodyClass)
.replace('{{html}}', html)
);
2016-06-16 07:36:44 +08:00
doc.close();
if (print)
{
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
*/
export function settingsSaveHelperFunction(fCallback, koTrigger, context = null, timer = 1000)
{
timer = pInt(timer);
return (type, data, cached, requestAction, requestParameters) => {
koTrigger.call(context, data && data.Result ? SaveSettingsStep.TrueResult : SaveSettingsStep.FalseResult);
if (fCallback)
{
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}
*/
2016-06-07 05:57:52 +08:00
export function settingsSaveHelperSimpleFunction(koTrigger, context)
{
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}
*/
2016-06-07 05:57:52 +08:00
export function settingsSaveHelperSubscribeFunction(remote, settingName, type, fTriggerFunction)
{
return (value) => {
if (remote)
{
switch (type)
{
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;
if (remote.saveAdminConfig)
{
remote.saveAdminConfig(fTriggerFunction || null, data);
}
else if (remote.saveSettings)
{
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
*/
export function findEmailAndLinks(html)
{
// return html;
2016-09-07 05:06:38 +08:00
return Autolinker ? Autolinker.link(html, {
2016-06-07 05:57:52 +08:00
newWindow: true,
stripPrefix: false,
urls: true,
email: true,
2016-09-07 05:06:38 +08:00
mention: false,
phone: false,
hashtag: false,
2016-09-07 05:06:38 +08:00
replaceFn: function(match) {
return !(match && 'url' === match.getType() && match.matchedText && 0 !== match.matchedText.indexOf('http'));
2016-06-07 05:57:52 +08:00
}
2016-09-07 05:06:38 +08:00
}) : 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
*/
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
2016-06-07 05:57:52 +08:00
convertBlockquote = (blockquoteText) => {
blockquoteText = '> ' + trim(blockquoteText).replace(/\n/gm, '\n> ');
2016-07-02 06:49:59 +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
},
2016-06-10 00:46:03 +08:00
convertDivs = (...args) => {
if (args && 1 < args.length)
2016-06-07 05:57:52 +08:00
{
2016-06-10 00:46:03 +08:00
let divText = trim(args[1]);
2016-06-07 05:57:52 +08:00
if (0 < divText.length)
{
divText = divText.replace(/<div[^>]*>([\s\S\r\n]*)<\/div>/gmi, convertDivs);
divText = '\n' + trim(divText) + '\n';
}
return divText;
}
return '';
},
2016-07-02 06:49:59 +08:00
convertPre = (...args) => (args && 1 < args.length ? args[1].toString().replace(/[\n]/gm, '<br />').replace(/[\r]/gm, '') : ''),
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, '')
.replace(/<pre[^>]*>([\s\S\r\n\t]*)<\/pre>/gmi, convertPre)
.replace(/[\s]+/gm, ' ')
.replace(/((?:href|data)\s?=\s?)("[^"]+?"|'[^']+?')/gmi, fixAttibuteValue)
.replace(/<br[^>]*>/gmi, '\n')
.replace(/<\/h[\d]>/gi, '\n')
.replace(/<\/p>/gi, '\n\n')
.replace(/<ul[^>]*>/gmi, '\n')
.replace(/<\/ul>/gi, '\n')
.replace(/<li[^>]*>/gmi, ' * ')
.replace(/<\/li>/gi, '\n')
.replace(/<\/td>/gi, '\n')
.replace(/<\/tr>/gi, '\n')
.replace(/<hr[^>]*>/gmi, '\n_______________________________\n\n')
.replace(/<div[^>]*>([\s\S\r\n]*)<\/div>/gmi, convertDivs)
.replace(/<blockquote[^>]*>/gmi, '\n__bq__start__\n')
.replace(/<\/blockquote>/gmi, '\n__bq__end__\n')
.replace(/<a [^>]*>([\s\S\r\n]*?)<\/a>/gmi, convertLinks)
.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
text = splitPlainText(trim(text));
pos = 0;
limit = 800;
2016-06-07 05:57:52 +08:00
while (0 < limit)
2016-06-07 05:57:52 +08:00
{
2016-06-30 08:02:45 +08:00
limit -= 1;
iP1 = text.indexOf('__bq__start__', pos);
2016-06-07 05:57:52 +08:00
if (-1 < iP1)
{
iP2 = text.indexOf('__bq__start__', iP1 + 5);
iP3 = text.indexOf('__bq__end__', iP1 + 5);
if ((-1 === iP2 || iP3 < iP2) && iP1 < iP3)
{
text = text.substring(0, iP1) +
convertBlockquote(text.substring(iP1 + 13, iP3)) +
text.substring(iP3 + 11);
pos = 0;
2016-06-07 05:57:52 +08:00
}
else if (-1 < iP2 && iP2 < iP3)
{
pos = iP2 - 1;
2016-06-07 05:57:52 +08:00
}
else
{
pos = 0;
2016-06-07 05:57:52 +08:00
}
}
else
{
break;
}
}
text = text
.replace(/__bq__start__/gm, '')
2016-06-30 08:02:45 +08:00
.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
*/
export function plainToHtml(plain, findEmailAndLinksInText = false)
{
plain = plain.toString().replace(/\r/g, '');
2016-07-02 06:49:59 +08:00
plain = plain.replace(/^>[> ]>+/gm, ([match]) => (match ? match.replace(/[ ]+/g, '') : match));
2016-06-07 05:57:52 +08:00
let
bIn = false,
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
do
{
bDo = false;
aNextText = [];
for (iIndex = 0; iIndex < aText.length; iIndex++)
{
sLine = aText[iIndex];
bStart = '>' === sLine.substr(0, 1);
if (bStart && !bIn)
{
bDo = true;
bIn = true;
aNextText.push('~~~blockquote~~~');
aNextText.push(sLine.substr(1));
}
else if (!bStart && bIn)
{
if ('' !== sLine)
{
bIn = false;
aNextText.push('~~~/blockquote~~~');
aNextText.push(sLine);
}
else
{
aNextText.push(sLine);
}
}
else if (bStart && bIn)
{
aNextText.push(sLine.substr(1));
}
else
{
aNextText.push(sLine);
}
}
if (bIn)
{
bIn = false;
aNextText.push('~~~/blockquote~~~');
}
aText = aNextText;
}
while (bDo);
plain = aText.join('\n');
plain = plain
// .replace(/~~~\/blockquote~~~\n~~~blockquote~~~/g, '\n')
.replace(/&/g, '&amp;')
.replace(/>/g, '&gt;').replace(/</g, '&lt;')
.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
*/
export function folderListOptionsBuilder(aSystem, aList, aDisabled, aHeaderLines,
iUnDeep, fDisableCallback, fVisibleCallback, fRenameCallback, bSystem, bBuildUnvisible)
{
let
/**
* @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;
if (!isArray(aDisabled))
{
aDisabled = [];
}
if (!isArray(aHeaderLines))
{
aHeaderLines = [];
}
for (iIndex = 0, iLen = aHeaderLines.length; iIndex < iLen; iIndex++)
{
aResult.push({
id: aHeaderLines[iIndex][0],
name: aHeaderLines[iIndex][1],
system: false,
seporator: false,
disabled: false
});
}
bSep = true;
for (iIndex = 0, iLen = aSystem.length; iIndex < iLen; iIndex++)
{
oItem = aSystem[iIndex];
2016-07-02 06:49:59 +08:00
if (fVisibleCallback ? fVisibleCallback(oItem) : true)
2016-06-07 05:57:52 +08:00
{
if (bSep && 0 < aResult.length)
{
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,
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;
for (iIndex = 0, iLen = aList.length; iIndex < iLen; iIndex++)
{
oItem = aList[iIndex];
// if (oItem.subScribed() || !oItem.existen || bBuildUnvisible)
if ((oItem.subScribed() || !oItem.existen || bBuildUnvisible) && (oItem.selectable || oItem.hasSubScribedSubfolders()))
{
2016-07-02 06:49:59 +08:00
if (fVisibleCallback ? fVisibleCallback(oItem) : true)
2016-06-07 05:57:52 +08:00
{
if (FolderType.User === oItem.type() || !bSystem || oItem.hasSubScribedSubfolders())
{
if (bSep && 0 < aResult.length)
{
aResult.push({
id: '---',
name: '---',
system: false,
seporator: true,
disabled: true
});
}
bSep = false;
aResult.push({
id: oItem.fullNameRaw,
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,
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
});
}
}
}
if (oItem.subScribed() && 0 < oItem.subFolders().length)
{
aResult = aResult.concat(folderListOptionsBuilder([], oItem.subFolders(), aDisabled, [],
iUnDeep, fDisableCallback, fVisibleCallback, fRenameCallback, bSystem, bBuildUnvisible));
}
}
return aResult;
}
2016-06-30 08:02:45 +08:00
/**
* @param {object} element
* @returns {void}
*/
2016-06-07 05:57:52 +08:00
export function selectElement(element)
{
2016-07-16 05:29:42 +08:00
let
sel = null,
range = null;
2016-06-07 05:57:52 +08:00
if (window.getSelection)
{
sel = window.getSelection();
sel.removeAllRanges();
range = window.document.createRange();
range.selectNodeContents(element);
sel.addRange(range);
}
else if (window.document.selection)
{
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');
};
if (delay)
{
_.delay(fFunc, 100);
}
else
{
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
*/
export function getConfigurationFromScriptTag(configuration)
{
if (!configurationScriptTagCache[configuration])
{
configurationScriptTagCache[configuration] = $('script[type="application/json"][data-configuration="' + configuration + '"]');
}
2016-06-30 08:02:45 +08:00
try
{
return JSON.parse(configurationScriptTagCache[configuration].text());
}
2016-07-02 06:49:59 +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
*/
export function disposeOne(propOrValue, value)
{
const disposable = value || propOrValue;
2016-06-30 08:02:45 +08:00
if (disposable && 'function' === typeof disposable.dispose)
2016-06-07 05:57:52 +08:00
{
disposable.dispose();
}
}
/**
* @param {Object} object
*/
export function disposeObject(object)
{
if (object)
{
if (isArray(object.disposables))
{
_.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
*/
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);
});
}
else if (objectOrObjects && objectOrObjects.onDestroy)
{
objectOrObjects.onDestroy();
}
}
}
2016-06-30 08:02:45 +08:00
/**
* @param {object} $styleTag
* @param {string} css
* @returns {boolean}
*/
2016-06-07 05:57:52 +08:00
export function appendStyles($styleTag, css)
{
if ($styleTag && $styleTag[0])
{
if ($styleTag[0].styleSheet && !isUnd($styleTag[0].styleSheet.cssText))
{
$styleTag[0].styleSheet.cssText = css;
}
else
{
$styleTag.text(css);
}
return true;
}
return false;
}
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}
*/
2016-08-22 05:30:34 +08:00
export function changeTheme(value, themeTrigger = noop)
2016-06-07 05:57:52 +08:00
{
2016-06-30 08:02:45 +08:00
const
2016-06-07 05:57:52 +08:00
themeLink = $('#app-theme-link'),
clearTimer = () => {
__themeTimer = window.setTimeout(() => themeTrigger(SaveSettingsStep.Idle), 1000);
__themeAjax = null;
2016-06-30 08:02:45 +08:00
};
let
themeStyle = $('#app-theme-style'),
url = themeLink.attr('href');
2016-06-07 05:57:52 +08:00
if (!url)
{
url = themeStyle.attr('data-href');
}
if (url)
{
url = url.toString().replace(/\/-\/[^\/]+\/\-\//, '/-/' + value + '/-/');
2016-09-10 06:38:16 +08:00
url = url.replace(/\/Css\/[^\/]+\/User\//, '/Css/0/User/');
url = url.replace(/\/Hash\/[^\/]+\//, '/Hash/-/');
2016-06-07 05:57:52 +08:00
if ('Json/' !== url.substring(url.length - 5, url.length))
{
url += 'Json/';
}
window.clearTimeout(__themeTimer);
2016-08-22 05:30:34 +08:00
2016-06-07 05:57:52 +08:00
themeTrigger(SaveSettingsStep.Animate);
if (__themeAjax && __themeAjax.abort)
{
__themeAjax.abort();
}
__themeAjax = $.ajax({
url: url,
dataType: 'json'
}).then((data) => {
2016-06-07 05:57:52 +08:00
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();
}
if (themeStyle && themeStyle[0])
{
if (appendStyles(themeStyle, data[1]))
{
themeStyle.attr('data-href', url).attr('data-theme', data[0]);
}
}
themeTrigger(SaveSettingsStep.TrueResult);
}
}).then(clearTimer, clearTimer);
2016-06-07 05:57:52 +08:00
}
}
2016-06-30 08:02:45 +08:00
/**
* @returns {function}
*/
2016-06-28 02:14:33 +08:00
export function computedPagenatorHelper(koCurrentPage, koPageCount)
2016-06-07 05:57:52 +08:00
{
2016-06-28 02:14:33 +08:00
return () => {
const
currentPage = koCurrentPage(),
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()
};
if (push)
{
result.push(data);
}
else
{
result.unshift(data);
}
2016-06-30 08:02:45 +08:00
};
2016-06-28 02:14:33 +08:00
let
prev = 0,
next = 0,
2016-06-30 08:02:45 +08:00
limit = 2;
2016-06-28 02:14:33 +08:00
if (1 < pageCount || (0 < pageCount && pageCount < currentPage))
{
if (pageCount < currentPage)
{
fAdd(pageCount);
prev = pageCount;
next = pageCount;
}
else
{
if (3 >= currentPage || pageCount - 2 <= currentPage)
{
limit += 2;
}
fAdd(currentPage);
prev = currentPage;
next = currentPage;
}
while (0 < limit) {
prev -= 1;
next += 1;
if (0 < prev)
{
fAdd(prev, false);
2016-06-30 08:02:45 +08:00
limit -= 1;
2016-06-28 02:14:33 +08:00
}
if (pageCount >= next)
{
fAdd(next, true);
2016-06-30 08:02:45 +08:00
limit -= 1;
2016-06-28 02:14:33 +08:00
}
else if (0 >= prev)
{
break;
}
}
if (3 === prev)
{
fAdd(2, false);
}
else if (3 < prev)
{
fAdd(Math.round((prev - 1) / 2), false, '...');
}
if (pageCount - 2 === next)
{
fAdd(pageCount - 1, true);
}
else if (pageCount - 2 > next)
{
fAdd(Math.round((pageCount + next) / 2), true, '...');
}
// first and last
if (1 < prev)
{
fAdd(1, false);
}
if (pageCount > next)
{
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
*/
export function getFileExtension(fileName)
{
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
*/
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();
if ('winmail.dat' === fileName)
{
return 'application/ms-tnef';
}
ext = getFileExtension(fileName);
if (ext && 0 < ext.length && !isUnd(Mime[ext]))
{
result = Mime[ext];
}
return result;
}
/**
* @param {string} url
* @param {number} value
* @param {Function} fCallback
*/
export function resizeAndCrop(url, value, fCallback)
{
const img = new window.Image();
2016-06-28 02:14:33 +08:00
img.onload = function() {
let
2016-06-30 08:02:45 +08:00
diff = [0, 0];
2016-06-28 02:14:33 +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;
if (this.width > this.height)
{
diff = [this.width - this.height, 0];
}
else
{
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
* @param {Function} PopupComposeVoreModel
2016-06-30 08:02:45 +08:00
* @returns {boolean}
2016-06-07 05:57:52 +08:00
*/
export function mailToHelper(mailToUrl, PopupComposeVoreModel)
{
if (mailToUrl && 'mailto:' === mailToUrl.toString().substr(0, 7).toLowerCase())
{
if (!PopupComposeVoreModel)
{
return true;
}
mailToUrl = mailToUrl.toString().substr(7);
let
to = [],
cc = null,
bcc = null,
2016-06-30 08:02:45 +08:00
params = {};
const
2016-06-07 05:57:52 +08:00
email = mailToUrl.replace(/\?.+$/, ''),
2016-06-30 08:02:45 +08:00
query = mailToUrl.replace(/^[^\?]*\?/, ''),
2016-07-07 05:03:30 +08:00
EmailModel = require('Model/Email').default,
2016-07-02 06:49:59 +08:00
emailObj = new EmailModel(),
fParseEmailLine = (line) => (line ? _.compact(_.map(decodeURIComponent(line).split(/[,]/), (item) => {
emailObj.clear();
emailObj.mailsoParse(item);
return '' !== emailObj.email ? emailObj : null;
})) : null);
2016-06-07 05:57:52 +08:00
to = fParseEmailLine(email);
2016-06-30 08:02:45 +08:00
params = simpleQueryParser(query);
2016-06-07 05:57:52 +08:00
if (!isUnd(params.cc))
{
cc = fParseEmailLine(decodeURIComponent(params.cc));
}
if (!isUnd(params.bcc))
{
bcc = fParseEmailLine(decodeURIComponent(params.bcc));
}
require('Knoin/Knoin').showScreenPopup(PopupComposeVoreModel, [
ComposeType.Empty, null, to, cc, bcc,
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}
*/
export function domReady(fn)
{
$(() => fn());
//
// if ('loading' !== window.document.readyState)
// {
// fn();
// }
// else
// {
// window.document.addEventListener('DOMContentLoaded', fn);
// }
}
2016-06-07 05:57:52 +08:00
export const windowResize = _.debounce((timeout) => {
if (isUnd(timeout) || isNull(timeout))
{
$win.resize();
}
else
{
2016-06-10 00:46:03 +08:00
window.setTimeout(() => {
2016-06-07 05:57:52 +08:00
$win.resize();
}, timeout);
}
}, 50);
2016-06-30 08:02:45 +08:00
/**
* @returns {void}
*/
2016-06-07 05:57:52 +08:00
export function windowResizeCallback()
{
windowResize();
}
2016-06-28 02:14:33 +08:00
let substr = window.String.substr;
2016-06-30 08:02:45 +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;
}