lists.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636
  1. $('#removeResultBtn').hide(); // 隱藏全部清除按鈕
  2. // 處理 radio 選取狀態
  3. $('.filter-list .form-check').click(function (e) {
  4. e.preventDefault();
  5. $(this).find('.form-check-input').prop('checked', true);
  6. // 取得 radio 按鈕的 label
  7. let radioLabel = $(this).find('.form-check-label').text().trim().replace(/\s+/g, ' '); // 移除換行空白
  8. // 取上一層 search-tab 按鈕文字
  9. let buttonValue = $(this).closest('.dropdown').find('.search-tab').text().trim();
  10. let buttonId = $(this).closest('.dropdown').find('.search-tab').attr('id');
  11. updateSelectedOptions(buttonId, buttonValue, radioLabel);
  12. });
  13. let filterList = []; // 篩選條件
  14. // 更新篩選狀態
  15. function updateSelectedOptions(id, button, radio) {
  16. page = 1;
  17. console.log('更新篩選狀態', button, radio);
  18. push_GA(button, radio); // 發送 GA
  19. if (filterList.length) {
  20. let exists = false; // 判斷是否已存在
  21. for (let index = 0; index < filterList.length; index++) {
  22. const element = filterList[index];
  23. // 如有重複 button 只改 radio 狀態
  24. if (element.text === button) {
  25. element.value = radio;
  26. exists = true;
  27. // 更新篩選的值
  28. $(`.search-tab-result .budget:contains(${button})`).find('p').text(`${button}:${radio}`);
  29. }
  30. }
  31. // 如果 filterList 中不存在則新增
  32. if (!exists) {
  33. const newItem = {
  34. id: id,
  35. text: button,
  36. value: radio
  37. };
  38. filterList.push(newItem);
  39. createFilterHtml(newItem);
  40. }
  41. } else {
  42. // 儲存篩選條件
  43. filterList.push({
  44. id: id,
  45. text: button,
  46. value: radio
  47. })
  48. for (let index = 0; index < filterList.length; index++) {
  49. const element = filterList[index];
  50. createFilterHtml(element);
  51. }
  52. }
  53. page = 1;
  54. setTimeout(() => {
  55. dataSearch(); // 搜尋
  56. }, 0)
  57. console.log('filterList', filterList);
  58. // 切換按鈕選取狀態
  59. $('.dropdown').each(function () {
  60. let hasCheckedRadio = $(this).find('input[type="radio"]:checked').length > 0;
  61. $(this).find('.search-tab').toggleClass('active', hasCheckedRadio);
  62. });
  63. }
  64. // 新增篩選條件 HTML
  65. function createFilterHtml(item) {
  66. let dom = `
  67. <span class="me-3">
  68. <span class="d-flex budget">
  69. <p class="me-1">${item.text}:${item.value}</p>
  70. <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">
  71. </span>
  72. </span>`;
  73. $('.search-tab-result').append(dom);
  74. $('#removeResultBtn').show();
  75. }
  76. // 清除單一篩選條件
  77. function removeBtn(element, id, val) {
  78. filterList = filterList.filter(item => item.text !== val);
  79. ; // 移除 div & active
  80. $(element).closest('.me-3').remove();
  81. $(`#${id}`).removeClass('active');
  82. // 移除同一層級 ul 內所有 radio 的選取狀態
  83. $(`#${id}`).siblings('.dropdown-menu').find('input[type="radio"]').prop('checked', false);
  84. if (filterList.length === 0) {
  85. $('#removeResultBtn').hide();
  86. }
  87. dataSearch();
  88. if (id === "keyword") {
  89. // 判斷函數是否存在
  90. if (typeof getAllSearchCount === 'function') {
  91. getAllSearchCount();
  92. }
  93. }
  94. }
  95. // 鍵盤 Enter 輸入
  96. $('.keywords').on('keydown', function (event) {
  97. if (event.key === 'Enter') {
  98. page = 1;
  99. dataSearch("keyword");
  100. }
  101. });
  102. // 點擊 tag 進行搜尋
  103. $(document).on('click', '.tag-item', function (event) {
  104. event.preventDefault(); // 阻止默認行為
  105. event.stopPropagation(); // 阻止冒泡
  106. const tag = $(this).data('tag');
  107. $(".keywords").val(tag);
  108. dataSearch();
  109. });
  110. let maxPagesMobile = 5; // 手機板最多顯示 5 頁
  111. let maxPagesDesktop = 10; // 電腦版最多顯示 10 頁
  112. // 分頁
  113. function setPagination(pages) {
  114. let screenWidth = $(window).width();
  115. let maxPages = screenWidth > 991 ? maxPagesDesktop : maxPagesMobile;
  116. function renderPagination(currentPage) {
  117. let dom = `
  118. <li class="page-item" onclick="handlePagination(this, 'previous')">
  119. <a class="page-link previous hidden" href="#" aria-label="Previous">
  120. <span aria-hidden="true"><</span>
  121. </a>
  122. </li>`;
  123. let startPage = Math.max(currentPage - Math.floor(maxPages / 2), 1);
  124. let endPage = Math.min(startPage + maxPages - 1, pages);
  125. if (endPage - startPage < maxPages) {
  126. startPage = Math.max(endPage - maxPages + 1, 1);
  127. }
  128. for (let index = startPage; index <= endPage; index++) {
  129. dom += `
  130. <li class="page-item ${index === currentPage ? 'active' : ''}" onclick="handlePagination(this)">
  131. <a class="page-link" href="#">${index}</a>
  132. </li>`;
  133. }
  134. dom += `
  135. <li class="page-item" onclick="handlePagination(this, 'next')">
  136. <a class="page-link next" href="#" aria-label="Next">
  137. <span aria-hidden="true">></span>
  138. </a>
  139. </li>`;
  140. $('.filter-list .pagination').html(dom);
  141. updateVisibility(currentPage, pages);
  142. }
  143. // 更新上頁 & 下頁的按鈕顯示狀態
  144. function updateVisibility(page, pages) {
  145. if (page === 1) {
  146. $('.page-link.previous').addClass('hidden');
  147. } else {
  148. $('.page-link.previous').removeClass('hidden');
  149. }
  150. if (page === pages) {
  151. $('.page-link.next').addClass('hidden');
  152. } else {
  153. $('.page-link.next').removeClass('hidden');
  154. }
  155. }
  156. window.handlePagination = function (item, type = "") {
  157. if (type === "previous" && page > 1) {
  158. page--;
  159. } else if (type === "next" && page < pages) {
  160. page++;
  161. } else if (!type) {
  162. page = parseInt($(item).find('.page-link').text());
  163. }
  164. renderPagination(page);
  165. dataSearch();
  166. }
  167. renderPagination(page);
  168. $(window).on('resize', function () {
  169. let newScreenWidth = $(window).width();
  170. if ((newScreenWidth > 991 && maxPages !== maxPagesDesktop) ||
  171. (newScreenWidth <= 991 && maxPages !== maxPagesMobile)) {
  172. maxPages = newScreenWidth > 991 ? maxPagesDesktop : maxPagesMobile;
  173. renderPagination(page);
  174. }
  175. });
  176. }
  177. // 頁碼處理
  178. function handlePagination(item, type = "") {
  179. if (type === "previous") {
  180. // 往前一頁
  181. if (page > 1) {
  182. page--;
  183. }
  184. console.log(page);
  185. } else if (type === "next") {
  186. // 往後一頁
  187. page++;
  188. console.log(page);
  189. } else {
  190. // 直接點擊頁碼
  191. page = parseInt($(item).find('.page-link')[0].innerText);
  192. }
  193. // 切換選取狀態
  194. $('.filter-list .page-item').removeClass('active');
  195. $('.filter-list .page-item').eq(page).addClass('active');
  196. // 設定上一頁按鈕的顯示狀態
  197. if (page === 1) {
  198. $('.page-link.previous').addClass('hidden');
  199. } else {
  200. $('.page-link.previous').removeClass('hidden');
  201. }
  202. dataSearch(); // 搜尋
  203. }
  204. // 熱搜關鍵字搜尋
  205. $(".search-bar-keyword a").click(function () {
  206. let keyword = $(this).text();
  207. $('.keywords').val(keyword);
  208. page = 1;
  209. dataSearch();
  210. });
  211. // 全部清除
  212. $("#removeResultBtn").click(function () {
  213. console.log('全部清除');
  214. filterList.length = 0; // 清空篩選陣列
  215. $('.keywords').val(''); // 清空搜尋欄位
  216. $('#removeResultBtn').hide(); // 隱藏全部清除按鈕
  217. $('.search-tab-result').empty(); // 清空篩選條件 dom
  218. // 取消選取狀態
  219. $('.filter-list input[type="radio"]').prop('checked', false);
  220. $('.search-tab').removeClass('active');
  221. dataSearch();
  222. });
  223. // 輸入框自動補全
  224. let designerKeywordList = [];
  225. async function getKeyword() {
  226. console.log('getKeyword');
  227. let url = '../../json/designer_keyword.json';
  228. try {
  229. const response = await axios.get(url);
  230. designerKeywordList = response.data.designer_keyword_list;
  231. } catch (error) {
  232. console.log("error", error);
  233. }
  234. }
  235. getKeyword();
  236. $('#keywordInput').on('input', function () {
  237. let input = $(this).val();
  238. closeAllLists();
  239. if (!input) {
  240. return false;
  241. }
  242. let divList = $('#autocomplete-list');
  243. for (let i = 0; i < designerKeywordList.length; i++) {
  244. if (designerKeywordList[i].substr(0, input.length).toUpperCase() === input.toUpperCase()) {
  245. let item = $('<div></div>');
  246. item.html("<strong>" + designerKeywordList[i].substr(0, input.length) + "</strong>" + designerKeywordList[i].substr(input.length));
  247. item.on('click', function () {
  248. $('#keywordInput').val($(this).text());
  249. closeAllLists();
  250. });
  251. item.on('mouseenter', function () {
  252. $('#keywordInput').val($(this).text());
  253. });
  254. divList.append(item);
  255. }
  256. }
  257. });
  258. // 清空自動補全列表
  259. function closeAllLists() {
  260. $('.autocomplete-items').empty();
  261. }
  262. $(document).on('click', function (e) {
  263. closeAllLists();
  264. });
  265. // 取得目前網址參數
  266. const queryString = window.location.search;
  267. const urlParams = new URLSearchParams(queryString);
  268. // 判斷網址是否有 keyword
  269. if (urlParams.has('keyword')) {
  270. const keywordValue = urlParams.get('keyword');
  271. console.log('網址 keyword', keywordValue);
  272. $('#keywordInput').val(keywordValue);
  273. setTimeout(() => {
  274. page = 1;
  275. isFirstLoad = false;
  276. dataSearch();
  277. }, 0)
  278. }
  279. // 發送 GA
  280. function push_GA(text, value) {
  281. if (text === "接案預算") {
  282. push_GA_tbudge_select(value);
  283. } else if (text === "接案風格" || text === "設計風格") {
  284. push_GA_style_select(value);
  285. } else if (text === "接案類型" || text === "房屋類型") {
  286. push_GA_dtype_select(value);
  287. } else if (text === "所在縣市") {
  288. push_GA_city_select(value);
  289. } else if (text === "房屋坪數") {
  290. push_GA_area_select(value);
  291. } else if (text === "房屋狀況") {
  292. push_GA_condition_select(value);
  293. }
  294. }
  295. // 房屋狀況 GA
  296. function push_GA_condition_select(key) {
  297. console.log('push_GA_condition_select', key);
  298. let eventLabel = '';
  299. switch (key) {
  300. case '新成屋':
  301. eventLabel = 'new_house';
  302. break;
  303. case '毛胚屋':
  304. eventLabel = 'bare_shell';
  305. break;
  306. case '老屋翻新':
  307. eventLabel = 'renovation_condition';
  308. break;
  309. case '自地自建':
  310. eventLabel = 'self-build';
  311. break;
  312. case '中古屋':
  313. eventLabel = 'second_hand';
  314. break;
  315. default:
  316. eventLabel = 'unknown';
  317. }
  318. dataLayer.push({ 'event': eventLabel });
  319. // 發送 Facebook Pixel 事件
  320. fbq('trackCustom', eventLabel);
  321. }
  322. // 房屋坪數 GA
  323. function push_GA_area_select(key) {
  324. console.log('push_GA_area_select', key);
  325. let eventLabel = '';
  326. switch (key) {
  327. case '20坪以下':
  328. eventLabel = '20_below';
  329. break;
  330. case '21~30坪':
  331. eventLabel = '21_30';
  332. break;
  333. case '31~50坪':
  334. eventLabel = '31_50';
  335. break;
  336. case '51坪以上':
  337. eventLabel = '50_up';
  338. break;
  339. default:
  340. eventLabel = 'unknown';
  341. }
  342. dataLayer.push({ 'event': eventLabel });
  343. // 發送 Facebook Pixel 事件
  344. fbq('trackCustom', eventLabel);
  345. }
  346. // 接案預算 GA
  347. function push_GA_tbudge_select(key) {
  348. console.log('push_GA_tbudge_select', key);
  349. let eventLabel = '';
  350. switch (key) {
  351. case '100萬以下':
  352. eventLabel = '1M_below';
  353. break;
  354. case '100~200萬':
  355. eventLabel = '1M_2M';
  356. break;
  357. case '200~300萬':
  358. eventLabel = '2M_3M';
  359. break;
  360. case '300~400萬':
  361. case '400~500萬':
  362. case '500萬以上':
  363. eventLabel = '3M_up';
  364. break;
  365. default:
  366. eventLabel = 'unknown';
  367. }
  368. dataLayer.push({ 'event': eventLabel });
  369. // 發送 Facebook Pixel 事件
  370. fbq('trackCustom', eventLabel);
  371. }
  372. // 設計風格/接案風格 GA
  373. function push_GA_style_select(key) {
  374. console.log('push_GA_style_select', key);
  375. let eventLabel = '';
  376. switch (key) {
  377. case '北歐風':
  378. eventLabel = 'Scandi_style';
  379. break;
  380. case '現代風':
  381. eventLabel = 'Modern_style';
  382. break;
  383. case '工業風':
  384. eventLabel = 'Industrial_style';
  385. break;
  386. case '美式風':
  387. eventLabel = 'American_style';
  388. break;
  389. case '休閒多元':
  390. eventLabel = 'Leisure_style';
  391. break;
  392. case '前衛風':
  393. eventLabel = 'Avant_garde_style';
  394. break;
  395. case '鄉村風':
  396. eventLabel = 'Rustic_style';
  397. break;
  398. case '混搭風':
  399. eventLabel = 'Eclectic_style';
  400. break;
  401. case '新古典':
  402. eventLabel = 'Neoclassic_style';
  403. break;
  404. case '奢華風':
  405. eventLabel = 'Luxurious_style';
  406. break;
  407. case '古典風':
  408. eventLabel = 'Classical_style';
  409. break;
  410. case '異國風':
  411. eventLabel = 'Exotic_style';
  412. break;
  413. case '日式蟬風':
  414. eventLabel = 'Zen_style';
  415. break;
  416. case '東方風':
  417. eventLabel = 'Oriental_style';
  418. break;
  419. case '無印風':
  420. eventLabel = 'Muju_style';
  421. break;
  422. case '簡約風':
  423. eventLabel = 'Minimalism_style';
  424. break;
  425. case '日式風':
  426. eventLabel = 'JP_style';
  427. break;
  428. case '輕奢風':
  429. eventLabel = 'Affordable_Luxury_style';
  430. break;
  431. default:
  432. eventLabel = 'unknown';
  433. }
  434. dataLayer.push({ 'event': eventLabel });
  435. // 發送 Facebook Pixel 事件
  436. fbq('trackCustom', eventLabel);
  437. }
  438. // 接案類型/房屋類型 GA
  439. function push_GA_dtype_select(key) {
  440. console.log('push_GA_dtype_select', key);
  441. let eventLabel = '';
  442. switch (key) {
  443. case '小坪數':
  444. eventLabel = 'small';
  445. break;
  446. case '標準格局':
  447. eventLabel = 'standard';
  448. break;
  449. case '大坪數':
  450. eventLabel = 'big';
  451. break;
  452. case '別墅':
  453. eventLabel = 'villa';
  454. break;
  455. case '挑高樓層':
  456. eventLabel = 'raisehigh';
  457. break;
  458. case '樓中樓':
  459. eventLabel = 'duplex_house';
  460. break;
  461. case '實品/樣品屋':
  462. eventLabel = 'sample';
  463. break;
  464. case '工作空間':
  465. case '商業空間':
  466. case '公共空間':
  467. eventLabel = 'commercial';
  468. break;
  469. case '民宿旅館':
  470. eventLabel = 'B&B';
  471. break;
  472. case '餐飲空間':
  473. eventLabel = 'dining';
  474. break;
  475. case '老屋翻新':
  476. eventLabel = 'renovation_condition';
  477. break;
  478. default:
  479. eventLabel = 'unknown';
  480. }
  481. dataLayer.push({ 'event': eventLabel });
  482. // 發送 Facebook Pixel 事件
  483. fbq('trackCustom', eventLabel);
  484. }
  485. // 所在縣市 GA
  486. function push_GA_city_select(key) {
  487. console.log('push_GA_city_select', key);
  488. let eventLabel = '';
  489. switch (key) {
  490. case '台北市':
  491. eventLabel = 'Taipei';
  492. break;
  493. case '新北市':
  494. eventLabel = 'New Taipei';
  495. break;
  496. case '基隆市':
  497. eventLabel = 'Keelung';
  498. break;
  499. case '桃園市':
  500. eventLabel = 'Taoyuan';
  501. break;
  502. case '新竹縣':
  503. eventLabel = 'Hsinchu';
  504. break;
  505. case '新竹市':
  506. eventLabel = 'Hsinchu';
  507. break;
  508. case '苗栗縣':
  509. eventLabel = 'Miaoli';
  510. break;
  511. case '台中市':
  512. eventLabel = 'Taichung';
  513. break;
  514. case '南投縣':
  515. eventLabel = 'Nantou';
  516. break;
  517. case '彰化縣':
  518. eventLabel = 'Changhua';
  519. break;
  520. case '雲林縣':
  521. eventLabel = 'Yunlin';
  522. break;
  523. case '嘉義縣':
  524. eventLabel = 'Chiayi';
  525. break;
  526. case '嘉義市':
  527. eventLabel = 'Chiayi';
  528. break;
  529. case '台南市':
  530. eventLabel = 'Tainan';
  531. break;
  532. case '高雄市':
  533. eventLabel = 'Kaohsiung';
  534. break;
  535. case '屏東縣':
  536. eventLabel = 'Pingtung';
  537. break;
  538. case '宜蘭縣':
  539. eventLabel = 'Yilan';
  540. break;
  541. case '花蓮縣':
  542. eventLabel = 'Hualien';
  543. break;
  544. case '台東縣':
  545. eventLabel = 'Taitung';
  546. break;
  547. case '澎湖縣':
  548. eventLabel = 'Penghu';
  549. break;
  550. case '金門縣':
  551. eventLabel = 'Kinmen';
  552. break;
  553. case '連江縣':
  554. eventLabel = 'Lienchiang';
  555. break;
  556. default:
  557. eventLabel = 'unknown';
  558. }
  559. dataLayer.push({ 'event': eventLabel });
  560. // 發送 Facebook Pixel 事件
  561. fbq('trackCustom', eventLabel);
  562. }