dees-domtools/ts/csstools.elementbasicsetup.ts

50 lines
1.6 KiB
TypeScript
Raw Normal View History

2020-05-23 15:00:01 +00:00
import { defer } from '@pushrocks/smartpromise';
/**
* a basic setup for elements
* makes sure everything is in check
*/
export const elementBasicSetup = async () => {
if (globalThis.deesCssToolsReady) {
await globalThis.deesCssToolsReady.promise;
} else {
2020-05-23 15:44:58 +00:00
// lets prevent double execution
2020-05-23 15:00:01 +00:00
globalThis.deesCssToolsReady = defer();
2020-05-23 15:44:58 +00:00
// lets make sure the dom is ready
2020-05-23 15:40:08 +00:00
const documentReady = defer();
document.onreadystatechange = () => {
2020-05-23 16:36:29 +00:00
if (document.readyState === 'interactive' || document.readyState === 'complete') {
2020-05-23 16:43:36 +00:00
console.log('elementBasicSetup: element basic setup complete')
2020-05-23 15:40:08 +00:00
documentReady.resolve();
2020-05-23 16:43:36 +00:00
} else {
console.log('elementBasicSetup: document not yet ready');
2020-05-23 15:40:08 +00:00
}
};
2020-05-23 16:43:36 +00:00
console.log('elementBasicSetup: waiting for document to be ready');
2020-05-23 15:40:08 +00:00
await documentReady.promise;
2020-05-23 16:43:36 +00:00
console.log('elementBasicSetup: document ready!');
2020-05-23 15:44:58 +00:00
// lets get started
2020-05-23 15:40:08 +00:00
const head = document.querySelector('head');
2020-05-23 15:44:58 +00:00
const body = document.querySelector('body');
// material font
const materialFontCss = `
@font-face {
font-family: 'Material Icons';
font-style: normal;
font-weight: 400;
src: url(https://fonts.gstatic.com/s/materialicons/v42/flUhRq6tzZclQEJ-Vdg-IuiaDsNcIhQ8tQ.woff2) format('woff2');
}
`;
2020-05-23 15:40:08 +00:00
const styleElement = document.createElement('style');
styleElement.type = 'text/css';
styleElement.appendChild(document.createTextNode(materialFontCss));
2020-05-23 15:44:58 +00:00
head.appendChild(styleElement);
2020-05-23 15:00:01 +00:00
globalThis.deesCssToolsReady.resolve();
}
};