Feature/#48 migrate to typescript #151

Merged
Serraniel merged 30 commits from feature/#48-migrate-to-typescript into develop 2020-12-30 17:24:49 +01:00
Showing only changes of commit ec87c72542 - Show all commits

View file

@ -1,11 +1,24 @@
import * as helper from './helpers'; import * as helper from './helpers';
/* SCRIPT LOGICS */ type ScriptCallback = () => void;
let __scripts = []; type NodeScriptCallback = (node: Node) => void;
let __afterLoadScripts = [];
let __afterLocationChangeScripts = [];
export function initCore() { type ScriptObj = {
function: ScriptCallback,
pattern: string
}
type NodeScriptObj = {
function: NodeScriptCallback,
pattern: string
}
/* SCRIPT LOGICS */
let __scripts: Array<NodeScriptObj> = [];
let __afterLoadScripts: Array<ScriptObj> = [];
let __afterLocationChangeScripts: Array<ScriptObj> = [];
export function initCore(): void {
let observer = new MutationObserver(mutations => { let observer = new MutationObserver(mutations => {
mutations.forEach(mutation => { mutations.forEach(mutation => {
for (let i = 0; i < mutation.addedNodes.length; i++) { for (let i = 0; i < mutation.addedNodes.length; i++) {
@ -46,11 +59,11 @@ export function initCore() {
helper.onReady(() => awaitPageLoaded()); helper.onReady(() => awaitPageLoaded());
} }
export function registerScript(func, pattern = '.*') { export function registerScript(func: NodeScriptCallback, pattern: string = '.*'): void {
__scripts.push({ "function": func, "pattern": pattern }); __scripts.push({ function: func, pattern: pattern });
} }
export function runScripts(node) { export function runScripts(node: Node): void {
__scripts.forEach(script => { __scripts.forEach(script => {
if (window.location.pathname.match(script.pattern)) { if (window.location.pathname.match(script.pattern)) {
script.function(node); script.function(node);
@ -58,20 +71,20 @@ export function runScripts(node) {
}); });
} }
function findPreloader() { function findPreloader(): HTMLElement {
return document.getElementById('preloader'); return document.getElementById('preloader');
} }
export function runAfterLoad(func, pattern = '.*') { export function runAfterLoad(func: ScriptCallback, pattern: string = '.*'): void {
let preloader = findPreloader(); let preloader = findPreloader();
if (typeof preloader !== undefined && preloader.style.display !== "none") { if (typeof preloader !== undefined && preloader.style.display !== "none") {
__afterLoadScripts.push({ "function": func, "pattern": pattern }); __afterLoadScripts.push({ function: func, pattern: pattern });
} else { } else {
func(); func();
} }
} }
function awaitPageLoaded() { function awaitPageLoaded(): void {
let preLoader = findPreloader(); let preLoader = findPreloader();
let runScripts = () => { let runScripts = () => {
@ -87,9 +100,9 @@ function awaitPageLoaded() {
return; return;
} }
let loop = setInterval(() => { let loop = window.setInterval(() => {
if (preLoader.style.display === "none" && document.readyState === 'complete') { if (preLoader.style.display === "none" && document.readyState === 'complete') {
clearInterval(loop); window.clearInterval(loop);
runScripts(); runScripts();
} }
@ -97,12 +110,12 @@ function awaitPageLoaded() {
} }
/* PATHNAME LOGIC */ /* PATHNAME LOGIC */
export function runAfterLocationChange(func, pattern = '.*') { export function runAfterLocationChange(func: ScriptCallback, pattern: string = '.*'): void {
__afterLocationChangeScripts.push({ "function": func, "pattern": pattern }); __afterLocationChangeScripts.push({ function: func, pattern: pattern });
} }
/* LOGIN LOGIC */ /* LOGIN LOGIC */
export function isLoggedIn() { export function isLoggedIn(): boolean {
let menu = document.getElementById('materialize-menu-dropdown'); let menu = document.getElementById('materialize-menu-dropdown');
let result = true; let result = true;