DocSpace-client/packages/asc-web-common/src/utils/index.js

112 lines
2.6 KiB
JavaScript
Raw Normal View History

import { AUTH_KEY, LANGUAGE } from "../constants";
import sjcl from "sjcl";
import { isMobile } from "react-device-detect";
export const toUrlParams = (obj, skipNull) => {
let str = "";
for (var key in obj) {
if (skipNull && !obj[key]) continue;
if (str !== "") {
str += "&";
}
str += key + "=" + encodeURIComponent(obj[key]);
}
return str;
};
export function getObjectByLocation(location) {
if (!location.search || !location.search.length) return null;
const searchUrl = location.search.substring(1);
const object = JSON.parse(
'{"' +
decodeURIComponent(searchUrl)
.replace(/"/g, '\\"')
.replace(/&/g, '","')
.replace(/=/g, '":"') +
'"}'
);
return object;
}
export function changeLanguage(
i18n,
currentLng = localStorage.getItem(LANGUAGE)
) {
return currentLng
? i18n.language !== currentLng
? i18n.changeLanguage(currentLng)
: Promise.resolve((...args) => i18n.t(...args))
: i18n.changeLanguage("en");
}
export function redirectToDefaultPage() {
if (
2020-09-25 16:14:50 +00:00
(window.location.pathname === "/" ||
window.location.pathname === "" ||
window.location.pathname === "/login") &&
localStorage.getItem(AUTH_KEY) !== null
) {
setTimeout(() => window.location.replace("/products/files"), 0);
return true;
}
return false;
}
2020-09-30 21:44:49 +00:00
export function createPasswordHash(password, hashSettings) {
if (
!password ||
!hashSettings ||
typeof password !== "string" ||
typeof hashSettings !== "object" ||
!hashSettings.hasOwnProperty("salt") ||
!hashSettings.hasOwnProperty("size") ||
!hashSettings.hasOwnProperty("iterations") ||
typeof hashSettings.size !== "number" ||
typeof hashSettings.iterations !== "number" ||
typeof hashSettings.salt !== "string"
)
throw new Error("Invalid params.");
2020-09-30 21:44:49 +00:00
const { size, iterations, salt } = hashSettings;
let bits = sjcl.misc.pbkdf2(password, salt, iterations);
bits = bits.slice(0, size / 32);
const hash = sjcl.codec.hex.fromBits(bits);
return hash;
}
export function removeTempContent() {
const tempElm = document.getElementById("temp-content");
if (tempElm) {
tempElm.outerHTML = "";
}
}
export function hideLoader() {
if (isMobile) return;
if (window.loadingTimeout) {
clearTimeout(window.loadingTimeout);
window.loadingTimeout = null;
}
document.body.classList.remove("loading");
}
export function showLoader() {
if (isMobile) return;
window.loadingTimeout = setTimeout(() => {
document.body.classList.add("loading");
}, 1000);
}
2020-11-17 03:29:24 +00:00
export { withLayoutSize } from "./withLayoutSize";