Grouping subjects by year in menu

This commit is contained in:
MrFry 2019-10-04 20:29:55 +02:00
parent 27692fe095
commit b647007337

155
main.js
View file

@ -35,7 +35,7 @@ function info () { return GM_info }
var data // all data, which is in the resource txt
var addEventListener // add event listener function
const lastChangeLog = 'Néhány szerkezeti átalakítás, és bugfix. Ha valami elromlott akkor pls report, thanx'
const lastChangeLog = 'Félév szerinti csoportosítás menüben'
const serverAdress = 'https://qmining.frylabs.net/'
// forcing pages for testing. unless you test, do not set these to true!
@ -212,28 +212,50 @@ class Subject {
this.active = false
}
active () {
this.active = true
}
set index (i) {
setIndex (i) {
this.index = i
}
get index () {
return this.index
getIndex () {
return this.index || -1
}
get length () {
return this.Questions.length
}
markActive () {
this.active = true
}
getIfActive () {
return this.active
}
AddQuestion (q) {
assert(q)
this.Questions.push(q)
}
getSubjNameWithoutYear () {
let t = this.Name.split(' - ')
if (t[0].match(/^[0-9]{4}\/[0-9]{2}\/[0-9]{1}$/i)) {
return t[1] || ''
} else {
return ''
}
}
getYear () {
let t = this.Name.split(' - ')[0]
if (t.match(/^[0-9]{4}\/[0-9]{2}\/[0-9]{1}$/i)) {
return t
} else {
return ''
}
}
Search (q, img) {
assert(q)
@ -1086,9 +1108,9 @@ function NLoad (resource, cwith) {
for (let i = 0; i < d.Subjects.length; i++) {
let s = new Subject(d.Subjects[i].Name)
s.setIndex(i)
if (getVal('Is' + i + 'Active')) {
s.active()
s.index = i
s.markActive()
var j = 0
for (j = 0; j < d.Subjects[i].Questions.length; j++) {
var currQ = d.Subjects[i].Questions[j]
@ -1894,53 +1916,102 @@ function ShowMenuList () {
tbl.style.width = '98%'
// adding headers ---------------------------------------------------------------------------------------------------------------
var subjTable = document.createElement('table')
var subjTable = document.createElement('div')
subjTable.style.margin = fiveMargin
subjTable.style.textAlign = 'left'
subjTable.style.width = '98%'
var tr = subjTable.insertRow()
var header1 = tr.insertCell()
// var tr = subjTable.insertRow()
// var header1 = tr.insertCell()
var headerSubjInfoParagraph = CreateNodeWithText(header1, 'Tárgynév [darab kérdés]', 'center')
headerSubjInfoParagraph.style.margin = fiveMargin // fancy margin
// var headerSubjInfoParagraph = CreateNodeWithText(header1, 'Tárgynév [darab kérdés]', 'center')
// headerSubjInfoParagraph.style.margin = fiveMargin // fancy margin
var header2 = tr.insertCell()
var headerSubjInfoParagraph2 = CreateNodeWithText(header2, 'Aktív')
headerSubjInfoParagraph2.style.margin = fiveMargin // fancy margin
// var header2 = tr.insertCell()
// var headerSubjInfoParagraph2 = CreateNodeWithText(header2, 'Aktív')
// headerSubjInfoParagraph2.style.margin = fiveMargin // fancy margin
if (data && data.length > 0) {
// TODO: group here
// let sorted = data.Subjects.sort((x) => {
let grouped = data.Subjects.reduce((res, s) => {
let sName = s.getSubjNameWithoutYear()
if (sName) {
if (!res[sName]) {
res[sName] = []
}
res[sName].push(s)
} else {
res.others.push(s)
}
return res
}, {
others: []
})
// })
let sorted = data.Subjects
let collapsibles = []
sorted.forEach((subj, i) => {
var subjRow = subjTable.insertRow()
subjRow.style.border = '1px solid #131319'
Object.entries(grouped).forEach(([subjName, subjGroup]) => {
let b = CreateNodeWithText(subjTable, subjName, 'button')
b.style.backgroundColor = '#222d32'
b.style.color = '#ffffff'
b.style.cursor = 'pointer'
b.style.padding = '5px'
b.style.width = '100%'
b.style.border = 'none'
b.style.textAlign = 'left'
b.style.outline = 'none'
collapsibles.push(b)
var td = subjRow.insertCell()
var text = subj.Name
if (subj.length !== 0) { text += ' [ ' + subj.length + 'db ]' }
let content = document.createElement('div')
content.style.padding = '0 18px'
content.style.overflow = 'hidden'
content.style.backgroundColor = '#222d32'
content.style.borderColor = '#212127'
content.style.borderStyle = 'solid'
content.style.borderWidth = '5px'
let ifGroupActive = subjGroup.some((x) => {
return x.getIfActive()
})
content.style.display = ifGroupActive ? 'block' : 'none'
var textBox = CreateNodeWithText(td, text)
subjTable.appendChild(content)
textBox.style.margin = fiveMargin // fancy margin
subjGroup.forEach((subj) => {
let tbl = document.createElement('table')
content.appendChild(tbl)
td = subjRow.insertCell()
var checkbox = document.createElement('input') // new paragraph
checkbox.type = 'checkbox'
checkbox.style.background = 'white'
checkbox.style.margin = '5px 5px 5px 5px' // fancy margin
td.appendChild(checkbox) // adding text box to main td
var row = tbl.insertRow()
let td = row.insertCell()
let text = subj.getYear() || subj.Name
if (subj.length !== 0) { text += ' [ ' + subj.length + 'db ]' }
CreateNodeWithText(td, text)
checkbox.checked = subj.active
checkbox.setAttribute('id', 'HelperTextNode' + i)
checkbox.addEventListener('click', function () {
var checked = document.getElementById('HelperTextNode' + i).checked
data.ChangeActive(i, checked)
}) // adding click
td = row.insertCell()
let checkbox = document.createElement('input') // new paragraph
checkbox.type = 'checkbox'
checkbox.style.background = 'white'
checkbox.style.margin = '5px 5px 5px 5px' // fancy margin
td.appendChild(checkbox) // adding text box to main td
checkbox.checked = subj.active
let i = subj.getIndex()
checkbox.setAttribute('id', 'HelperTextNode' + i)
checkbox.addEventListener('click', function () {
var checked = document.getElementById('HelperTextNode' + i).checked
data.ChangeActive(i, checked)
}) // adding click
})
})
collapsibles.forEach((x) => {
x.addEventListener('click', function () {
this.classList.toggle('active')
var content = this.nextElementSibling
if (content.style.display === 'block') {
content.style.display = 'none'
} else {
content.style.display = 'block'
}
})
})
var scrollDiv = document.createElement('div')