moved is#active-s to array

This commit is contained in:
MrFry 2020-01-14 12:36:50 +01:00
parent 05508571e6
commit 546e38ccb0

View file

@ -31,6 +31,7 @@
// @grant GM_info
// @grant GM_getValue
// @grant GM_setValue
// @grant GM_deleteValue
// @grant GM_xmlhttpRequest
// @grant GM_openInTab
// @license GNU General Public License v3.0 or later
@ -49,6 +50,7 @@
const a = Main
function getVal (name) { return GM_getValue(name) }
function setVal (name, val) { return GM_setValue(name, val) }
function delVal (name) { return GM_deleteValue(name) }
function openInTab (address, options) { GM_openInTab(address, options) }
function xmlhttpRequest (opts) { GM_xmlhttpRequest(opts) }
function info () { return GM_info }
@ -57,8 +59,8 @@
var data // all data, which is in the resource txt
var addEventListener // add event listener function
const lastChangeLog = '' // TODO
// const serverAdress = 'https://qmining.frylabs.net/'
const serverAdress = 'http://localhost:8080/' // TODO
const serverAdress = 'https://qmining.frylabs.net/'
// const serverAdress = 'http://localhost:8080/'
// forcing pages for testing. unless you test, do not set these to true!
// only one of these should be true for testing
@ -349,8 +351,8 @@
return this.Questions.length
}
markActive () {
this.active = true
setActive (val) {
this.active = !!val
}
getIfActive () {
@ -416,10 +418,11 @@
}
class QuestionDB {
constructor (getVal, setVal) {
constructor (getVal, setVal, delVal) {
this.Subjects = []
this.getVal = getVal
this.setVal = setVal
this.delVal = delVal
}
get length () {
@ -427,21 +430,35 @@
}
get activeIndexes () {
var r = []
for (var i = 0; i < this.length; i++) {
if (this.getVal('Is' + i + 'Active')) {
r.push(i)
return this.Subjects.reduce((acc, item, i) => {
if (item.getIfActive()) {
acc.push(i)
}
}
return r
return acc
}, [])
}
GetIfActive (ind) {
return this.getVal('Is' + ind + 'Active')
return this.Subjects[ind].getIfActive()
}
ChangeActive (i, value) {
this.setVal('Is' + i + 'Active', !!value)
ChangeActive (subjName, value) {
this.Subjects.find((x) => {
return x.Name === subjName
}).setActive(value)
let actives = JSON.parse(getVal('actives'))
if (value) {
actives.push(subjName)
} else {
actives = actives.reduce((acc, item) => {
if (item !== subjName) {
acc.push(item)
}
return acc
}, [])
}
setVal('actives', JSON.stringify(actives))
}
AddQuestion (subj, q) {
@ -948,6 +965,7 @@
}
console.log('Moodle Test Script run time:')
console.timeEnd('main')
SetActivesAsJSON()
})
if (forceTestPage || forceResultPage || forceDefaultPage) {
@ -1005,11 +1023,24 @@
FreshStart()
Version15()
Version16()
}
// : Version action functions {{{
function SetActivesAsJSON () {
if (!getVal('actives')) {
let res = []
for (let i = 0; i < 100; i++) {
let a = getVal('Is' + i + 'Active')
if (a && data.Subjects[i]) {
res.push(data.Subjects[i].Name)
}
delVal('Is' + i + 'Active')
}
setVal('actives', JSON.stringify(res))
}
}
function FreshStart () {
var firstRun = getVal('firstRun') // if the current run is the frst
if (firstRun === undefined || firstRun === true) {
@ -1031,18 +1062,6 @@
}
}
function Version16 () {
var version16 = getVal('version16') // if the current run is the frst
if (version16 === undefined || version16 === true) {
var i = 0
while (getVal('Is' + i + 'Active') !== undefined) {
setVal('Is' + i + 'Active', false)
i++
}
setVal('version16', false)
}
}
// : }}}
function ReadNetDB (cwith) {
@ -1116,17 +1135,30 @@
isSimple: true
}, undefined, ShowHelp)
}
var r = new QuestionDB(getVal, setVal)
var r = new QuestionDB(getVal, setVal, delVal)
var rt = []
var allCount = -1
LoadMOTD(d)
LoadVersion(d)
let actives = []
try {
actives = JSON.parse(getVal('actives'))
if (!Array.isArray(actives)) {
throw new Error('not an array')
}
} catch (e) {
Log('Unable to parse active subjects!')
setVal('actives', '[]')
actives = []
}
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.markActive()
let isActive = actives.includes(d.Subjects[i].Name)
if (isActive) {
s.setActive(true)
var j = 0
for (j = 0; j < d.Subjects[i].Questions.length; j++) {
var currQ = d.Subjects[i].Questions[j]
@ -1144,11 +1176,6 @@
data = r
count = allCount + 1 // couse starting with -1 to show errors
let i = 0
while (i < data.length && !getVal('Is' + i + 'Active')) {
i++
}
} catch (e) {
Exception(e, 'script error at loading:')
count = -1 // returns -1 if error
@ -2032,7 +2059,7 @@
checkbox.setAttribute('id', 'HelperTextNode' + i)
checkbox.addEventListener('click', function () {
var checked = document.getElementById('HelperTextNode' + i).checked
data.ChangeActive(i, checked)
data.ChangeActive(subj.Name, checked)
}) // adding click
})
})