CommonJS (research/3)

This commit is contained in:
RainLoop Team 2014-08-22 19:08:56 +04:00
parent 586abbb802
commit 06bb124379
99 changed files with 51037 additions and 42961 deletions

66
dev/Common/Events.js Normal file
View file

@ -0,0 +1,66 @@
/* RainLoop Webmail (c) RainLoop Team | Licensed under CC BY-NC-SA 3.0 */
(function (module) {
'use strict';
var
_ = require('../External/underscore.js'),
Utils = require('./Utils.js'),
Plugins = require('./Plugins.js')
;
/**
* @constructor
*/
function Events()
{
this.oSubs = {};
}
Events.prototype.oSubs = {};
/**
* @param {string} sName
* @param {Function} fFunc
* @param {Object=} oContext
* @return {Events}
*/
Events.prototype.sub = function (sName, fFunc, oContext)
{
if (Utils.isUnd(this.oSubs[sName]))
{
this.oSubs[sName] = [];
}
this.oSubs[sName].push([fFunc, oContext]);
return this;
};
/**
* @param {string} sName
* @param {Array=} aArgs
* @return {Events}
*/
Events.prototype.pub = function (sName, aArgs)
{
Plugins.runHook('rl-pub', [sName, aArgs]);
if (!Utils.isUnd(this.oSubs[sName]))
{
_.each(this.oSubs[sName], function (aItem) {
if (aItem[0])
{
aItem[0].apply(aItem[1] || null, aArgs || []);
}
});
}
return this;
};
module.exports = new Events();
}(module));

View file

