Annotation of modules/damieng/graphical_editor/loncapa_daxe/web/nodes/section.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 HTML section, with a possible simple UI.
22: * Jaxe display type: 'section'.
23: */
24: class Section extends LCDBlock {
25:
26: Section.fromRef(x.Element elementRef) : super.fromRef(elementRef) {
1.5 ! damieng 27: displaySimpleButton = true;
1.1 damieng 28: }
29:
30: Section.fromNode(x.Node node, DaxeNode parent) : super.fromNode(node, parent) {
1.5 ! damieng 31: displaySimpleButton = true;
1.4 damieng 32: if (simpleUIPossibleNoThrow()) {
33: bool node_simpleUI = node.getUserData('simpleUI');
34: if (node_simpleUI == null || node_simpleUI)
1.1 damieng 35: setupSimpleUI();
36: }
37: }
38:
39: @override
40: bool simpleUIPossible() {
41: for (DaxeAttr att in attributes) {
1.2 damieng 42: bool advanced = false;
43: if (att.name != 'class')
44: advanced = true;
45: if (!advanced) {
46: if (!att.value.startsWith('role-'))
47: advanced = true;
48: }
49: if (!advanced) {
50: String role = att.value.substring(5);
51: if (!knownSectionRoles.contains(role))
52: advanced = true;
53: }
54: if (advanced)
1.1 damieng 55: throw new SimpleUIException('section: ' + LCDStrings.get('attribute_problem') + ' ' + att.name);
56: }
57: if (firstChild != null && firstChild is! H1)
58: return false;
59: return true;
60: }
61:
62: @override
63: h.Element html() {
64: if (!simpleUI)
65: return super.html();
66: h.DivElement div = new h.DivElement();
67: div.id = "$id";
68: div.classes.add('dn');
69: if (!valid)
70: div.classes.add('invalid');
71: div.classes.add('section');
72: setStyle(div);
73: DaxeNode dn = firstChild;
74: while (dn != null) {
75: h.Element he = dn.html();
76: if (dn is DNHiddenP && dn.firstChild == null && dn.previousSibling is H1 &&
77: dn.previousSibling.previousSibling == null && dn.nextSibling == null) {
78: he.style.position = 'relative';
79: h.SpanElement span = new h.SpanElement();
80: span.style.position = 'absolute';
81: span.style.color = '#CCC';
82: span.appendText(LCDStrings.get('section_contents'));
83: he.append(span);
84: }
85: div.append(he);
86: dn = dn.nextSibling;
87: }
88: if (lastChild == null || lastChild.nodeType == DaxeNode.TEXT_NODE)
89: div.appendText('\n');
90: return(div);
91: }
92:
93: void setupRestrictions() {
94: for (int i=0; i<attRefs.length; i++) {
95: x.Element refAttr = attRefs[i];
1.2 damieng 96: if (doc.cfg.attributeName(refAttr) != 'class') {
1.1 damieng 97: attRefs.removeAt(i);
98: i--;
99: }
100: }
101: }
102:
103: @override
104: void setupSimpleUI() {
105: simpleUI = true;
106: setupRestrictions();
107: fixChildrenForSimpleUI();
108: state = 0;
109: }
110:
111: @override
112: void newNodeCreationUI(ActionFunction okfct) {
113: setupSimpleUI();
114: okfct();
1.2 damieng 115: page.cursor.moveTo(new Position(firstChild, 0));
1.1 damieng 116: }
117:
118: @override
119: h.Element getHTMLContentsNode() {
120: if (!simpleUI)
121: return super.getHTMLContentsNode();
122: return getHTMLNode();
123: }
124:
1.2 damieng 125: @override
1.3 damieng 126: void attributeDialog([ActionFunction okfct]) {
127: // this is called by the contextual menu
128: if (simpleUI) {
129: AttributeDialog dlg = new AttributeDialog(this, okfct);
130: dlg.show();
131: } else {
132: super.attributeDialog(okfct);
133: }
134: }
135:
136: @override
1.2 damieng 137: void updateAttributes() {
138: if (!simpleUI)
139: super.updateAttributes();
140: else
141: updateHTML();
142: }
143:
1.1 damieng 144: H1 newH1() {
145: List<x.Element> h1Refs = doc.cfg.elementReferences('h1');
146: x.Element h1Ref = doc.cfg.findSubElement(ref, h1Refs);
147: H1 h1 = NodeFactory.create(h1Ref);
148: h1.state = 1;
149: return h1;
150: }
151:
152: /**
153: * This inserts simpleUI children if missing.
154: */
155: void fixChildrenForSimpleUI() {
156: bool foundH1 = (firstChild is H1 && firstChild.nodeName == 'h1');
157: if (!foundH1) {
158: if (firstChild == null)
159: appendChild(newH1());
160: else
161: insertBefore(newH1(), firstChild);
162: }
1.2 damieng 163: if (firstChild.nextSibling == null) {
164: x.Element hiddenp = doc.cfg.findSubElement(ref, doc.hiddenParaRefs);
165: appendChild(new DNHiddenP.fromRef(hiddenp));
166: }
1.1 damieng 167: }
168:
1.2 damieng 169: void showRolesMenu(h.Point pt) {
170: Menu popup = new Menu('');
171: popup.add(new MenuItem(LCDStrings.get('no_specific_role'), () {
172: doc.doNewEdit(new UndoableEdit.changeAttribute(this, new DaxeAttr('class', null)));
173: }));
174: for (String role in knownSectionRoles) {
175: popup.add(new MenuItem(LCDStrings.get(role), () {
176: doc.doNewEdit(new UndoableEdit.changeAttribute(this, new DaxeAttr('class', 'role-' + role)));
177: }));
178: }
179: h.DivElement div = popup.htmlMenu();
180: div.style.position = 'absolute';
181: div.style.display = 'block';
182: div.style.left = "${pt.x}px";
183: div.style.top = "${pt.y}px";
184: div.style.width = "12em";
185: ((div.firstChild) as h.Element).style.width = "12em"; // for the table
186: h.document.body.append(div);
187: StreamSubscription<h.MouseEvent> subscription = h.document.onMouseUp.listen(null);
188: subscription.onData((h.MouseEvent event) {
189: subscription.cancel();
190: div.remove();
191: event.preventDefault();
192: });
193: }
194:
195: void fillSelectWithRoles(h.SelectElement select) {
196: h.OptionElement option = new h.OptionElement();
197: option.text = LCDStrings.get('no_specific_role');
198: select.append(option);
199: for (String role in knownSectionRoles) {
200: h.OptionElement option = new h.OptionElement();
201: option.text = LCDStrings.get(role);
202: option.value = role;
203: select.append(option);
204: }
205: select.onChange.listen((h.Event e) {
206: String role = select.value;
207: UndoableEdit edit;
208: if (role != null && role.length > 0)
209: edit = new UndoableEdit.changeAttribute(this, new DaxeAttr('class', 'role-' + role));
210: else
211: edit = new UndoableEdit.changeAttribute(this, new DaxeAttr('class', null));
212: doc.doNewEdit(edit);
213: });
214: }
1.1 damieng 215: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>