(function () { 'use strict'; var oEncryptObject = null, Utils = {}, window = require('window'), _ = require('_'), $ = require('$'), ko = require('ko'), Autolinker = require('Autolinker'), JSEncrypt = require('JSEncrypt'), Mime = require('Common/Mime'), Enums = require('Common/Enums'), Globals = require('Common/Globals') ; Utils.trim = $.trim; Utils.inArray = $.inArray; Utils.isArray = _.isArray; Utils.isObject = _.isObject; Utils.isFunc = _.isFunction; Utils.isUnd = _.isUndefined; Utils.isNull = _.isNull; Utils.emptyFunction = Utils.noop = function () {}; /** * @param {*} oValue * @return {boolean} */ Utils.isNormal = function (oValue) { return !Utils.isUnd(oValue) && !Utils.isNull(oValue); }; Utils.windowResize = _.debounce(function (iTimeout) { if (Utils.isUnd(iTimeout)) { Globals.$win.resize(); } else { window.setTimeout(function () { Globals.$win.resize(); }, iTimeout); } }, 50); Utils.windowResizeCallback = function () { Utils.windowResize(); }; /** * @param {(string|number)} mValue * @param {boolean=} bIncludeZero * @return {boolean} */ Utils.isPosNumeric = function (mValue, bIncludeZero) { return Utils.isNormal(mValue) ? ((Utils.isUnd(bIncludeZero) ? true : !!bIncludeZero) ? (/^[0-9]*$/).test(mValue.toString()) : (/^[1-9]+[0-9]*$/).test(mValue.toString())) : false; }; /** * @param {*} iValue * @param {number=} iDefault = 0 * @return {number} */ Utils.pInt = function (iValue, iDefault) { var iResult = Utils.isNormal(iValue) && '' !== iValue ? window.parseInt(iValue, 10) : (iDefault || 0); return window.isNaN(iResult) ? (iDefault || 0) : iResult; }; /** * @param {*} mValue * @return {string} */ Utils.pString = function (mValue) { return Utils.isNormal(mValue) ? '' + mValue : ''; }; /** * @param {*} mValue * @return {boolean} */ Utils.pBool = function (mValue) { return !!mValue; }; /** * @param {string} sComponent * @return {string} */ Utils.encodeURIComponent = function (sComponent) { return window.encodeURIComponent(sComponent); }; /** * @param {*} aValue * @return {boolean} */ Utils.isNonEmptyArray = function (aValue) { return Utils.isArray(aValue) && 0 < aValue.length; }; /** * @param {string} sQueryString * @return {Object} */ Utils.simpleQueryParser = function (sQueryString) { var oParams = {}, aQueries = [], aTemp = [], iIndex = 0, iLen = 0 ; aQueries = sQueryString.split('&'); for (iIndex = 0, iLen = aQueries.length; iIndex < iLen; iIndex++) { aTemp = aQueries[iIndex].split('='); oParams[window.decodeURIComponent(aTemp[0])] = window.decodeURIComponent(aTemp[1]); } return oParams; }; /** * @param {string} sMailToUrl * @param {Function} PopupComposeVoreModel * @return {boolean} */ Utils.mailToHelper = function (sMailToUrl, PopupComposeVoreModel) { if (sMailToUrl && 'mailto:' === sMailToUrl.toString().substr(0, 7).toLowerCase()) { if (!PopupComposeVoreModel) { return true; } sMailToUrl = sMailToUrl.toString().substr(7); var aTo = [], aCc = null, aBcc = null, oParams = {}, EmailModel = require('Model/Email'), sEmail = sMailToUrl.replace(/\?.+$/, ''), sQueryString = sMailToUrl.replace(/^[^\?]*\?/, ''), fParseEmailLine = function (sLine) { return sLine ? _.compact(_.map(window.decodeURIComponent(sLine).split(/[,]/), function (sItem) { var oEmailModel = new EmailModel(); oEmailModel.mailsoParse(sItem); return '' !== oEmailModel.email ? oEmailModel : null; })) : null; } ; aTo = fParseEmailLine(sEmail); oParams = Utils.simpleQueryParser(sQueryString); if (!Utils.isUnd(oParams.cc)) { aCc = fParseEmailLine(window.decodeURIComponent(oParams.cc)); } if (!Utils.isUnd(oParams.bcc)) { aBcc = fParseEmailLine(window.decodeURIComponent(oParams.bcc)); } require('Knoin/Knoin').showScreenPopup(PopupComposeVoreModel, [Enums.ComposeType.Empty, null, aTo, aCc, aBcc, Utils.isUnd(oParams.subject) ? null : Utils.pString(window.decodeURIComponent(oParams.subject)), Utils.isUnd(oParams.body) ? null : Utils.plainToHtml(Utils.pString(window.decodeURIComponent(oParams.body))) ]); return true; } return false; }; /** * @param {string} sPublicKey * @return {JSEncrypt} */ Utils.rsaObject = function (sPublicKey) { if (JSEncrypt && sPublicKey && (null === oEncryptObject || (oEncryptObject && oEncryptObject.__sPublicKey !== sPublicKey)) && window.crypto && window.crypto.getRandomValues) { oEncryptObject = new JSEncrypt(); oEncryptObject.setPublicKey(sPublicKey); oEncryptObject.__sPublicKey = sPublicKey; } else { oEncryptObject = false; } return oEncryptObject; }; /** * @param {string} sValue * @param {string} sPublicKey * @return {string} */ Utils.rsaEncode = function (sValue, sPublicKey) { if (window.crypto && window.crypto.getRandomValues && sPublicKey) { var sResultValue = false, oEncrypt = Utils.rsaObject(sPublicKey) ; if (oEncrypt) { sResultValue = oEncrypt.encrypt(Utils.fakeMd5() + ':' + sValue + ':' + Utils.fakeMd5()); if (false !== sResultValue && Utils.isNormal(sResultValue)) { return 'rsa:xxx:' + sResultValue; } } } return sValue; }; Utils.rsaEncode.supported = !!(window.crypto && window.crypto.getRandomValues && false && JSEncrypt); /** * @param {string} sText * @return {string} */ Utils.encodeHtml = function (sText) { return Utils.isNormal(sText) ? _.escape(sText.toString()) : ''; }; /** * @param {string} sText * @param {number=} iLen * @return {string} */ Utils.splitPlainText = function (sText, iLen) { var sPrefix = '', sSubText = '', sResult = sText, iSpacePos = 0, iNewLinePos = 0 ; iLen = Utils.isUnd(iLen) ? 100 : iLen; while (sResult.length > iLen) { sSubText = sResult.substring(0, iLen); iSpacePos = sSubText.lastIndexOf(' '); iNewLinePos = sSubText.lastIndexOf('\n'); if (-1 !== iNewLinePos) { iSpacePos = iNewLinePos; } if (-1 === iSpacePos) { iSpacePos = iLen; } sPrefix += sSubText.substring(0, iSpacePos) + '\n'; sResult = sResult.substring(iSpacePos + 1); } return sPrefix + sResult; }; Utils.timeOutAction = (function () { var oTimeOuts = {} ; return function (sAction, fFunction, iTimeOut) { if (Utils.isUnd(oTimeOuts[sAction])) { oTimeOuts[sAction] = 0; } window.clearTimeout(oTimeOuts[sAction]); oTimeOuts[sAction] = window.setTimeout(fFunction, iTimeOut); }; }()); Utils.timeOutActionSecond = (function () { var oTimeOuts = {} ; return function (sAction, fFunction, iTimeOut) { if (!oTimeOuts[sAction]) { oTimeOuts[sAction] = window.setTimeout(function () { fFunction(); oTimeOuts[sAction] = 0; }, iTimeOut); } }; }()); /** * @param {(Object|null|undefined)} oObject * @param {string} sProp * @return {boolean} */ Utils.hos = function (oObject, sProp) { return oObject && window.Object && window.Object.hasOwnProperty ? window.Object.hasOwnProperty.call(oObject, sProp) : false; }; /** * @return {boolean} */ Utils.inFocus = function () { if (window.document.activeElement) { if (Utils.isUnd(window.document.activeElement.__inFocusCache)) { window.document.activeElement.__inFocusCache = $(window.document.activeElement).is('input,textarea,iframe,.cke_editable'); } return !!window.document.activeElement.__inFocusCache; } return false; }; Utils.removeInFocus = function (force) { if (window.document && window.document.activeElement && window.document.activeElement.blur) { var oA = $(window.document.activeElement); if (oA.is('input,textarea')) { window.document.activeElement.blur(); } else if (force) { try { window.document.activeElement.blur(); } catch (e) {} } } }; Utils.removeSelection = function () { if (window && window.getSelection) { var oSel = window.getSelection(); if (oSel && oSel.removeAllRanges) { oSel.removeAllRanges(); } } else if (window.document && window.document.selection && window.document.selection.empty) { window.document.selection.empty(); } }; /** * @param {string} sPrefix * @param {string} sSubject * @return {string} */ Utils.replySubjectAdd = function (sPrefix, sSubject) { sPrefix = Utils.trim(sPrefix.toUpperCase()); sSubject = Utils.trim(sSubject.replace(/[\s]+/g, ' ')); var bDrop = false, aSubject = [], bRe = 'RE' === sPrefix, bFwd = 'FWD' === sPrefix, bPrefixIsRe = !bFwd ; if ('' !== sSubject) { _.each(sSubject.split(':'), function (sPart) { var sTrimmedPart = Utils.trim(sPart); if (!bDrop && (/^(RE|FWD)$/i.test(sTrimmedPart) || /^(RE|FWD)[\[\(][\d]+[\]\)]$/i.test(sTrimmedPart))) { if (!bRe) { bRe = !!(/^RE/i.test(sTrimmedPart)); } if (!bFwd) { bFwd = !!(/^FWD/i.test(sTrimmedPart)); } } else { aSubject.push(sPart); bDrop = true; } }); } if (bPrefixIsRe) { bRe = false; } else { bFwd = false; } return Utils.trim( (bPrefixIsRe ? 'Re: ' : 'Fwd: ') + (bRe ? 'Re: ' : '') + (bFwd ? 'Fwd: ' : '') + Utils.trim(aSubject.join(':')) ); }; /** * @param {number} iNum * @param {number} iDec * @return {number} */ Utils.roundNumber = function (iNum, iDec) { return window.Math.round(iNum * window.Math.pow(10, iDec)) / window.Math.pow(10, iDec); }; /** * @param {(number|string)} iSizeInBytes * @return {string} */ Utils.friendlySize = function (iSizeInBytes) { iSizeInBytes = Utils.pInt(iSizeInBytes); if (iSizeInBytes >= 1073741824) { return Utils.roundNumber(iSizeInBytes / 1073741824, 1) + 'GB'; } else if (iSizeInBytes >= 1048576) { return Utils.roundNumber(iSizeInBytes / 1048576, 1) + 'MB'; } else if (iSizeInBytes >= 1024) { return Utils.roundNumber(iSizeInBytes / 1024, 0) + 'KB'; } return iSizeInBytes + 'B'; }; /** * @param {string} sDesc */ Utils.log = function (sDesc) { if (window.console && window.console.log) { window.console.log(sDesc); } }; /** * @param {?} oObject * @param {string} sMethodName * @param {Array=} aParameters * @param {number=} nDelay */ Utils.delegateRun = function (oObject, sMethodName, aParameters, nDelay) { if (oObject && oObject[sMethodName]) { nDelay = Utils.pInt(nDelay); if (0 >= nDelay) { oObject[sMethodName].apply(oObject, Utils.isArray(aParameters) ? aParameters : []); } else { _.delay(function () { oObject[sMethodName].apply(oObject, Utils.isArray(aParameters) ? aParameters : []); }, nDelay); } } }; /** * @param {?} oEvent */ Utils.kill_CtrlA_CtrlS = function (oEvent) { oEvent = oEvent || window.event; if (oEvent && oEvent.ctrlKey && !oEvent.shiftKey && !oEvent.altKey) { var oSender = oEvent.target || oEvent.srcElement, iKey = oEvent.keyCode || oEvent.which ; if (iKey === Enums.EventKeyCode.S) { oEvent.preventDefault(); return; } else if (iKey === Enums.EventKeyCode.A) { if (oSender && ('true' === '' + oSender.contentEditable || (oSender.tagName && oSender.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(); } oEvent.preventDefault(); } } }; /** * @param {(Object|null|undefined)} oContext * @param {Function} fExecute * @param {(Function|boolean|null)=} fCanExecute * @return {Function} */ Utils.createCommand = function (oContext, fExecute, fCanExecute) { var fNonEmpty = function () { if (fResult && fResult.canExecute && fResult.canExecute()) { fExecute.apply(oContext, Array.prototype.slice.call(arguments)); } return false; }, fResult = fExecute ? fNonEmpty : Utils.emptyFunction ; fResult.enabled = ko.observable(true); fCanExecute = Utils.isUnd(fCanExecute) ? true : fCanExecute; if (Utils.isFunc(fCanExecute)) { fResult.canExecute = ko.computed(function () { return fResult.enabled() && fCanExecute.call(oContext); }); } else { fResult.canExecute = ko.computed(function () { return fResult.enabled() && !!fCanExecute; }); } return fResult; }; /** * @param {string} sTheme * @return {string} */ Utils.convertThemeName = _.memoize(function (sTheme) { if ('@custom' === sTheme.substr(-7)) { sTheme = Utils.trim(sTheme.substring(0, sTheme.length - 7)); } return Utils.trim(sTheme.replace(/[^a-zA-Z0-9]+/g, ' ').replace(/([A-Z])/g, ' $1').replace(/[\s]+/g, ' ')); }); /** * @param {string} sName * @return {string} */ Utils.quoteName = function (sName) { return sName.replace(/["]/g, '\\"'); }; /** * @return {number} */ Utils.microtime = function () { return (new window.Date()).getTime(); }; /** * @return {number} */ Utils.timestamp = function () { return window.Math.round(Utils.microtime() / 1000); }; /** * * @param {string} sLanguage * @param {boolean=} bEng = false * @return {string} */ Utils.convertLangName = function (sLanguage, bEng) { return require('Common/Translator').i18n('LANGS_NAMES' + (true === bEng ? '_EN' : '') + '/LANG_' + sLanguage.toUpperCase().replace(/[^a-zA-Z0-9]+/g, '_'), null, sLanguage); }; /** * @param {number=} iLen * @return {string} */ Utils.fakeMd5 = function(iLen) { var sResult = '', sLine = '0123456789abcdefghijklmnopqrstuvwxyz' ; iLen = Utils.isUnd(iLen) ? 32 : Utils.pInt(iLen); while (sResult.length < iLen) { sResult += sLine.substr(window.Math.round(window.Math.random() * sLine.length), 1); } return sResult; }; Utils.draggablePlace = function () { return $('
]*><\/p>/gi, '') .replace(/
]*>([\s\S\r\n\t]*)<\/pre>/gmi, convertPre) .replace(/[\s]+/gm, ' ') .replace(/((?:href|data)\s?=\s?)("[^"]+?"|'[^']+?')/gmi, fixAttibuteValue) .replace(/
]*>/gmi, '\n') .replace(/<\/h[\d]>/gi, '\n') .replace(/<\/p>/gi, '\n\n') .replace(/
]*>/gmi, '\n__bq__start__\n') .replace(/<\/blockquote>/gmi, '\n__bq__end__\n') .replace(/]*>([\s\S\r\n]*?)<\/a>/gmi, convertLinks) .replace(/<\/div>/gi, '\n') .replace(/ /gi, ' ') .replace(/"/gi, '"') .replace(/<[^>]*>/gm, '') ; sText = Globals.$div.html(sText).text(); sText = sText .replace(/\n[ \t]+/gm, '\n') .replace(/[\n]{3,}/gm, '\n\n') .replace(/>/gi, '>') .replace(/</gi, '<') .replace(/&/gi, '&') ; sText = Utils.splitPlainText(Utils.trim(sText)); iPos = 0; iLimit = 800; while (0 < iLimit) { iLimit--; iP1 = sText.indexOf('__bq__start__', iPos); if (-1 < iP1) { iP2 = sText.indexOf('__bq__start__', iP1 + 5); iP3 = sText.indexOf('__bq__end__', iP1 + 5); if ((-1 === iP2 || iP3 < iP2) && iP1 < iP3) { sText = sText.substring(0, iP1) + convertBlockquote(sText.substring(iP1 + 13, iP3)) + sText.substring(iP3 + 11); iPos = 0; } else if (-1 < iP2 && iP2 < iP3) { iPos = iP2 - 1; } else { iPos = 0; } } else { break; } } sText = sText .replace(/__bq__start__/gm, '') .replace(/__bq__end__/gm, '') ; return sText; }; /** * @param {string} sPlain * @param {boolean} bFindEmailAndLinks = false * @return {string} */ Utils.plainToHtml = function (sPlain, bFindEmailAndLinks) { sPlain = sPlain.toString().replace(/\r/g, ''); bFindEmailAndLinks = Utils.isUnd(bFindEmailAndLinks) ? false : !!bFindEmailAndLinks; var bIn = false, bDo = true, bStart = true, aNextText = [], sLine = '', iIndex = 0, aText = sPlain.split("\n") ; 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); sPlain = aText.join("\n"); sPlain = sPlain // .replace(/~~~\/blockquote~~~\n~~~blockquote~~~/g, '\n') .replace(/&/g, '&') .replace(/>/g, '>').replace(/') .replace(/[\s]*~~~\/blockquote~~~/g, '') .replace(/\u200C([\s\S]*)\u200C/g, '\u0002$1\u0002') .replace(/\n/g, '