/*
This file is part of LONCAPA-Daxe.
LONCAPA-Daxe is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
LONCAPA-Daxe is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Daxe. If not, see <http://www.gnu.org/licenses/>.
*/
part of loncapa_daxe;
/**
* Display for rankresponse, with a possible simple UI.
* Jaxe display type: 'rankresponse'.
*/
class RankResponse extends LCDBlock {
static List<String> simpleUIAttributes = ['max', 'randomize', 'id'];
RankResponse.fromRef(x.Element elementRef) : super.fromRef(elementRef) {
displaySimpleButton = true;
}
RankResponse.fromNode(x.Node node, DaxeNode parent) : super.fromNode(node, parent) {
displaySimpleButton = true;
if (simpleUIPossibleNoThrow()) {
bool node_simpleUI = node.getUserData('simpleUI');
if (node_simpleUI == null || node_simpleUI)
setupSimpleUI();
}
}
@override
bool simpleUIPossible() {
for (DaxeAttr att in attributes) {
if (!simpleUIAttributes.contains(att.localName))
throw new SimpleUIException('rankresponse: ' + LCDStrings.get('attribute_problem') + ' ' + att.name);
if (att.name == 'randomize' && att.value != null && att.value != 'yes')
throw new SimpleUIException('rankresponse: ' + LCDStrings.get('attribute_problem') + ' ' + att.name);
}
int nbHintgroup = 0;
for (DaxeNode dn=firstChild; dn!= null; dn=dn.nextSibling) {
if (dn is RankFoilgroup) {
if (!dn.simpleUIPossible())
return false;
} else if (dn is Hintgroup) {
if (!dn.simpleUIPossible())
return false;
nbHintgroup++;
} else if (dn.nodeType == DaxeNode.ELEMENT_NODE) {
String title = doc.cfg.elementTitle(dn.ref);
throw new SimpleUIException(LCDStrings.get('element_prevents') + ' ' + dn.nodeName +
(title != null ? " ($title)" : ''));
} else if (dn.nodeType == DaxeNode.TEXT_NODE) {
if (dn.nodeValue.trim() != '')
return false;
} else {
return false;
}
}
if (nbHintgroup > 1)
throw new SimpleUIException(LCDStrings.get('one_hintgroup_max'));
return true;
}
void setupRestrictions() {
for (int i=0; i<attRefs.length; i++) {
x.Element refAttr = attRefs[i];
if (!simpleUIAttributes.contains(doc.cfg.attributeName(refAttr))) {
attRefs.removeAt(i);
i--;
}
}
restrictedInserts = [];
}
@override
void setupSimpleUI() {
// NOTE: changes here do not need to be undoable, because a new node is created.
simpleUI = true;
setupRestrictions();
fixChildrenForSimpleUI();
}
@override
void newNodeCreationUI(ActionFunction okfct) {
setupSimpleUI();
okfct();
}
/**
* Returns the first foilgroup.
*/
RankFoilgroup getFoilgroup() {
for (DaxeNode dn in childNodes) {
if (dn is RankFoilgroup)
return dn;
}
return null;
}
/**
* Returns the first hintgroup.
*/
Hintgroup getHintgroup() {
for (DaxeNode dn in childNodes) {
if (dn is Hintgroup)
return dn;
}
return null;
}
RankFoilgroup newFoilgroup() {
List<x.Element> foilgroupRefs = doc.cfg.elementReferences('foilgroup');
x.Element foilgroupRef = doc.cfg.findSubElement(ref, foilgroupRefs);
RankFoilgroup foilgroup = new RankFoilgroup.fromRef(foilgroupRef);
foilgroup.addFirstFoil();
return foilgroup;
}
Hintgroup newHintgroup() {
List<x.Element> hintgroupRefs = doc.cfg.elementReferences('hintgroup');
x.Element hintgroupRef = doc.cfg.findSubElement(ref, hintgroupRefs);
Hintgroup hintgroup = new Hintgroup.fromRef(hintgroupRef);
hintgroup.state = 1;
return hintgroup;
}
/**
* Inserts a foilgroup and hintgroup if missing, fixes them otherwise.
*/
void fixChildrenForSimpleUI() {
RankFoilgroup foilgroup = getFoilgroup();
Hintgroup hintgroup = getHintgroup();
if (foilgroup != null)
foilgroup.setupSimpleUI();
else {
foilgroup = newFoilgroup();
insertBefore(foilgroup, hintgroup);
}
foilgroup.userCannotRemove = true;
if (hintgroup != null)
hintgroup.setupSimpleUI();
else {
hintgroup = newHintgroup();
appendChild(hintgroup);
}
hintgroup.userCannotRemove = true;
}
}
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>