#35 rewrite code to modules

This commit is contained in:
Serraniel 2020-08-26 20:31:56 +02:00
parent 329031aff3
commit 0cb210dd4b
Signed by: Serraniel
GPG key ID: 3690B4E7364525D3
6 changed files with 65 additions and 35 deletions

View file

@ -2,6 +2,7 @@ import * as core from '../utils/aniwatchCore';
import * as color from '../utils/colors'; import * as color from '../utils/colors';
import * as helper from '../utils/helpers'; import * as helper from '../utils/helpers';
export function init() {
core.registerScript(node => { core.registerScript(node => {
// run the scripts // run the scripts
if (helper.isHtmlElement(node)) { if (helper.isHtmlElement(node)) {
@ -10,6 +11,7 @@ core.registerScript(node => {
removeUnknownUsers(node); removeUnknownUsers(node);
} }
}, "/requests"); }, "/requests");
}
function changeFollowedStarColor(node) { function changeFollowedStarColor(node) {
const starIcon = 'star'; const starIcon = 'star';

View file

@ -1,12 +1,14 @@
import * as core from '../utils/aniwatchCore'; import * as core from '../utils/aniwatchCore';
import * as helper from '../utils/helpers'; import * as helper from '../utils/helpers';
export function init() {
core.registerScript(node => { core.registerScript(node => {
// run the scripts // run the scripts
if (helper.isHtmlElement(node)) { if (helper.isHtmlElement(node)) {
addListHorizontalSeparators(node) addListHorizontalSeparators(node)
} }
}, ".*"); }, ".*");
}
function addListHorizontalSeparators(node) { function addListHorizontalSeparators(node) {
const targetTagName = 'MD-LIST-ITEM'; // tagName is upper case const targetTagName = 'MD-LIST-ITEM'; // tagName is upper case

View file

@ -4,11 +4,14 @@ import * as helper from '../utils/helpers';
const quickSearchID = 'ea-quickSearch'; const quickSearchID = 'ea-quickSearch';
const quickSearchLink = 'ea-quickSearchLink'; const quickSearchLink = 'ea-quickSearchLink';
export function init() {
core.runAfterLoad(() => { core.runAfterLoad(() => {
initSearch(); initSearch();
}, ".*"); }, ".*");
}
function initSearch() { function initSearch() {
console.log("INITSEARCH")
let entry = document.createElement('li'); let entry = document.createElement('li');
entry.setAttribute('ng-repeat', 'item in navbar'); entry.setAttribute('ng-repeat', 'item in navbar');
entry.setAttribute('ng-class', '{\'anime-indicator\': item[\'@attributes\'].title==\'Anime\'}'); entry.setAttribute('ng-class', '{\'anime-indicator\': item[\'@attributes\'].title==\'Anime\'}');

19
src/javascript/index.js Normal file
View file

@ -0,0 +1,19 @@
// core
import { initCore } from './utils/aniwatchCore';
// helper
import { initHelpers } from './utils/helpers';
// enhancements
import { init as animeRequests } from './enhancements/animeRequests';
import { init as lists } from './enhancements/lists';
import { init as quickSearch } from './enhancements/quickSearch';
// core
initCore();
//helper
initHelpers();
// enhancements
animeRequests();
lists();
quickSearch();

View file

@ -1,18 +1,7 @@
let __scripts = []; let __scripts = [];
let __afterLoadScripts = []; let __afterLoadScripts = [];
export function registerScript(func, pattern = '.*') { export function initCore() {
__scripts.push({ "function": func, "pattern": pattern });
}
export function runScripts(node) {
__scripts.forEach(script => {
if (window.location.pathname.match(script.pattern)) {
script.function(node);
}
});
}
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++) {
@ -27,6 +16,21 @@ observer.observe(document.documentElement || document.body, {
attributes: true attributes: true
}); });
document.addEventListener("DOMContentLoaded", event => awaitPageLoaded(), false);
}
export function registerScript(func, pattern = '.*') {
__scripts.push({ "function": func, "pattern": pattern });
}
export function runScripts(node) {
__scripts.forEach(script => {
if (window.location.pathname.match(script.pattern)) {
script.function(node);
}
});
}
function findPreloader() { function findPreloader() {
return document.getElementById('preloader'); return document.getElementById('preloader');
} }
@ -40,8 +44,6 @@ export function runAfterLoad(func, pattern = '.*') {
} }
} }
document.addEventListener("DOMContentLoaded", event => awaitPageLoaded(), false);
function awaitPageLoaded() { function awaitPageLoaded() {
let preLoader = findPreloader(); let preLoader = findPreloader();

View file

@ -5,8 +5,10 @@ export function isHtmlElement(object) {
return object instanceof HTMLElement; return object instanceof HTMLElement;
} }
export function initHelpers() {
document.addEventListener('keydown', event => handleKeyDown(event)); document.addEventListener('keydown', event => handleKeyDown(event));
document.addEventListener('keyup', event => handleKeyUp(event)); document.addEventListener('keyup', event => handleKeyUp(event));
}
function handleKeyDown(event) { function handleKeyDown(event) {
handleKeyToggle(event, true); handleKeyToggle(event, true);