@openmrs/esm-globals
Version:
78 lines (77 loc) • 3.22 kB
JavaScript
const connectivityChangedEventName = 'openmrs:connectivity-changed';
/** @internal */ export function dispatchConnectivityChanged(online) {
window.dispatchEvent(new CustomEvent(connectivityChangedEventName, {
detail: {
online
}
}));
}
/** @category Offline */ export function subscribeConnectivityChanged(cb) {
if (!window.offlineEnabled) {
return ()=>{};
}
const handler = (ev)=>cb(ev.detail);
window.addEventListener(connectivityChangedEventName, handler);
return ()=>window.removeEventListener(connectivityChangedEventName, handler);
}
/** @category Offline */ export function subscribeConnectivity(cb) {
cb({
online: window.offlineEnabled ? navigator.onLine : true
});
return subscribeConnectivityChanged(cb);
}
const precacheStaticDependenciesEventName = 'openmrs:precache-static-dependencies';
export function dispatchPrecacheStaticDependencies(data = {}) {
window.dispatchEvent(new CustomEvent(precacheStaticDependenciesEventName, {
detail: data
}));
}
/** @category Offline */ export function subscribePrecacheStaticDependencies(cb) {
const handler = (ev)=>cb(ev.detail);
window.addEventListener(precacheStaticDependenciesEventName, handler);
return ()=>window.removeEventListener(precacheStaticDependenciesEventName, handler);
}
const notificationShownName = 'openmrs:notification-shown';
const actionableNotificationShownName = 'openmrs:actionable-notification-shown';
const toastShownName = 'openmrs:toast-shown';
const snackbarShownName = 'openmrs:snack-bar-shown';
export function dispatchNotificationShown(data) {
window.dispatchEvent(new CustomEvent(notificationShownName, {
detail: data
}));
}
/** @internal */ export function dispatchActionableNotificationShown(data) {
window.dispatchEvent(new CustomEvent(actionableNotificationShownName, {
detail: data
}));
}
/** @internal */ export function dispatchSnackbarShown(data) {
window.dispatchEvent(new CustomEvent(snackbarShownName, {
detail: data
}));
}
/** @internal */ export function dispatchToastShown(data) {
window.dispatchEvent(new CustomEvent(toastShownName, {
detail: data
}));
}
/** @category UI */ export function subscribeNotificationShown(cb) {
const handler = (ev)=>cb(ev.detail);
window.addEventListener(notificationShownName, handler);
return ()=>window.removeEventListener(notificationShownName, handler);
}
/** @category UI */ export function subscribeActionableNotificationShown(cb) {
const handler = (ev)=>cb(ev.detail);
window.addEventListener(actionableNotificationShownName, handler);
return ()=>window.removeEventListener(actionableNotificationShownName, handler);
}
/** @category UI */ export function subscribeToastShown(cb) {
const handler = (ev)=>cb(ev.detail);
window.addEventListener(toastShownName, handler);
return ()=>window.removeEventListener(toastShownName, handler);
}
/** @category UI */ export function subscribeSnackbarShown(cb) {
const handler = (ev)=>cb(ev.detail);
window.addEventListener(snackbarShownName, handler);
return ()=>window.removeEventListener(snackbarShownName, handler);
}