Skip to content

Commit

Permalink
feat: refine the searching part
Browse files Browse the repository at this point in the history
  • Loading branch information
babuzeer committed Mar 18, 2024
1 parent 4dac845 commit 2b80832
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 54 deletions.
109 changes: 59 additions & 50 deletions src/components/searchlan.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@
const pb = new PocketBase(PocketBase_URL);
let searchQuery = "";
// let isMainPage = true;
let errorMessage = "";
let searchchannels = [];
let searchnotices = [];
let searchtags = [];
let isFocused = false;
let isMainPage = false;
function jumpnew(id) {
originChannelID.set(id);
Expand All @@ -38,56 +38,80 @@
isFocused = false;
}
}
// function isid() {
// if ($currentchannelid != null) isMainPage = false;
// }
// function search(){
// alert(isMainPage);
// }
function isid() {
if (window.location.href === "http://localhost:5173/#/main") {
isMainPage = true;
} else {
isMainPage = false;
}
}
async function search() {
errorMessage = "";
try {
// 获取所有频道及通知记录
const allRecords = await pb.collection("channels").getFullList();
const allnotices = await pb.collection("notices").getFullList();
// 根据输入过滤并限制结果数量为最多4个
if (searchQuery.length > 0) {
const filteredRecords = allRecords.filter((record) =>
record.channelName.startsWith(searchQuery),
);
const limitedRecords = filteredRecords.slice(0, 4);
searchchannels = limitedRecords;
if (isMainPage) {
if (searchQuery.length > 0) {
const filteredRecords = allRecords.filter((record) =>
record.channelName.startsWith(searchQuery),
);
searchchannels = filteredRecords;
const filterednotices = allnotices.filter((record) =>
record.tittle.startsWith(searchQuery),
);
const limitednotices = filterednotices.slice(0, 4);
searchnotices = limitednotices;
const filterednotices = allnotices.filter((record) =>
record.tittle.startsWith(searchQuery),
);
searchnotices = filterednotices;
const filteredtags = allnotices.filter((record) =>
record.tag.startsWith(searchQuery),
);
const limitedtags = filteredtags.slice(0, 4);
searchtags = limitedtags;
if (
limitedRecords.length === 0 &&
limitednotices.length === 0 &&
limitedtags.length === 0
) {
errorMessage = "没有找到相关频道或通知。";
const filteredtags = allnotices.filter((record) =>
record.tag.startsWith(searchQuery),
);
searchtags = filteredtags;
if (
searchchannels.length === 0 &&
searchnotices.length === 0 &&
filteredtags.length === 0
) {
errorMessage = "没有找到相关频道或通知。";
}
} else {
searchchannels = [];
searchnotices = [];
searchtags = [];
}
} else {
searchchannels = [];
searchnotices = [];
//当前页面不是主页时
if (searchQuery.length > 0) {
const filterednotices = allnotices.filter(
(record) =>
record.tittle.startsWith(searchQuery) &&
record.channelid == $originChannelID,
);
searchnotices = filterednotices;
const filteredtags = allnotices.filter(
(record) =>
record.tag.startsWith(searchQuery) &&
record.channelid == $originChannelID,
);
searchtags = filteredtags;
if (searchnotices.length === 0 && searchtags.length === 0) {
errorMessage = "没有找到相关通知。";
}
} else {
searchtags = [];
searchnotices = [];
}
}
} catch (error) {
console.error("搜索频道时发生错误", error);
errorMessage = "搜索频道时发生错误" + error.message;
console.error("搜索通知时发生错误", error);
errorMessage = "搜索通知时发生错误" + error.message;
}
}
onMount(() => {
// isid();
isid();
document.addEventListener("click", handleClickOutside);
});
onDestroy(() => {
Expand All @@ -104,7 +128,6 @@
placeholder="search everything..."
on:focus={() => (isFocused = true)}
/>
<button on:click={() => search()}>search</button>
{#if errorMessage}
<div class="searchdrop">
<div
Expand Down Expand Up @@ -202,20 +225,6 @@
width: 400px;
border-color: #007bff;
}
button {
padding: 8px 15px;
font-size: 16px;
background-color: #c8c8c8;
color: white;
border: none;
border-radius: 16px;
cursor: pointer;
margin-left: 10px;
outline: none;
}
button:hover {
background-color: #0056b3;
}
.channame {
display: flex; /* 启用Flexbox布局 */
align-items: center; /* 垂直居中 */
Expand Down
3 changes: 3 additions & 0 deletions src/router.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ import Jumptag from "./routes/jumptag.svelte";
import InfoPage from "./routes/infoPage.svelte";
import Updatenotice from "./routes/updatenotice.svelte";
import Mychannels from "./routes/myChannels.svelte";
import Searchresult from "./routes/searchresult.svelte";

export default {
"/": DoorPage,
"/login": Login,
Expand All @@ -37,4 +39,5 @@ export default {
"/infoPage": InfoPage,
"/updatenotice": Updatenotice,
"/myChannels": Mychannels,
"/searchresult": Searchresult,
};
1 change: 1 addition & 0 deletions src/routes/checknotice.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@
<style>
.record {
width: auto;
height: 98%;
border: 1px solid #ccc;
padding: 15px;
margin: 10px 0;
Expand Down
4 changes: 0 additions & 4 deletions src/routes/mainpage.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,6 @@
let showtodo = true;
let selectedChannel = null;
function ismain() {
currentchannelid.set(null);
}
function editChannel(channelName) {
currentchannelName.set(channelName);
push("/updateChannel");
Expand Down Expand Up @@ -249,7 +246,6 @@
checkNotice();
fetchCreatedChannels();
checkTodolist();
ismain();
});
let src = "userPicture.jpeg";
Expand Down

0 comments on commit 2b80832

Please sign in to comment.