@ -8,7 +8,10 @@
Globals = {},
window = require('../External/window.js'),
ko = require('../External/ko.js'),
$html = require('../External/$html.js')
key = require('../External/key.js'),
$html = require('../External/$html.js'),
Enums = require('../Common/Enums.js')
;
/**
@ -36,6 +39,11 @@
*/
Globals.langChangeTrigger = ko.observable(true);
/**
* @type {boolean}
*/
Globals.useKeyboardShortcuts = ko.observable(true);
/**
* @type {number}
*/
@ -100,6 +108,11 @@
* @type {string}
*/
Globals.sAnimationType = '';
/**
* @type {*}
*/
Globals.__RL = null;
/**
* @type {Object}
@ -167,17 +180,96 @@
});
}
Globals.oI18N = {},
Globals.oI18N = window['rainloopI18N'] || {};
Globals.oNotificationI18N = {},
Globals.oNotificationI18N = {};
Globals.aBootstrapDropdowns = [],
Globals.aBootstrapDropdowns = [];
Globals.aViewModels = {
'settings': [],
'settings-removed': [],
'settings-disabled': []
};
Globals.leftPanelDisabled = ko.observable(false);
// popups
Globals.popupVisibilityNames = ko.observableArray([]);
Globals.popupVisibility = ko.computed(function () {
return 0 < Globals.popupVisibilityNames().length;
}, this);
// keys
Globals.keyScopeReal = ko.observable(Enums.KeyState.All);
Globals.keyScopeFake = ko.observable(Enums.KeyState.All);
Globals.keyScope = ko.computed({
'owner': this,
'read': function () {
return Globals.keyScopeFake();
},
'write': function (sValue) {
if (Enums.KeyState.Menu !== sValue)
{
if (Enums.KeyState.Compose === sValue)
{
// disableKeyFilter
key.filter = function () {
return Globals.useKeyboardShortcuts();
};
}
else
{
// restoreKeyFilter
key.filter = function (event) {
if (Globals.useKeyboardShortcuts())
{
var
oElement = event.target || event.srcElement,
sTagName = oElement ? oElement.tagName : ''
;
sTagName = sTagName.toUpperCase();
return !(sTagName === 'INPUT' || sTagName === 'SELECT' || sTagName === 'TEXTAREA' ||
(oElement && sTagName === 'DIV' && 'editorHtmlArea' === oElement.className && oElement.contentEditable)
);
}
return false;
};
}
Globals.keyScopeFake(sValue);
if (Globals.dropdownVisibility())
{
sValue = Enums.KeyState.Menu;
}
}
Globals.keyScopeReal(sValue);
}
});
Globals.keyScopeReal.subscribe(function (sValue) {
// window.console.log(sValue);
key.setScope(sValue);
});
Globals.dropdownVisibility.subscribe(function (bValue) {
if (bValue)
{
Globals.tooltipTrigger(!Globals.tooltipTrigger());
Globals.keyScope(Enums.KeyState.Menu);
}
else if (Enums.KeyState.Menu === key.getScope())
{
Globals.keyScope(Globals.keyScopeFake());
}
});
module.exports = Globals;

View file

@ -6,7 +6,8 @@
var
window = require('../External/window.js'),
Utils = require('./Utils.js')
Utils = require('./Utils.js'),
AppSettings = require('../Storages/AppSettings.js')
;
/**
@ -16,9 +17,9 @@
{
this.sBase = '#/';
this.sServer = './?';
this.sVersion = RL.settingsGet('Version');
this.sSpecSuffix = RL.settingsGet('AuthAccountHash') || '0';
this.sStaticPrefix = RL.settingsGet('StaticPrefix') || 'rainloop/v/' + this.sVersion + '/static/';
this.sVersion = AppSettings.settingsGet('Version');
this.sSpecSuffix = AppSettings.settingsGet('AuthAccountHash') || '0';
this.sStaticPrefix = AppSettings.settingsGet('StaticPrefix') || 'rainloop/v/' + this.sVersion + '/static/';
}
/**

View file

@ -6,7 +6,8 @@
var
window = require('../External/window.js'),
Globals = require('./Globals.js')
Globals = require('./Globals.js'),
AppSettings = require('../Storages/AppSettings.js')
;
/**
@ -164,8 +165,8 @@
var
oConfig = Globals.oHtmlEditorDefaultConfig,
sLanguage = RL.settingsGet('Language'), // TODO cjs
bSource = !!RL.settingsGet('AllowHtmlEditorSourceButton')
sLanguage = AppSettings.settingsGet('Language'),
bSource = !!AppSettings.settingsGet('AllowHtmlEditorSourceButton')
;
if (bSource && oConfig.toolbarGroups && !oConfig.toolbarGroups.__SourceInited)

View file

@ -5,10 +5,13 @@
'use strict';
var
Plugins = {},
Utils = require('./Utils.js'),
Remote = require('../Remote.js'),
RL = require('../RL.js')
Plugins = {
__boot: null,
__remote: null,
__data: null
},
_ = require('../External/underscore.js'),
Utils = require('./Utils.js')
;
/**
@ -72,7 +75,12 @@
*/
Plugins.mainSettingsGet = function (sName)
{
return RL ? RL().settingsGet(sName) : null;
if (Plugins.__boot)
{
return Plugins.__boot.settingsGet(sName);
}
return null;
};
/**
@ -85,9 +93,9 @@
*/
Plugins.remoteRequest = function (fCallback, sAction, oParameters, iTimeout, sGetAdd, aAbortActions)
{
if (Remote)
if (Plugins.__remote)
{
Remote().defaultRequest(fCallback, sAction, oParameters, iTimeout, sGetAdd, aAbortActions);
Plugins.__remote.defaultRequest(fCallback, sAction, oParameters, iTimeout, sGetAdd, aAbortActions);
}
};

View file

