/*
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 gnuplot, with a possible simple UI.
* Jaxe display type: 'gnuplot'.
*/
class Gnuplot extends LCDBlock {
static List<String> simpleUIAttributes = ['width', 'height'];
Gnuplot.fromRef(x.Element elementRef) : super.fromRef(elementRef) {
displaySimpleButton = true;
}
Gnuplot.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.name)) {
// check if it's a default value
bool isDefault = false;
for (x.Element attref in attRefs) {
if (att.localName == doc.cfg.attributeName(attref) &&
att.namespaceURI == doc.cfg.attributeNamespace(attref)) {
String defaultValue = doc.cfg.defaultAttributeValue(attref);
if (att.value == defaultValue)
isDefault = true;
break;
}
}
if (!isDefault)
throw new SimpleUIException('gnuplot: ' + LCDStrings.get('attribute_problem') + ' ' + att.name);
}
}
int nbTitle = 0;
int nbAxis = 0;
int nbCurve = 0;
int nbXLabel = 0;
int nbYLabel = 0;
for (DaxeNode dn=firstChild; dn!= null; dn=dn.nextSibling) {
if (dn is FakeAttributeElement && dn.nodeName == 'title') {
if (!dn.simpleUIPossible())
return false;
nbTitle++;
} else if (dn is PlotAxis) {
if (!dn.simpleUIPossible())
return false;
nbAxis++;
} else if (dn is PlotCurve) {
if (!dn.simpleUIPossible())
return false;
nbCurve++;
} else if (dn is FakeAttributeElement && dn.nodeName == 'xlabel') {
if (!dn.simpleUIPossible())
return false;
nbXLabel++;
} else if (dn is FakeAttributeElement && dn.nodeName == 'ylabel') {
if (!dn.simpleUIPossible())
return false;
nbYLabel++;
} else if (dn.nodeType != DaxeNode.TEXT_NODE) {
String title = doc.cfg.elementTitle(dn.ref);
throw new SimpleUIException(LCDStrings.get('element_prevents') + ' ' + dn.nodeName +
(title != null ? " ($title)" : ''));
} else if (dn.nodeValue.trim() != '') {
return false;
}
}
if (nbTitle > 1 || nbAxis > 1 || nbCurve > 1 || nbXLabel > 1 || nbYLabel > 1)
return false;
return true;
}
@override
h.Element html() {
if (!simpleUI)
return super.html();
h.DivElement div = new h.DivElement();
div.id = "$id";
div.classes.add('dn');
if (!valid)
div.classes.add('invalid');
div.classes.add('dnblock');
h.DivElement headerDiv = createHeaderDiv();
headerDiv.classes.add('without-content-afterwards');
div.append(headerDiv);
return(div);
}
@override
h.Element createAttributesHTML() {
h.Element attHTML = super.createAttributesHTML();
if (!simpleUI || attHTML == null)
return attHTML;
// add the fake attributes
if (state == 0) {
h.DivElement div = new h.DivElement();
h.TableElement table = attHTML;
div.append(table);
for (DaxeNode dn in childNodes) {
if (dn is FakeAttributeElement || dn is PlotAxis || dn is PlotCurve) {
h.Element he = dn.html(); // might be table, tr or div
if (he is h.TableRowElement) {
if (table == null) {
table = new h.TableElement();
table.classes.add('expand');
div.append(table);
}
table.append(he);
} else {
div.append(he);
table = null;
}
}
}
return div;
} else if (state == 1) {
h.DivElement attDiv = attHTML;
for (DaxeNode dn=firstChild; dn!= null; dn=dn.nextSibling) {
if (dn is FakeAttributeElement && dn.firstChild is DNText) {
appendFakeAttributeShort(attDiv, dn.nodeName, dn.firstChild.nodeValue);
} else if (dn is PlotAxis) {
for (DaxeAttr att in dn.attributes) {
if (att.name == 'xmin' || att.name == 'xmax' || att.name == 'ymin' || att.name == 'ymax')
appendFakeAttributeShort(attDiv, att.name, att.value);
}
} else if (dn is PlotCurve && dn.firstChild is FakeAttributeElement) {
FakeAttributeElement fct = dn.firstChild;
if (fct.firstChild is DNText)
appendFakeAttributeShort(attDiv, fct.nodeName, fct.firstChild.nodeValue);
}
}
return attDiv;
} else
return null;
}
void appendFakeAttributeShort(h.DivElement attDiv, String title, String value) {
attDiv.append(new h.Text(" "));
h.Element att_name = new h.SpanElement();
att_name.attributes['class'] = 'attribute_name';
att_name.text = title;
attDiv.append(att_name);
attDiv.append(new h.Text("="));
h.Element att_val = new h.SpanElement();
att_val.attributes['class'] = 'attribute_value';
att_val.text = value;
attDiv.append(att_val);
}
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() {
simpleUI = true;
removeDefaultAttributes();
setupRestrictions();
fixChildrenForSimpleUI();
state = 0;
}
@override
void newNodeCreationUI(ActionFunction okfct) {
setupSimpleUI();
okfct();
}
FakeAttributeElement newTitle() {
List<x.Element> titleRefs = doc.cfg.elementReferences('title');
x.Element titleRef = doc.cfg.findSubElement(ref, titleRefs);
FakeAttributeElement title = NodeFactory.create(titleRef);
title.state = 1;
return title;
}
PlotAxis newAxis() {
List<x.Element> axisRefs = doc.cfg.elementReferences('axis');
x.Element axisRef = doc.cfg.findSubElement(ref, axisRefs);
PlotAxis axis = NodeFactory.create(axisRef);
axis.state = 1;
return axis;
}
PlotCurve newCurve() {
List<x.Element> curveRefs = doc.cfg.elementReferences('curve');
x.Element curveRef = doc.cfg.findSubElement(ref, curveRefs);
PlotCurve curve = NodeFactory.create(curveRef);
curve.setupSimpleUI();
return curve;
}
FakeAttributeElement newXLabel() {
List<x.Element> xlabelRefs = doc.cfg.elementReferences('xlabel');
x.Element xlabelRef = doc.cfg.findSubElement(ref, xlabelRefs);
FakeAttributeElement xlabel = NodeFactory.create(xlabelRef);
xlabel.state = 1;
return xlabel;
}
FakeAttributeElement newYLabel() {
List<x.Element> ylabelRefs = doc.cfg.elementReferences('ylabel');
x.Element ylabelRef = doc.cfg.findSubElement(ref, ylabelRefs);
FakeAttributeElement ylabel = NodeFactory.create(ylabelRef);
ylabel.state = 1;
return ylabel;
}
/**
* This inserts simpleUI children if missing.
*/
void fixChildrenForSimpleUI() {
bool foundTitle = false;
bool foundAxis = false;
bool foundCurve = false;
bool foundXLabel = false;
bool foundYLabel = false;
for (DaxeNode dn in childNodes) {
if (dn is FakeAttributeElement && dn.nodeName == 'title')
foundTitle = true;
else if (dn is PlotAxis)
foundAxis = true;
else if (dn is PlotCurve)
foundCurve = true;
else if (dn is FakeAttributeElement && dn.nodeName == 'xlabel')
foundXLabel = true;
else if (dn is FakeAttributeElement && dn.nodeName == 'ylabel')
foundYLabel = true;
}
if (!foundTitle)
appendChild(newTitle());
if (!foundAxis)
appendChild(newAxis());
if (!foundCurve)
appendChild(newCurve());
if (!foundXLabel)
appendChild(newXLabel());
if (!foundYLabel)
appendChild(newYLabel());
}
void removeDefaultAttributes() {
List<DaxeAttr> list = new List.from(attributes);
for (DaxeAttr att in list) {
if (!simpleUIAttributes.contains(att.name)) {
for (x.Element attref in attRefs) {
if (att.localName == doc.cfg.attributeName(attref) &&
att.namespaceURI == doc.cfg.attributeNamespace(attref)) {
String defaultValue = doc.cfg.defaultAttributeValue(attref);
if (att.value == defaultValue)
removeAttribute(att.name);
break;
}
}
}
}
}
}
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>