Spaces:
Running
Running
File size: 46,382 Bytes
3f0377e 91f3927 feabc9f 3f0377e 921a78a 3f0377e 921a78a 3f0377e 921a78a 3f0377e 921a78a 3f0377e feabc9f 921a78a 3f0377e feabc9f 3f0377e feabc9f 3f0377e feabc9f 3f0377e feabc9f 3f0377e feabc9f 3f0377e 921a78a feabc9f 3f0377e 921a78a 69fb140 921a78a 4190920 69fb140 4190920 feabc9f 3f0377e 91f3927 3f0377e 91f3927 3f0377e 91f3927 3f0377e 91f3927 3f0377e 91f3927 3f0377e 91f3927 3f0377e 91f3927 3f0377e 91f3927 3f0377e 91f3927 3f0377e 91f3927 feabc9f 91f3927 feabc9f 91f3927 feabc9f 91f3927 feabc9f 3f0377e feabc9f 91f3927 feabc9f 91f3927 feabc9f 3f0377e feabc9f 3f0377e 91f3927 3f0377e 921a78a 3f0377e 921a78a 91f3927 921a78a 3f0377e 91f3927 3f0377e 91f3927 3f0377e 91f3927 3f0377e 91f3927 3f0377e 91f3927 3f0377e 91f3927 3f0377e 91f3927 3f0377e 91f3927 3f0377e 921a78a 91f3927 921a78a 91f3927 4190920 921a78a 91f3927 921a78a 91f3927 921a78a 4190920 921a78a 91f3927 921a78a 91f3927 921a78a 91f3927 921a78a 91f3927 921a78a 91f3927 921a78a 4190920 921a78a 69fb140 91f3927 69fb140 91f3927 69fb140 91f3927 69fb140 91f3927 69fb140 91f3927 69fb140 91f3927 69fb140 921a78a 91f3927 921a78a 91f3927 4190920 921a78a 91f3927 921a78a 91f3927 921a78a 91f3927 921a78a 91f3927 921a78a 91f3927 921a78a 4190920 921a78a 91f3927 921a78a 91f3927 921a78a 91f3927 921a78a 91f3927 921a78a 4190920 921a78a 4190920 921a78a 91f3927 921a78a 91f3927 69f3746 921a78a 69f3746 921a78a 91f3927 921a78a 4190920 921a78a 4190920 921a78a 4190920 921a78a 91f3927 921a78a 91f3927 921a78a 4190920 921a78a 69fb140 91f3927 69fb140 91f3927 69fb140 91f3927 69fb140 91f3927 69fb140 91f3927 69fb140 91f3927 69fb140 91f3927 69fb140 921a78a 91f3927 921a78a 91f3927 4190920 921a78a 91f3927 921a78a 91f3927 921a78a 4190920 921a78a 91f3927 921a78a 4190920 921a78a 4190920 921a78a 4190920 feabc9f 91f3927 feabc9f 91f3927 feabc9f 91f3927 feabc9f 91f3927 feabc9f 91f3927 feabc9f 91f3927 feabc9f 91f3927 feabc9f 91f3927 feabc9f 91f3927 feabc9f 91f3927 feabc9f 91f3927 feabc9f 91f3927 feabc9f 91f3927 feabc9f 91f3927 feabc9f 91f3927 feabc9f 91f3927 feabc9f 91f3927 feabc9f 91f3927 feabc9f 91f3927 feabc9f 91f3927 feabc9f 91f3927 feabc9f 91f3927 feabc9f 91f3927 feabc9f 91f3927 feabc9f 91f3927 feabc9f 91f3927 feabc9f 91f3927 feabc9f 91f3927 feabc9f 91f3927 feabc9f 3f0377e |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 |
const positions = ['pos-top-right', 'pos-top-left', 'pos-bottom-right', 'pos-bottom-left'];
let usedPositions = [];
const MAX_CARDS = 4;
const LABELS = {
zh: {
temperature: '溫度', condition: '狀況', humidity: '濕度', wind_speed: '風速',
weather: '天氣', city: '城市', description: '描述',
feels_like: '體感', pressure: '氣壓', sunrise: '日出', sunset: '日落',
heart_rate: '心率', step_count: '步數', oxygen_level: '血氧', respiratory_rate: '呼吸',
sleep_analysis: '睡眠', record_time: '記錄時間', average: '平均值',
no_news: '無新聞', no_data: '無數據', unknown: '未知',
exchange_rate: '匯率', conversion: '轉換', time: '時間',
train_type: '車種', origin_station: '起站', dest_station: '迄站',
departure: '出發', arrival: '抵達', duration: '行駛時間',
distance: '距離', walking_time: '步行時間', station: '車站',
available_bikes: '可借車輛', available_spaces: '可還空位',
bike_type: '類型', service_status: '服務狀態', operating: '營運中', suspended: '暫停服務',
location: '位置', coordinates: '座標', origin: '起點', destination: '目的地',
estimated_time: '預估時間', view_in_maps: '在 Google Maps 中查看',
line: '路線', address: '地址', road: '道路', area: '區域'
},
en: {
temperature: 'Temperature', condition: 'Condition', humidity: 'Humidity', wind_speed: 'Wind Speed',
weather: 'Weather', city: 'City', description: 'Description',
feels_like: 'Feels Like', pressure: 'Pressure', sunrise: 'Sunrise', sunset: 'Sunset',
heart_rate: 'Heart Rate', step_count: 'Steps', oxygen_level: 'Oxygen', respiratory_rate: 'Respiratory',
sleep_analysis: 'Sleep', record_time: 'Record Time', average: 'Average',
no_news: 'No News', no_data: 'No Data', unknown: 'Unknown',
exchange_rate: 'Exchange Rate', conversion: 'Conversion', time: 'Time',
train_type: 'Train Type', origin_station: 'Origin', dest_station: 'Destination',
departure: 'Departure', arrival: 'Arrival', duration: 'Duration',
distance: 'Distance', walking_time: 'Walking Time', station: 'Station',
available_bikes: 'Available Bikes', available_spaces: 'Available Spaces',
bike_type: 'Type', service_status: 'Service Status', operating: 'Operating', suspended: 'Suspended',
location: 'Location', coordinates: 'Coordinates', origin: 'Origin', destination: 'Destination',
estimated_time: 'Estimated Time', view_in_maps: 'View in Google Maps',
line: 'Line', address: 'Address', road: 'Road', area: 'Area'
},
ko: {
temperature: '온도', condition: '상태', humidity: '습도', wind_speed: '풍속',
weather: '날씨', city: '도시', description: '설명',
feels_like: '체감', pressure: '기압', sunrise: '일출', sunset: '일몰',
heart_rate: '심박수', step_count: '걸음 수', oxygen_level: '혈중 산소', respiratory_rate: '호흡',
sleep_analysis: '수면', record_time: '기록 시간', average: '평균',
no_news: '뉴스 없음', no_data: '데이터 없음', unknown: '알 수 없음',
exchange_rate: '환율', conversion: '환전', time: '시간',
train_type: '열차 종류', origin_station: '출발역', dest_station: '도착역',
departure: '출발', arrival: '도착', duration: '소요 시간',
distance: '거리', walking_time: '도보 시간', station: '역',
available_bikes: '대여 가능', available_spaces: '반납 가능',
bike_type: '유형', service_status: '서비스 상태', operating: '운영 중', suspended: '일시 중단',
location: '위치', coordinates: '좌표', origin: '출발지', destination: '목적지',
estimated_time: '예상 시간', view_in_maps: 'Google Maps에서 보기',
line: '노선', address: '주소', road: '도로', area: '지역'
},
ja: {
temperature: '気温', condition: '状況', humidity: '湿度', wind_speed: '風速',
weather: '天気', city: '都市', description: '説明',
feels_like: '体感', pressure: '気圧', sunrise: '日の出', sunset: '日の入り',
heart_rate: '心拍数', step_count: '歩数', oxygen_level: '血中酸素', respiratory_rate: '呼吸',
sleep_analysis: '睡眠', record_time: '記録時刻', average: '平均',
no_news: 'ニュースなし', no_data: 'データなし', unknown: '不明',
exchange_rate: '為替レート', conversion: '換算', time: '時刻',
train_type: '列車種別', origin_station: '出発駅', dest_station: '到着駅',
departure: '出発', arrival: '到着', duration: '所要時間',
distance: '距離', walking_time: '徒歩時間', station: '駅',
available_bikes: '利用可能', available_spaces: '返却可能',
bike_type: 'タイプ', service_status: 'サービス状態', operating: '運行中', suspended: '一時停止',
location: '場所', coordinates: '座標', origin: '出発地', destination: '目的地',
estimated_time: '予想時間', view_in_maps: 'Google Mapsで見る',
line: '路線', address: '住所', road: '道路', area: 'エリア'
},
id: {
temperature: 'Suhu', condition: 'Kondisi', humidity: 'Kelembaban', wind_speed: 'Kecepatan Angin',
weather: 'Cuaca', city: 'Kota', description: 'Deskripsi',
feels_like: 'Terasa', pressure: 'Tekanan', sunrise: 'Matahari Terbit', sunset: 'Matahari Terbenam',
heart_rate: 'Detak Jantung', step_count: 'Langkah', oxygen_level: 'Oksigen', respiratory_rate: 'Pernapasan',
sleep_analysis: 'Tidur', record_time: 'Waktu Rekam', average: 'Rata-rata',
no_news: 'Tidak Ada Berita', no_data: 'Tidak Ada Data', unknown: 'Tidak Diketahui',
exchange_rate: 'Nilai Tukar', conversion: 'Konversi', time: 'Waktu',
train_type: 'Jenis Kereta', origin_station: 'Stasiun Asal', dest_station: 'Stasiun Tujuan',
departure: 'Keberangkatan', arrival: 'Kedatangan', duration: 'Durasi',
distance: 'Jarak', walking_time: 'Waktu Jalan', station: 'Stasiun',
available_bikes: 'Sepeda Tersedia', available_spaces: 'Tempat Tersedia',
bike_type: 'Tipe', service_status: 'Status Layanan', operating: 'Beroperasi', suspended: 'Ditangguhkan',
location: 'Lokasi', coordinates: 'Koordinat', origin: 'Asal', destination: 'Tujuan',
estimated_time: 'Waktu Estimasi', view_in_maps: 'Lihat di Google Maps',
line: 'Jalur', address: 'Alamat', road: 'Jalan', area: 'Area'
},
vi: {
temperature: 'Nhiệt độ', condition: 'Tình trạng', humidity: 'Độ ẩm', wind_speed: 'Tốc độ gió',
weather: 'Thời tiết', city: 'Thành phố', description: 'Mô tả',
feels_like: 'Cảm giác', pressure: 'Áp suất', sunrise: 'Mặt trời mọc', sunset: 'Mặt trời lặn',
heart_rate: 'Nhịp tim', step_count: 'Số bước', oxygen_level: 'Oxy', respiratory_rate: 'Hô hấp',
sleep_analysis: 'Giấc ngủ', record_time: 'Thời gian ghi', average: 'Trung bình',
no_news: 'Không có tin', no_data: 'Không có dữ liệu', unknown: 'Không rõ',
exchange_rate: 'Tỷ giá', conversion: 'Chuyển đổi', time: 'Thời gian',
train_type: 'Loại tàu', origin_station: 'Ga đi', dest_station: 'Ga đến',
departure: 'Khởi hành', arrival: 'Đến', duration: 'Thời gian di chuyển',
distance: 'Khoảng cách', walking_time: 'Thời gian đi bộ', station: 'Ga',
available_bikes: 'Xe có sẵn', available_spaces: 'Chỗ trống',
bike_type: 'Loại', service_status: 'Trạng thái dịch vụ', operating: 'Hoạt động', suspended: 'Tạm ngừng',
location: 'Vị trí', coordinates: 'Tọa độ', origin: 'Điểm đi', destination: 'Điểm đến',
estimated_time: 'Thời gian ước tính', view_in_maps: 'Xem trên Google Maps',
line: 'Tuyến', address: 'Địa chỉ', road: 'Đường', area: 'Khu vực'
}
};
let currentLanguage = 'zh';
let toolDrawer = null;
let toolDrawerToggle = null;
let toolDrawerContent = null;
let toolDrawerOverlay = null;
let toolDrawerClose = null;
let isDrawerOpen = false;
function initToolDrawer() {
toolDrawer = document.getElementById('toolDrawer');
toolDrawerToggle = document.getElementById('toolDrawerToggle');
toolDrawerContent = document.getElementById('toolDrawerContent');
toolDrawerOverlay = document.getElementById('toolDrawerOverlay');
toolDrawerClose = document.getElementById('toolDrawerClose');
if (!toolDrawer || !toolDrawerToggle) {
console.warn('⚠️ 工具抽屜元素未找到');
return;
}
toolDrawerToggle.addEventListener('click', toggleToolDrawer);
if (toolDrawerClose) {
toolDrawerClose.addEventListener('click', hideToolDrawer);
}
if (toolDrawerOverlay) {
toolDrawerOverlay.addEventListener('click', hideToolDrawer);
}
}
function showToolDrawerToggle() {
if (toolDrawerToggle) {
toolDrawerToggle.classList.add('visible');
}
}
function hideToolDrawerToggle() {
if (toolDrawerToggle) {
toolDrawerToggle.classList.remove('visible');
toolDrawerToggle.classList.remove('open');
}
}
function toggleToolDrawer() {
if (isDrawerOpen) {
hideToolDrawer();
} else {
showToolDrawer();
}
}
function showToolDrawer() {
if (toolDrawer) {
toolDrawer.classList.add('open');
toolDrawerToggle?.classList.add('open');
toolDrawerOverlay?.classList.add('visible');
isDrawerOpen = true;
}
}
function hideToolDrawer() {
if (toolDrawer) {
toolDrawer.classList.remove('open');
toolDrawerToggle?.classList.remove('open');
toolDrawerOverlay?.classList.remove('visible');
isDrawerOpen = false;
}
}
function hideToolCards() {
hideToolDrawer();
hideToolDrawerToggle();
if (toolDrawerContent) {
toolDrawerContent.innerHTML = '';
}
clearAllCards();
}
function getNextPosition() {
if (usedPositions.length >= MAX_CARDS) {
console.warn('⚠️ 卡片數量已達上限(4張),請先清除現有卡片');
return null;
}
for (const pos of positions) {
if (!usedPositions.includes(pos)) {
usedPositions.push(pos);
return pos;
}
}
return null;
}
function addToolCard(type) {
const position = getNextPosition();
if (!position) {
return;
}
const card = document.createElement('div');
card.className = `voice-tool-card ${position}`;
card.dataset.type = type;
if (type === 'weather') {
card.innerHTML = `
<div class="card-header">
<div class="card-icon">🌤️</div>
<h3>台北天氣</h3>
</div>
<div class="card-content">
<div class="data-row">
<span class="data-label">溫度</span>
<span class="data-value">23°C</span>
</div>
<div class="data-row">
<span class="data-label">狀況</span>
<span class="data-value">晴朗</span>
</div>
<div class="data-row">
<span class="data-label">濕度</span>
<span class="data-value">65%</span>
</div>
</div>
`;
} else if (type === 'news') {
card.innerHTML = `
<div class="card-header">
<div class="card-icon">📰</div>
<h3>今日科技新聞</h3>
</div>
<div class="card-content">
<div class="data-row">
<span style="font-size: 13px; line-height: 1.6;">
• OpenAI 發布新模型<br>
• 蘋果推出 Vision Pro 2<br>
• 台積電宣布 2nm 製程
</span>
</div>
</div>
`;
} else if (type === 'health') {
card.innerHTML = `
<div class="card-header">
<div class="card-icon">❤️</div>
<h3>健康數據</h3>
</div>
<div class="card-content">
<div class="data-row">
<span class="data-label">心率</span>
<span class="data-value">72 bpm</span>
</div>
<div class="data-row">
<span class="data-label">步數</span>
<span class="data-value">8,542</span>
</div>
<div class="data-row">
<span class="data-label">血氧</span>
<span class="data-value">98%</span>
</div>
</div>
`;
}
cardsContainer.appendChild(card);
}
function clearAllCards() {
const cards = cardsContainer.querySelectorAll('.voice-tool-card');
cards.forEach(card => {
card.classList.add('exiting');
setTimeout(() => card.remove(), 300);
});
usedPositions = [];
}
function initToolCardControls() {
document.getElementById('simulate-weather').addEventListener('click', () => {
clearAllCards();
setTimeout(() => addToolCard('weather'), 100);
});
document.getElementById('simulate-news').addEventListener('click', () => {
clearAllCards();
setTimeout(() => addToolCard('news'), 100);
});
document.getElementById('simulate-health').addEventListener('click', () => {
clearAllCards();
setTimeout(() => addToolCard('health'), 100);
});
document.getElementById('simulate-next-input').addEventListener('click', () => {
clearAllCards();
transcript.textContent = '請說話...';
transcript.className = 'voice-transcript provisional';
});
}
async function syncToolMetadata() {
try {
const response = await fetch('/api/mcp/tools', {
headers: {
'Authorization': `Bearer ${localStorage.getItem('jwt_token')}`
}
});
if (response.ok) {
const data = await response.json();
if (data.success && data.tools) {
toolsMetadata = {};
data.tools.forEach(tool => {
toolsMetadata[tool.name] = tool;
});
}
}
} catch (error) {
console.error('❌ 同步工具 metadata 失敗:', error);
}
}
function getIconForTool(toolName, category) {
const iconMap = {
'健康': '❤️',
'天氣': '🌤️',
'新聞': '📰',
'匯率': '💱',
'時間': '⏰',
'提醒': '⏰',
'日曆': '📅',
'音樂': '🎵',
'地圖': '🗺️',
'翻譯': '🌐',
'計算': '🔢',
'道路運輸': '🚌',
'軌道運輸': '🚇',
'地理定位': '📍',
'healthkit_query': '❤️',
'weather_query': '🌤️',
'news_query': '📰',
'exchange_rate': '💱',
'time_query': '⏰',
'reminder': '⏰',
'calendar': '📅',
'tdx_bus_arrival': '🚌',
'tdx_metro': '🚇',
'reverse_geocode': '📍',
'forward_geocode': '📍',
'directions': '🗺️'
};
if (iconMap[toolName]) {
return iconMap[toolName];
}
if (category && iconMap[category]) {
return iconMap[category];
}
return '🔧';
}
function displayToolCard(toolName, toolData) {
clearAllCards();
const toolMeta = toolsMetadata[toolName] || {};
const category = toolMeta.category || '未知';
const icon = getIconForTool(toolName, category);
const contentHTML = renderCardContent(toolName, toolData);
const card = document.createElement('div');
card.className = 'voice-tool-card';
card.dataset.type = toolName;
card.innerHTML = `
<div class="card-header">
<div class="card-icon">${icon}</div>
<h3>${category}</h3>
</div>
<div class="card-content" style="max-height: 300px; overflow-y: auto; overflow-x: hidden; padding-right: 8px;">${contentHTML}</div>
`;
if (toolDrawerContent) {
toolDrawerContent.innerHTML = '';
toolDrawerContent.appendChild(card.cloneNode(true));
showToolDrawerToggle();
}
const position = getNextPosition();
if (position && cardsContainer) {
card.classList.add(position);
cardsContainer.appendChild(card);
}
}
function renderCardContent(toolName, toolData) {
if (!toolData) {
console.warn('⚠️ toolData 為空');
return '<p class="data-row">無數據</p>';
}
const healthData = toolData.health_data || toolData.raw_data?.health_data;
if (healthData && Array.isArray(healthData)) {
return renderHealthMetrics(healthData);
}
const articlesData = toolData.articles || toolData.raw_data?.articles;
if (articlesData && Array.isArray(articlesData)) {
return renderNewsList(articlesData);
}
const weatherData = toolData.raw_data || toolData;
if (weatherData.main && weatherData.weather) {
return renderWeatherData(weatherData);
}
if (toolData.arrivals && Array.isArray(toolData.arrivals)) {
return renderBusArrivals(toolData.arrivals, toolData.route_name);
}
if (toolData.stops && Array.isArray(toolData.stops)) {
return renderNearbyStops(toolData.stops);
}
const exchangeData = toolData.raw_data || toolData;
if (exchangeData.rate !== undefined && exchangeData.from_currency !== undefined) {
return renderExchangeRate(exchangeData);
}
if (toolData.trains && Array.isArray(toolData.trains)) {
return renderTrainList(toolData.trains);
}
if (toolData.stations && Array.isArray(toolData.stations) &&
(toolName === 'tdx_youbike' || toolData.stations[0]?.available_bikes !== undefined)) {
return renderYouBikeStations(toolData.stations);
}
if (toolData.stations && Array.isArray(toolData.stations) && toolName === 'tdx_train') {
return renderTrainStations(toolData.stations);
}
if (toolData.display_name && toolData.lat && toolData.lon && toolName === 'reverse_geocode') {
return renderReverseGeocode(toolData);
}
if ((toolData.distance_m !== undefined || toolData.duration_s !== undefined) &&
(toolName === 'directions' || toolData.polyline !== undefined)) {
return renderDirections(toolData);
}
if (toolData.arrivals && Array.isArray(toolData.arrivals) && toolName === 'tdx_metro') {
return renderMetroArrivals(toolData.arrivals);
}
if (toolData.stations && Array.isArray(toolData.stations) && toolName === 'tdx_metro') {
return renderMetroStations(toolData.stations);
}
if (toolData.lat && toolData.lon && toolData.display_name && toolName === 'forward_geocode') {
return renderForwardGeocode(toolData);
}
if (toolData.raw_data && typeof toolData.raw_data === 'object') {
return renderKeyValuePairs(toolData.raw_data);
}
console.warn('⚠️ 未匹配任何模式,使用 JSON fallback');
return renderJSONFallback(toolData);
}
function renderWeatherData(data) {
const main = data.main || {};
const weather = data.weather?.[0] || {};
const wind = data.wind || {};
const sys = data.sys || {};
const labels = LABELS[currentLanguage] || LABELS.zh;
const formatTime = (timestamp) => {
if (!timestamp) return '--:--';
const date = new Date(timestamp * 1000);
return date.toLocaleTimeString('zh-TW', { hour: '2-digit', minute: '2-digit' });
};
return `
<div class="data-row">
<span class="data-label">🌡️ ${labels.temperature}</span>
<span class="data-value">${main.temp?.toFixed(1) || '--'}°C</span>
</div>
<div class="data-row">
<span class="data-label">🤔 ${labels.feels_like}</span>
<span class="data-value">${main.feels_like?.toFixed(1) || '--'}°C</span>
</div>
<div class="data-row">
<span class="data-label">☁️ ${labels.condition}</span>
<span class="data-value">${weather.description || '--'}</span>
</div>
<div class="data-row">
<span class="data-label">💧 ${labels.humidity}</span>
<span class="data-value">${main.humidity || '--'}%</span>
</div>
<div class="data-row">
<span class="data-label">🌪️ ${labels.wind_speed}</span>
<span class="data-value">${wind.speed?.toFixed(1) || '--'} m/s</span>
</div>
<div class="data-row">
<span class="data-label">📊 ${labels.pressure}</span>
<span class="data-value">${main.pressure || '--'} hPa</span>
</div>
<div class="data-row">
<span class="data-label">🌅 ${labels.sunrise}</span>
<span class="data-value">${formatTime(sys.sunrise)}</span>
</div>
<div class="data-row">
<span class="data-label">🌇 ${labels.sunset}</span>
<span class="data-value">${formatTime(sys.sunset)}</span>
</div>
`;
}
function renderHealthMetrics(healthData) {
const labels = LABELS[currentLanguage] || LABELS.zh;
if (!healthData || healthData.length === 0) {
return `<p class="data-row">${labels.no_data}</p>`;
}
const metricIcons = {
heart_rate: '❤️',
step_count: '�',
oxygen_level: '🫁',
respiratory_rate: '💨',
sleep_analysis: '😴'
};
const grouped = {};
healthData.forEach(item => {
const metric = item.metric || item.type;
if (!grouped[metric]) {
grouped[metric] = [];
}
grouped[metric].push(item);
});
let html = '<div class="health-metrics">';
Object.entries(grouped).forEach(([metric, items], index) => {
const icon = metricIcons[metric] || '📊';
const label = labels[metric] || metric;
const latestItem = items[0]; // 最新的數據
const value = latestItem.value;
const unit = latestItem.unit || '';
let timeStr = '';
if (latestItem.timestamp) {
try {
const date = new Date(latestItem.timestamp);
timeStr = date.toLocaleString('zh-TW', {
month: 'numeric',
day: 'numeric',
hour: '2-digit',
minute: '2-digit'
});
} catch (e) {
timeStr = '';
}
}
html += `
<div class="health-metric-item" style="border-bottom: 1px solid #eee; padding: 10px 0; ${index === Object.keys(grouped).length - 1 ? 'border-bottom: none;' : ''}">
<div class="data-row">
<span class="data-label">${icon} ${label}</span>
<span class="data-value" style="font-weight: bold;">${value} ${unit}</span>
</div>
${timeStr ? `
<div class="data-row" style="opacity: 0.7;">
<span class="data-label" style="font-size: 0.85em;">${labels.record_time}</span>
<span class="data-value" style="font-size: 0.85em;">${timeStr}</span>
</div>
` : ''}
${items.length > 1 ? `
<div class="data-row" style="opacity: 0.6;">
<span class="data-label" style="font-size: 0.8em;">${labels.average}</span>
<span class="data-value" style="font-size: 0.8em;">${(items.reduce((sum, i) => sum + i.value, 0) / items.length).toFixed(1)} ${unit}</span>
</div>
` : ''}
</div>
`;
});
html += '</div>';
return html;
}
function renderNewsList(articles) {
const labels = LABELS[currentLanguage] || LABELS.zh;
let html = '';
articles.slice(0, 3).forEach(article => {
html += `
<div class="data-row" style="flex-direction: column; align-items: flex-start; margin-bottom: 10px;">
<span class="data-label" style="font-weight: bold;">${article.title || labels.unknown}</span>
<span class="data-value" style="font-size: 0.85em; opacity: 0.8;">${article.source?.name || article.source || ''}</span>
</div>
`;
});
return html || `<p>${labels.no_news}</p>`;
}
function renderKeyValuePairs(data) {
const labels = LABELS[currentLanguage] || LABELS.zh;
const keyMap = {
city: labels.city,
temp: labels.temperature,
temperature: labels.temperature,
condition: labels.condition,
weather: labels.weather,
humidity: labels.humidity,
wind_speed: labels.wind_speed,
description: labels.description
};
let html = '';
for (const [key, value] of Object.entries(data)) {
if (typeof value === 'object') continue; // 跳過巢狀物件
const label = keyMap[key] || key;
let displayValue = value;
if (key.includes('temp') && typeof value === 'number') {
displayValue = `${value}°C`;
}
html += `
<div class="data-row">
<span class="data-label">${label}</span>
<span class="data-value">${displayValue}</span>
</div>
`;
}
return html || '<p>無數據</p>';
}
function renderExchangeRate(data) {
const labels = LABELS[currentLanguage] || LABELS.zh;
const currencySymbols = {
"USD": "$", "TWD": "NT$", "JPY": "¥", "EUR": "€",
"GBP": "£", "CNY": "¥", "KRW": "₩", "HKD": "HK$"
};
const fromCurrency = data.from_currency || "USD";
const toCurrency = data.to_currency || "TWD";
const fromSymbol = currencySymbols[fromCurrency] || fromCurrency;
const toSymbol = currencySymbols[toCurrency] || toCurrency;
let html = '';
if (data.rate !== undefined) {
html += `
<div class="data-row">
<span class="data-label">💰 ${labels.exchange_rate}</span>
<span class="data-value">1 ${fromCurrency} = ${data.rate.toFixed(4)} ${toCurrency}</span>
</div>
`;
}
if (data.amount && data.converted_amount !== undefined) {
html += `
<div class="data-row">
<span class="data-label">🔄 ${labels.conversion}</span>
<span class="data-value">${fromSymbol}${data.amount.toFixed(2)} = ${toSymbol}${data.converted_amount.toFixed(2)}</span>
</div>
`;
}
if (data.raw_data?.metadata?.timestamp) {
const time = new Date(data.raw_data.metadata.timestamp).toLocaleString('zh-TW');
html += `
<div class="data-row">
<span class="data-label">⏰ ${labels.time}</span>
<span class="data-value">${time}</span>
</div>
`;
}
return html || `<p>${labels.no_data}</p>`;
}
function renderTrainList(trains) {
const labels = LABELS[currentLanguage] || LABELS.zh;
if (!trains || trains.length === 0) {
return `<p class="data-row">${labels.no_data}</p>`;
}
let html = '<div class="train-list">';
trains.forEach((train, index) => {
const trainType = train.train_type || labels.unknown;
const trainNo = train.train_no || '---';
const departTime = train.departure_time ? train.departure_time.substring(0, 5) : '--:--';
const arriveTime = train.arrival_time ? train.arrival_time.substring(0, 5) : '--:--';
const durationText = train.duration_min ? `${train.duration_min}${currentLanguage === 'zh' ? '分鐘' : currentLanguage === 'en' ? ' min' : currentLanguage === 'ko' ? '분' : currentLanguage === 'ja' ? '分' : currentLanguage === 'id' ? ' menit' : ' phút'}` : labels.unknown;
const originStation = train.origin_station || labels.unknown;
const destStation = train.destination_station || labels.unknown;
html += `
<div class="train-item" style="border-bottom: 1px solid #eee; padding: 12px 0; ${index === trains.length - 1 ? 'border-bottom: none;' : ''}">
<div class="data-row" style="margin-bottom: 8px;">
<span class="data-label" style="font-weight: bold; color: #0066cc;">🚂 ${trainType} ${trainNo}</span>
</div>
<div class="data-row">
<span class="data-label">📍 ${labels.origin_station} → ${labels.dest_station}</span>
<span class="data-value">${originStation} → ${destStation}</span>
</div>
<div class="data-row">
<span class="data-label">⏰ ${labels.departure}</span>
<span class="data-value">${departTime}</span>
</div>
<div class="data-row">
<span class="data-label">⏱️ ${labels.arrival}</span>
<span class="data-value">${arriveTime}</span>
</div>
<div class="data-row">
<span class="data-label">🕐 ${labels.duration}</span>
<span class="data-value">${durationText}</span>
</div>
</div>
`;
});
html += '</div>';
return html;
}
function renderTrainStations(stations) {
const labels = LABELS[currentLanguage] || LABELS.zh;
if (!stations || stations.length === 0) {
return `<p class="data-row">${labels.no_data}</p>`;
}
let html = '<div class="station-list">';
stations.forEach((station, index) => {
const stationName = station.station_name || station.name || labels.unknown;
const distanceUnit = currentLanguage === 'zh' ? '公尺' : currentLanguage === 'en' ? 'm' : currentLanguage === 'ko' ? '미터' : currentLanguage === 'ja' ? 'メートル' : currentLanguage === 'id' ? 'm' : 'm';
const distance = station.distance_m ? `${Math.round(station.distance_m)}${distanceUnit}` : '';
const walkTimeText = station.walking_time_min ? `${currentLanguage === 'zh' ? '步行約' : ''}${station.walking_time_min}${currentLanguage === 'zh' ? '分鐘' : currentLanguage === 'en' ? ' min walk' : currentLanguage === 'ko' ? '분 도보' : currentLanguage === 'ja' ? '分 徒歩' : currentLanguage === 'id' ? ' menit jalan' : ' phút đi bộ'}` : '';
html += `
<div class="station-item" style="border-bottom: 1px solid #eee; padding: 12px 0; ${index === stations.length - 1 ? 'border-bottom: none;' : ''}">
<div class="data-row" style="margin-bottom: 4px;">
<span class="data-label" style="font-weight: bold; color: #0066cc;">🚉 ${stationName}</span>
</div>
${distance ? `
<div class="data-row">
<span class="data-label">📏 ${labels.distance}</span>
<span class="data-value">${distance}</span>
</div>
` : ''}
${walkTimeText ? `
<div class="data-row">
<span class="data-label">🚶 ${labels.walking_time}</span>
<span class="data-value">${walkTimeText}</span>
</div>
` : ''}
</div>
`;
});
html += '</div>';
return html;
}
function renderYouBikeStations(stations) {
const labels = LABELS[currentLanguage] || LABELS.zh;
if (!stations || stations.length === 0) {
return `<p class="data-row">${labels.no_data}</p>`;
}
let html = '<div class="youbike-list">';
stations.forEach((station, index) => {
const stationName = station.station_name || labels.unknown;
const availableBikes = station.available_bikes ?? 0;
const availableSpaces = station.available_spaces ?? 0;
const distance = station.distance_m || 0;
const walkingTime = station.walking_time_min || 0;
const bikeType = station.bike_type || 'YouBike';
const serviceStatus = station.service_status === 1 ? labels.operating : labels.suspended;
const walkText = currentLanguage === 'zh' ? `步行約 ${walkingTime} 分鐘` : currentLanguage === 'en' ? `${walkingTime} min walk` : currentLanguage === 'ko' ? `도보 ${walkingTime}분` : currentLanguage === 'ja' ? `徒歩${walkingTime}分` : currentLanguage === 'id' ? `${walkingTime} menit jalan` : `${walkingTime} phút đi bộ`;
const bikeUnit = currentLanguage === 'zh' ? '輛' : currentLanguage === 'en' ? '' : currentLanguage === 'ko' ? '대' : currentLanguage === 'ja' ? '台' : currentLanguage === 'id' ? '' : '';
const spaceUnit = currentLanguage === 'zh' ? '個' : currentLanguage === 'en' ? '' : currentLanguage === 'ko' ? '개' : currentLanguage === 'ja' ? '個' : currentLanguage === 'id' ? '' : '';
let bikeStatusColor = '#e74c3c';
let bikeStatusIcon = '🚫';
if (availableBikes > 3) {
bikeStatusColor = '#27ae60';
bikeStatusIcon = '✅';
} else if (availableBikes > 0) {
bikeStatusColor = '#f39c12';
bikeStatusIcon = '⚠️';
}
html += `
<div class="youbike-item" style="border-bottom: 1px solid #eee; padding: 12px 0; ${index === stations.length - 1 ? 'border-bottom: none;' : ''}">
<div class="data-row" style="margin-bottom: 8px;">
<span class="data-label" style="font-weight: bold; color: #e67e22;">🚲 ${stationName}</span>
</div>
<div class="data-row">
<span class="data-label">📍 ${labels.distance}</span>
<span class="data-value">${distance}m (${walkText})</span>
</div>
<div class="data-row">
<span class="data-label">🚴 ${labels.available_bikes}</span>
<span class="data-value" style="color: ${bikeStatusColor}; font-weight: bold;">${bikeStatusIcon} ${availableBikes} ${bikeUnit}</span>
</div>
<div class="data-row">
<span class="data-label">🅿️ ${labels.available_spaces}</span>
<span class="data-value">${availableSpaces} ${spaceUnit}</span>
</div>
<div class="data-row">
<span class="data-label">ℹ️ ${labels.bike_type}</span>
<span class="data-value">${bikeType} (${serviceStatus})</span>
</div>
</div>
`;
});
html += '</div>';
return html;
}
function renderBusArrivals(arrivals, routeName) {
const labels = LABELS[currentLanguage] || LABELS.zh;
if (!arrivals || arrivals.length === 0) {
return `<p>${labels.no_data}</p>`;
}
let html = '';
const stopGroups = {};
arrivals.forEach(arr => {
const stopName = arr.stop_name || labels.unknown;
if (!stopGroups[stopName]) {
stopGroups[stopName] = [];
}
stopGroups[stopName].push(arr);
});
Object.entries(stopGroups).slice(0, 3).forEach(([stopName, stopArrivals], index) => {
const firstArr = stopArrivals[0];
const distance = firstArr.distance_m ? `${Math.round(firstArr.distance_m)}m` : '';
html += `
<div class="data-row" style="flex-direction: column; align-items: flex-start; margin-bottom: 12px; padding-bottom: 8px; border-bottom: 1px solid rgba(0,0,0,0.05);">
<div style="display: flex; justify-content: space-between; width: 100%; margin-bottom: 4px;">
<span class="data-label" style="font-weight: 600;">🚏 ${stopName}</span>
${distance ? `<span class="data-value" style="font-size: 0.85em; opacity: 0.7;">${distance}</span>` : ''}
</div>
`;
stopArrivals.forEach(arr => {
const directionText = arr.direction === 0 ? (currentLanguage === 'zh' ? '往 ↑' : currentLanguage === 'en' ? 'To ↑' : currentLanguage === 'ko' ? '방향 ↑' : currentLanguage === 'ja' ? '行き ↑' : currentLanguage === 'id' ? 'Ke ↑' : 'Đến ↑') : (currentLanguage === 'zh' ? '返 ↓' : currentLanguage === 'en' ? 'Return ↓' : currentLanguage === 'ko' ? '회차 ↓' : currentLanguage === 'ja' ? '戻り ↓' : currentLanguage === 'id' ? 'Kembali ↓' : 'Về ↓');
const status = arr.status || labels.unknown;
html += `
<div style="display: flex; justify-content: space-between; width: 100%; padding: 2px 0;">
<span style="font-size: 0.9em; opacity: 0.8;">${directionText}</span>
<span class="data-value" style="font-size: 0.9em;">${status}</span>
</div>
`;
});
html += `</div>`;
});
return html;
}
function renderReverseGeocode(data) {
const labels = LABELS[currentLanguage] || LABELS.zh;
const displayName = data.display_name || labels.unknown;
const city = data.city || '';
const road = data.road || '';
const houseNumber = data.house_number || '';
const suburb = data.suburb || '';
const admin = data.admin || '';
const countryCode = data.country_code || '';
const lat = data.lat?.toFixed(6) || '';
const lon = data.lon?.toFixed(6) || '';
let detailedAddress = [];
if (city) detailedAddress.push(city);
if (admin && admin !== city) detailedAddress.push(admin);
if (suburb) detailedAddress.push(suburb);
if (road) detailedAddress.push(road);
if (houseNumber) detailedAddress.push(houseNumber);
const addressText = detailedAddress.length > 0 ? detailedAddress.join(', ') : displayName;
const mapsUrl = `https://www.google.com/maps?q=${lat},${lon}`;
return `
<div class="data-row">
<span class="data-label">📍 ${labels.location}</span>
<span class="data-value" style="font-weight: bold;">${displayName}</span>
</div>
${city ? `
<div class="data-row">
<span class="data-label">🏙️ ${labels.city}</span>
<span class="data-value">${city}</span>
</div>
` : ''}
${road ? `
<div class="data-row">
<span class="data-label">🛣️ ${labels.road}</span>
<span class="data-value">${road}${houseNumber ? ' ' + houseNumber : ''}</span>
</div>
` : ''}
${suburb ? `
<div class="data-row">
<span class="data-label">🏘️ ${labels.area}</span>
<span class="data-value">${suburb}</span>
</div>
` : ''}
<div class="data-row">
<span class="data-label">🌐 ${labels.coordinates}</span>
<span class="data-value" style="font-size: 0.85em;">${lat}, ${lon}</span>
</div>
<div class="data-row" style="margin-top: 8px;">
<a href="${mapsUrl}" target="_blank" style="color: #0066cc; text-decoration: none; font-size: 0.9em;">
🗺️ ${labels.view_in_maps} →
</a>
</div>
`;
}
function renderNearbyStops(stops) {
const labels = LABELS[currentLanguage] || LABELS.zh;
if (!stops || stops.length === 0) {
return `<p>${labels.no_data}</p>`;
}
let html = '';
stops.slice(0, 5).forEach((stop, index) => {
const stopName = stop.stop_name || labels.unknown;
const distance = stop.distance_m ? `${Math.round(stop.distance_m)}m` : '';
const walkTimeText = stop.walking_time_min ? `${currentLanguage === 'zh' ? '步行 ' : ''}${stop.walking_time_min}${currentLanguage === 'zh' ? ' 分' : currentLanguage === 'en' ? ' min walk' : currentLanguage === 'ko' ? '분 도보' : currentLanguage === 'ja' ? '分 徒歩' : currentLanguage === 'id' ? ' menit jalan' : ' phút đi bộ'}` : '';
html += `
<div class="data-row" style="margin-bottom: 8px;">
<div style="flex: 1;">
<div style="font-weight: 600; margin-bottom: 2px;">🚏 ${stopName}</div>
<div style="font-size: 0.85em; opacity: 0.7;">${walkTimeText} ${distance ? `(${distance})` : ''}</div>
</div>
</div>
`;
});
return html;
}
function renderDirections(data) {
const labels = LABELS[currentLanguage] || LABELS.zh;
const originLabel = data.origin_label || labels.origin;
const destLabel = data.dest_label || labels.destination;
const distanceM = data.distance_m;
const durationS = data.duration_s;
let distanceStr = '--';
if (distanceM !== undefined) {
if (distanceM >= 1000) {
const kmUnit = currentLanguage === 'zh' ? '公里' : currentLanguage === 'en' ? ' km' : currentLanguage === 'ko' ? '킬로미터' : currentLanguage === 'ja' ? 'キロ' : currentLanguage === 'id' ? ' km' : ' km';
distanceStr = `${(distanceM / 1000).toFixed(1)}${kmUnit}`;
} else {
const mUnit = currentLanguage === 'zh' ? '公尺' : currentLanguage === 'en' ? ' m' : currentLanguage === 'ko' ? '미터' : currentLanguage === 'ja' ? 'メートル' : currentLanguage === 'id' ? ' m' : ' m';
distanceStr = `${Math.round(distanceM)}${mUnit}`;
}
}
let durationStr = '--';
if (durationS !== undefined) {
const minutes = Math.round(durationS / 60);
if (minutes >= 60) {
const hours = Math.floor(minutes / 60);
const mins = minutes % 60;
const hourUnit = currentLanguage === 'zh' ? '小時' : currentLanguage === 'en' ? ' hr' : currentLanguage === 'ko' ? '시간' : currentLanguage === 'ja' ? '時間' : currentLanguage === 'id' ? ' jam' : ' giờ';
const minUnit = currentLanguage === 'zh' ? '分鐘' : currentLanguage === 'en' ? ' min' : currentLanguage === 'ko' ? '분' : currentLanguage === 'ja' ? '分' : currentLanguage === 'id' ? ' menit' : ' phút';
durationStr = mins > 0 ? `${hours}${hourUnit} ${mins}${minUnit}` : `${hours}${hourUnit}`;
} else {
const minUnit = currentLanguage === 'zh' ? '分鐘' : currentLanguage === 'en' ? ' min' : currentLanguage === 'ko' ? '분' : currentLanguage === 'ja' ? '分' : currentLanguage === 'id' ? ' menit' : ' phút';
durationStr = `${minutes}${minUnit}`;
}
}
let mapsLink = '';
if (data.origin_lat && data.origin_lon && data.dest_lat && data.dest_lon) {
const mapsUrl = `https://www.google.com/maps/dir/${data.origin_lat},${data.origin_lon}/${data.dest_lat},${data.dest_lon}`;
mapsLink = `
<div class="data-row" style="margin-top: 8px;">
<a href="${mapsUrl}" target="_blank" style="color: #0066cc; text-decoration: none; font-size: 0.9em;">
🗺️ ${labels.view_in_maps} →
</a>
</div>
`;
}
return `
<div class="data-row">
<span class="data-label">📍 ${labels.origin}</span>
<span class="data-value">${originLabel}</span>
</div>
<div class="data-row">
<span class="data-label">🎯 ${labels.destination}</span>
<span class="data-value">${destLabel}</span>
</div>
<div class="data-row">
<span class="data-label">📏 ${labels.distance}</span>
<span class="data-value">${distanceStr}</span>
</div>
<div class="data-row">
<span class="data-label">⏱️ ${labels.estimated_time}</span>
<span class="data-value">${durationStr}</span>
</div>
${mapsLink}
`;
}
function renderMetroArrivals(arrivals) {
const labels = LABELS[currentLanguage] || LABELS.zh;
if (!arrivals || arrivals.length === 0) {
return `<p class="data-row">${labels.no_data}</p>`;
}
let html = '<div class="metro-arrivals">';
const lineGroups = {};
arrivals.forEach(arr => {
const lineName = arr.line_name || labels.unknown;
if (!lineGroups[lineName]) {
lineGroups[lineName] = [];
}
lineGroups[lineName].push(arr);
});
Object.entries(lineGroups).forEach(([lineName, lineArrivals], index) => {
html += `
<div class="metro-line" style="border-bottom: 1px solid #eee; padding: 12px 0; ${index === Object.keys(lineGroups).length - 1 ? 'border-bottom: none;' : ''}">
<div class="data-row" style="margin-bottom: 8px;">
<span class="data-label" style="font-weight: bold; color: #0066cc;">🚇 ${lineName}</span>
</div>
`;
lineArrivals.slice(0, 3).forEach(arr => {
const dest = arr.destination || labels.unknown;
const timeSec = arr.arrival_time_sec;
const status = arr.train_status || labels.unknown;
let timeStr = status;
if (timeSec > 0) {
const min = Math.floor(timeSec / 60);
const sec = timeSec % 60;
const minUnit = currentLanguage === 'zh' ? '分' : currentLanguage === 'en' ? ' min' : currentLanguage === 'ko' ? '분' : currentLanguage === 'ja' ? '分' : currentLanguage === 'id' ? ' menit' : ' phút';
const secUnit = currentLanguage === 'zh' ? '秒' : currentLanguage === 'en' ? ' sec' : currentLanguage === 'ko' ? '초' : currentLanguage === 'ja' ? '秒' : currentLanguage === 'id' ? ' detik' : ' giây';
timeStr = min > 0 ? `${min}${minUnit} ${sec}${secUnit}` : `${sec}${secUnit}`;
}
html += `
<div class="data-row">
<span class="data-label">→ ${dest}</span>
<span class="data-value">${timeStr}</span>
</div>
`;
});
html += '</div>';
});
html += '</div>';
return html;
}
function renderMetroStations(stations) {
const labels = LABELS[currentLanguage] || LABELS.zh;
if (!stations || stations.length === 0) {
return `<p class="data-row">${labels.no_data}</p>`;
}
let html = '<div class="metro-stations">';
stations.forEach((station, index) => {
const stationName = station.station_name || labels.unknown;
const distanceUnit = currentLanguage === 'zh' ? '公尺' : currentLanguage === 'en' ? 'm' : currentLanguage === 'ko' ? '미터' : currentLanguage === 'ja' ? 'メートル' : currentLanguage === 'id' ? 'm' : 'm';
const distance = station.distance_m ? `${Math.round(station.distance_m)} ${distanceUnit}` : '';
const walkTimeText = station.walking_time_min ? `${currentLanguage === 'zh' ? '步行約 ' : ''}${station.walking_time_min}${currentLanguage === 'zh' ? ' 分鐘' : currentLanguage === 'en' ? ' min walk' : currentLanguage === 'ko' ? '분 도보' : currentLanguage === 'ja' ? '分 徒歩' : currentLanguage === 'id' ? ' menit jalan' : ' phút đi bộ'}` : '';
const address = station.address || '';
html += `
<div class="metro-station-item" style="border-bottom: 1px solid #eee; padding: 12px 0; ${index === stations.length - 1 ? 'border-bottom: none;' : ''}">
<div class="data-row" style="margin-bottom: 4px;">
<span class="data-label" style="font-weight: bold; color: #0066cc;">🚇 ${stationName}</span>
</div>
${distance ? `
<div class="data-row">
<span class="data-label">📏 ${labels.distance}</span>
<span class="data-value">${distance}</span>
</div>
` : ''}
${walkTimeText ? `
<div class="data-row">
<span class="data-label">🚶 ${labels.walking_time}</span>
<span class="data-value">${walkTimeText}</span>
</div>
` : ''}
${address ? `
<div class="data-row">
<span class="data-label">📍 ${labels.address}</span>
<span class="data-value" style="font-size: 0.85em;">${address}</span>
</div>
` : ''}
</div>
`;
});
html += '</div>';
return html;
}
function renderForwardGeocode(data) {
const labels = LABELS[currentLanguage] || LABELS.zh;
const displayName = data.display_name || labels.unknown;
const lat = data.lat?.toFixed(6) || '';
const lon = data.lon?.toFixed(6) || '';
const city = data.city || '';
const road = data.road || '';
const suburb = data.suburb || '';
const mapsUrl = `https://www.google.com/maps?q=${lat},${lon}`;
return `
<div class="data-row">
<span class="data-label">📍 ${labels.location}</span>
<span class="data-value" style="font-weight: bold;">${displayName}</span>
</div>
${city ? `
<div class="data-row">
<span class="data-label">🏙️ ${labels.city}</span>
<span class="data-value">${city}</span>
</div>
` : ''}
${road ? `
<div class="data-row">
<span class="data-label">🛣️ ${labels.road}</span>
<span class="data-value">${road}</span>
</div>
` : ''}
${suburb ? `
<div class="data-row">
<span class="data-label">🏘️ ${labels.area}</span>
<span class="data-value">${suburb}</span>
</div>
` : ''}
<div class="data-row">
<span class="data-label">🌐 ${labels.coordinates}</span>
<span class="data-value" style="font-size: 0.85em;">${lat}, ${lon}</span>
</div>
<div class="data-row" style="margin-top: 8px;">
<a href="${mapsUrl}" target="_blank" style="color: #0066cc; text-decoration: none; font-size: 0.9em;">
🗺️ ${labels.view_in_maps} →
</a>
</div>
`;
}
function renderJSONFallback(data) {
return `<pre style="font-size: 0.85em; white-space: pre-wrap;">${JSON.stringify(data, null, 2)}</pre>`;
}
|