babeljs: step 2

This commit is contained in:
RainLoop Team 2015-11-18 20:32:29 +03:00
parent 007a03b8d4
commit 53cf543795
74 changed files with 2551 additions and 2963 deletions

View file

@ -7,15 +7,17 @@ import Events from 'Common/Events';
class Audio
{
player = null;
notificator = null;
supported = false;
supportedMp3 = false;
supportedOgg = false;
supportedWav = false;
supportedNotification = false;
constructor()
{
this.notificator = null;
this.supportedMp3 = false;
this.supportedOgg = false;
this.supportedWav = false;
this.supportedNotification = false;
this.player = this.createNewObject();
this.supported = !Globals.bMobileDevice && !Globals.bSafari && !!this.player && !!this.player.play;
@ -38,13 +40,9 @@ class Audio
if (this.supported)
{
$(this.player).on('ended error', () => {
this.stop();
});
$(this.player).on('ended error', () => this.stop());
Events.sub('audio.api.stop', () => {
this.stop();
});
Events.sub('audio.api.stop', () => this.stop());
}
}

View file

@ -8,9 +8,8 @@ const BASE_64_CHR = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456
const Base64 = {
// public method for urlsafe encoding
urlsafe_encode: (input) => {
return Base64.encode(input).replace(/[+]/g, '-').replace(/[\/]/g, '_').replace(/[=]/g, '.');
},
urlsafe_encode: (input) => Base64.encode(input)
.replace(/[+]/g, '-').replace(/[\/]/g, '_').replace(/[=]/g, '.'),
// public method for encoding
encode: (input) => {

View file

@ -1,412 +0,0 @@
(function () {
'use strict';
var
_ = require('_'),
Enums = require('Common/Enums'),
Utils = require('Common/Utils'),
Links = require('Common/Links'),
Settings = require('Storage/Settings')
;
/**
* @constructor
*/
function CacheUserStorage()
{
this.oFoldersCache = {};
this.oFoldersNamesCache = {};
this.oFolderHashCache = {};
this.oFolderUidNextCache = {};
this.oMessageListHashCache = {};
this.oMessageFlagsCache = {};
this.oNewMessage = {};
this.oRequestedMessage = {};
this.bCapaGravatar = Settings.capa(Enums.Capa.Gravatar);
}
/**
* @type {boolean}
*/
CacheUserStorage.prototype.bCapaGravatar = false;
/**
* @type {Object}
*/
CacheUserStorage.prototype.oFoldersCache = {};
/**
* @type {Object}
*/
CacheUserStorage.prototype.oFoldersNamesCache = {};
/**
* @type {Object}
*/
CacheUserStorage.prototype.oFolderHashCache = {};
/**
* @type {Object}
*/
CacheUserStorage.prototype.oFolderUidNextCache = {};
/**
* @type {Object}
*/
CacheUserStorage.prototype.oMessageListHashCache = {};
/**
* @type {Object}
*/
CacheUserStorage.prototype.oMessageFlagsCache = {};
/**
* @type {Object}
*/
CacheUserStorage.prototype.oNewMessage = {};
/**
* @type {Object}
*/
CacheUserStorage.prototype.oRequestedMessage = {};
CacheUserStorage.prototype.clear = function ()
{
this.oFoldersCache = {};
this.oFoldersNamesCache = {};
this.oFolderHashCache = {};
this.oFolderUidNextCache = {};
this.oMessageListHashCache = {};
this.oMessageFlagsCache = {};
};
/**
* @param {string} sEmail
* @param {Function} fCallback
* @return {string}
*/
CacheUserStorage.prototype.getUserPic = function (sEmail, fCallback)
{
sEmail = Utils.trim(sEmail);
fCallback(this.bCapaGravatar && '' !== sEmail ? Links.avatarLink(sEmail) : '', sEmail);
};
/**
* @param {string} sFolderFullNameRaw
* @param {string} sUid
* @return {string}
*/
CacheUserStorage.prototype.getMessageKey = function (sFolderFullNameRaw, sUid)
{
return sFolderFullNameRaw + '#' + sUid;
};
/**
* @param {string} sFolder
* @param {string} sUid
*/
CacheUserStorage.prototype.addRequestedMessage = function (sFolder, sUid)
{
this.oRequestedMessage[this.getMessageKey(sFolder, sUid)] = true;
};
/**
* @param {string} sFolder
* @param {string} sUid
* @return {boolean}
*/
CacheUserStorage.prototype.hasRequestedMessage = function (sFolder, sUid)
{
return true === this.oRequestedMessage[this.getMessageKey(sFolder, sUid)];
};
/**
* @param {string} sFolderFullNameRaw
* @param {string} sUid
*/
CacheUserStorage.prototype.addNewMessageCache = function (sFolderFullNameRaw, sUid)
{
this.oNewMessage[this.getMessageKey(sFolderFullNameRaw, sUid)] = true;
};
/**
* @param {string} sFolderFullNameRaw
* @param {string} sUid
*/
CacheUserStorage.prototype.hasNewMessageAndRemoveFromCache = function (sFolderFullNameRaw, sUid)
{
if (this.oNewMessage[this.getMessageKey(sFolderFullNameRaw, sUid)])
{
this.oNewMessage[this.getMessageKey(sFolderFullNameRaw, sUid)] = null;
return true;
}
return false;
};
CacheUserStorage.prototype.clearNewMessageCache = function ()
{
this.oNewMessage = {};
};
/**
* @type {string}
*/
CacheUserStorage.prototype.sInboxFolderName = '';
/**
* @return {string}
*/
CacheUserStorage.prototype.getFolderInboxName = function ()
{
return '' === this.sInboxFolderName ? 'INBOX' : this.sInboxFolderName;
};
/**
* @param {string} sFolderHash
* @return {string}
*/
CacheUserStorage.prototype.getFolderFullNameRaw = function (sFolderHash)
{
return '' !== sFolderHash && this.oFoldersNamesCache[sFolderHash] ? this.oFoldersNamesCache[sFolderHash] : '';
};
/**
* @param {string} sFolderHash
* @param {string} sFolderFullNameRaw
*/
CacheUserStorage.prototype.setFolderFullNameRaw = function (sFolderHash, sFolderFullNameRaw)
{
this.oFoldersNamesCache[sFolderHash] = sFolderFullNameRaw;
if ('INBOX' === sFolderFullNameRaw || '' === this.sInboxFolderName)
{
this.sInboxFolderName = sFolderFullNameRaw;
}
};
/**
* @param {string} sFolderFullNameRaw
* @return {string}
*/
CacheUserStorage.prototype.getFolderHash = function (sFolderFullNameRaw)
{
return '' !== sFolderFullNameRaw && this.oFolderHashCache[sFolderFullNameRaw] ? this.oFolderHashCache[sFolderFullNameRaw] : '';
};
/**
* @param {string} sFolderFullNameRaw
* @param {string} sFolderHash
*/
CacheUserStorage.prototype.setFolderHash = function (sFolderFullNameRaw, sFolderHash)
{
if ('' !== sFolderFullNameRaw)
{
this.oFolderHashCache[sFolderFullNameRaw] = sFolderHash;
}
};
/**
* @param {string} sFolderFullNameRaw
* @return {string}
*/
CacheUserStorage.prototype.getFolderUidNext = function (sFolderFullNameRaw)
{
return '' !== sFolderFullNameRaw && this.oFolderUidNextCache[sFolderFullNameRaw] ? this.oFolderUidNextCache[sFolderFullNameRaw] : '';
};
/**
* @param {string} sFolderFullNameRaw
* @param {string} sUidNext
*/
CacheUserStorage.prototype.setFolderUidNext = function (sFolderFullNameRaw, sUidNext)
{
this.oFolderUidNextCache[sFolderFullNameRaw] = sUidNext;
};
/**
* @param {string} sFolderFullNameRaw
* @return {?FolderModel}
*/
CacheUserStorage.prototype.getFolderFromCacheList = function (sFolderFullNameRaw)
{
return '' !== sFolderFullNameRaw && this.oFoldersCache[sFolderFullNameRaw] ? this.oFoldersCache[sFolderFullNameRaw] : null;
};
/**
* @param {string} sFolderFullNameRaw
* @param {?FolderModel} oFolder
*/
CacheUserStorage.prototype.setFolderToCacheList = function (sFolderFullNameRaw, oFolder)
{
this.oFoldersCache[sFolderFullNameRaw] = oFolder;
};
/**
* @param {string} sFolderFullNameRaw
*/
CacheUserStorage.prototype.removeFolderFromCacheList = function (sFolderFullNameRaw)
{
this.setFolderToCacheList(sFolderFullNameRaw, null);
};
/**
* @param {string} sFolderFullName
* @param {string} sUid
* @return {?Array}
*/
CacheUserStorage.prototype.getMessageFlagsFromCache = function (sFolderFullName, sUid)
{
return this.oMessageFlagsCache[sFolderFullName] && this.oMessageFlagsCache[sFolderFullName][sUid] ?
this.oMessageFlagsCache[sFolderFullName][sUid] : null;
};
/**
* @param {string} sFolderFullName
* @param {string} sUid
* @param {Array} aFlagsCache
*/
CacheUserStorage.prototype.setMessageFlagsToCache = function (sFolderFullName, sUid, aFlagsCache)
{
if (!this.oMessageFlagsCache[sFolderFullName])
{
this.oMessageFlagsCache[sFolderFullName] = {};
}
this.oMessageFlagsCache[sFolderFullName][sUid] = aFlagsCache;
};
/**
* @param {string} sFolderFullName
*/
CacheUserStorage.prototype.clearMessageFlagsFromCacheByFolder = function (sFolderFullName)
{
this.oMessageFlagsCache[sFolderFullName] = {};
};
/**
* @param {(MessageModel|null)} oMessage
*/
CacheUserStorage.prototype.initMessageFlagsFromCache = function (oMessage)
{
if (oMessage)
{
var
self = this,
sUid = oMessage.uid,
aFlags = this.getMessageFlagsFromCache(oMessage.folderFullNameRaw, sUid),
mUnseenSubUid = null,
mFlaggedSubUid = null
;
if (aFlags && 0 < aFlags.length)
{
oMessage.flagged(!!aFlags[1]);
if (!oMessage.__simple_message__)
{
oMessage.unseen(!!aFlags[0]);
oMessage.answered(!!aFlags[2]);
oMessage.forwarded(!!aFlags[3]);
oMessage.isReadReceipt(!!aFlags[4]);
oMessage.deletedMark(!!aFlags[5]);
}
}
if (0 < oMessage.threads().length)
{
mUnseenSubUid = _.find(oMessage.threads(), function (sSubUid) {
if (sUid === sSubUid){
return false;
}
var aFlags = self.getMessageFlagsFromCache(oMessage.folderFullNameRaw, sSubUid);
return aFlags && 0 < aFlags.length && !!aFlags[0];
});
mFlaggedSubUid = _.find(oMessage.threads(), function (sSubUid) {
if (sUid === sSubUid){
return false;
}
var aFlags = self.getMessageFlagsFromCache(oMessage.folderFullNameRaw, sSubUid);
return aFlags && 0 < aFlags.length && !!aFlags[1];
});
oMessage.hasUnseenSubMessage(mUnseenSubUid && 0 < Utils.pInt(mUnseenSubUid));
oMessage.hasFlaggedSubMessage(mFlaggedSubUid && 0 < Utils.pInt(mFlaggedSubUid));
}
}
};
/**
* @param {(MessageModel|null)} oMessage
*/
CacheUserStorage.prototype.storeMessageFlagsToCache = function (oMessage)
{
if (oMessage)
{
this.setMessageFlagsToCache(
oMessage.folderFullNameRaw,
oMessage.uid,
[oMessage.unseen(), oMessage.flagged(), oMessage.answered(), oMessage.forwarded(),
oMessage.isReadReceipt(), oMessage.deletedMark()]
);
}
};
/**
* @param {string} sFolder
* @param {string} sUid
* @param {Array} aFlags
*/
CacheUserStorage.prototype.storeMessageFlagsToCacheByFolderAndUid = function (sFolder, sUid, aFlags)
{
if (Utils.isArray(aFlags) && 0 < aFlags.length)
{
this.setMessageFlagsToCache(sFolder, sUid, aFlags);
}
};
/**
* @param {string} sFolder
* @param {string} sUid
* @param {number} iSetAction
*/
CacheUserStorage.prototype.storeMessageFlagsToCacheBySetAction = function (sFolder, sUid, iSetAction)
{
var iUnread = 0, aFlags = this.getMessageFlagsFromCache(sFolder, sUid);
if (Utils.isArray(aFlags) && 0 < aFlags.length)
{
if (aFlags[0])
{
iUnread = 1;
}
switch (iSetAction)
{
case Enums.MessageSetAction.SetSeen:
aFlags[0] = false;
break;
case Enums.MessageSetAction.UnsetSeen:
aFlags[0] = true;
break;
case Enums.MessageSetAction.SetFlag:
aFlags[1] = true;
break;
case Enums.MessageSetAction.UnsetFlag:
aFlags[1] = false;
break;
}
this.setMessageFlagsToCache(sFolder, sUid, aFlags);
}
return iUnread;
};
module.exports = new CacheUserStorage();
}());

329
dev/Common/Cache.jsx Normal file
View file

@ -0,0 +1,329 @@
import {_} from 'common';
import {Capa, MessageSetAction} from 'Common/Enums';
import Utils from 'Common/Utils';
import Links from 'Common/Links';
import Settings from 'Storage/Settings';
class CacheUserStorage
{
oFoldersCache = {};
oFoldersNamesCache = {};
oFolderHashCache = {};
oFolderUidNextCache = {};
oMessageListHashCache = {};
oMessageFlagsCache = {};
oNewMessage = {};
oRequestedMessage = {};
bCapaGravatar = false;
inboxFolderName = '';
constructor()
{
this.bCapaGravatar = Settings.capa(Capa.Gravatar);
}
clear() {
this.oFoldersCache = {};
this.oFoldersNamesCache = {};
this.oFolderHashCache = {};
this.oFolderUidNextCache = {};
this.oMessageListHashCache = {};
this.oMessageFlagsCache = {};
}
/**
* @param {string} email
* @param {Function} callback
* @return {string}
*/
getUserPic(email, callback) {
email = Utils.trim(email);
callback(this.bCapaGravatar && '' !== email ? Links.avatarLink(email) : '', email);
}
/**
* @param {string} folderFullNameRaw
* @param {string} uid
* @return {string}
*/
getMessageKey(folderFullNameRaw, uid) {
return `${folderFullNameRaw}#${uid}`;
}
/**
* @param {string} folder
* @param {string} uid
*/
addRequestedMessage(folder, uid) {
this.oRequestedMessage[this.getMessageKey(folder, uid)] = true;
}
/**
* @param {string} folder
* @param {string} uid
* @return {boolean}
*/
hasRequestedMessage(folder, uid) {
return true === this.oRequestedMessage[this.getMessageKey(folder, uid)];
}
/**
* @param {string} folderFullNameRaw
* @param {string} uid
*/
addNewMessageCache(folderFullNameRaw, uid) {
this.oNewMessage[this.getMessageKey(folderFullNameRaw, uid)] = true;
}
/**
* @param {string} folderFullNameRaw
* @param {string} uid
*/
hasNewMessageAndRemoveFromCache(folderFullNameRaw, uid) {
if (this.oNewMessage[this.getMessageKey(folderFullNameRaw, uid)])
{
this.oNewMessage[this.getMessageKey(folderFullNameRaw, uid)] = null;
return true;
}
return false;
}
clearNewMessageCache() {
this.oNewMessage = {};
}
/**
* @return {string}
*/
getFolderInboxName() {
return '' === this.inboxFolderName ? 'INBOX' : this.inboxFolderName;
}
/**
* @param {string} folderHash
* @return {string}
*/
getFolderFullNameRaw(folderHash) {
return '' !== folderHash && this.oFoldersNamesCache[folderHash] ? this.oFoldersNamesCache[folderHash] : '';
}
/**
* @param {string} folderHash
* @param {string} folderFullNameRaw
*/
setFolderFullNameRaw(folderHash, folderFullNameRaw) {
this.oFoldersNamesCache[folderHash] = folderFullNameRaw;
if ('INBOX' === folderFullNameRaw || '' === this.inboxFolderName)
{
this.inboxFolderName = folderFullNameRaw;
}
}
/**
* @param {string} folderFullNameRaw
* @return {string}
*/
getFolderHash(folderFullNameRaw) {
return '' !== folderFullNameRaw && this.oFolderHashCache[folderFullNameRaw] ? this.oFolderHashCache[folderFullNameRaw] : '';
}
/**
* @param {string} folderFullNameRaw
* @param {string} folderHash
*/
setFolderHash(folderFullNameRaw, folderHash) {
if ('' !== folderFullNameRaw)
{
this.oFolderHashCache[folderFullNameRaw] = folderHash;
}
}
/**
* @param {string} folderFullNameRaw
* @return {string}
*/
getFolderUidNext(folderFullNameRaw) {
return '' !== folderFullNameRaw && this.oFolderUidNextCache[folderFullNameRaw] ? this.oFolderUidNextCache[folderFullNameRaw] : '';
}
/**
* @param {string} folderFullNameRaw
* @param {string} uidNext
*/
setFolderUidNext(folderFullNameRaw, uidNext) {
this.oFolderUidNextCache[folderFullNameRaw] = uidNext;
}
/**
* @param {string} folderFullNameRaw
* @return {?FolderModel}
*/
getFolderFromCacheList(folderFullNameRaw) {
return '' !== folderFullNameRaw && this.oFoldersCache[folderFullNameRaw] ? this.oFoldersCache[folderFullNameRaw] : null;
}
/**
* @param {string} folderFullNameRaw
* @param {?FolderModel} folder
*/
setFolderToCacheList(folderFullNameRaw, folder) {
this.oFoldersCache[folderFullNameRaw] = folder;
}
/**
* @param {string} folderFullNameRaw
*/
removeFolderFromCacheList(folderFullNameRaw) {
this.setFolderToCacheList(folderFullNameRaw, null);
}
/**
* @param {string} folderFullName
* @param {string} uid
* @return {?Array}
*/
getMessageFlagsFromCache(folderFullName, uid) {
return this.oMessageFlagsCache[folderFullName] && this.oMessageFlagsCache[folderFullName][uid] ?
this.oMessageFlagsCache[folderFullName][uid] : null;
}
/**
* @param {string} folderFullName
* @param {string} uid
* @param {Array} flagsCache
*/
setMessageFlagsToCache(folderFullName, uid, flagsCache) {
if (!this.oMessageFlagsCache[folderFullName])
{
this.oMessageFlagsCache[folderFullName] = {};
}
this.oMessageFlagsCache[folderFullName][uid] = flagsCache;
}
/**
* @param {string} folderFullName
*/
clearMessageFlagsFromCacheByFolder(folderFullName) {
this.oMessageFlagsCache[folderFullName] = {};
}
/**
* @param {(MessageModel|null)} message
*/
initMessageFlagsFromCache(message) {
if (message)
{
const
uid = message.uid,
flags = this.getMessageFlagsFromCache(message.folderFullNameRaw, uid)
;
if (flags && 0 < flags.length)
{
message.flagged(!!flags[1]);
if (!message.__simple_message__)
{
message.unseen(!!flags[0]);
message.answered(!!flags[2]);
message.forwarded(!!flags[3]);
message.isReadReceipt(!!flags[4]);
message.deletedMark(!!flags[5]);
}
}
if (0 < message.threads().length)
{
const unseenSubUid = _.find(message.threads(), (sSubUid) => {
if (uid !== sSubUid){
const flags = this.getMessageFlagsFromCache(message.folderFullNameRaw, sSubUid);
return flags && 0 < flags.length && !!flags[0];
}
return false;
});
const flaggedSubUid = _.find(message.threads(), (sSubUid) => {
if (uid !== sSubUid) {
const flags = this.getMessageFlagsFromCache(message.folderFullNameRaw, sSubUid);
return flags && 0 < flags.length && !!flags[1];
}
return false;
});
message.hasUnseenSubMessage(unseenSubUid && 0 < Utils.pInt(unseenSubUid));
message.hasFlaggedSubMessage(flaggedSubUid && 0 < Utils.pInt(flaggedSubUid));
}
}
}
/**
* @param {(MessageModel|null)} message
*/
storeMessageFlagsToCache(message) {
if (message)
{
this.setMessageFlagsToCache(
message.folderFullNameRaw, message.uid,
[message.unseen(), message.flagged(), message.answered(), message.forwarded(),
message.isReadReceipt(), message.deletedMark()]
);
}
}
/**
* @param {string} folder
* @param {string} uid
* @param {Array} flags
*/
storeMessageFlagsToCacheByFolderAndUid(folder, uid, flags) {
if (Utils.isArray(flags) && 0 < flags.length)
{
this.setMessageFlagsToCache(folder, uid, flags);
}
}
/**
* @param {string} folder
* @param {string} uid
* @param {number} setAction
*/
storeMessageFlagsToCacheBySetAction(folder, uid, setAction) {
let unread = 0;
const flags = this.getMessageFlagsFromCache(folder, uid);
if (Utils.isArray(flags) && 0 < flags.length)
{
if (flags[0])
{
unread = 1;
}
switch (setAction)
{
case MessageSetAction.SetSeen:
flags[0] = false;
break;
case MessageSetAction.UnsetSeen:
flags[0] = true;
break;
case MessageSetAction.SetFlag:
flags[1] = true;
break;
case MessageSetAction.UnsetFlag:
flags[1] = false;
break;
}
this.setMessageFlagsToCache(folder, uid, flags);
}
return unread;
}
}
module.exports = new CacheUserStorage();

View file

@ -5,9 +5,9 @@ import Plugins from 'Common/Plugins';
class Events
{
constructor() {
this.subs = {};
}
subs = {};
constructor() {}
/**
* @param {string|Object} name

View file

@ -1,435 +0,0 @@
(function () {
'use strict';
var
window = require('window'),
_ = require('_'),
Globals = require('Common/Globals'),
Settings = require('Storage/Settings')
;
/**
* @constructor
* @param {Object} oElement
* @param {Function=} fOnBlur
* @param {Function=} fOnReady
* @param {Function=} fOnModeChange
*/
function HtmlEditor(oElement, fOnBlur, fOnReady, fOnModeChange)
{
this.editor = null;
this.iBlurTimer = 0;
this.fOnBlur = fOnBlur || null;
this.fOnReady = fOnReady || null;
this.fOnModeChange = fOnModeChange || null;
this.$element = $(oElement);
this.resize = _.throttle(_.bind(this.resize, this), 100);
this.__inited = false;
this.init();
}
HtmlEditor.prototype.blurTrigger = function ()
{
if (this.fOnBlur)
{
var self = this;
window.clearTimeout(this.iBlurTimer);
this.iBlurTimer = window.setTimeout(function () {
self.fOnBlur();
}, 200);
}
};
HtmlEditor.prototype.focusTrigger = function ()
{
if (this.fOnBlur)
{
window.clearTimeout(this.iBlurTimer);
}
};
/**
* @return {boolean}
*/
HtmlEditor.prototype.isHtml = function ()
{
return this.editor ? 'wysiwyg' === this.editor.mode : false;
};
/**
* @param {string} sSignature
* @param {bool} bHtml
* @param {bool} bInsertBefore
*/
HtmlEditor.prototype.setSignature = function (sSignature, bHtml, bInsertBefore)
{
if (this.editor)
{
this.editor.execCommand('insertSignature', {
'isHtml': bHtml,
'insertBefore': bInsertBefore,
'signature': sSignature
});
}
};
/**
* @return {boolean}
*/
HtmlEditor.prototype.checkDirty = function ()
{
return this.editor ? this.editor.checkDirty() : false;
};
HtmlEditor.prototype.resetDirty = function ()
{
if (this.editor)
{
this.editor.resetDirty();
}
};
/**
* @param {string} sText
* @return {string}
*/
HtmlEditor.prototype.clearSignatureSigns = function (sText)
{
return sText.replace(/(\u200C|\u0002)/g, '');
};
/**
* @param {boolean=} bWrapIsHtml = false
* @param {boolean=} bClearSignatureSigns = false
* @return {string}
*/
HtmlEditor.prototype.getData = function (bWrapIsHtml, bClearSignatureSigns)
{
var sResult = '';
if (this.editor)
{
try
{
if ('plain' === this.editor.mode && this.editor.plugins.plain && this.editor.__plain)
{
sResult = this.editor.__plain.getRawData();
}
else
{
sResult = bWrapIsHtml ?
'<div data-html-editor-font-wrapper="true" style="font-family: arial, sans-serif; font-size: 13px;">' +
this.editor.getData() + '</div>' : this.editor.getData();
}
}
catch (e) {}
if (bClearSignatureSigns)
{
sResult = this.clearSignatureSigns(sResult);
}
}
return sResult;
};
/**
* @param {boolean=} bWrapIsHtml = false
* @param {boolean=} bClearSignatureSigns = false
* @return {string}
*/
HtmlEditor.prototype.getDataWithHtmlMark = function (bWrapIsHtml, bClearSignatureSigns)
{
return (this.isHtml() ? ':HTML:' : '') + this.getData(bWrapIsHtml, bClearSignatureSigns);
};
HtmlEditor.prototype.modeToggle = function (bPlain, bResize)
{
if (this.editor)
{
try {
if (bPlain)
{
if ('plain' === this.editor.mode)
{
this.editor.setMode('wysiwyg');
}
}
else
{
if ('wysiwyg' === this.editor.mode)
{
this.editor.setMode('plain');
}
}
} catch(e) {}
if (bResize)
{
this.resize();
}
}
};
HtmlEditor.prototype.setHtmlOrPlain = function (sText, bFocus)
{
if (':HTML:' === sText.substr(0, 6))
{
this.setHtml(sText.substr(6), bFocus);
}
else
{
this.setPlain(sText, bFocus);
}
};
HtmlEditor.prototype.setHtml = function (sHtml, bFocus)
{
if (this.editor && this.__inited)
{
this.modeToggle(true);
sHtml = sHtml.replace(/<p[^>]*><\/p>/ig, '');
try {
this.editor.setData(sHtml);
} catch (e) {}
if (bFocus)
{
this.focus();
}
}
};
HtmlEditor.prototype.replaceHtml = function (mFind, sReplaceHtml)
{
if (this.editor && this.__inited && 'wysiwyg' === this.editor.mode)
{
try {
this.editor.setData(
this.editor.getData().replace(mFind, sReplaceHtml));
} catch (e) {}
}
};
HtmlEditor.prototype.setPlain = function (sPlain, bFocus)
{
if (this.editor && this.__inited)
{
this.modeToggle(false);
if ('plain' === this.editor.mode && this.editor.plugins.plain && this.editor.__plain)
{
return this.editor.__plain.setRawData(sPlain);
}
else
{
try {
this.editor.setData(sPlain);
} catch (e) {}
}
if (bFocus)
{
this.focus();
}
}
};
HtmlEditor.prototype.init = function ()
{
if (this.$element && this.$element[0] && !this.editor)
{
var
self = this,
fInit = function () {
var
oConfig = Globals.oHtmlEditorDefaultConfig,
sLanguage = Settings.settingsGet('Language'),
bSource = !!Settings.settingsGet('AllowHtmlEditorSourceButton'),
bBiti = !!Settings.settingsGet('AllowHtmlEditorBitiButtons')
;
if ((bSource || !bBiti) && !oConfig.toolbarGroups.__cfgInited)
{
oConfig.toolbarGroups.__cfgInited = true;
if (bSource)
{
oConfig.removeButtons = oConfig.removeButtons.replace(',Source', '');
}
if (!bBiti)
{
oConfig.removePlugins += (oConfig.removePlugins ? ',' : '') + 'bidi';
}
}
oConfig.enterMode = window.CKEDITOR.ENTER_BR;
oConfig.shiftEnterMode = window.CKEDITOR.ENTER_P;
oConfig.language = Globals.oHtmlEditorLangsMap[sLanguage] || 'en';
if (window.CKEDITOR.env)
{
window.CKEDITOR.env.isCompatible = true;
}
self.editor = window.CKEDITOR.appendTo(self.$element[0], oConfig);
self.editor.on('key', function(oEvent) {
if (oEvent && oEvent.data && 9 /* Tab */ === oEvent.data.keyCode)
{
return false;
}
});
self.editor.on('blur', function() {
self.blurTrigger();
});
self.editor.on('mode', function() {
self.blurTrigger();
if (self.fOnModeChange)
{
self.fOnModeChange('plain' !== self.editor.mode);
}
});
self.editor.on('focus', function() {
self.focusTrigger();
});
if (window.FileReader)
{
self.editor.on('drop', function(evt) {
if (0 < evt.data.dataTransfer.getFilesCount())
{
var file = evt.data.dataTransfer.getFile(0);
if (file && window.FileReader && evt.data.dataTransfer.id &&
file.type && file.type.match(/^image/i))
{
var
id = evt.data.dataTransfer.id,
imageId = '[img=' + id +']',
reader = new window.FileReader()
;
reader.onloadend = function () {
if (reader.result)
{
self.replaceHtml(imageId, '<img src="' + reader.result + '" />');
}
};
reader.readAsDataURL(file);
evt.data.dataTransfer.setData('text/html', imageId);
}
}
});
}
self.editor.on('instanceReady', function () {
if (self.editor.removeMenuItem)
{
self.editor.removeMenuItem('cut');
self.editor.removeMenuItem('copy');
self.editor.removeMenuItem('paste');
}
self.__resizable = true;
self.__inited = true;
self.resize();
if (self.fOnReady)
{
self.fOnReady();
}
});
}
;
if (window.CKEDITOR)
{
fInit();
}
else
{
window.__initEditor = fInit;
}
}
};
HtmlEditor.prototype.focus = function ()
{
if (this.editor)
{
try {
this.editor.focus();
} catch (e) {}
}
};
HtmlEditor.prototype.hasFocus = function ()
{
if (this.editor)
{
try {
return !!this.editor.focusManager.hasFocus;
} catch (e) {}
}
return false;
};
HtmlEditor.prototype.blur = function ()
{
if (this.editor)
{
try {
this.editor.focusManager.blur(true);
} catch (e) {}
}
};
HtmlEditor.prototype.resize = function ()
{
if (this.editor && this.__resizable)
{
try {
this.editor.resize(this.$element.width(), this.$element.innerHeight());
} catch (e) {}
}
};
HtmlEditor.prototype.setReadOnly = function (bValue)
{
if (this.editor)
{
try {
this.editor.setReadOnly(!!bValue);
} catch (e) {}
}
};
HtmlEditor.prototype.clear = function (bFocus)
{
this.setHtml('', bFocus);
};
module.exports = HtmlEditor;
}());

412
dev/Common/HtmlEditor.jsx Normal file
View file

@ -0,0 +1,412 @@
import {window, _} from 'common';
import Globals from 'Common/Globals';
import Settings from 'Storage/Settings';
class HtmlEditor
{
editor = null;
$element = null;
blurTimer = 0;
onBlur = null;
onReady = null;
onModeChange = null;
__inited = null;
/**
* @param {Object} element
* @param {Function=} onBlur
* @param {Function=} onReady
* @param {Function=} onModeChange
*/
constructor(element, onBlur = null, onReady = null, onModeChange = null)
{
this.onBlur = onBlur;
this.onReady = onReady;
this.onModeChange = onModeChange;
this.$element = $(element);
this.resize = _.throttle(_.bind(this.resize, this), 100);
this.__inited = false;
this.init();
}
blurTrigger() {
if (this.onBlur)
{
window.clearTimeout(this.blurTimer);
this.blurTimer = window.setTimeout(() => {
this.onBlur();
}, 200);
}
}
focusTrigger() {
if (this.onBlur)
{
window.clearTimeout(this.blurTimer);
}
}
/**
* @return {boolean}
*/
isHtml() {
return this.editor ? 'wysiwyg' === this.editor.mode : false;
}
/**
* @param {string} signature
* @param {bool} html
* @param {bool} insertBefore
*/
setSignature(signature, html, insertBefore) {
if (this.editor)
{
this.editor.execCommand('insertSignature', {
'isHtml': html,
'insertBefore': insertBefore,
'signature': signature
});
}
}
/**
* @return {boolean}
*/
checkDirty() {
return this.editor ? this.editor.checkDirty() : false;
}
resetDirty() {
if (this.editor)
{
this.editor.resetDirty();
}
}
/**
* @param {string} text
* @return {string}
*/
clearSignatureSigns(text) {
return text.replace(/(\u200C|\u0002)/g, '');
}
/**
* @param {boolean=} wrapIsHtml = false
* @param {boolean=} clearSignatureSigns = false
* @return {string}
*/
getData(wrapIsHtml = false, clearSignatureSigns = false) {
let result = '';
if (this.editor)
{
try
{
if ('plain' === this.editor.mode && this.editor.plugins.plain && this.editor.__plain)
{
result = this.editor.__plain.getRawData();
}
else
{
result = wrapIsHtml ?
'<div data-html-editor-font-wrapper="true" style="font-family: arial, sans-serif; font-size: 13px;">' +
this.editor.getData() + '</div>' : this.editor.getData();
}
}
catch (e) {}
if (clearSignatureSigns)
{
result = this.clearSignatureSigns(result);
}
}
return result;
}
/**
* @param {boolean=} wrapIsHtml = false
* @param {boolean=} clearSignatureSigns = false
* @return {string}
*/
getDataWithHtmlMark(wrapIsHtml = false, clearSignatureSigns = false) {
return (this.isHtml() ? ':HTML:' : '') + this.getData(wrapIsHtml, clearSignatureSigns);
}
modeToggle(plain, resize) {
if (this.editor)
{
try {
if (plain)
{
if ('plain' === this.editor.mode)
{
this.editor.setMode('wysiwyg');
}
}
else
{
if ('wysiwyg' === this.editor.mode)
{
this.editor.setMode('plain');
}
}
} catch(e) {}
if (resize)
{
this.resize();
}
}
}
setHtmlOrPlain(text, focus) {
if (':HTML:' === text.substr(0, 6))
{
this.setHtml(text.substr(6), focus);
}
else
{
this.setPlain(text, focus);
}
}
setHtml(html, focus) {
if (this.editor && this.__inited)
{
this.modeToggle(true);
html = html.replace(/<p[^>]*><\/p>/ig, '');
try {
this.editor.setData(html);
} catch (e) {}
if (focus)
{
this.focus();
}
}
}
replaceHtml(find, replaceHtml) {
if (this.editor && this.__inited && 'wysiwyg' === this.editor.mode)
{
try {
this.editor.setData(
this.editor.getData().replace(find, replaceHtml));
} catch (e) {}
}
}
setPlain(plain, focus) {
if (this.editor && this.__inited)
{
this.modeToggle(false);
if ('plain' === this.editor.mode && this.editor.plugins.plain && this.editor.__plain)
{
return this.editor.__plain.setRawData(plain);
}
else
{
try {
this.editor.setData(plain);
} catch (e) {}
}
if (focus)
{
this.focus();
}
}
}
init() {
if (this.$element && this.$element[0] && !this.editor)
{
const
initFunc = () => {
const
config = Globals.oHtmlEditorDefaultConfig,
language = Settings.settingsGet('Language'),
allowSource = !!Settings.settingsGet('AllowHtmlEditorSourceButton'),
biti = !!Settings.settingsGet('AllowHtmlEditorBitiButtons')
;
if ((allowSource || !biti) && !config.toolbarGroups.__cfgInited)
{
config.toolbarGroups.__cfgInited = true;
if (allowSource)
{
config.removeButtons = config.removeButtons.replace(',Source', '');
}
if (!biti)
{
config.removePlugins += (config.removePlugins ? ',' : '') + 'bidi';
}
}
config.enterMode = window.CKEDITOR.ENTER_BR;
config.shiftEnterMode = window.CKEDITOR.ENTER_P;
config.language = Globals.oHtmlEditorLangsMap[language] || 'en';
if (window.CKEDITOR.env)
{
window.CKEDITOR.env.isCompatible = true;
}
this.editor = window.CKEDITOR.appendTo(this.$element[0], config);
this.editor.on('key', (event) => {
if (event && event.data && 9 /* Tab */ === event.data.keyCode)
{
return false;
}
});
this.editor.on('blur', () => {
this.blurTrigger();
});
this.editor.on('mode', () => {
this.blurTrigger();
if (this.onModeChange)
{
this.onModeChange('plain' !== this.editor.mode);
}
});
this.editor.on('focus', () => {
this.focusTrigger();
});
if (window.FileReader)
{
this.editor.on('drop', (event) => {
if (0 < event.data.dataTransfer.getFilesCount())
{
const file = event.data.dataTransfer.getFile(0);
if (file && window.FileReader && event.data.dataTransfer.id &&
file.type && file.type.match(/^image/i))
{
var
id = event.data.dataTransfer.id,
imageId = `[img=${id}]`,
reader = new window.FileReader()
;
reader.onloadend = () => {
if (reader.result)
{
this.replaceHtml(imageId, `<img src="${reader.result}" />`);
}
};
reader.readAsDataURL(file);
event.data.dataTransfer.setData('text/html', imageId);
}
}
});
}
this.editor.on('instanceReady', () => {
if (this.editor.removeMenuItem)
{
this.editor.removeMenuItem('cut');
this.editor.removeMenuItem('copy');
this.editor.removeMenuItem('paste');
}
this.__resizable = true;
this.__inited = true;
this.resize();
if (this.onReady)
{
this.onReady();
}
});
}
;
if (window.CKEDITOR)
{
initFunc();
}
else
{
window.__initEditor = initFunc;
}
}
}
focus() {
if (this.editor)
{
try {
this.editor.focus();
} catch (e) {}
}
}
hasFocus() {
if (this.editor)
{
try {
return !!this.editor.focusManager.hasFocus;
} catch (e) {}
}
return false;
}
blur() {
if (this.editor)
{
try {
this.editor.focusManager.blur(true);
} catch (e) {}
}
}
resize() {
if (this.editor && this.__resizable)
{
try {
this.editor.resize(this.$element.width(), this.$element.innerHeight());
} catch (e) {}
}
}
setReadOnly(value) {
if (this.editor)
{
try {
this.editor.setReadOnly(!!value);
} catch (e) {}
}
}
clear(focus) {
this.setHtml('', focus);
}
}
export {HtmlEditor, HtmlEditor as default};
module.exports = HtmlEditor;

View file

@ -4,11 +4,11 @@ import Translator from 'Common/Translator';
class Momentor
{
_moment = null;
_momentNow = 0;
constructor()
{
this._moment = null;
this._momentNow = 0;
this.updateMomentNow = _.debounce(() => {
this._moment = moment();
}, 500, true);

View file

@ -6,11 +6,11 @@ import Settings from 'Storage/Settings';
class Plugins
{
constructor() {
this.oSimpleHooks = {};
this.aUserViewModelsHooks = [];
this.aAdminViewModelsHooks = [];
}
oSimpleHooks = {};
aUserViewModelsHooks = [];
aAdminViewModelsHooks = [];
constructor() {}
/**
* @param {string} name

View file

@ -1,801 +0,0 @@
(function () {
'use strict';
var
_ = require('_'),
$ = require('$'),
ko = require('ko'),
key = require('key'),
Enums = require('Common/Enums'),
Utils = require('Common/Utils')
;
/**
* @constructor
* @param {koProperty} oKoList
* @param {koProperty} oKoSelectedItem
* @param {koProperty} oKoFocusedItem
* @param {string} sItemSelector
* @param {string} sItemSelectedSelector
* @param {string} sItemCheckedSelector
* @param {string} sItemFocusedSelector
*/
function Selector(oKoList, oKoSelectedItem, oKoFocusedItem,
sItemSelector, sItemSelectedSelector, sItemCheckedSelector, sItemFocusedSelector)
{
this.list = oKoList;
this.listChecked = ko.computed(function () {
return _.filter(this.list(), function (oItem) {
return oItem.checked();
});
}, this).extend({'rateLimit': 0});
this.isListChecked = ko.computed(function () {
return 0 < this.listChecked().length;
}, this);
this.focusedItem = oKoFocusedItem || ko.observable(null);
this.selectedItem = oKoSelectedItem || ko.observable(null);
this.selectedItemUseCallback = true;
this.itemSelectedThrottle = _.debounce(_.bind(this.itemSelected, this), 300);
this.listChecked.subscribe(function (aItems) {
if (0 < aItems.length)
{
if (null === this.selectedItem())
{
if (this.selectedItem.valueHasMutated)
{
this.selectedItem.valueHasMutated();
}
}
else
{
this.selectedItem(null);
}
}
else if (this.autoSelect() && this.focusedItem())
{
this.selectedItem(this.focusedItem());
}
}, this);
this.selectedItem.subscribe(function (oItem) {
if (oItem)
{
if (this.isListChecked())
{
_.each(this.listChecked(), function (oSubItem) {
oSubItem.checked(false);
});
}
if (this.selectedItemUseCallback)
{
this.itemSelectedThrottle(oItem);
}
}
else if (this.selectedItemUseCallback)
{
this.itemSelected(null);
}
}, this);
this.selectedItem = this.selectedItem.extend({'toggleSubscribe': [null,
function (oPrev) {
if (oPrev)
{
oPrev.selected(false);
}
}, function (oNext) {
if (oNext)
{
oNext.selected(true);
}
}
]});
this.focusedItem = this.focusedItem.extend({'toggleSubscribe': [null,
function (oPrev) {
if (oPrev)
{
oPrev.focused(false);
}
}, function (oNext) {
if (oNext)
{
oNext.focused(true);
}
}
]});
this.iSelectNextHelper = 0;
this.iFocusedNextHelper = 0;
this.oContentVisible = null;
this.oContentScrollable = null;
this.sItemSelector = sItemSelector;
this.sItemSelectedSelector = sItemSelectedSelector;
this.sItemCheckedSelector = sItemCheckedSelector;
this.sItemFocusedSelector = sItemFocusedSelector;
this.sLastUid = '';
this.oCallbacks = {};
this.emptyFunction = function () {};
this.emptyTrueFunction = function () { return true; };
this.focusedItem.subscribe(function (oItem) {
if (oItem)
{
this.sLastUid = this.getItemUid(oItem);
}
}, this);
var
aCache = [],
aCheckedCache = [],
mFocused = null,
mSelected = null
;
this.list.subscribe(function (aItems) {
var self = this;
if (Utils.isArray(aItems))
{
_.each(aItems, function (oItem) {
if (oItem)
{
var sUid = self.getItemUid(oItem);
aCache.push(sUid);
if (oItem.checked())
{
aCheckedCache.push(sUid);
}
if (null === mFocused && oItem.focused())
{
mFocused = sUid;
}
if (null === mSelected && oItem.selected())
{
mSelected = sUid;
}
}
});
}
}, this, 'beforeChange');
this.list.subscribe(function (aItems) {
var
self = this,
oTemp = null,
bGetNext = false,
aUids = [],
mNextFocused = mFocused,
bChecked = false,
bSelected = false,
iLen = 0
;
this.selectedItemUseCallback = false;
this.focusedItem(null);
this.selectedItem(null);
if (Utils.isArray(aItems))
{
iLen = aCheckedCache.length;
_.each(aItems, function (oItem) {
var sUid = self.getItemUid(oItem);
aUids.push(sUid);
if (null !== mFocused && mFocused === sUid)
{
self.focusedItem(oItem);
mFocused = null;
}
if (0 < iLen && -1 < Utils.inArray(sUid, aCheckedCache))
{
bChecked = true;
oItem.checked(true);
iLen--;
}
if (!bChecked && null !== mSelected && mSelected === sUid)
{
bSelected = true;
self.selectedItem(oItem);
mSelected = null;
}
});
this.selectedItemUseCallback = true;
if (!bChecked && !bSelected && this.autoSelect())
{
if (self.focusedItem())
{
self.selectedItem(self.focusedItem());
}
else if (0 < aItems.length)
{
if (null !== mNextFocused)
{
bGetNext = false;
mNextFocused = _.find(aCache, function (sUid) {
if (bGetNext && -1 < Utils.inArray(sUid, aUids))
{
return sUid;
}
else if (mNextFocused === sUid)
{
bGetNext = true;
}
return false;
});
if (mNextFocused)
{
oTemp = _.find(aItems, function (oItem) {
return mNextFocused === self.getItemUid(oItem);
});
}
}
self.selectedItem(oTemp || null);
self.focusedItem(self.selectedItem());
}
}
if ((0 !== this.iSelectNextHelper || 0 !== this.iFocusedNextHelper) && 0 < aItems.length && !self.focusedItem())
{
oTemp = null;
if (0 !== this.iFocusedNextHelper)
{
oTemp = aItems[-1 === this.iFocusedNextHelper ? aItems.length - 1 : 0] || null;
}
if (!oTemp && 0 !== this.iSelectNextHelper)
{
oTemp = aItems[-1 === this.iSelectNextHelper ? aItems.length - 1 : 0] || null;
}
if (oTemp)
{
if (0 !== this.iSelectNextHelper)
{
self.selectedItem(oTemp || null);
}
self.focusedItem(oTemp || null);
self.scrollToFocused();
_.delay(function () {
self.scrollToFocused();
}, 100);
}
this.iSelectNextHelper = 0;
this.iFocusedNextHelper = 0;
}
}
aCache = [];
aCheckedCache = [];
mFocused = null;
mSelected = null;
}, this);
}
Selector.prototype.itemSelected = function (oItem)
{
if (this.isListChecked())
{
if (!oItem)
{
(this.oCallbacks['onItemSelect'] || this.emptyFunction)(oItem || null);
}
}
else
{
if (oItem)
{
(this.oCallbacks['onItemSelect'] || this.emptyFunction)(oItem);
}
}
};
Selector.prototype.goDown = function (bForceSelect)
{
this.newSelectPosition(Enums.EventKeyCode.Down, false, bForceSelect);
};
Selector.prototype.goUp = function (bForceSelect)
{
this.newSelectPosition(Enums.EventKeyCode.Up, false, bForceSelect);
};
Selector.prototype.unselect = function ()
{
this.selectedItem(null);
this.focusedItem(null);
};
Selector.prototype.init = function (oContentVisible, oContentScrollable, sKeyScope)
{
this.oContentVisible = oContentVisible;
this.oContentScrollable = oContentScrollable;
sKeyScope = sKeyScope || 'all';
if (this.oContentVisible && this.oContentScrollable)
{
var
self = this
;
$(this.oContentVisible)
.on('selectstart', function (oEvent) {
if (oEvent && oEvent.preventDefault)
{
oEvent.preventDefault();
}
})
.on('click', this.sItemSelector, function (oEvent) {
self.actionClick(ko.dataFor(this), oEvent);
})
.on('click', this.sItemCheckedSelector, function (oEvent) {
var oItem = ko.dataFor(this);
if (oItem)
{
if (oEvent && oEvent.shiftKey)
{
self.actionClick(oItem, oEvent);
}
else
{
self.focusedItem(oItem);
oItem.checked(!oItem.checked());
}
}
})
;
key('enter', sKeyScope, function () {
if (self.focusedItem() && !self.focusedItem().selected())
{
self.actionClick(self.focusedItem());
return false;
}
return true;
});
key('ctrl+up, command+up, ctrl+down, command+down', sKeyScope, function () {
return false;
});
key('up, shift+up, down, shift+down, home, end, pageup, pagedown, insert, space', sKeyScope, function (event, handler) {
if (event && handler && handler.shortcut)
{
var iKey = 0;
switch (handler.shortcut)
{
case 'up':
case 'shift+up':
iKey = Enums.EventKeyCode.Up;
break;
case 'down':
case 'shift+down':
iKey = Enums.EventKeyCode.Down;
break;
case 'insert':
iKey = Enums.EventKeyCode.Insert;
break;
case 'space':
iKey = Enums.EventKeyCode.Space;
break;
case 'home':
iKey = Enums.EventKeyCode.Home;
break;
case 'end':
iKey = Enums.EventKeyCode.End;
break;
case 'pageup':
iKey = Enums.EventKeyCode.PageUp;
break;
case 'pagedown':
iKey = Enums.EventKeyCode.PageDown;
break;
}
if (0 < iKey)
{
self.newSelectPosition(iKey, key.shift);
return false;
}
}
});
}
};
/**
* @return {boolean}
*/
Selector.prototype.autoSelect = function ()
{
return !!(this.oCallbacks['onAutoSelect'] || this.emptyTrueFunction)();
};
/**
* @param {boolean}
*/
Selector.prototype.doUpUpOrDownDown = function (bUp)
{
(this.oCallbacks['onUpUpOrDownDown'] || this.emptyTrueFunction)(!!bUp);
};
/**
* @param {Object} oItem
* @return {string}
*/
Selector.prototype.getItemUid = function (oItem)
{
var
sUid = '',
fGetItemUidCallback = this.oCallbacks['onItemGetUid'] || null
;
if (fGetItemUidCallback && oItem)
{
sUid = fGetItemUidCallback(oItem);
}
return sUid.toString();
};
/**
* @param {number} iEventKeyCode
* @param {boolean} bShiftKey
* @param {boolean=} bForceSelect = false
*/
Selector.prototype.newSelectPosition = function (iEventKeyCode, bShiftKey, bForceSelect)
{
var
iIndex = 0,
iPageStep = 10,
bNext = false,
bStop = false,
oResult = null,
aList = this.list(),
iListLen = aList ? aList.length : 0,
oFocused = this.focusedItem()
;
if (0 < iListLen)
{
if (!oFocused)
{
if (Enums.EventKeyCode.Down === iEventKeyCode || Enums.EventKeyCode.Insert === iEventKeyCode || Enums.EventKeyCode.Space === iEventKeyCode || Enums.EventKeyCode.Home === iEventKeyCode || Enums.EventKeyCode.PageUp === iEventKeyCode)
{
oResult = aList[0];
}
else if (Enums.EventKeyCode.Up === iEventKeyCode || Enums.EventKeyCode.End === iEventKeyCode || Enums.EventKeyCode.PageDown === iEventKeyCode)
{
oResult = aList[aList.length - 1];
}
}
else if (oFocused)
{
if (Enums.EventKeyCode.Down === iEventKeyCode || Enums.EventKeyCode.Up === iEventKeyCode || Enums.EventKeyCode.Insert === iEventKeyCode || Enums.EventKeyCode.Space === iEventKeyCode)
{
_.each(aList, function (oItem) {
if (!bStop)
{
switch (iEventKeyCode) {
case Enums.EventKeyCode.Up:
if (oFocused === oItem)
{
bStop = true;
}
else
{
oResult = oItem;
}
break;
case Enums.EventKeyCode.Down:
case Enums.EventKeyCode.Insert:
if (bNext)
{
oResult = oItem;
bStop = true;
}
else if (oFocused === oItem)
{
bNext = true;
}
break;
}
}
});
if (!oResult && (Enums.EventKeyCode.Down === iEventKeyCode || Enums.EventKeyCode.Up === iEventKeyCode))
{
this.doUpUpOrDownDown(Enums.EventKeyCode.Up === iEventKeyCode);
}
}
else if (Enums.EventKeyCode.Home === iEventKeyCode || Enums.EventKeyCode.End === iEventKeyCode)
{
if (Enums.EventKeyCode.Home === iEventKeyCode)
{
oResult = aList[0];
}
else if (Enums.EventKeyCode.End === iEventKeyCode)
{
oResult = aList[aList.length - 1];
}
}
else if (Enums.EventKeyCode.PageDown === iEventKeyCode)
{
for (; iIndex < iListLen; iIndex++)
{
if (oFocused === aList[iIndex])
{
iIndex += iPageStep;
iIndex = iListLen - 1 < iIndex ? iListLen - 1 : iIndex;
oResult = aList[iIndex];
break;
}
}
}
else if (Enums.EventKeyCode.PageUp === iEventKeyCode)
{
for (iIndex = iListLen; iIndex >= 0; iIndex--)
{
if (oFocused === aList[iIndex])
{
iIndex -= iPageStep;
iIndex = 0 > iIndex ? 0 : iIndex;
oResult = aList[iIndex];
break;
}
}
}
}
}
if (oResult)
{
this.focusedItem(oResult);
if (oFocused)
{
if (bShiftKey)
{
if (Enums.EventKeyCode.Up === iEventKeyCode || Enums.EventKeyCode.Down === iEventKeyCode)
{
oFocused.checked(!oFocused.checked());
}
}
else if (Enums.EventKeyCode.Insert === iEventKeyCode || Enums.EventKeyCode.Space === iEventKeyCode)
{
oFocused.checked(!oFocused.checked());
}
}
if ((this.autoSelect() || !!bForceSelect) &&
!this.isListChecked() && Enums.EventKeyCode.Space !== iEventKeyCode)
{
this.selectedItem(oResult);
}
this.scrollToFocused();
}
else if (oFocused)
{
if (bShiftKey && (Enums.EventKeyCode.Up === iEventKeyCode || Enums.EventKeyCode.Down === iEventKeyCode))
{
oFocused.checked(!oFocused.checked());
}
else if (Enums.EventKeyCode.Insert === iEventKeyCode || Enums.EventKeyCode.Space === iEventKeyCode)
{
oFocused.checked(!oFocused.checked());
}
this.focusedItem(oFocused);
}
};
/**
* @return {boolean}
*/
Selector.prototype.scrollToFocused = function ()
{
if (!this.oContentVisible || !this.oContentScrollable)
{
return false;
}
var
iOffset = 20,
aList = this.list(),
oFocused = $(this.sItemFocusedSelector, this.oContentScrollable),
oPos = oFocused.position(),
iVisibleHeight = this.oContentVisible.height(),
iFocusedHeight = oFocused.outerHeight()
;
if (aList && aList[0] && aList[0].focused())
{
this.oContentScrollable.scrollTop(0);
return true;
}
else if (oPos && (oPos.top < 0 || oPos.top + iFocusedHeight > iVisibleHeight))
{
if (oPos.top < 0)
{
this.oContentScrollable.scrollTop(this.oContentScrollable.scrollTop() + oPos.top - iOffset);
}
else
{
this.oContentScrollable.scrollTop(this.oContentScrollable.scrollTop() + oPos.top - iVisibleHeight + iFocusedHeight + iOffset);
}
return true;
}
return false;
};
/**
* @param {boolean=} bFast = false
* @return {boolean}
*/
Selector.prototype.scrollToTop = function (bFast)
{
if (!this.oContentVisible || !this.oContentScrollable)
{
return false;
}
if (bFast || 50 > this.oContentScrollable.scrollTop())
{
this.oContentScrollable.scrollTop(0);
}
else
{
this.oContentScrollable.stop().animate({'scrollTop': 0}, 200);
}
return true;
};
Selector.prototype.eventClickFunction = function (oItem, oEvent)
{
var
sUid = this.getItemUid(oItem),
iIndex = 0,
iLength = 0,
oListItem = null,
sLineUid = '',
bChangeRange = false,
bIsInRange = false,
aList = [],
bChecked = false
;
if (oEvent && oEvent.shiftKey)
{
if ('' !== sUid && '' !== this.sLastUid && sUid !== this.sLastUid)
{
aList = this.list();
bChecked = oItem.checked();
for (iIndex = 0, iLength = aList.length; iIndex < iLength; iIndex++)
{
oListItem = aList[iIndex];
sLineUid = this.getItemUid(oListItem);
bChangeRange = false;
if (sLineUid === this.sLastUid || sLineUid === sUid)
{
bChangeRange = true;
}
if (bChangeRange)
{
bIsInRange = !bIsInRange;
}
if (bIsInRange || bChangeRange)
{
oListItem.checked(bChecked);
}
}
}
}
this.sLastUid = '' === sUid ? '' : sUid;
};
/**
* @param {Object} oItem
* @param {Object=} oEvent
*/
Selector.prototype.actionClick = function (oItem, oEvent)
{
if (oItem)
{
var
bClick = true,
sUid = this.getItemUid(oItem)
;
if (oEvent)
{
if (oEvent.shiftKey && !(oEvent.ctrlKey || oEvent.metaKey) && !oEvent.altKey)
{
bClick = false;
if ('' === this.sLastUid)
{
this.sLastUid = sUid;
}
oItem.checked(!oItem.checked());
this.eventClickFunction(oItem, oEvent);
this.focusedItem(oItem);
}
else if ((oEvent.ctrlKey || oEvent.metaKey) && !oEvent.shiftKey && !oEvent.altKey)
{
bClick = false;
this.focusedItem(oItem);
if (this.selectedItem() && oItem !== this.selectedItem())
{
this.selectedItem().checked(true);
}
oItem.checked(!oItem.checked());
}
}
if (bClick)
{
this.selectMessageItem(oItem);
}
}
};
Selector.prototype.on = function (sEventName, fCallback)
{
this.oCallbacks[sEventName] = fCallback;
};
Selector.prototype.selectMessageItem = function (oMessageItem)
{
this.focusedItem(oMessageItem);
this.selectedItem(oMessageItem);
this.scrollToFocused();
};
module.exports = Selector;
}());

763
dev/Common/Selector.jsx Normal file
View file

@ -0,0 +1,763 @@
import {$, _, key} from 'common';
import ko from 'ko';
import {EventKeyCode} from 'Common/Enums';
import Utils from 'Common/Utils';
class Selector
{
/**
* @constructor
* @param {koProperty} koList
* @param {koProperty} koSelectedItem
* @param {koProperty} koFocusedItem
* @param {string} sItemSelector
* @param {string} sItemSelectedSelector
* @param {string} sItemCheckedSelector
* @param {string} sItemFocusedSelector
*/
constructor(koList, koSelectedItem, koFocusedItem,
sItemSelector, sItemSelectedSelector, sItemCheckedSelector, sItemFocusedSelector)
{
this.list = koList;
this.listChecked = ko.computed(() => {
return _.filter(this.list(), (item) => item.checked());
}, this).extend({'rateLimit': 0});
this.isListChecked = ko.computed(() => 0 < this.listChecked().length);
this.focusedItem = koFocusedItem || ko.observable(null);
this.selectedItem = koSelectedItem || ko.observable(null);
this.selectedItemUseCallback = true;
this.itemSelectedThrottle = _.debounce(_.bind(this.itemSelected, this), 300);
this.listChecked.subscribe((items) => {
if (0 < items.length)
{
if (null === this.selectedItem())
{
if (this.selectedItem.valueHasMutated)
{
this.selectedItem.valueHasMutated();
}
}
else
{
this.selectedItem(null);
}
}
else if (this.autoSelect() && this.focusedItem())
{
this.selectedItem(this.focusedItem());
}
}, this);
this.selectedItem.subscribe((item) => {
if (item)
{
if (this.isListChecked())
{
_.each(this.listChecked(), (subItem) => {
subItem.checked(false);
});
}
if (this.selectedItemUseCallback)
{
this.itemSelectedThrottle(item);
}
}
else if (this.selectedItemUseCallback)
{
this.itemSelected(null);
}
}, this);
this.selectedItem = this.selectedItem.extend({'toggleSubscribe': [null,
(prev) => {
if (prev)
{
prev.selected(false);
}
}, (next) => {
if (next)
{
next.selected(true);
}
}
]});
this.focusedItem = this.focusedItem.extend({'toggleSubscribe': [null,
(prev) => {
if (prev)
{
prev.focused(false);
}
}, (next) => {
if (next)
{
next.focused(true);
}
}
]});
this.iSelectNextHelper = 0;
this.iFocusedNextHelper = 0;
this.oContentVisible = null;
this.oContentScrollable = null;
this.sItemSelector = sItemSelector;
this.sItemSelectedSelector = sItemSelectedSelector;
this.sItemCheckedSelector = sItemCheckedSelector;
this.sItemFocusedSelector = sItemFocusedSelector;
this.sLastUid = '';
this.oCallbacks = {};
this.emptyFunction = () => {};
this.emptyTrueFunction = () => true;
this.focusedItem.subscribe((item) => {
if (item)
{
this.sLastUid = this.getItemUid(item);
}
}, this);
let
aCache = [],
aCheckedCache = [],
mFocused = null,
mSelected = null
;
this.list.subscribe((items) => {
if (Utils.isArray(items))
{
_.each(items, (item) => {
if (item)
{
const uid = this.getItemUid(item);
aCache.push(uid);
if (item.checked())
{
aCheckedCache.push(uid);
}
if (null === mFocused && item.focused())
{
mFocused = uid;
}
if (null === mSelected && item.selected())
{
mSelected = uid;
}
}
});
}
}, this, 'beforeChange');
this.list.subscribe((aItems) => {
var
oTemp = null,
bGetNext = false,
aUids = [],
mNextFocused = mFocused,
bChecked = false,
bSelected = false,
iLen = 0
;
this.selectedItemUseCallback = false;
this.focusedItem(null);
this.selectedItem(null);
if (Utils.isArray(aItems))
{
iLen = aCheckedCache.length;
_.each(aItems, (oItem) => {
var sUid = this.getItemUid(oItem);
aUids.push(sUid);
if (null !== mFocused && mFocused === sUid)
{
this.focusedItem(oItem);
mFocused = null;
}
if (0 < iLen && -1 < Utils.inArray(sUid, aCheckedCache))
{
bChecked = true;
oItem.checked(true);
iLen--;
}
if (!bChecked && null !== mSelected && mSelected === sUid)
{
bSelected = true;
this.selectedItem(oItem);
mSelected = null;
}
});
this.selectedItemUseCallback = true;
if (!bChecked && !bSelected && this.autoSelect())
{
if (this.focusedItem())
{
this.selectedItem(this.focusedItem());
}
else if (0 < aItems.length)
{
if (null !== mNextFocused)
{
bGetNext = false;
mNextFocused = _.find(aCache, (sUid) => {
if (bGetNext && -1 < Utils.inArray(sUid, aUids))
{
return sUid;
}
else if (mNextFocused === sUid)
{
bGetNext = true;
}
return false;
});
if (mNextFocused)
{
oTemp = _.find(aItems, (oItem) => mNextFocused === this.getItemUid(oItem));
}
}
this.selectedItem(oTemp || null);
this.focusedItem(this.selectedItem());
}
}
if ((0 !== this.iSelectNextHelper || 0 !== this.iFocusedNextHelper) && 0 < aItems.length && !this.focusedItem())
{
oTemp = null;
if (0 !== this.iFocusedNextHelper)
{
oTemp = aItems[-1 === this.iFocusedNextHelper ? aItems.length - 1 : 0] || null;
}
if (!oTemp && 0 !== this.iSelectNextHelper)
{
oTemp = aItems[-1 === this.iSelectNextHelper ? aItems.length - 1 : 0] || null;
}
if (oTemp)
{
if (0 !== this.iSelectNextHelper)
{
this.selectedItem(oTemp || null);
}
this.focusedItem(oTemp || null);
this.scrollToFocused();
_.delay(() => this.scrollToFocused(), 100);
}
this.iSelectNextHelper = 0;
this.iFocusedNextHelper = 0;
}
}
aCache = [];
aCheckedCache = [];
mFocused = null;
mSelected = null;
}, this);
}
itemSelected(item) {
if (this.isListChecked())
{
if (!item)
{
(this.oCallbacks['onItemSelect'] || this.emptyFunction)(item || null);
}
}
else
{
if (item)
{
(this.oCallbacks['onItemSelect'] || this.emptyFunction)(item);
}
}
}
/**
* @param {boolean} forceSelect
*/
goDown(forceSelect) {
this.newSelectPosition(EventKeyCode.Down, false, forceSelect);
}
/**
* @param {boolean} forceSelect
*/
goUp(forceSelect) {
this.newSelectPosition(EventKeyCode.Up, false, forceSelect);
}
unselect() {
this.selectedItem(null);
this.focusedItem(null);
}
init(contentVisible, contentScrollable, keyScope = 'all') {
this.oContentVisible = contentVisible;
this.oContentScrollable = contentScrollable;
if (this.oContentVisible && this.oContentScrollable)
{
$(this.oContentVisible)
.on('selectstart', (event) => {
if (event && event.preventDefault)
{
event.preventDefault();
}
})
.on('click', this.sItemSelector, (event) => {
this.actionClick(ko.dataFor(event.currentTarget), event);
})
.on('click', this.sItemCheckedSelector, (event) => {
const item = ko.dataFor(event.currentTarget);
if (item)
{
if (event && event.shiftKey)
{
this.actionClick(item, event);
}
else
{
this.focusedItem(item);
item.checked(!item.checked());
}
}
})
;
key('enter', keyScope, () => {
if (this.focusedItem() && !this.focusedItem().selected())
{
this.actionClick(this.focusedItem());
return false;
}
return true;
});
key('ctrl+up, command+up, ctrl+down, command+down', keyScope, () => false);
key('up, shift+up, down, shift+down, home, end, pageup, pagedown, insert, space', keyScope, (event, handler) => {
if (event && handler && handler.shortcut)
{
let eventKey = 0;
switch (handler.shortcut)
{
case 'up':
case 'shift+up':
eventKey = EventKeyCode.Up;
break;
case 'down':
case 'shift+down':
eventKey = EventKeyCode.Down;
break;
case 'insert':
eventKey = EventKeyCode.Insert;
break;
case 'space':
eventKey = EventKeyCode.Space;
break;
case 'home':
eventKey = EventKeyCode.Home;
break;
case 'end':
eventKey = EventKeyCode.End;
break;
case 'pageup':
eventKey = EventKeyCode.PageUp;
break;
case 'pagedown':
eventKey = EventKeyCode.PageDown;
break;
}
if (0 < eventKey)
{
this.newSelectPosition(eventKey, key.shift);
return false;
}
}
});
}
}
/**
* @return {boolean}
*/
autoSelect() {
return !!(this.oCallbacks['onAutoSelect'] || this.emptyTrueFunction)();
}
/**
* @param {boolean} up
*/
doUpUpOrDownDown(up) {
(this.oCallbacks['onUpUpOrDownDown'] || this.emptyTrueFunction)(!!up);
}
/**
* @param {Object} oItem
* @return {string}
*/
getItemUid(item) {
let uid = '';
const getItemUidCallback = this.oCallbacks['onItemGetUid'] || null;
if (getItemUidCallback && item)
{
uid = getItemUidCallback(item);
}
return uid.toString();
}
/**
* @param {number} iEventKeyCode
* @param {boolean} bShiftKey
* @param {boolean=} bForceSelect = false
*/
newSelectPosition(iEventKeyCode, bShiftKey, bForceSelect) {
var
iIndex = 0,
iPageStep = 10,
bNext = false,
bStop = false,
oResult = null,
aList = this.list(),
iListLen = aList ? aList.length : 0,
oFocused = this.focusedItem()
;
if (0 < iListLen)
{
if (!oFocused)
{
if (EventKeyCode.Down === iEventKeyCode || EventKeyCode.Insert === iEventKeyCode || EventKeyCode.Space === iEventKeyCode || EventKeyCode.Home === iEventKeyCode || EventKeyCode.PageUp === iEventKeyCode)
{
oResult = aList[0];
}
else if (EventKeyCode.Up === iEventKeyCode || EventKeyCode.End === iEventKeyCode || EventKeyCode.PageDown === iEventKeyCode)
{
oResult = aList[aList.length - 1];
}
}
else if (oFocused)
{
if (EventKeyCode.Down === iEventKeyCode || EventKeyCode.Up === iEventKeyCode || EventKeyCode.Insert === iEventKeyCode || EventKeyCode.Space === iEventKeyCode)
{
_.each(aList, (item) => {
if (!bStop)
{
switch (iEventKeyCode) {
case EventKeyCode.Up:
if (oFocused === item)
{
bStop = true;
}
else
{
oResult = item;
}
break;
case EventKeyCode.Down:
case EventKeyCode.Insert:
if (bNext)
{
oResult = item;
bStop = true;
}
else if (oFocused === item)
{
bNext = true;
}
break;
}
}
});
if (!oResult && (EventKeyCode.Down === iEventKeyCode || EventKeyCode.Up === iEventKeyCode))
{
this.doUpUpOrDownDown(EventKeyCode.Up === iEventKeyCode);
}
}
else if (EventKeyCode.Home === iEventKeyCode || EventKeyCode.End === iEventKeyCode)
{
if (EventKeyCode.Home === iEventKeyCode)
{
oResult = aList[0];
}
else if (EventKeyCode.End === iEventKeyCode)
{
oResult = aList[aList.length - 1];
}
}
else if (EventKeyCode.PageDown === iEventKeyCode)
{
for (; iIndex < iListLen; iIndex++)
{
if (oFocused === aList[iIndex])
{
iIndex += iPageStep;
iIndex = iListLen - 1 < iIndex ? iListLen - 1 : iIndex;
oResult = aList[iIndex];
break;
}
}
}
else if (EventKeyCode.PageUp === iEventKeyCode)
{
for (iIndex = iListLen; iIndex >= 0; iIndex--)
{
if (oFocused === aList[iIndex])
{
iIndex -= iPageStep;
iIndex = 0 > iIndex ? 0 : iIndex;
oResult = aList[iIndex];
break;
}
}
}
}
}
if (oResult)
{
this.focusedItem(oResult);
if (oFocused)
{
if (bShiftKey)
{
if (EventKeyCode.Up === iEventKeyCode || EventKeyCode.Down === iEventKeyCode)
{
oFocused.checked(!oFocused.checked());
}
}
else if (EventKeyCode.Insert === iEventKeyCode || EventKeyCode.Space === iEventKeyCode)
{
oFocused.checked(!oFocused.checked());
}
}
if ((this.autoSelect() || !!bForceSelect) &&
!this.isListChecked() && EventKeyCode.Space !== iEventKeyCode)
{
this.selectedItem(oResult);
}
this.scrollToFocused();
}
else if (oFocused)
{
if (bShiftKey && (EventKeyCode.Up === iEventKeyCode || EventKeyCode.Down === iEventKeyCode))
{
oFocused.checked(!oFocused.checked());
}
else if (EventKeyCode.Insert === iEventKeyCode || EventKeyCode.Space === iEventKeyCode)
{
oFocused.checked(!oFocused.checked());
}
this.focusedItem(oFocused);
}
}
/**
* @return {boolean}
*/
scrollToFocused() {
if (!this.oContentVisible || !this.oContentScrollable)
{
return false;
}
const
offset = 20,
list = this.list(),
$focused = $(this.sItemFocusedSelector, this.oContentScrollable),
pos = $focused.position(),
visibleHeight = this.oContentVisible.height(),
focusedHeight = $focused.outerHeight()
;
if (list && list[0] && list[0].focused())
{
this.oContentScrollable.scrollTop(0);
return true;
}
else if (pos && (pos.top < 0 || pos.top + focusedHeight > visibleHeight))
{
this.oContentScrollable.scrollTop(pos.top < 0 ?
this.oContentScrollable.scrollTop() + pos.top - offset :
this.oContentScrollable.scrollTop() + pos.top - visibleHeight + focusedHeight + offset
);
return true;
}
return false;
}
/**
* @param {boolean=} fast = false
* @return {boolean}
*/
scrollToTop(fast = false) {
if (!this.oContentVisible || !this.oContentScrollable)
{
return false;
}
if (fast || 50 > this.oContentScrollable.scrollTop())
{
this.oContentScrollable.scrollTop(0);
}
else
{
this.oContentScrollable.stop().animate({'scrollTop': 0}, 200);
}
return true;
}
eventClickFunction(item, event) {
let
index = 0,
length = 0,
changeRange = false,
isInRange = false,
list = [],
checked = false,
listItem = null,
lineUid = ''
;
const uid = this.getItemUid(item);
if (event && event.shiftKey)
{
if ('' !== uid && '' !== this.sLastUid && uid !== this.sLastUid)
{
list = this.list();
checked = item.checked();
for (index = 0, length = list.length; index < length; index++)
{
listItem = list[index];
lineUid = this.getItemUid(listItem);
changeRange = false;
if (lineUid === this.sLastUid || lineUid === uid)
{
changeRange = true;
}
if (changeRange)
{
isInRange = !isInRange;
}
if (isInRange || changeRange)
{
listItem.checked(checked);
}
}
}
}
this.sLastUid = '' === uid ? '' : uid;
}
/**
* @param {Object} item
* @param {Object=} event
*/
actionClick = function (item, event = null) {
if (item)
{
let click = true;
if (event)
{
if (event.shiftKey && !(event.ctrlKey || event.metaKey) && !event.altKey)
{
click = false;
if ('' === this.sLastUid)
{
this.sLastUid = this.getItemUid(item);
}
item.checked(!item.checked());
this.eventClickFunction(item, event);
this.focusedItem(item);
}
else if ((event.ctrlKey || event.metaKey) && !event.shiftKey && !event.altKey)
{
click = false;
this.focusedItem(item);
if (this.selectedItem() && item !== this.selectedItem())
{
this.selectedItem().checked(true);
}
item.checked(!item.checked());
}
}
if (click)
{
this.selectMessageItem(item);
}
}
}
on(eventName, callback) {
this.oCallbacks[eventName] = callback;
}
selectMessageItem(messageItem) {
this.focusedItem(messageItem);
this.selectedItem(messageItem);
this.scrollToFocused();
}
}
export {Selector, Selector as default};
module.exports = Selector;

View file

@ -7,9 +7,11 @@ import Globals from 'Common/Globals';
class Translator
{
data = {};
notificationI18N = {};
constructor() {
this.data = window['rainloopI18N'] || {};
this.notificationI18N = {};
this.trigger = ko.observable(false);
this.i18n = _.bind(this.i18n, this);
this.init();
@ -185,7 +187,7 @@ class Translator
this.notificationI18N = this.notificationI18N || {};
_.each(map, (item) => {
map.forEach((item) => {
this.notificationI18N[item[0]] = this.i18n(item[1]);
});
}