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