admin-script.js
1 week ago
dashboard-posts.js
1 week ago
free-template-admin.js
1 week ago
front-script.js
1 week ago
jquery.notify.min.js
1 week ago
license_script.js
1 week ago
plugin-admin.js
1 week ago
template-admin.js
1 week ago
dashboard-posts.js
117 lines
| 1 | document.addEventListener('DOMContentLoaded', function () { |
| 2 | const dashpostsApiEndpoint = fleximp_dashposts_ajax_object.apiEndpoint; |
| 3 | let isFetching = false; |
| 4 | let swiper; // declare globally |
| 5 | |
| 6 | function fetchPosts() { |
| 7 | if (isFetching) return; |
| 8 | isFetching = true; |
| 9 | |
| 10 | fetch(dashpostsApiEndpoint, { |
| 11 | method: 'POST', |
| 12 | headers: { |
| 13 | 'Content-Type': 'application/json' |
| 14 | }, |
| 15 | body: JSON.stringify({ |
| 16 | action: 'getLatestPosts' |
| 17 | }) |
| 18 | }) |
| 19 | .then(response => response.json()) |
| 20 | .then(data => { |
| 21 | if (data.code == 200 && Array.isArray(data.data)) { |
| 22 | displayPosts(data.data); |
| 23 | initSwiper(); |
| 24 | } else { |
| 25 | console.error('Error fetching plugins:', data); |
| 26 | } |
| 27 | }) |
| 28 | .catch(error => { |
| 29 | console.error('Error:', error); |
| 30 | }) |
| 31 | .finally(() => { |
| 32 | isFetching = false; |
| 33 | }); |
| 34 | } |
| 35 | |
| 36 | function displayPosts(posts) { |
| 37 | const swiperWrapper = document.querySelector('.swiper-container-2 .swiper-wrapper'); |
| 38 | swiperWrapper.innerHTML = ''; |
| 39 | |
| 40 | posts.forEach(post => { |
| 41 | const postHTML = ` |
| 42 | <div class="swiper-slide"> |
| 43 | <div class="plugin-item"> |
| 44 | <div class="flex-imp-sidebar-image-box"> |
| 45 | <img src="${post.image}" alt="img" style="max-height:75px; max-width:75px;" class="video-img"> |
| 46 | <a href="${post.url}" target="_blank"> |
| 47 | <h3 class="plugin-post-title pb-2 pt-2">${post.title}</h3> |
| 48 | </a> |
| 49 | <a href="${post.url}" |
| 50 | class="fleximp-dash-btn fleximp-dash-btn-review flex-demo-btn mb-4 flex-contact-btn" |
| 51 | target="_blank" rel="noopener">Read More</a> |
| 52 | </div> |
| 53 | </div> |
| 54 | </div>`; |
| 55 | |
| 56 | swiperWrapper.insertAdjacentHTML('beforeend', postHTML); |
| 57 | }); |
| 58 | } |
| 59 | |
| 60 | function initSwiper() { |
| 61 | |
| 62 | const container = document.querySelector('.swiper-container-2'); |
| 63 | |
| 64 | // If already initialized → destroy first |
| 65 | if (container.swiper) { |
| 66 | container.swiper.destroy(true, true); |
| 67 | } |
| 68 | |
| 69 | swiper = new Swiper('.swiper-container-2', { |
| 70 | loop: true, |
| 71 | slidesPerView: 4, // default fallback |
| 72 | spaceBetween: 30, |
| 73 | slidesPerGroup: 1, |
| 74 | navigation: { |
| 75 | nextEl: '.swiper-button-next', |
| 76 | prevEl: '.swiper-button-prev' |
| 77 | }, |
| 78 | |
| 79 | breakpoints: { |
| 80 | 0: { |
| 81 | slidesPerView: 1, |
| 82 | spaceBetween: 20 |
| 83 | }, |
| 84 | 768: { |
| 85 | slidesPerView: 2, |
| 86 | spaceBetween: 25 |
| 87 | }, |
| 88 | 1024: { |
| 89 | slidesPerView: 4, |
| 90 | spaceBetween: 30 |
| 91 | } |
| 92 | }, |
| 93 | |
| 94 | observer: true, |
| 95 | observeParents: true |
| 96 | }); |
| 97 | } |
| 98 | |
| 99 | // Initialization function for dashboard tab |
| 100 | let initialised = false; |
| 101 | |
| 102 | function initDashboard() { |
| 103 | if (initialised) return; |
| 104 | initialised = true; |
| 105 | fetchPosts(); |
| 106 | } |
| 107 | |
| 108 | // Check if Dashboard tab is already active on page load |
| 109 | if (jQuery('#dashboard').hasClass('active') || jQuery('#dashboard').hasClass('show')) { |
| 110 | initDashboard(); |
| 111 | } |
| 112 | |
| 113 | // Bootstrap 3/4 tab shown event |
| 114 | jQuery(document).on('shown.bs.tab', 'a[href="#dashboard"]', function () { |
| 115 | initDashboard(); |
| 116 | }); |
| 117 | }); |