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