Annotation of modules/damieng/graphical_editor/loncapa_daxe/web/nodes/gnuplot.dart, revision 1.2
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;
1.2 ! damieng 147: } else
! 148: return null;
1.1 damieng 149: }
150:
151: void appendFakeAttributeShort(h.DivElement attDiv, String title, String value) {
152: attDiv.append(new h.Text(" "));
153: h.Element att_name = new h.SpanElement();
154: att_name.attributes['class'] = 'attribute_name';
155: att_name.text = title;
156: attDiv.append(att_name);
157: attDiv.append(new h.Text("="));
158: h.Element att_val = new h.SpanElement();
159: att_val.attributes['class'] = 'attribute_value';
160: att_val.text = value;
161: attDiv.append(att_val);
162: }
163:
164: void setupRestrictions() {
165: for (int i=0; i<attRefs.length; i++) {
166: x.Element refAttr = attRefs[i];
167: if (!simpleUIAttributes.contains(doc.cfg.attributeName(refAttr))) {
168: attRefs.removeAt(i);
169: i--;
170: }
171: }
172: restrictedInserts = [];
173: }
174:
175: @override
176: void setupSimpleUI() {
177: simpleUI = true;
178: setupRestrictions();
179: fixChildrenForSimpleUI();
180: state = 0;
181: }
182:
183: @override
184: void newNodeCreationUI(ActionFunction okfct) {
185: setupSimpleUI();
186: okfct();
187: }
188:
189: FakeAttributeElement newTitle() {
190: List<x.Element> titleRefs = doc.cfg.elementReferences('title');
191: x.Element titleRef = doc.cfg.findSubElement(ref, titleRefs);
192: FakeAttributeElement title = NodeFactory.create(titleRef);
193: title.state = 1;
194: return title;
195: }
196:
197: PlotAxis newAxis() {
198: List<x.Element> axisRefs = doc.cfg.elementReferences('axis');
199: x.Element axisRef = doc.cfg.findSubElement(ref, axisRefs);
200: PlotAxis axis = NodeFactory.create(axisRef);
201: axis.state = 1;
202: return axis;
203: }
204:
205: PlotCurve newCurve() {
206: List<x.Element> curveRefs = doc.cfg.elementReferences('curve');
207: x.Element curveRef = doc.cfg.findSubElement(ref, curveRefs);
208: PlotCurve curve = NodeFactory.create(curveRef);
209: curve.setupSimpleUI();
210: return curve;
211: }
212:
213: FakeAttributeElement newXLabel() {
214: List<x.Element> xlabelRefs = doc.cfg.elementReferences('xlabel');
215: x.Element xlabelRef = doc.cfg.findSubElement(ref, xlabelRefs);
216: FakeAttributeElement xlabel = NodeFactory.create(xlabelRef);
217: xlabel.state = 1;
218: return xlabel;
219: }
220:
221: FakeAttributeElement newYLabel() {
222: List<x.Element> ylabelRefs = doc.cfg.elementReferences('ylabel');
223: x.Element ylabelRef = doc.cfg.findSubElement(ref, ylabelRefs);
224: FakeAttributeElement ylabel = NodeFactory.create(ylabelRef);
225: ylabel.state = 1;
226: return ylabel;
227: }
228:
229: /**
230: * This inserts simpleUI children if missing.
231: */
232: void fixChildrenForSimpleUI() {
233: bool foundTitle = false;
234: bool foundAxis = false;
235: bool foundCurve = false;
236: bool foundXLabel = false;
237: bool foundYLabel = false;
238: for (DaxeNode dn in childNodes) {
239: if (dn is FakeAttributeElement && dn.nodeName == 'title')
240: foundTitle = true;
241: else if (dn is PlotAxis)
242: foundAxis = true;
243: else if (dn is PlotCurve)
244: foundCurve = true;
245: else if (dn is FakeAttributeElement && dn.nodeName == 'xlabel')
246: foundXLabel = true;
247: else if (dn is FakeAttributeElement && dn.nodeName == 'ylabel')
248: foundYLabel = true;
249: }
250: if (!foundTitle)
251: appendChild(newTitle());
252: if (!foundAxis)
253: appendChild(newAxis());
254: if (!foundCurve)
255: appendChild(newCurve());
256: if (!foundXLabel)
257: appendChild(newXLabel());
258: if (!foundYLabel)
259: appendChild(newYLabel());
260: }
261: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>