Skip to content

Commit

Permalink
add library random picker
Browse files Browse the repository at this point in the history
  • Loading branch information
cdhigh committed Jun 20, 2024
1 parent 907ae89 commit 49cf0bd
Show file tree
Hide file tree
Showing 8 changed files with 282 additions and 205 deletions.
37 changes: 37 additions & 0 deletions application/static/library.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,17 @@ var g_rssByLang = {};
//由BuildSharedRssByCategory()根据搜索词动态更新此数组
var g_rssByCat = [];

//将一个数组打乱,返回原数组
function shuffleArray(array) {
for (let i = array.length - 1; i > 0; i--) {
const j = Math.floor(Math.random() * (i + 1));
let temp = array[i];
array[i] = array[j];
array[j] = temp;
}
return array;
}

//排序网友分享库的RSS,先按流行程度(订阅数)倒序,订阅数相同的按时间倒序
function SortSharedRssDataArray() {
if (!g_SharedRss) {
Expand Down Expand Up @@ -114,16 +125,39 @@ function GetRssListByTime(lang, txt) {
return byTextTime;
}

//返回特定关键词搜索的随机排序的列表
function GetRssShuffledListByText(lang, txt) {
rss = g_rssByLang[lang];
if (!rss) {
return [];
}

txt = txt ? txt.toUpperCase() : '';
var byText = [];
var title, url;
for (var i = 0; i < rss.length; i++) {
title = rss[i].t.toUpperCase();
url = rss[i].u.toUpperCase();
if ((title.indexOf(txt) > -1) || (url.indexOf(txt) > -1)) {
byText.push(rss[i])
}
}

return shuffleArray(byText);
}

//根据搜索词动态更新此数组 g_rssByCat[]
//lang: 用户选择的语言代码,两位字母
//txt: 搜索词,可以为空
function BuildSharedRssByCategory(lang, txt) {
var bySearchText = GetRssListByText(lang, txt);
var byTextTime = GetRssListByTime(lang, txt);
var byShuffled = GetRssShuffledListByText(lang, txt);

g_rssByCat = [];
g_rssByCat[i18n.catAll] = bySearchText;
g_rssByCat[i18n.catByTime] = byTextTime;
g_rssByCat[i18n.catRandom] = byShuffled;
for (var idx = 0; idx < bySearchText.length; idx++) {
var item = bySearchText[idx];
var category = item.c || i18n.uncategoried; //c:category
Expand Down Expand Up @@ -285,6 +319,9 @@ function GeneratePaginationButtons(category, currentPage, maxPage) {

//选择了某一个分类,根据分类填充内容列表
function SelectCategory(obj, category) {
if ((category == i18n.catRandom) && g_rssByCat[i18n.catRandom] && g_rssByCat[i18n.catRandom].length > 0) {
shuffleArray(g_rssByCat[i18n.catRandom]);
}
HightlightCategory($(obj).parent());
ToPage(category, 1);
};
Expand Down
5 changes: 4 additions & 1 deletion application/static/reader.css
Original file line number Diff line number Diff line change
Expand Up @@ -318,7 +318,7 @@ body::-webkit-scrollbar-thumb {
margin-right: auto;
margin-top: -200px;
background-color: white;
border: 1px solid #302e2e;;
border: 1px solid #302e2e;
border-radius: 20px;
box-shadow: 0px 0px 3px #aaa;
display: none;
Expand Down Expand Up @@ -407,6 +407,7 @@ body::-webkit-scrollbar-thumb {
min-height: 100px;
max-height: 350px; /* tr-result - tr-word */
overflow: hidden;
padding: 0px 10px 10px 10px;
}
.tr-text-container {
width: 100%;
Expand All @@ -415,6 +416,8 @@ body::-webkit-scrollbar-thumb {
overflow-y: auto;
}
.tr-text {
word-break: break-word;
box-sizing: border-box;
margin: 0px auto 10px auto;
padding: 0px 10px 10px 10px;
/*text-align: center;*/
Expand Down
1 change: 1 addition & 0 deletions application/templates/base.html
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@
confirmShareRecipe: "{{_('Are you confirming to share the recipe ({0})?')|safe}}",
catAll: "{{_('[All]')|safe}}",
catByTime: "{{_('[By Time]')|safe}}",
catRandom: "{{_('[Random]')|safe}}",
uncategoried: "{{_('[Uncategoried]')|safe}}",
noLinksFound: "{{_('There are no links found.')|safe}}",
invalidReport: "{{_('Invalid report')|safe}}",
Expand Down
Loading

0 comments on commit 49cf0bd

Please sign in to comment.