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