Annotation of modules/damieng/graphical_editor/loncapa_daxe/web/nodes/gnuplot.dart, revision 1.5
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) {
1.5 ! damieng 33: if (simpleUIPossibleNoThrow()) {
! 34: bool node_simpleUI = node.getUserData('simpleUI');
! 35: if (node_simpleUI == null || node_simpleUI)
1.1 damieng 36: setupSimpleUI();
37: }
38: }
39:
40: @override
41: bool simpleUIPossible() {
42: for (DaxeAttr att in attributes) {
43: if (!simpleUIAttributes.contains(att.name)) {
1.3 damieng 44: // check if it's a default value
45: bool isDefault = false;
46: for (x.Element attref in attRefs) {
47: if (att.localName == doc.cfg.attributeName(attref) &&
48: att.namespaceURI == doc.cfg.attributeNamespace(attref)) {
49: String defaultValue = doc.cfg.defaultAttributeValue(attref);
50: if (att.value == defaultValue)
51: isDefault = true;
52: break;
53: }
54: }
55: if (!isDefault)
56: throw new SimpleUIException('gnuplot: ' + LCDStrings.get('attribute_problem') + ' ' + att.name);
1.1 damieng 57: }
58: }
59: int nbTitle = 0;
60: int nbAxis = 0;
61: int nbCurve = 0;
62: int nbXLabel = 0;
63: int nbYLabel = 0;
64: for (DaxeNode dn=firstChild; dn!= null; dn=dn.nextSibling) {
65: if (dn is FakeAttributeElement && dn.nodeName == 'title') {
66: if (!dn.simpleUIPossible())
67: return false;
68: nbTitle++;
69: } else if (dn is PlotAxis) {
70: if (!dn.simpleUIPossible())
71: return false;
72: nbAxis++;
73: } else if (dn is PlotCurve) {
74: if (!dn.simpleUIPossible())
75: return false;
76: nbCurve++;
77: } else if (dn is FakeAttributeElement && dn.nodeName == 'xlabel') {
78: if (!dn.simpleUIPossible())
79: return false;
80: nbXLabel++;
81: } else if (dn is FakeAttributeElement && dn.nodeName == 'ylabel') {
82: if (!dn.simpleUIPossible())
83: return false;
84: nbYLabel++;
85: } else if (dn.nodeType != DaxeNode.TEXT_NODE) {
86: String title = doc.cfg.elementTitle(dn.ref);
87: throw new SimpleUIException(LCDStrings.get('element_prevents') + ' ' + dn.nodeName +
88: (title != null ? " ($title)" : ''));
89: } else if (dn.nodeValue.trim() != '') {
90: return false;
91: }
92: }
93: if (nbTitle > 1 || nbAxis > 1 || nbCurve > 1 || nbXLabel > 1 || nbYLabel > 1)
94: return false;
95: return true;
96: }
97:
98: @override
99: h.Element html() {
100: if (!simpleUI)
101: return super.html();
102: h.DivElement div = new h.DivElement();
103: div.id = "$id";
104: div.classes.add('dn');
105: if (!valid)
106: div.classes.add('invalid');
107: div.classes.add('lcdblock');
108:
109: h.DivElement headerDiv = createHeaderDiv();
110: headerDiv.classes.add('without-content-afterwards');
111: div.append(headerDiv);
112:
113: return(div);
114: }
115:
116: @override
117: h.Element createAttributesHTML() {
118: h.Element attHTML = super.createAttributesHTML();
119: if (!simpleUI || attHTML == null)
120: return attHTML;
121: // add the fake attributes
122: if (state == 0) {
123: h.DivElement div = new h.DivElement();
124: h.TableElement table = attHTML;
125: div.append(table);
126: for (DaxeNode dn in childNodes) {
127: if (dn is FakeAttributeElement || dn is PlotAxis || dn is PlotCurve) {
128: h.Element he = dn.html(); // might be table, tr or div
129: if (he is h.TableRowElement) {
130: if (table == null) {
131: table = new h.TableElement();
132: table.classes.add('expand');
133: div.append(table);
134: }
135: table.append(he);
136: } else {
137: div.append(he);
138: table = null;
139: }
140: }
141: }
142: return div;
143: } else if (state == 1) {
144: h.DivElement attDiv = attHTML;
145: for (DaxeNode dn=firstChild; dn!= null; dn=dn.nextSibling) {
146: if (dn is FakeAttributeElement && dn.firstChild is DNText) {
147: appendFakeAttributeShort(attDiv, dn.nodeName, dn.firstChild.nodeValue);
148: } else if (dn is PlotAxis) {
149: for (DaxeAttr att in dn.attributes) {
150: if (att.name == 'xmin' || att.name == 'xmax' || att.name == 'ymin' || att.name == 'ymax')
151: appendFakeAttributeShort(attDiv, att.name, att.value);
152: }
153: } else if (dn is PlotCurve && dn.firstChild is FakeAttributeElement) {
154: FakeAttributeElement fct = dn.firstChild;
155: if (fct.firstChild is DNText)
156: appendFakeAttributeShort(attDiv, fct.nodeName, fct.firstChild.nodeValue);
157: }
158: }
159: return attDiv;
1.2 damieng 160: } else
161: return null;
1.1 damieng 162: }
163:
164: void appendFakeAttributeShort(h.DivElement attDiv, String title, String value) {
165: attDiv.append(new h.Text(" "));
166: h.Element att_name = new h.SpanElement();
167: att_name.attributes['class'] = 'attribute_name';
168: att_name.text = title;
169: attDiv.append(att_name);
170: attDiv.append(new h.Text("="));
171: h.Element att_val = new h.SpanElement();
172: att_val.attributes['class'] = 'attribute_value';
173: att_val.text = value;
174: attDiv.append(att_val);
175: }
176:
177: void setupRestrictions() {
178: for (int i=0; i<attRefs.length; i++) {
179: x.Element refAttr = attRefs[i];
180: if (!simpleUIAttributes.contains(doc.cfg.attributeName(refAttr))) {
181: attRefs.removeAt(i);
182: i--;
183: }
184: }
185: restrictedInserts = [];
186: }
187:
188: @override
189: void setupSimpleUI() {
190: simpleUI = true;
1.3 damieng 191: removeDefaultAttributes();
1.1 damieng 192: setupRestrictions();
193: fixChildrenForSimpleUI();
194: state = 0;
195: }
196:
197: @override
198: void newNodeCreationUI(ActionFunction okfct) {
199: setupSimpleUI();
200: okfct();
201: }
202:
203: FakeAttributeElement newTitle() {
204: List<x.Element> titleRefs = doc.cfg.elementReferences('title');
205: x.Element titleRef = doc.cfg.findSubElement(ref, titleRefs);
206: FakeAttributeElement title = NodeFactory.create(titleRef);
207: title.state = 1;
208: return title;
209: }
210:
211: PlotAxis newAxis() {
212: List<x.Element> axisRefs = doc.cfg.elementReferences('axis');
213: x.Element axisRef = doc.cfg.findSubElement(ref, axisRefs);
214: PlotAxis axis = NodeFactory.create(axisRef);
215: axis.state = 1;
216: return axis;
217: }
218:
219: PlotCurve newCurve() {
220: List<x.Element> curveRefs = doc.cfg.elementReferences('curve');
221: x.Element curveRef = doc.cfg.findSubElement(ref, curveRefs);
222: PlotCurve curve = NodeFactory.create(curveRef);
223: curve.setupSimpleUI();
224: return curve;
225: }
226:
227: FakeAttributeElement newXLabel() {
228: List<x.Element> xlabelRefs = doc.cfg.elementReferences('xlabel');
229: x.Element xlabelRef = doc.cfg.findSubElement(ref, xlabelRefs);
230: FakeAttributeElement xlabel = NodeFactory.create(xlabelRef);
231: xlabel.state = 1;
232: return xlabel;
233: }
234:
235: FakeAttributeElement newYLabel() {
236: List<x.Element> ylabelRefs = doc.cfg.elementReferences('ylabel');
237: x.Element ylabelRef = doc.cfg.findSubElement(ref, ylabelRefs);
238: FakeAttributeElement ylabel = NodeFactory.create(ylabelRef);
239: ylabel.state = 1;
240: return ylabel;
241: }
242:
243: /**
244: * This inserts simpleUI children if missing.
245: */
246: void fixChildrenForSimpleUI() {
247: bool foundTitle = false;
248: bool foundAxis = false;
249: bool foundCurve = false;
250: bool foundXLabel = false;
251: bool foundYLabel = false;
252: for (DaxeNode dn in childNodes) {
253: if (dn is FakeAttributeElement && dn.nodeName == 'title')
254: foundTitle = true;
255: else if (dn is PlotAxis)
256: foundAxis = true;
257: else if (dn is PlotCurve)
258: foundCurve = true;
259: else if (dn is FakeAttributeElement && dn.nodeName == 'xlabel')
260: foundXLabel = true;
261: else if (dn is FakeAttributeElement && dn.nodeName == 'ylabel')
262: foundYLabel = true;
263: }
264: if (!foundTitle)
265: appendChild(newTitle());
266: if (!foundAxis)
267: appendChild(newAxis());
268: if (!foundCurve)
269: appendChild(newCurve());
270: if (!foundXLabel)
271: appendChild(newXLabel());
272: if (!foundYLabel)
273: appendChild(newYLabel());
274: }
1.3 damieng 275:
276: void removeDefaultAttributes() {
277: List<DaxeAttr> list = new List.from(attributes);
278: for (DaxeAttr att in list) {
279: if (!simpleUIAttributes.contains(att.name)) {
280: for (x.Element attref in attRefs) {
281: if (att.localName == doc.cfg.attributeName(attref) &&
282: att.namespaceURI == doc.cfg.attributeNamespace(attref)) {
283: String defaultValue = doc.cfg.defaultAttributeValue(attref);
284: if (att.value == defaultValue)
285: removeAttribute(att.name);
286: break;
287: }
288: }
289: }
290: }
291: }
1.1 damieng 292: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>