Small code cleanups and fix translations

This commit is contained in:
djmaze 2021-01-27 00:26:31 +01:00
parent b31bf3d7f5
commit 7ccc44616d
54 changed files with 288 additions and 426 deletions

View file

@ -1,4 +1,5 @@
import * as Links from 'Common/Links';
import { doc } from 'Common/Globals';
let notificator = null,
player = null,
@ -48,7 +49,7 @@ let notificator = null,
console.log('AudioContext ' + audioCtx.state);
audioCtx.resume();
}
unlockEvents.forEach(type => document.removeEventListener(type, unlock, true));
unlockEvents.forEach(type => doc.removeEventListener(type, unlock, true));
// setTimeout(()=>Audio.playNotification(1),1);
};
@ -56,27 +57,21 @@ if (audioCtx) {
audioCtx = audioCtx ? new audioCtx : null;
audioCtx.onstatechange = unlock;
}
unlockEvents.forEach(type => document.addEventListener(type, unlock, true));
unlockEvents.forEach(type => doc.addEventListener(type, unlock, true));
/**
* Browsers can't play without user interaction
*/
const SMAudio = new class {
supported = false;
supportedMp3 = false;
supportedOgg = false;
supportedWav = false;
constructor() {
player || (player = createNewObject());
this.supported = !!player;
this.supportedMp3 = canPlay('audio/mpeg;');
this.supportedWav = canPlay('audio/wav; codecs="1"');
this.supportedOgg = canPlay('audio/ogg; codecs="vorbis"');
if (player) {
this.supportedMp3 = !!canPlay('audio/mpeg;');
this.supportedWav = !!canPlay('audio/wav; codecs="1"');
this.supportedOgg = !!canPlay('audio/ogg; codecs="vorbis"');
const stopFn = () => this.pause();
player.addEventListener('ended', stopFn);
player.addEventListener('error', stopFn);

View file

@ -175,11 +175,11 @@ export const File = {
let result = FileType.Unknown;
const mimeTypeParts = mimeType.split('/'),
type = mimeTypeParts[1],
type = mimeTypeParts[1].replace('x-','').replace('-compressed',''),
match = str => type.includes(str);
switch (true) {
case 'image' === mimeTypeParts[0] || ['png', 'jpg', 'jpeg', 'gif'].includes(ext):
case 'image' === mimeTypeParts[0] || ['png', 'jpg', 'jpeg', 'gif', 'webp'].includes(ext):
result = FileType.Image;
break;
case 'audio' === mimeTypeParts[0] || ['mp3', 'ogg', 'oga', 'wav'].includes(ext):
@ -207,28 +207,18 @@ export const File = {
'rar',
'gzip',
'bzip',
'bzip2',
'x-zip',
'x-7z',
'x-rar',
'x-tar',
'x-gzip',
'x-bzip',
'x-bzip2',
'x-zip-compressed',
'x-7z-compressed',
'x-rar-compressed'
'bzip2'
].includes(type) || ['zip', '7z', 'tar', 'rar', 'gzip', 'bzip', 'bzip2'].includes(ext):
result = FileType.Archive;
break;
case ['pdf', 'x-pdf'].includes(type) || ['pdf'].includes(ext):
case 'pdf' == type || 'pdf' == ext:
result = FileType.Pdf;
break;
case ['application/pgp-signature', 'application/pgp-keys'].includes(mimeType) ||
['asc', 'pem', 'ppk'].includes(ext):
result = FileType.Certificate;
break;
case ['application/pkcs7-signature'].includes(mimeType) || ['p7s'].includes(ext):
case ['application/pkcs7-signature'].includes(mimeType) || 'p7s' == ext:
result = FileType.CertificateBin;
break;
case match(msOffice+'.wordprocessingml') || match(openDoc+'.text') || match('vnd.ms-word')

View file

@ -1,47 +1,55 @@
import ko from 'ko';
import { KeyState } from 'Common/Enums';
export const $html = document.documentElement;
export const $htmlCL = $html.classList;
export const doc = document;
export const $htmlCL = doc.documentElement.classList;
export const Settings = rl.settings;
/**
* @type {?}
*/
export const dropdownVisibility = ko.observable(false).extend({ rateLimit: 0 });
export const moveAction = ko.observable(false);
export const leftPanelDisabled = ko.observable(false);
export const leftPanelType = ko.observable('');
export const leftPanelWidth = ko.observable(0);
leftPanelDisabled.subscribe(value => value && moveAction() && moveAction(false));
leftPanelDisabled.subscribe(value => {
value && moveAction() && moveAction(false);
$htmlCL.toggle('rl-left-panel-disabled', value);
});
leftPanelType.subscribe(sValue => {
$htmlCL.toggle('rl-left-panel-none', 'none' === sValue);
$htmlCL.toggle('rl-left-panel-short', 'short' === sValue);
});
moveAction.subscribe(value => value && leftPanelDisabled() && leftPanelDisabled(false));
// keys
export const keyScopeReal = ko.observable(KeyState.All);
export const keyScopeFake = ko.observable(KeyState.All);
export const keyScope = ko.computed({
read: () => keyScopeFake(),
write: value => {
if (KeyState.Menu !== value) {
keyScopeFake(value);
if (dropdownVisibility()) {
value = KeyState.Menu;
}
export const keyScope = (()=>{
let keyScopeFake = KeyState.All;
dropdownVisibility.subscribe(value => {
if (value) {
keyScope(KeyState.Menu);
} else if (KeyState.Menu === shortcuts.getScope()) {
keyScope(keyScopeFake);
}
});
return ko.computed({
read: () => keyScopeFake,
write: value => {
if (KeyState.Menu !== value) {
keyScopeFake = value;
if (dropdownVisibility()) {
value = KeyState.Menu;
}
}
keyScopeReal(value);
}
});
keyScopeReal(value);
}
});
})();
keyScopeReal.subscribe(value => shortcuts.setScope(value));
dropdownVisibility.subscribe(value => {
if (value) {
keyScope(KeyState.Menu);
} else if (KeyState.Menu === shortcuts.getScope()) {
keyScope(keyScopeFake());
}
});

View file

@ -1,7 +1,7 @@
import { pString, pInt } from 'Common/Utils';
import { Settings } from 'Common/Globals';
const
Settings = rl.settings,
ROOT = './',
HASH_PREFIX = '#/',
SERVER_PREFIX = './?',
@ -42,25 +42,17 @@ export function rootUser() {
/**
* @param {string} type
* @param {string} download
* @param {string} hash
* @param {string=} customSpecSuffix
* @returns {string}
*/
export function attachmentRaw(type, download, customSpecSuffix) {
customSpecSuffix = undefined === customSpecSuffix ? getHash() : customSpecSuffix;
return (
SERVER_PREFIX +
'/Raw/' +
SUB_QUERY_PREFIX +
'/' +
customSpecSuffix +
'/' +
type +
'/' +
SUB_QUERY_PREFIX +
'/' +
download
);
export function serverRequestRaw(type, hash, customSpecSuffix) {
return SERVER_PREFIX + '/Raw/' + SUB_QUERY_PREFIX + '/'
+ (null == customSpecSuffix ? getHash() : customSpecSuffix) + '/'
+ (type
? type + '/' + (hash ? SUB_QUERY_PREFIX + '/' + hash : '')
: '')
;
}
/**
@ -69,7 +61,7 @@ export function attachmentRaw(type, download, customSpecSuffix) {
* @returns {string}
*/
export function attachmentDownload(download, customSpecSuffix) {
return attachmentRaw('Download', download, customSpecSuffix);
return serverRequestRaw('Download', download, customSpecSuffix);
}
/**
@ -78,7 +70,7 @@ export function attachmentDownload(download, customSpecSuffix) {
* @returns {string}
*/
export function attachmentPreview(download, customSpecSuffix) {
return attachmentRaw('View', download, customSpecSuffix);
return serverRequestRaw('View', download, customSpecSuffix);
}
/**
@ -87,7 +79,7 @@ export function attachmentPreview(download, customSpecSuffix) {
* @returns {string}
*/
export function attachmentThumbnailPreview(download, customSpecSuffix) {
return attachmentRaw('ViewThumbnail', download, customSpecSuffix);
return serverRequestRaw('ViewThumbnail', download, customSpecSuffix);
}
/**
@ -96,7 +88,7 @@ export function attachmentThumbnailPreview(download, customSpecSuffix) {
* @returns {string}
*/
export function attachmentPreviewAsPlain(download, customSpecSuffix) {
return attachmentRaw('ViewAsPlain', download, customSpecSuffix);
return serverRequestRaw('ViewAsPlain', download, customSpecSuffix);
}
/**
@ -105,7 +97,7 @@ export function attachmentPreviewAsPlain(download, customSpecSuffix) {
* @returns {string}
*/
export function attachmentFramed(download, customSpecSuffix) {
return attachmentRaw('FramedView', download, customSpecSuffix);
return serverRequestRaw('FramedView', download, customSpecSuffix);
}
/**
@ -157,17 +149,7 @@ export function change(email) {
* @returns {string}
*/
export function messageViewLink(requestHash) {
return (
SERVER_PREFIX +
'/Raw/' +
SUB_QUERY_PREFIX +
'/' +
getHash() +
'/ViewAsPlain/' +
SUB_QUERY_PREFIX +
'/' +
requestHash
);
return serverRequestRaw('ViewAsPlain', requestHash);
}
/**
@ -175,17 +157,7 @@ export function messageViewLink(requestHash) {
* @returns {string}
*/
export function messageDownloadLink(requestHash) {
return (
SERVER_PREFIX + '/Raw/' + SUB_QUERY_PREFIX + '/' + getHash() + '/Download/' + SUB_QUERY_PREFIX + '/' + requestHash
);
}
/**
* @param {string} email
* @returns {string}
*/
export function avatarLink(email) {
return SERVER_PREFIX + '/Raw/0/Avatar/' + encodeURIComponent(email) + '/';
return serverRequestRaw('Download', requestHash);
}
/**
@ -201,9 +173,7 @@ export function publicLink(hash) {
* @returns {string}
*/
export function userBackground(hash) {
return (
SERVER_PREFIX + '/Raw/' + SUB_QUERY_PREFIX + '/' + getHash() + '/UserBackground/' + SUB_QUERY_PREFIX + '/' + hash
);
return serverRequestRaw('UserBackground', hash);
}
/**
@ -219,14 +189,14 @@ export function langLink(lang, isAdmin) {
* @returns {string}
*/
export function exportContactsVcf() {
return SERVER_PREFIX + '/Raw/' + SUB_QUERY_PREFIX + '/' + getHash() + '/ContactsVcf/';
return serverRequestRaw('ContactsVcf');
}
/**
* @returns {string}
*/
export function exportContactsCsv() {
return SERVER_PREFIX + '/Raw/' + SUB_QUERY_PREFIX + '/' + getHash() + '/ContactsCsv/';
return serverRequestRaw('ContactsCsv');
}
/**
@ -266,13 +236,6 @@ export function openPgpWorkerJs() {
return staticPrefix('js/min/openpgp.worker.min.js');
}
/**
* @returns {string}
*/
export function openPgpWorkerPath() {
return staticPrefix('js/min/');
}
/**
* @param {string} theme
* @returns {string}
@ -303,13 +266,6 @@ export function settings(screenName = '') {
return HASH_PREFIX + 'settings' + (screenName ? '/' + screenName : '');
}
/**
* @returns {string}
*/
export function about() {
return HASH_PREFIX + 'about';
}
/**
* @param {string} screenName
* @returns {string}

View file

@ -1,6 +1,7 @@
import { doc } from 'Common/Globals';
import { i18n } from 'Common/Translator';
export function format(timeStampInUTC, formatStr) {
export function timestampToString(timeStampInUTC, formatStr) {
const now = Date.now(),
time = 0 < timeStampInUTC ? Math.min(now, timeStampInUTC * 1000) : (0 === timeStampInUTC ? now : 0);
@ -36,17 +37,15 @@ export function timeToNode(element, time) {
try {
time = time || (Date.parse(element.dateTime) / 1000);
if (time) {
let key, m = new Date(time * 1000);
element.dateTime = m.format('Y-m-d\\TH:i:s');
element.dateTime = (new Date(time * 1000)).format('Y-m-d\\TH:i:s');
key = element.dataset.momentFormat;
let key = element.dataset.momentFormat;
if (key) {
element.textContent = format(time, key);
element.textContent = timestampToString(time, key);
}
key = element.dataset.momentFormatTitle;
if (key) {
element.title = format(time, key);
if ((key = element.dataset.momentFormatTitle)) {
element.title = timestampToString(time, key);
}
}
} catch (e) {
@ -56,6 +55,6 @@ export function timeToNode(element, time) {
}
addEventListener('reload-time', () => setTimeout(() =>
document.querySelectorAll('[data-bind*="moment:"]').forEach(element => timeToNode(element))
doc.querySelectorAll('[data-bind*="moment:"]').forEach(element => timeToNode(element))
, 1)
);

View file

@ -3,14 +3,6 @@ import { settingsAddViewModel } from 'Screen/AbstractSettings';
const USER_VIEW_MODELS_HOOKS = [],
ADMIN_VIEW_MODELS_HOOKS = [];
/**
* @param {string} name
* @returns {?}
*/
export function mainSettingsGet(name) {
return rl.settings.get(name);
}
/**
* @param {Function} callback
* @param {string} action

View file

@ -1,11 +1,10 @@
import ko from 'ko';
import { Notification, UploadErrorCode } from 'Common/Enums';
import { langLink } from 'Common/Links';
import { doc } from 'Common/Globals';
let I18N_DATA = window.rainloopI18N || {};
const doc = document;
export const trigger = ko.observable(false);
/**
@ -54,8 +53,10 @@ const i18nToNode = element => {
}
}
},
getKeyByValue = (o, v) => Object.keys(o).find(key => o[key] === v);
i18nKey = key => key.replace(/([a-z])([A-Z])/g, '$1_$2').toUpperCase(),
getKeyByValue = (o, v) => Object.keys(o).find(key => o[key] === v);
/**
* @param {Object} elements
@ -80,9 +81,8 @@ export function initOnStartOrLangChange(startCallback, langCallback = null) {
function getNotificationMessage(code) {
let key = getKeyByValue(Notification, code);
if (key) {
key = key.replace('CantCopyMessage', 'CantMoveMessage').replace('UnknownNotification', 'UnknownError');
key = 'NOTIFICATIONS/' + key.replace(/([a-z])([A-Z])/g, '$1_$2').toUpperCase();
return I18N_DATA[key];
key = i18nKey(key).replace('_NOTIFICATION', '_ERROR');
return I18N_DATA['NOTIFICATIONS/' + key];
}
}
@ -129,7 +129,7 @@ export function getUploadErrorDescByCode(code) {
case UploadErrorCode.MissingTempFolder:
case UploadErrorCode.OnSavingFile:
case UploadErrorCode.FileType:
result = getKeyByValue(UploadErrorCode, code).replace(/([a-z])([A-Z])/g, '$1_$2').toUpperCase();
result = i18nKey(getKeyByValue(UploadErrorCode, code));
break;
}
return i18n('UPLOAD/ERROR_' + result);

View file

@ -1,7 +1,9 @@
import { SaveSettingsStep } from 'Common/Enums';
import { doc } from 'Common/Globals';
const
doc = document;
export const
isArray = Array.isArray,
isNonEmptyArray = Array.isNotEmpty;
/**
* @param {*} value

View file

@ -2,10 +2,8 @@ import { ComposeType, FolderType } from 'Common/EnumsUser';
import { EmailModel } from 'Model/Email';
import { showScreenPopup } from 'Knoin/Knoin';
import { encodeHtml } from 'Common/Html';
const
tpl = document.createElement('template'),
isArray = Array.isArray;
import { isArray } from 'Common/Utils';
import { doc } from 'Common/Globals';
/**
* @param {(string|number)} value
@ -81,7 +79,9 @@ export function htmlToPlain(html) {
return '';
};
const convertPre = (...args) =>
const
tpl = doc.createElement('template'),
convertPre = (...args) =>
args && 1 < args.length
? args[1]
.toString()