Annotation of modules/damieng/graphical_editor/loncapa_daxe/web/nodes/gnuplot.dart, revision 1.1
1.1 ! damieng 1: /*
! 2: This file is part of LONCAPA-Daxe.
! 3:
! 4: LONCAPA-Daxe is free software: you can redistribute it and/or modify
! 5: it under the terms of the GNU General Public License as published by
! 6: the Free Software Foundation, either version 3 of the License, or
! 7: (at your option) any later version.
! 8:
! 9: LONCAPA-Daxe is distributed in the hope that it will be useful,
! 10: but WITHOUT ANY WARRANTY; without even the implied warranty of
! 11: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
! 12: GNU General Public License for more details.
! 13:
! 14: You should have received a copy of the GNU General Public License
! 15: along with Daxe. If not, see <http://www.gnu.org/licenses/>.
! 16: */
! 17:
! 18: part of loncapa_daxe;
! 19:
! 20: /**
! 21: * Display for gnuplot, with a possible simple UI.
! 22: * Jaxe display type: 'gnuplot'.
! 23: */
! 24: class Gnuplot extends LCDBlock {
! 25:
! 26: static List<String> simpleUIAttributes = ['width', 'height'];
! 27: bool displaySimpleButton = true;
! 28:
! 29: Gnuplot.fromRef(x.Element elementRef) : super.fromRef(elementRef) {
! 30: }
! 31:
! 32: Gnuplot.fromNode(x.Node node, DaxeNode parent) : super.fromNode(node, parent) {
! 33: if (node.ownerDocument.documentElement != null) {
! 34: if (simpleUIPossibleNoThrow())
! 35: setupSimpleUI();
! 36: }
! 37: }
! 38:
! 39: @override
! 40: bool simpleUIPossible() {
! 41: for (DaxeAttr att in attributes) {
! 42: if (!simpleUIAttributes.contains(att.name)) {
! 43: throw new SimpleUIException('gnuplot: ' + LCDStrings.get('attribute_problem') + ' ' + att.name);
! 44: }
! 45: }
! 46: int nbTitle = 0;
! 47: int nbAxis = 0;
! 48: int nbCurve = 0;
! 49: int nbXLabel = 0;
! 50: int nbYLabel = 0;
! 51: for (DaxeNode dn=firstChild; dn!= null; dn=dn.nextSibling) {
! 52: if (dn is FakeAttributeElement && dn.nodeName == 'title') {
! 53: if (!dn.simpleUIPossible())
! 54: return false;
! 55: nbTitle++;
! 56: } else if (dn is PlotAxis) {
! 57: if (!dn.simpleUIPossible())
! 58: return false;
! 59: nbAxis++;
! 60: } else if (dn is PlotCurve) {
! 61: if (!dn.simpleUIPossible())
! 62: return false;
! 63: nbCurve++;
! 64: } else if (dn is FakeAttributeElement && dn.nodeName == 'xlabel') {
! 65: if (!dn.simpleUIPossible())
! 66: return false;
! 67: nbXLabel++;
! 68: } else if (dn is FakeAttributeElement && dn.nodeName == 'ylabel') {
! 69: if (!dn.simpleUIPossible())
! 70: return false;
! 71: nbYLabel++;
! 72: } else if (dn.nodeType != DaxeNode.TEXT_NODE) {
! 73: String title = doc.cfg.elementTitle(dn.ref);
! 74: throw new SimpleUIException(LCDStrings.get('element_prevents') + ' ' + dn.nodeName +
! 75: (title != null ? " ($title)" : ''));
! 76: } else if (dn.nodeValue.trim() != '') {
! 77: return false;
! 78: }
! 79: }
! 80: if (nbTitle > 1 || nbAxis > 1 || nbCurve > 1 || nbXLabel > 1 || nbYLabel > 1)
! 81: return false;
! 82: return true;
! 83: }
! 84:
! 85: @override
! 86: h.Element html() {
! 87: if (!simpleUI)
! 88: return super.html();
! 89: h.DivElement div = new h.DivElement();
! 90: div.id = "$id";
! 91: div.classes.add('dn');
! 92: if (!valid)
! 93: div.classes.add('invalid');
! 94: div.classes.add('lcdblock');
! 95:
! 96: h.DivElement headerDiv = createHeaderDiv();
! 97: headerDiv.classes.add('without-content-afterwards');
! 98: div.append(headerDiv);
! 99:
! 100: return(div);
! 101: }
! 102:
! 103: @override
! 104: h.Element createAttributesHTML() {
! 105: h.Element attHTML = super.createAttributesHTML();
! 106: if (!simpleUI || attHTML == null)
! 107: return attHTML;
! 108: // add the fake attributes
! 109: if (state == 0) {
! 110: h.DivElement div = new h.DivElement();
! 111: h.TableElement table = attHTML;
! 112: div.append(table);
! 113: for (DaxeNode dn in childNodes) {
! 114: if (dn is FakeAttributeElement || dn is PlotAxis || dn is PlotCurve) {
! 115: h.Element he = dn.html(); // might be table, tr or div
! 116: if (he is h.TableRowElement) {
! 117: if (table == null) {
! 118: table = new h.TableElement();
! 119: table.classes.add('expand');
! 120: div.append(table);
! 121: }
! 122: table.append(he);
! 123: } else {
! 124: div.append(he);
! 125: table = null;
! 126: }
! 127: }
! 128: }
! 129: return div;
! 130: } else if (state == 1) {
! 131: h.DivElement attDiv = attHTML;
! 132: for (DaxeNode dn=firstChild; dn!= null; dn=dn.nextSibling) {
! 133: if (dn is FakeAttributeElement && dn.firstChild is DNText) {
! 134: appendFakeAttributeShort(attDiv, dn.nodeName, dn.firstChild.nodeValue);
! 135: } else if (dn is PlotAxis) {
! 136: for (DaxeAttr att in dn.attributes) {
! 137: if (att.name == 'xmin' || att.name == 'xmax' || att.name == 'ymin' || att.name == 'ymax')
! 138: appendFakeAttributeShort(attDiv, att.name, att.value);
! 139: }
! 140: } else if (dn is PlotCurve && dn.firstChild is FakeAttributeElement) {
! 141: FakeAttributeElement fct = dn.firstChild;
! 142: if (fct.firstChild is DNText)
! 143: appendFakeAttributeShort(attDiv, fct.nodeName, fct.firstChild.nodeValue);
! 144: }
! 145: }
! 146: return attDiv;
! 147: }
! 148: }
! 149:
! 150: void appendFakeAttributeShort(h.DivElement attDiv, String title, String value) {
! 151: attDiv.append(new h.Text(" "));
! 152: h.Element att_name = new h.SpanElement();
! 153: att_name.attributes['class'] = 'attribute_name';
! 154: att_name.text = title;
! 155: attDiv.append(att_name);
! 156: attDiv.append(new h.Text("="));
! 157: h.Element att_val = new h.SpanElement();
! 158: att_val.attributes['class'] = 'attribute_value';
! 159: att_val.text = value;
! 160: attDiv.append(att_val);
! 161: }
! 162:
! 163: void setupRestrictions() {
! 164: for (int i=0; i<attRefs.length; i++) {
! 165: x.Element refAttr = attRefs[i];
! 166: if (!simpleUIAttributes.contains(doc.cfg.attributeName(refAttr))) {
! 167: attRefs.removeAt(i);
! 168: i--;
! 169: }
! 170: }
! 171: restrictedInserts = [];
! 172: }
! 173:
! 174: @override
! 175: void setupSimpleUI() {
! 176: simpleUI = true;
! 177: setupRestrictions();
! 178: fixChildrenForSimpleUI();
! 179: state = 0;
! 180: }
! 181:
! 182: @override
! 183: void newNodeCreationUI(ActionFunction okfct) {
! 184: setupSimpleUI();
! 185: okfct();
! 186: }
! 187:
! 188: FakeAttributeElement newTitle() {
! 189: List<x.Element> titleRefs = doc.cfg.elementReferences('title');
! 190: x.Element titleRef = doc.cfg.findSubElement(ref, titleRefs);
! 191: FakeAttributeElement title = NodeFactory.create(titleRef);
! 192: title.state = 1;
! 193: return title;
! 194: }
! 195:
! 196: PlotAxis newAxis() {
! 197: List<x.Element> axisRefs = doc.cfg.elementReferences('axis');
! 198: x.Element axisRef = doc.cfg.findSubElement(ref, axisRefs);
! 199: PlotAxis axis = NodeFactory.create(axisRef);
! 200: axis.state = 1;
! 201: return axis;
! 202: }
! 203:
! 204: PlotCurve newCurve() {
! 205: List<x.Element> curveRefs = doc.cfg.elementReferences('curve');
! 206: x.Element curveRef = doc.cfg.findSubElement(ref, curveRefs);
! 207: PlotCurve curve = NodeFactory.create(curveRef);
! 208: curve.setupSimpleUI();
! 209: return curve;
! 210: }
! 211:
! 212: FakeAttributeElement newXLabel() {
! 213: List<x.Element> xlabelRefs = doc.cfg.elementReferences('xlabel');
! 214: x.Element xlabelRef = doc.cfg.findSubElement(ref, xlabelRefs);
! 215: FakeAttributeElement xlabel = NodeFactory.create(xlabelRef);
! 216: xlabel.state = 1;
! 217: return xlabel;
! 218: }
! 219:
! 220: FakeAttributeElement newYLabel() {
! 221: List<x.Element> ylabelRefs = doc.cfg.elementReferences('ylabel');
! 222: x.Element ylabelRef = doc.cfg.findSubElement(ref, ylabelRefs);
! 223: FakeAttributeElement ylabel = NodeFactory.create(ylabelRef);
! 224: ylabel.state = 1;
! 225: return ylabel;
! 226: }
! 227:
! 228: /**
! 229: * This inserts simpleUI children if missing.
! 230: */
! 231: void fixChildrenForSimpleUI() {
! 232: bool foundTitle = false;
! 233: bool foundAxis = false;
! 234: bool foundCurve = false;
! 235: bool foundXLabel = false;
! 236: bool foundYLabel = false;
! 237: for (DaxeNode dn in childNodes) {
! 238: if (dn is FakeAttributeElement && dn.nodeName == 'title')
! 239: foundTitle = true;
! 240: else if (dn is PlotAxis)
! 241: foundAxis = true;
! 242: else if (dn is PlotCurve)
! 243: foundCurve = true;
! 244: else if (dn is FakeAttributeElement && dn.nodeName == 'xlabel')
! 245: foundXLabel = true;
! 246: else if (dn is FakeAttributeElement && dn.nodeName == 'ylabel')
! 247: foundYLabel = true;
! 248: }
! 249: if (!foundTitle)
! 250: appendChild(newTitle());
! 251: if (!foundAxis)
! 252: appendChild(newAxis());
! 253: if (!foundCurve)
! 254: appendChild(newCurve());
! 255: if (!foundXLabel)
! 256: appendChild(newXLabel());
! 257: if (!foundYLabel)
! 258: appendChild(newYLabel());
! 259: }
! 260: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>