@ -10,17 +10,14 @@
$ = require('../External/jquery.js'),
_ = require('../External/underscore.js'),
ko = require('../External/ko.js'),
key = require('../External/key.js'),
window = require('../External/window.js'),
$window = require('../External/$window.js'),
$html = require('../External/$html.js'),
$doc = require('../External/$doc.js'),
NotificationClass = require('../External/NotificationClass.js'),
LocalStorage = require('../Storages/LocalStorage.js'),
kn = require('../Knoin/Knoin.js'),
Enums = require('./Enums.js'),
Consts = require('./Consts.js'),
Globals = require('./Globals.js')
;
@ -154,24 +151,24 @@
Utils.exportPath = function (sPath, oObject, oObjectToExportTo)
{
var
part = null,
parts = sPath.split('.'),
cur = oObjectToExportTo || window
sPart = null,
aParts = sPath.split('.'),
oCur = oObjectToExportTo || window
;
for (; parts.length && (part = parts.shift());)
for (; aParts.length && (sPart = aParts.shift());)
{
if (!parts.length && !Utils.isUnd(oObject))
if (!aParts.length && !Utils.isUnd(oObject))
{
cur[part] = oObject;
oCur[sPart] = oObject;
}
else if (cur[part])
else if (oCur[sPart])
{
cur = cur[part];
oCur = oCur[sPart];
}
else
{
cur = cur[part] = {};
oCur = oCur[sPart] = {};
}
}
};
@ -694,7 +691,7 @@
*/
Utils.roundNumber = function (iNum, iDec)
{
return Math.round(iNum * Math.pow(10, iDec)) / Math.pow(10, iDec);
return window.Math.round(iNum * window.Math.pow(10, iDec)) / window.Math.pow(10, iDec);
};
/**
@ -1241,112 +1238,6 @@
}, oObject);
};
/**
* @param {string} sFullNameHash
* @return {boolean}
*/
Utils.isFolderExpanded = function (sFullNameHash)
{
var aExpandedList = /** @type {Array|null} */ LocalStorage.get(Enums.ClientSideKeyName.ExpandedFolders);
return _.isArray(aExpandedList) && -1 !== _.indexOf(aExpandedList, sFullNameHash);
};
/**
* @param {string} sFullNameHash
* @param {boolean} bExpanded
*/
Utils.setExpandedFolder = function (sFullNameHash, bExpanded)
{
var aExpandedList = /** @type {Array|null} */ LocalStorage.get(Enums.ClientSideKeyName.ExpandedFolders);
if (!_.isArray(aExpandedList))
{
aExpandedList = [];
}
if (bExpanded)
{
aExpandedList.push(sFullNameHash);
aExpandedList = _.uniq(aExpandedList);
}
else
{
aExpandedList = _.without(aExpandedList, sFullNameHash);
}
LocalStorage.set(Enums.ClientSideKeyName.ExpandedFolders, aExpandedList);
};
Utils.initLayoutResizer = function (sLeft, sRight, sClientSideKeyName)
{
var
iDisabledWidth = 60,
iMinWidth = 155,
oLeft = $(sLeft),
oRight = $(sRight),
mLeftWidth = LocalStorage.get(sClientSideKeyName) || null,
fSetWidth = function (iWidth) {
if (iWidth)
{
oLeft.css({
'width': '' + iWidth + 'px'
});
oRight.css({
'left': '' + iWidth + 'px'
});
}
},
fDisable = function (bDisable) {
if (bDisable)
{
oLeft.resizable('disable');
fSetWidth(iDisabledWidth);
}
else
{
oLeft.resizable('enable');
var iWidth = Utils.pInt(LocalStorage.get(sClientSideKeyName)) || iMinWidth;
fSetWidth(iWidth > iMinWidth ? iWidth : iMinWidth);
}
},
fResizeFunction = function (oEvent, oObject) {
if (oObject && oObject.size && oObject.size.width)
{
LocalStorage.set(sClientSideKeyName, oObject.size.width);
oRight.css({
'left': '' + oObject.size.width + 'px'
});
}
}
;
if (null !== mLeftWidth)
{
fSetWidth(mLeftWidth > iMinWidth ? mLeftWidth : iMinWidth);
}
oLeft.resizable({
'helper': 'ui-resizable-helper',
'minWidth': iMinWidth,
'maxWidth': 350,
'handles': 'e',
'stop': fResizeFunction
});
RL.sub('left-panel.off', function () {
fDisable(true);
});
RL.sub('left-panel.on', function () {
fDisable(false);
});
};
/**
* @param {Object} oMessageTextBody
*/
@ -1526,7 +1417,10 @@
Utils.i18nToNode(oBody);
kn.applyExternal(oViewModel, $('#rl-content', oBody)[0]);
if (oViewModel && $('#rl-content', oBody)[0])
{
ko.applyBindings(oViewModel, $('#rl-content', oBody)[0]);
}
window[sFunc] = null;
@ -1863,6 +1757,135 @@
oTempImg.src = sUrl;
};
/**
* @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
* @return {Array}
*/
Utils.folderListOptionsBuilder = function (aSystem, aList, aDisabled, aHeaderLines, iUnDeep, fDisableCallback, fVisibleCallback, fRenameCallback, bSystem, bBuildUnvisible)
{
var
/**
* @type {?FolderModel}
*/
oItem = null,
bSep = false,
iIndex = 0,
iLen = 0,
sDeepPrefix = '\u00A0\u00A0\u00A0',
aResult = []
;
bSystem = !Utils.isNormal(bSystem) ? 0 < aSystem.length : bSystem;
bBuildUnvisible = Utils.isUnd(bBuildUnvisible) ? false : !!bBuildUnvisible;
iUnDeep = !Utils.isNormal(iUnDeep) ? 0 : iUnDeep;
fDisableCallback = Utils.isNormal(fDisableCallback) ? fDisableCallback : null;
fVisibleCallback = Utils.isNormal(fVisibleCallback) ? fVisibleCallback : null;
fRenameCallback = Utils.isNormal(fRenameCallback) ? fRenameCallback : null;
if (!Utils.isArray(aDisabled))
{
aDisabled = [];
}
if (!Utils.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];
if (fVisibleCallback ? fVisibleCallback.call(null, oItem) : true)
{
if (bSep && 0 < aResult.length)
{
aResult.push({
'id': '---',
'name': '---',
'system': false,
'seporator': true,
'disabled': true
});
}
bSep = false;
aResult.push({
'id': oItem.fullNameRaw,
'name': fRenameCallback ? fRenameCallback.call(null, oItem) : oItem.name(),
'system': true,
'seporator': false,
'disabled': !oItem.selectable || -1 < Utils.inArray(oItem.fullNameRaw, aDisabled) ||
(fDisableCallback ? fDisableCallback.call(null, oItem) : false)
});
}
}
bSep = true;
for (iIndex = 0, iLen = aList.length; iIndex < iLen; iIndex++)
{
oItem = aList[iIndex];
if (oItem.subScribed() || !oItem.existen)
{
if (fVisibleCallback ? fVisibleCallback.call(null, oItem) : true)
{
if (Enums.FolderType.User === oItem.type() || !bSystem || 0 < oItem.subFolders().length)
{
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) +
(fRenameCallback ? fRenameCallback.call(null, oItem) : oItem.name()),
'system': false,
'seporator': false,
'disabled': !oItem.selectable || -1 < Utils.inArray(oItem.fullNameRaw, aDisabled) ||
(fDisableCallback ? fDisableCallback.call(null, oItem) : false)
});
}
}
}
if (oItem.subScribed() && 0 < oItem.subFolders().length)
{
aResult = aResult.concat(Utils.folderListOptionsBuilder([], oItem.subFolders(), aDisabled, [],
iUnDeep, fDisableCallback, fVisibleCallback, fRenameCallback, bSystem, bBuildUnvisible));
}
}
return aResult;
};
Utils.computedPagenatorHelper = function (koCurrentPage, koPageCount)
{
return function() {
@ -1949,7 +1972,7 @@
}
else if (3 < iPrev)
{
fAdd(Math.round((iPrev - 1) / 2), false, '...');
fAdd(window.Math.round((iPrev - 1) / 2), false, '...');
}
if (iPageCount - 2 === iNext)
@ -1958,7 +1981,7 @@
}
else if (iPageCount - 2 > iNext)
{
fAdd(Math.round((iPageCount + iNext) / 2), true, '...');
fAdd(window.Math.round((iPageCount + iNext) / 2), true, '...');
}
// first and last
@ -1984,53 +2007,19 @@
{
var sel = window.getSelection();
sel.removeAllRanges();
var range = document.createRange();
var range = window.document.createRange();
range.selectNodeContents(element);
sel.addRange(range);
}
else if (document.selection)
else if (window.document.selection)
{
var textRange = document.body.createTextRange();
var textRange = window.document.body.createTextRange();
textRange.moveToElementText(element);
textRange.select();
}
/* jshint onevar: true */
};
Utils.disableKeyFilter = function ()
{
if (window.key)
{
key.filter = function () {
return RL.data().useKeyboardShortcuts();
};
}
};
Utils.restoreKeyFilter = function ()
{
if (window.key)
{
key.filter = function (event) {
if (RL.data().useKeyboardShortcuts())
{
var
element = event.target || event.srcElement,
tagName = element ? element.tagName : ''
;
tagName = tagName.toUpperCase();
return !(tagName === 'INPUT' || tagName === 'SELECT' || tagName === 'TEXTAREA' ||
(element && tagName === 'DIV' && 'editorHtmlArea' === element.className && element.contentEditable)
);
}
return false;
};
}
};
Utils.detectDropdownVisibility = _.debounce(function () {
Globals.dropdownVisibility(!!_.find(Globals.aBootstrapDropdowns, function (oItem) {
return oItem.hasClass('open');