lists.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632
  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. $(".keywords").val(keywordValue);
  273. // page = 1;
  274. // isFirstLoad = false;
  275. // // dataSearch();
  276. }
  277. // 發送 GA
  278. function push_GA(text, value) {
  279. if (text === "接案預算") {
  280. push_GA_tbudge_select(value);
  281. } else if (text === "接案風格" || text === "設計風格") {
  282. push_GA_style_select(value);
  283. } else if (text === "接案類型" || text === "房屋類型") {
  284. push_GA_dtype_select(value);
  285. } else if (text === "所在縣市") {
  286. push_GA_city_select(value);
  287. } else if (text === "房屋坪數") {
  288. push_GA_area_select(value);
  289. } else if (text === "房屋狀況") {
  290. push_GA_condition_select(value);
  291. }
  292. }
  293. // 房屋狀況 GA
  294. function push_GA_condition_select(key) {
  295. console.log('push_GA_condition_select', key);
  296. let eventLabel = '';
  297. switch (key) {
  298. case '新成屋':
  299. eventLabel = 'new_house';
  300. break;
  301. case '毛胚屋':
  302. eventLabel = 'bare_shell';
  303. break;
  304. case '老屋翻新':
  305. eventLabel = 'renovation_condition';
  306. break;
  307. case '自地自建':
  308. eventLabel = 'self-build';
  309. break;
  310. case '中古屋':
  311. eventLabel = 'second_hand';
  312. break;
  313. default:
  314. eventLabel = 'unknown';
  315. }
  316. dataLayer.push({ 'event': eventLabel });
  317. // 發送 Facebook Pixel 事件
  318. fbq('trackCustom', eventLabel);
  319. }
  320. // 房屋坪數 GA
  321. function push_GA_area_select(key) {
  322. console.log('push_GA_area_select', key);
  323. let eventLabel = '';
  324. switch (key) {
  325. case '20坪以下':
  326. eventLabel = '20_below';
  327. break;
  328. case '21~30坪':
  329. eventLabel = '21_30';
  330. break;
  331. case '31~50坪':
  332. eventLabel = '31_50';
  333. break;
  334. case '51坪以上':
  335. eventLabel = '50_up';
  336. break;
  337. default:
  338. eventLabel = 'unknown';
  339. }
  340. dataLayer.push({ 'event': eventLabel });
  341. // 發送 Facebook Pixel 事件
  342. fbq('trackCustom', eventLabel);
  343. }
  344. // 接案預算 GA
  345. function push_GA_tbudge_select(key) {
  346. console.log('push_GA_tbudge_select', key);
  347. let eventLabel = '';
  348. switch (key) {
  349. case '100萬以下':
  350. eventLabel = '1M_below';
  351. break;
  352. case '100~200萬':
  353. eventLabel = '1M_2M';
  354. break;
  355. case '200~300萬':
  356. eventLabel = '2M_3M';
  357. break;
  358. case '300~400萬':
  359. case '400~500萬':
  360. case '500萬以上':
  361. eventLabel = '3M_up';
  362. break;
  363. default:
  364. eventLabel = 'unknown';
  365. }
  366. dataLayer.push({ 'event': eventLabel });
  367. // 發送 Facebook Pixel 事件
  368. fbq('trackCustom', eventLabel);
  369. }
  370. // 設計風格/接案風格 GA
  371. function push_GA_style_select(key) {
  372. console.log('push_GA_style_select', key);
  373. let eventLabel = '';
  374. switch (key) {
  375. case '北歐風':
  376. eventLabel = 'Scandi_style';
  377. break;
  378. case '現代風':
  379. eventLabel = 'Modern_style';
  380. break;
  381. case '工業風':
  382. eventLabel = 'Industrial_style';
  383. break;
  384. case '美式風':
  385. eventLabel = 'American_style';
  386. break;
  387. case '休閒多元':
  388. eventLabel = 'Leisure_style';
  389. break;
  390. case '前衛風':
  391. eventLabel = 'Avant_garde_style';
  392. break;
  393. case '鄉村風':
  394. eventLabel = 'Rustic_style';
  395. break;
  396. case '混搭風':
  397. eventLabel = 'Eclectic_style';
  398. break;
  399. case '新古典':
  400. eventLabel = 'Neoclassic_style';
  401. break;
  402. case '奢華風':
  403. eventLabel = 'Luxurious_style';
  404. break;
  405. case '古典風':
  406. eventLabel = 'Classical_style';
  407. break;
  408. case '異國風':
  409. eventLabel = 'Exotic_style';
  410. break;
  411. case '日式蟬風':
  412. eventLabel = 'Zen_style';
  413. break;
  414. case '東方風':
  415. eventLabel = 'Oriental_style';
  416. break;
  417. case '無印風':
  418. eventLabel = 'Muju_style';
  419. break;
  420. case '簡約風':
  421. eventLabel = 'Minimalism_style';
  422. break;
  423. case '日式風':
  424. eventLabel = 'JP_style';
  425. break;
  426. case '輕奢風':
  427. eventLabel = 'Affordable_Luxury_style';
  428. break;
  429. default:
  430. eventLabel = 'unknown';
  431. }
  432. dataLayer.push({ 'event': eventLabel });
  433. // 發送 Facebook Pixel 事件
  434. fbq('trackCustom', eventLabel);
  435. }
  436. // 接案類型/房屋類型 GA
  437. function push_GA_dtype_select(key) {
  438. console.log('push_GA_dtype_select', key);
  439. let eventLabel = '';
  440. switch (key) {
  441. case '小坪數':
  442. eventLabel = 'small';
  443. break;
  444. case '標準格局':
  445. eventLabel = 'standard';
  446. break;
  447. case '大坪數':
  448. eventLabel = 'big';
  449. break;
  450. case '別墅':
  451. eventLabel = 'villa';
  452. break;
  453. case '挑高樓層':
  454. eventLabel = 'raisehigh';
  455. break;
  456. case '樓中樓':
  457. eventLabel = 'duplex_house';
  458. break;
  459. case '實品/樣品屋':
  460. eventLabel = 'sample';
  461. break;
  462. case '工作空間':
  463. case '商業空間':
  464. case '公共空間':
  465. eventLabel = 'commercial';
  466. break;
  467. case '民宿旅館':
  468. eventLabel = 'B&B';
  469. break;
  470. case '餐飲空間':
  471. eventLabel = 'dining';
  472. break;
  473. case '老屋翻新':
  474. eventLabel = 'renovation_condition';
  475. break;
  476. default:
  477. eventLabel = 'unknown';
  478. }
  479. dataLayer.push({ 'event': eventLabel });
  480. // 發送 Facebook Pixel 事件
  481. fbq('trackCustom', eventLabel);
  482. }
  483. // 所在縣市 GA
  484. function push_GA_city_select(key) {
  485. console.log('push_GA_city_select', key);
  486. let eventLabel = '';
  487. switch (key) {
  488. case '台北市':
  489. eventLabel = 'Taipei';
  490. break;
  491. case '新北市':
  492. eventLabel = 'New Taipei';
  493. break;
  494. case '基隆市':
  495. eventLabel = 'Keelung';
  496. break;
  497. case '桃園市':
  498. eventLabel = 'Taoyuan';
  499. break;
  500. case '新竹縣':
  501. eventLabel = 'Hsinchu';
  502. break;
  503. case '新竹市':
  504. eventLabel = 'Hsinchu';
  505. break;
  506. case '苗栗縣':
  507. eventLabel = 'Miaoli';
  508. break;
  509. case '台中市':
  510. eventLabel = 'Taichung';
  511. break;
  512. case '南投縣':
  513. eventLabel = 'Nantou';
  514. break;
  515. case '彰化縣':
  516. eventLabel = 'Changhua';
  517. break;
  518. case '雲林縣':
  519. eventLabel = 'Yunlin';
  520. break;
  521. case '嘉義縣':
  522. eventLabel = 'Chiayi';
  523. break;
  524. case '嘉義市':
  525. eventLabel = 'Chiayi';
  526. break;
  527. case '台南市':
  528. eventLabel = 'Tainan';
  529. break;
  530. case '高雄市':
  531. eventLabel = 'Kaohsiung';
  532. break;
  533. case '屏東縣':
  534. eventLabel = 'Pingtung';
  535. break;
  536. case '宜蘭縣':
  537. eventLabel = 'Yilan';
  538. break;
  539. case '花蓮縣':
  540. eventLabel = 'Hualien';
  541. break;
  542. case '台東縣':
  543. eventLabel = 'Taitung';
  544. break;
  545. case '澎湖縣':
  546. eventLabel = 'Penghu';
  547. break;
  548. case '金門縣':
  549. eventLabel = 'Kinmen';
  550. break;
  551. case '連江縣':
  552. eventLabel = 'Lienchiang';
  553. break;
  554. default:
  555. eventLabel = 'unknown';
  556. }
  557. dataLayer.push({ 'event': eventLabel });
  558. // 發送 Facebook Pixel 事件
  559. fbq('trackCustom', eventLabel);
  560. }