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