Skip to content

Commit

Permalink
= 4.2.5.8 =
Browse files Browse the repository at this point in the history
~ Check all class lp-load-ajax-element loaded to handle on 3 case: 1- file js loaded, 2-  readystatechange, 3- DomContentLoaded.
  • Loading branch information
tungnxt89 committed Dec 25, 2023
1 parent cf391d2 commit 9683a72
Showing 1 changed file with 11 additions and 14 deletions.
25 changes: 11 additions & 14 deletions assets/src/js/loadAJAX.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,11 @@ const lpAJAX = ( () => {
getElements: () => {
//console.log( 'getElements' );
// Finds all elements with the class '.lp-load-ajax-element'
const elements = document.querySelectorAll( '.lp-load-ajax-element' );
const elements = document.querySelectorAll( '.lp-load-ajax-element:not(.loaded)' );
if ( elements.length ) {
elements.forEach( ( element ) => {
//console.log( 'Element handing', element );
element.classList.add( 'loaded' );
const url = API.frontend.apiAJAX;
const elTarget = element.querySelector( '.lp-target' );
const dataObj = JSON.parse( elTarget.dataset.send );
Expand Down Expand Up @@ -78,26 +80,21 @@ const lpAJAX = ( () => {
};
} );

// Case 1: file JS loaded, find all elements with the class '.lp-load-ajax-element' not have class 'loaded'
if ( 'undefined' === typeof window.lpAJAXG ) {
window.lpAJAXG = lpAJAX();
window.lpAJAXG.getElements();
}

/*document.addEventListener( 'readystatechange', ( event ) => {
console.log( 'readystatechange' );
const elements = document.querySelectorAll( '.lp-load-ajax-element' );
elements.forEach( ( element ) => {
if ( ! element.classList.contains( 'loading-first' ) ) {
console.log( 'okokkk' );
element.classList.add( 'loading-first' );
} else {
console.log( 'nooooo' );
}
} );
// Case 2: readystatechange, find all elements with the class '.lp-load-ajax-element' not have class 'loaded'
document.addEventListener( 'readystatechange', ( event ) => {
//console.log( 'readystatechange' );
window.lpAJAXG.getElements();
} );

// Case 3: DOMContentLoaded, find all elements with the class '.lp-load-ajax-element' not have class 'loaded'
document.addEventListener( 'DOMContentLoaded', () => {
console.log( 'DOMContentLoaded' );
} );*/
window.lpAJAXG.getElements();
} );

export default lpAJAX;

0 comments on commit 9683a72

Please sign in to comment.