Skip to content

Commit

Permalink
Merge pull request #522 from manaba-enhanced-for-tsukuba/main
Browse files Browse the repository at this point in the history
v3.3.1
  • Loading branch information
mkobayashime authored Dec 1, 2022
2 parents 27b9c90 + d932adf commit e9662a4
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 36 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "manaba-enhanced-for-tsukuba",
"version": "3.3.0",
"version": "3.3.1",
"description": "Make your manaba a little bit more comfortable",
"private": true,
"devDependencies": {
Expand Down
83 changes: 48 additions & 35 deletions src/methods/filterCourses.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,6 @@ import { checkLang } from "./checkLang"

let lang: checkLang.langCode

const translations = {
ja: {
spring: "春",
autumn: "秋",
allModules: "すべてのモジュール",
},
en: {
spring: "Spring",
autumn: "Autumn",
allModules: "All modules",
},
}

export const filterCourses = (): void => {
lang = checkLang()

Expand Down Expand Up @@ -69,8 +56,28 @@ const parseModuleCode = (
* @param {string} seasonCode "spring" or "autumn"
* @return {string} "春", "Spring", etc...
*/
const seasonCodeToText = (lang: checkLang.langCode, seasonCode: SeasonCode) =>
translations[lang][seasonCode]
const seasonCodeToText = (seasonCode: SeasonCode) => {
switch (seasonCode) {
case "spring": {
if (lang === "ja") {
return "春"
} else if (lang === "en") {
return "Spring"
} else {
return ""
}
}
case "autumn": {
if (lang === "ja") {
return "秋"
} else if (lang === "en") {
return "Autumn"
} else {
return ""
}
}
}
}

const createModuleSelector = () => {
const selectorsContainer = document.querySelector(
Expand All @@ -91,10 +98,17 @@ const createModuleSelector = () => {
]

const moduleCodeToText = (moduleCode: ModuleCode) => {
if (moduleCode === "all") return translations[lang].allModules
if (moduleCode === "all") {
if (lang === "ja") {
return "すべてのモジュール"
} else if (lang === "en") {
return "All modules"
}
}

const parsedModuleCode = parseModuleCode(moduleCode)
const season = seasonCodeToText(lang, parsedModuleCode.season)

const season = seasonCodeToText(parsedModuleCode.season)

return `${season}${parsedModuleCode.module.toUpperCase()}`
}
Expand All @@ -121,18 +135,27 @@ const createModuleSelector = () => {
}

const applyFilter = (moduleCode: ModuleCode): void => {
const coursesListContainer =
document.querySelector<HTMLElement>(".courselist tbody")
const coursesThumbnailContainer = document.querySelector<HTMLElement>(
let viewMode: "list" | "thumbnail"
let courses: HTMLElement[]

const coursesListContainer = document.querySelector(".courselist tbody")
const coursesThumbnailContainer = document.querySelector(
".mycourses-body .section"
)
const coursesContainer = coursesListContainer ?? coursesThumbnailContainer

if (!coursesContainer || (coursesListContainer && coursesThumbnailContainer))
return
if (coursesListContainer) {
viewMode = "list"

courses = Array.from(coursesListContainer.children) as HTMLElement[]
courses.shift()
} else if (coursesThumbnailContainer) {
viewMode = "thumbnail"

const viewMode = coursesListContainer ? "list" : "thumbnail"
const courses = getCourses(coursesContainer, viewMode)
courses = Array.from(coursesThumbnailContainer.children) as HTMLElement[]
courses.pop()
} else {
throw "invalid viewMode"
}

/**
* Parse course info string on the UI
Expand Down Expand Up @@ -259,13 +282,3 @@ const applyFilter = (moduleCode: ModuleCode): void => {
})
}
}

const getCourses = (coursesContainer: HTMLElement, viewMode: string) =>
(Array.from(coursesContainer.children) as HTMLElement[]).filter(
(_, i) =>
i !==
rowIndexToBeExcluded(viewMode, coursesContainer.childElementCount - 1)
)

const rowIndexToBeExcluded = (viewMode: string, lastIndex: number) =>
viewMode === "list" ? 0 : lastIndex

0 comments on commit e9662a4

Please sign in to comment.