Files
Hydro/packages/ui-default/utils/index.ts
T
2025-04-29 15:02:20 +01:00

83 lines
2.8 KiB
TypeScript

import 'streamsaver/examples/zip-stream';
import { request } from './base';
export async function api(method: string, args: Record<string, any>, projection: any) {
const res = await request.post(`/d/${UiContext.domainId}/api/${encodeURIComponent(method)}`, { args, projection });
if (res.error) throw new Error(res.error);
return res;
}
export function getAvailableLangs(langsList?: string[]) {
const prefixes = new Set(Object.keys(window.LANGS).filter((i) => i.includes('.')).map((i) => i.split('.')[0]));
const Langs = {};
for (const key in window.LANGS) {
if (prefixes.has(key)) continue;
if ((langsList instanceof Array) && !langsList.includes(key)) continue;
if (window.LANGS[key].hidden && !langsList?.includes(key)) continue;
if (window.LANGS[key].disabled) continue;
Langs[key] = window.LANGS[key];
}
return Langs;
}
export const createZipStream = (window as any).ZIP;
export function createZipBlob(underlyingSource) {
return new Response(createZipStream(underlyingSource)).blob();
}
export async function pipeStream(read, write, abort) {
if (window.WritableStream && read.pipeTo) {
const abortController = new AbortController();
if (abort) abort.abort = abortController.abort.bind(abortController);
await read.pipeTo(write, abortController);
} else {
const writer = write.getWriter();
if (abort) abort.abort = writer.abort.bind(writer);
const reader = read.getReader();
// eslint-disable-next-line no-constant-condition
while (1) {
const readResult = await reader.read();
if (readResult.done) {
writer.close();
break;
} else writer.write(readResult.value);
}
}
}
// https://github.com/andrasq/node-mongoid-js/blob/master/mongoid.js
export function mongoId(idstring: string) {
if (typeof idstring !== 'string') idstring = String(idstring);
return {
timestamp: parseInt(idstring.slice(0, 0 + 8), 16),
machineid: parseInt(idstring.slice(8, 8 + 6), 16),
pid: parseInt(idstring.slice(14, 14 + 4), 16),
sequence: parseInt(idstring.slice(18, 18 + 6), 16),
};
}
export function emulateAnchorClick(ev: KeyboardEvent, targetUrl: string, alwaysOpenInNewWindow = false) {
let openInNewWindow;
if (alwaysOpenInNewWindow) openInNewWindow = true;
else openInNewWindow = (ev.ctrlKey || ev.shiftKey || ev.metaKey);
if (openInNewWindow) window.open(targetUrl);
else window.location.href = targetUrl;
}
export { default as pjax } from './pjax';
export { default as base64 } from './base64';
export { default as loadReactRedux } from './loadReactRedux';
export * as mediaQuery from './mediaQuery';
export * from './slide';
export * from './base';
const zip = { createZipStream, createZipBlob };
Object.assign(window.Hydro.utils, {
zip,
pipeStream,
mongoId,
emulateAnchorClick,
});