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