main.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361
  1. // 載入共用 template
  2. $('#navbar').load('../../template/navbar.html');
  3. $('#footer').load('../../template/footer.html');
  4. $('#btn-box').load('../../template/button.html');
  5. $('#topCarousel').load('../../template/top_carousel.html');
  6. $('#removeResultBtn').hide(); // 隱藏全部清除按鈕
  7. // 處理 radio 選取狀態
  8. $('.filter-list .form-check').click(function (e) {
  9. e.preventDefault();
  10. $(this).find('.form-check-input').prop('checked', true);
  11. // 取得 radio 按鈕的 label
  12. let radioLabel = $(this).find('.form-check-label').text().trim().replace(/\s+/g, ' '); // 移除換行空白
  13. // 取上一層 search-tab 按鈕文字
  14. let buttonValue = $(this).closest('.dropdown').find('.search-tab').text().trim();
  15. let buttonId = $(this).closest('.dropdown').find('.search-tab').attr('id');
  16. updateSelectedOptions(buttonId, buttonValue, radioLabel);
  17. });
  18. let filterList = []; // 篩選條件
  19. // 更新篩選狀態
  20. function updateSelectedOptions(id, button, radio) {
  21. page = 1;
  22. console.log('更新篩選狀態', button, radio);
  23. if (filterList.length) {
  24. let exists = false; // 判斷是否已存在
  25. for (let index = 0; index < filterList.length; index++) {
  26. const element = filterList[index];
  27. // 如有重複 button 只改 radio 狀態
  28. if (element.text === button) {
  29. element.value = radio;
  30. exists = true;
  31. // 更新篩選的值
  32. $(`.search-tab-result .budget:contains(${button})`).find('p').text(`${button}:${radio}`);
  33. }
  34. }
  35. // 如果 filterList 中不存在則新增
  36. if (!exists) {
  37. const newItem = {
  38. id: id,
  39. text: button,
  40. value: radio
  41. };
  42. filterList.push(newItem);
  43. createFilterHtml(newItem);
  44. }
  45. } else {
  46. // 儲存篩選條件
  47. filterList.push({
  48. id: id,
  49. text: button,
  50. value: radio
  51. })
  52. for (let index = 0; index < filterList.length; index++) {
  53. const element = filterList[index];
  54. createFilterHtml(element);
  55. }
  56. }
  57. videoSearch(); // 搜尋
  58. console.log('filterList', filterList);
  59. // 切換按鈕選取狀態
  60. $('.dropdown').each(function () {
  61. let hasCheckedRadio = $(this).find('input[type="radio"]:checked').length > 0;
  62. $(this).find('.search-tab').toggleClass('active', hasCheckedRadio);
  63. });
  64. }
  65. // 新增篩選條件 HTML
  66. function createFilterHtml(item) {
  67. let dom = `
  68. <span class="me-3">
  69. <span class="d-flex budget">
  70. <p class="me-1">${item.text}:${item.value}</p>
  71. <img onclick="removeBtn(this, '${item.id}', '${item.text}')" src="https://hhh.com.tw/assets/images/section/icon/close-btn-search.svg" alt="close-btn-search">
  72. </span>
  73. </span>`;
  74. $('.search-tab-result').append(dom);
  75. $('#removeResultBtn').show();
  76. }
  77. // 清除單一篩選條件
  78. function removeBtn(element, id, val) {
  79. console.log('id, val', id, val);
  80. filterList = filterList.filter(item => item.text !== val);
  81. ; // 移除 div & active
  82. $(element).closest('.me-3').remove();
  83. $(`#${id}`).removeClass('active');
  84. // 移除同一層級 ul 內所有 radio 的選取狀態
  85. $(`#${id}`).siblings('.dropdown-menu').find('input[type="radio"]').prop('checked', false);
  86. if (filterList.length === 0) {
  87. $('#removeResultBtn').hide();
  88. }
  89. videoSearch();
  90. }
  91. let assignOrder = ""; // 當前排序
  92. // new, hot, recommend 排序 (預設推薦)
  93. $(".search-btn-filter button").click(function () {
  94. // 切換選取狀態
  95. $('.search-btn-filter').find('.active').removeClass('active');
  96. $(this).addClass('active');
  97. assignOrder = $(this).attr('id');
  98. videoSearch("order_by");
  99. });
  100. let page = 1; // 當前頁數
  101. let pageSize = 18; // 每頁數量
  102. let isFirstLoad = true; // 初始載入
  103. // 列表篩選
  104. async function videoSearch(type = "") {
  105. let url;
  106. $('#videoSpinner').show();
  107. $('#videoList').hide();
  108. if (isFirstLoad) {
  109. // 第一次載入使用本地 JSON 文件
  110. // url = './json/designers_lists_data.json';
  111. url = `https://m3.hhh.com.tw:18673/video_search?page=${page}&page_size=${pageSize}&order_by=new`;
  112. } else {
  113. // 後續使用 API (預設排序為 recommend)
  114. url = `https://m3.hhh.com.tw:18673/video_search?page=${page}&page_size=${pageSize}`;
  115. if (type === "order_by") {
  116. url += `&${type}=${assignOrder}`;
  117. }
  118. if (assignOrder === "") {
  119. url += "&order_by=new";
  120. }
  121. let input = $(".keywords").val();
  122. if (input !== "") {
  123. url += `&keyword=${input}`;
  124. }
  125. if (filterList.length) {
  126. filterList.map(item => {
  127. url += `&${item.id}=${item.value}`;
  128. });
  129. }
  130. }
  131. try {
  132. const response = await axios.get(url);
  133. console.log('response.data.videos', response.data.videos);
  134. let totalCount = response.data.total_count;
  135. let totalPages = Math.ceil(totalCount / pageSize);
  136. $("#totalCount").html(totalCount);
  137. if (totalPages) {
  138. $('.filter-list .pagination').show();
  139. setPagination(totalPages); // 分頁處理
  140. } else {
  141. $('.filter-list .pagination').hide();
  142. }
  143. let resultHtml = '';
  144. if (response.data.videos.length) {
  145. response.data.videos.forEach((item) => {
  146. let tagsHtml = '';
  147. item.VideoTag.forEach((tag) => {
  148. tagsHtml += `<span>${tag}</span>`;
  149. });
  150. console.log('tagsHtml', tagsHtml);
  151. resultHtml += `
  152. <div class="col-md-4 mb-4">
  153. <a href="${item.DesignerLink}">
  154. <div class="card lists-card">
  155. <img src="${item.VideoCoverImg}" class="video-cover-img" alt="${item.DesignerName} ${item.DesignerTitle}">
  156. <div class="card-body d-flex flex-column align-items-center">
  157. <section class="d-flex align-items-center w-100">
  158. <div class="person-img me-3 me-md-2 me-lg-3" style="background-image: url('${item.DesignerCoverImg}');"></div>
  159. <h5 class="text-muted mb-2">${item.DesignerTitle}</h5>
  160. </section>
  161. <h3 class="my-3 video-title">${item.VideoTitle}</h3>
  162. <div class="tags-container mt-2 me-auto">${tagsHtml}</div>
  163. </div>
  164. </div>
  165. </a>
  166. </div>`;
  167. });
  168. } else {
  169. resultHtml += "<p class='text-center mt-5'>找不到符合的資料,請重新搜尋。</p>"
  170. }
  171. $('#videoList').html(resultHtml);
  172. setTimeout(() => {
  173. $('#videoList').show();
  174. $('#videoSpinner').hide();
  175. }, 100)
  176. // 更新初始載入狀態
  177. if (isFirstLoad) {
  178. isFirstLoad = false;
  179. }
  180. } catch (error) {
  181. console.log("error", error);
  182. }
  183. }
  184. videoSearch();
  185. let maxPagesMobile = 5; // 手機板最多顯示 5 頁
  186. let maxPagesDesktop = 10; // 電腦版最多顯示 10 頁
  187. // 分頁
  188. function setPagination(pages) {
  189. let screenWidth = $(window).width();
  190. let maxPages = screenWidth > 991 ? maxPagesDesktop : maxPagesMobile;
  191. function renderPagination(currentPage) {
  192. let dom = `
  193. <li class="page-item" onclick="handlePagination(this, 'previous')">
  194. <a class="page-link previous hidden" href="#" aria-label="Previous">
  195. <span aria-hidden="true"><</span>
  196. </a>
  197. </li>`;
  198. let startPage = Math.max(currentPage - Math.floor(maxPages / 2), 1);
  199. let endPage = Math.min(startPage + maxPages - 1, pages);
  200. if (endPage - startPage < maxPages) {
  201. startPage = Math.max(endPage - maxPages + 1, 1);
  202. }
  203. for (let index = startPage; index <= endPage; index++) {
  204. dom += `
  205. <li class="page-item ${index === currentPage ? 'active' : ''}" onclick="handlePagination(this)">
  206. <a class="page-link" href="#">${index}</a>
  207. </li>`;
  208. }
  209. dom += `
  210. <li class="page-item" onclick="handlePagination(this, 'next')">
  211. <a class="page-link next" href="#" aria-label="Next">
  212. <span aria-hidden="true">></span>
  213. </a>
  214. </li>`;
  215. $('.filter-list .pagination').html(dom);
  216. updateVisibility(currentPage, pages);
  217. }
  218. // 更新上頁 & 下頁的按鈕顯示狀態
  219. function updateVisibility(page, pages) {
  220. if (page === 1) {
  221. $('.page-link.previous').addClass('hidden');
  222. } else {
  223. $('.page-link.previous').removeClass('hidden');
  224. }
  225. if (page === pages) {
  226. $('.page-link.next').addClass('hidden');
  227. } else {
  228. $('.page-link.next').removeClass('hidden');
  229. }
  230. }
  231. window.handlePagination = function (item, type = "") {
  232. if (type === "previous" && page > 1) {
  233. page--;
  234. } else if (type === "next" && page < pages) {
  235. page++;
  236. } else if (!type) {
  237. page = parseInt($(item).find('.page-link').text());
  238. }
  239. renderPagination(page);
  240. videoSearch();
  241. }
  242. renderPagination(page);
  243. $(window).on('resize', function () {
  244. let newScreenWidth = $(window).width();
  245. if ((newScreenWidth > 991 && maxPages !== maxPagesDesktop) ||
  246. (newScreenWidth <= 991 && maxPages !== maxPagesMobile)) {
  247. maxPages = newScreenWidth > 991 ? maxPagesDesktop : maxPagesMobile;
  248. renderPagination(page);
  249. }
  250. });
  251. }
  252. // 頁碼處理
  253. function handlePagination(item, type = "") {
  254. if (type === "previous") {
  255. // 往前一頁
  256. if (page > 1) {
  257. page--;
  258. }
  259. console.log(page);
  260. } else if (type === "next") {
  261. // 往後一頁
  262. page++;
  263. console.log(page);
  264. } else {
  265. // 直接點擊頁碼
  266. page = parseInt($(item).find('.page-link')[0].innerText);
  267. }
  268. // 切換選取狀態
  269. $('.filter-list .page-item').removeClass('active');
  270. $('.filter-list .page-item').eq(page).addClass('active');
  271. // 設定上一頁按鈕的顯示狀態
  272. if (page === 1) {
  273. $('.page-link.previous').addClass('hidden');
  274. } else {
  275. $('.page-link.previous').removeClass('hidden');
  276. }
  277. videoSearch(); // 搜尋
  278. }
  279. // 熱搜關鍵字搜尋
  280. $(".search-bar-keyword a").click(function () {
  281. let keyword = $(this).text();
  282. $('.keywords').val(keyword);
  283. videoSearch();
  284. });
  285. // 全部清除
  286. $("#removeResultBtn").click(function () {
  287. console.log('全部清除');
  288. filterList.length = 0; // 清空篩選陣列
  289. $('#removeResultBtn').hide(); // 隱藏全部清除按鈕
  290. $('.search-tab-result').empty(); // 清空篩選條件 dom
  291. // 取消選取狀態
  292. $('.filter-list input[type="radio"]').prop('checked', false);
  293. $('.search-tab').removeClass('active');
  294. videoSearch();
  295. });