| 1 |
const urlCourses = lpGlobalSettings.courses_url || ''; |
| 2 |
const urlCurrent = document.location.href; |
| 3 |
let filterCourses = JSON.parse( window.localStorage.getItem( 'lp_filter_courses' ) ) || {}; |
| 4 |
let skeleton; |
| 5 |
let skeletonClone; |
| 6 |
let isLoading = false; |
| 7 |
let firstLoad = 1; |
| 8 |
let elNoLoadAjaxFirst = null; |
| 9 |
let elArchive = null; |
| 10 |
|
| 11 |
if ( lpGlobalSettings.is_course_archive ) { |
| 12 |
const queryString = window.location.search; |
| 13 |
|
| 14 |
if ( ! queryString.length && urlCurrent.search( 'page' ) === -1 ) { |
| 15 |
filterCourses = {}; |
| 16 |
} |
| 17 |
} |
| 18 |
|
| 19 |
const lpArchiveAddQueryArgs = ( endpoint, args ) => { |
| 20 |
const url = new URL( endpoint ); |
| 21 |
|
| 22 |
Object.keys( args ).forEach( ( arg ) => { |
| 23 |
url.searchParams.set( arg, args[ arg ] ); |
| 24 |
} ); |
| 25 |
|
| 26 |
return url; |
| 27 |
}; |
| 28 |
|
| 29 |
const lpArchiveCourse = () => { |
| 30 |
skeleton = document.querySelector( '.lp-archive-course-skeleton' ); |
| 31 |
elNoLoadAjaxFirst = document.querySelector( '.no-first-load-ajax' ); |
| 32 |
|
| 33 |
if ( ! skeleton ) { |
| 34 |
return; |
| 35 |
} |
| 36 |
|
| 37 |
if ( skeleton && ! elNoLoadAjaxFirst ) { |
| 38 |
lpArchiveRequestCourse( filterCourses ); |
| 39 |
} |
| 40 |
|
| 41 |
lpArchivePaginationCourse(); |
| 42 |
lpArchiveSearchCourse(); |
| 43 |
}; |
| 44 |
|
| 45 |
window.lpArchiveRequestCourse = ( args, callBackSuccess ) => { |
| 46 |
const wpRestUrl = lpGlobalSettings.lp_rest_url; |
| 47 |
|
| 48 |
if ( ! wpRestUrl ) { |
| 49 |
return; |
| 50 |
} |
| 51 |
|
| 52 |
if ( ! skeleton ) { |
| 53 |
return; |
| 54 |
} |
| 55 |
|
| 56 |
const archiveCourse = elArchive && elArchive.querySelector( 'div.lp-archive-courses .lp-content-area' ); |
| 57 |
const listCourse = archiveCourse && archiveCourse.querySelector( 'ul.learn-press-courses' ); |
| 58 |
|
| 59 |
if ( ! listCourse ) { |
| 60 |
return; |
| 61 |
} |
| 62 |
|
| 63 |
if ( isLoading ) { |
| 64 |
return; |
| 65 |
} |
| 66 |
|
| 67 |
isLoading = true; |
| 68 |
|
| 69 |
if ( ! skeletonClone ) { |
| 70 |
skeletonClone = skeleton.outerHTML; |
| 71 |
} else { |
| 72 |
listCourse.append( skeleton ); |
| 73 |
// return; |
| 74 |
} |
| 75 |
|
| 76 |
const urlCourseArchive = lpArchiveAddQueryArgs( wpRestUrl + 'lp/v1/courses/archive-course', { ...lpArchiveSkeleton, ...args } ); |
| 77 |
const url = lpGlobalSettings.lp_rest_url + 'lp/v1/courses/archive-course' + urlCourseArchive.search; |
| 78 |
|
| 79 |
fetch( url, { |
| 80 |
method: 'GET', |
| 81 |
} ) |
| 82 |
.then( ( response ) => response.json() ) |
| 83 |
.then( ( response ) => { |
| 84 |
if ( typeof response.data.content !== 'undefined' && listCourse ) { |
| 85 |
listCourse.innerHTML = response.data.content || ''; |
| 86 |
} |
| 87 |
|
| 88 |
const pagination = response.data.pagination; |
| 89 |
|
| 90 |
// lpArchiveSearchCourse(); |
| 91 |
|
| 92 |
const paginationEle = document.querySelector( '.learn-press-pagination' ); |
| 93 |
if ( paginationEle ) { |
| 94 |
paginationEle.remove(); |
| 95 |
} |
| 96 |
|
| 97 |
if ( typeof pagination !== 'undefined' ) { |
| 98 |
const paginationHTML = new DOMParser().parseFromString( pagination, 'text/html' ); |
| 99 |
const paginationNewNode = paginationHTML.querySelector( '.learn-press-pagination' ); |
| 100 |
//const paginationInnerHTML = paginationSelector && paginationSelector.innerHTML; |
| 101 |
|
| 102 |
if ( paginationNewNode ) { |
| 103 |
listCourse.after( paginationNewNode ); |
| 104 |
lpArchivePaginationCourse(); |
| 105 |
} |
| 106 |
} |
| 107 |
|
| 108 |
wp.hooks.doAction( 'lp-js-get-courses', response ); |
| 109 |
|
| 110 |
if ( typeof callBackSuccess === 'function' ) { |
| 111 |
callBackSuccess( response ); |
| 112 |
} |
| 113 |
} ).catch( ( error ) => { |
| 114 |
listCourse.innerHTML += `<div class="lp-ajax-message error" style="display:block">${ error.message || 'Error: Query lp/v1/courses/archive-course' }</div>`; |
| 115 |
console.log( error ); |
| 116 |
} ).finally( () => { |
| 117 |
isLoading = false; |
| 118 |
// skeleton && skeleton.remove(); |
| 119 |
|
| 120 |
jQuery( 'form.search-courses button' ).removeClass( 'loading' ); |
| 121 |
|
| 122 |
if ( ! firstLoad ) { |
| 123 |
// Scroll to archive element |
| 124 |
const optionScroll = { behavior: 'smooth' }; |
| 125 |
elArchive.scrollIntoView( optionScroll ); |
| 126 |
} else { |
| 127 |
firstLoad = 0; |
| 128 |
} |
| 129 |
|
| 130 |
// Save filter courses to Storage |
| 131 |
window.localStorage.setItem( 'lp_filter_courses', JSON.stringify( args ) ); |
| 132 |
// Change url by params filter courses |
| 133 |
const urlPush = lpArchiveAddQueryArgs( document.location, args ); |
| 134 |
window.history.pushState( '', '', urlPush ); |
| 135 |
} ); |
| 136 |
}; |
| 137 |
|
| 138 |
const lpArchiveSearchCourse = () => { |
| 139 |
const searchForm = document.querySelectorAll( 'form.search-courses' ); |
| 140 |
const filterCourses = JSON.parse( window.localStorage.getItem( 'lp_filter_courses' ) ) || {}; |
| 141 |
|
| 142 |
searchForm.forEach( ( s ) => { |
| 143 |
const search = s.querySelector( 'input[name="c_search"]' ); |
| 144 |
const btn = s.querySelector( '[type="submit"]' ); |
| 145 |
let timeOutSearch; |
| 146 |
|
| 147 |
search.addEventListener( 'keyup', ( event ) => { |
| 148 |
if ( skeleton ) { |
| 149 |
skeleton.style.display = 'block'; |
| 150 |
} |
| 151 |
event.preventDefault(); |
| 152 |
|
| 153 |
const s = event.target.value.trim(); |
| 154 |
|
| 155 |
if ( ! s || ( s && s.length > 2 ) ) { |
| 156 |
if ( undefined !== timeOutSearch ) { |
| 157 |
clearTimeout( timeOutSearch ); |
| 158 |
} |
| 159 |
|
| 160 |
timeOutSearch = setTimeout( function() { |
| 161 |
btn.classList.add( 'loading' ); |
| 162 |
|
| 163 |
filterCourses.c_search = s; |
| 164 |
filterCourses.paged = 1; |
| 165 |
|
| 166 |
lpArchiveRequestCourse( { ...filterCourses } ); |
| 167 |
}, 800 ); |
| 168 |
} |
| 169 |
} ); |
| 170 |
|
| 171 |
s.addEventListener( 'submit', ( e ) => { |
| 172 |
e.preventDefault(); |
| 173 |
|
| 174 |
const eleSearch = s.querySelector( 'input[name="c_search"]' ); |
| 175 |
eleSearch && eleSearch.dispatchEvent( new Event( 'keyup' ) ); |
| 176 |
} ); |
| 177 |
} ); |
| 178 |
}; |
| 179 |
|
| 180 |
const lpArchivePaginationCourse = () => { |
| 181 |
const paginationEle = document.querySelectorAll( '.lp-archive-courses .learn-press-pagination .page-numbers' ); |
| 182 |
|
| 183 |
paginationEle.length > 0 && paginationEle.forEach( ( ele ) => ele.addEventListener( 'click', ( event ) => { |
| 184 |
event.preventDefault(); |
| 185 |
event.stopPropagation(); |
| 186 |
|
| 187 |
if ( ! elArchive ) { |
| 188 |
return; |
| 189 |
} |
| 190 |
|
| 191 |
if ( skeleton ) { |
| 192 |
skeleton.style.display = 'block'; |
| 193 |
} |
| 194 |
|
| 195 |
// Scroll to archive element |
| 196 |
elArchive.scrollIntoView( { behavior: 'smooth' } ); |
| 197 |
|
| 198 |
let filterCourses = {}; |
| 199 |
filterCourses = JSON.parse( window.localStorage.getItem( 'lp_filter_courses' ) ) || {}; |
| 200 |
|
| 201 |
const urlString = event.currentTarget.getAttribute( 'href' ); |
| 202 |
|
| 203 |
if ( urlString ) { |
| 204 |
const current = [ ...paginationEle ].filter( ( el ) => el.classList.contains( 'current' ) ); |
| 205 |
const paged = event.currentTarget.textContent || ( ele.classList.contains( 'next' ) && parseInt( current[ 0 ].textContent ) + 1 ) || ( ele.classList.contains( 'prev' ) && parseInt( current[ 0 ].textContent ) - 1 ); |
| 206 |
filterCourses.paged = paged; |
| 207 |
|
| 208 |
lpArchiveRequestCourse( { ...filterCourses } ); |
| 209 |
} |
| 210 |
} ) ); |
| 211 |
}; |
| 212 |
|
| 213 |
const lpArchiveGridListCourse = () => { |
| 214 |
const layout = LP.Cookies.get( 'courses-layout' ); |
| 215 |
|
| 216 |
const switches = document.querySelectorAll( '.lp-courses-bar .switch-layout [name="lp-switch-layout-btn"]' ); |
| 217 |
|
| 218 |
switches.length > 0 && [ ...switches ].map( ( ele ) => ele.value === layout && ( ele.checked = true ) ); |
| 219 |
}; |
| 220 |
|
| 221 |
const lpArchiveGridListCourseHandle = () => { |
| 222 |
const gridList = document.querySelectorAll( '.lp-archive-courses input[name="lp-switch-layout-btn"]' ); |
| 223 |
|
| 224 |
gridList.length > 0 && gridList.forEach( ( element ) => element.addEventListener( 'change', ( e ) => { |
| 225 |
e.preventDefault(); |
| 226 |
|
| 227 |
const layout = e.target.value; |
| 228 |
|
| 229 |
if ( layout ) { |
| 230 |
const dataLayout = document.querySelector( '.lp-archive-courses .learn-press-courses[data-layout]' ); |
| 231 |
|
| 232 |
dataLayout && ( dataLayout.dataset.layout = layout ); |
| 233 |
LP.Cookies.set( 'courses-layout', layout ); |
| 234 |
} |
| 235 |
} ) ); |
| 236 |
}; |
| 237 |
|
| 238 |
function LPArchiveCourseInit() { |
| 239 |
elArchive = document.querySelector( '.lp-archive-courses' ); |
| 240 |
lpArchiveCourse(); |
| 241 |
lpArchiveGridListCourseHandle(); |
| 242 |
lpArchiveGridListCourse(); |
| 243 |
} |
| 244 |
|
| 245 |
document.addEventListener( 'DOMContentLoaded', function( event ) { |
| 246 |
LPArchiveCourseInit(); |
| 247 |
} ); |
| 248 |
|