Annotation of modules/damieng/graphical_editor/loncapa_daxe/web/nodes/section.dart, revision 1.2
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
! 125: void updateAttributes() {
! 126: if (!simpleUI)
! 127: super.updateAttributes();
! 128: else
! 129: updateHTML();
! 130: }
! 131:
1.1 damieng 132: H1 newH1() {
133: List<x.Element> h1Refs = doc.cfg.elementReferences('h1');
134: x.Element h1Ref = doc.cfg.findSubElement(ref, h1Refs);
135: H1 h1 = NodeFactory.create(h1Ref);
136: h1.state = 1;
137: return h1;
138: }
139:
140: /**
141: * This inserts simpleUI children if missing.
142: */
143: void fixChildrenForSimpleUI() {
144: bool foundH1 = (firstChild is H1 && firstChild.nodeName == 'h1');
145: if (!foundH1) {
146: if (firstChild == null)
147: appendChild(newH1());
148: else
149: insertBefore(newH1(), firstChild);
150: }
1.2 ! damieng 151: if (firstChild.nextSibling == null) {
! 152: x.Element hiddenp = doc.cfg.findSubElement(ref, doc.hiddenParaRefs);
! 153: appendChild(new DNHiddenP.fromRef(hiddenp));
! 154: }
1.1 damieng 155: }
156:
1.2 ! damieng 157: void showRolesMenu(h.Point pt) {
! 158: Menu popup = new Menu('');
! 159: popup.add(new MenuItem(LCDStrings.get('no_specific_role'), () {
! 160: doc.doNewEdit(new UndoableEdit.changeAttribute(this, new DaxeAttr('class', null)));
! 161: }));
! 162: for (String role in knownSectionRoles) {
! 163: popup.add(new MenuItem(LCDStrings.get(role), () {
! 164: doc.doNewEdit(new UndoableEdit.changeAttribute(this, new DaxeAttr('class', 'role-' + role)));
! 165: }));
! 166: }
! 167: h.DivElement div = popup.htmlMenu();
! 168: div.style.position = 'absolute';
! 169: div.style.display = 'block';
! 170: div.style.left = "${pt.x}px";
! 171: div.style.top = "${pt.y}px";
! 172: div.style.width = "12em";
! 173: ((div.firstChild) as h.Element).style.width = "12em"; // for the table
! 174: h.document.body.append(div);
! 175: StreamSubscription<h.MouseEvent> subscription = h.document.onMouseUp.listen(null);
! 176: subscription.onData((h.MouseEvent event) {
! 177: subscription.cancel();
! 178: div.remove();
! 179: event.preventDefault();
! 180: });
! 181: }
! 182:
! 183: void fillSelectWithRoles(h.SelectElement select) {
! 184: h.OptionElement option = new h.OptionElement();
! 185: option.text = LCDStrings.get('no_specific_role');
! 186: select.append(option);
! 187: for (String role in knownSectionRoles) {
! 188: h.OptionElement option = new h.OptionElement();
! 189: option.text = LCDStrings.get(role);
! 190: option.value = role;
! 191: select.append(option);
! 192: }
! 193: select.onChange.listen((h.Event e) {
! 194: String role = select.value;
! 195: UndoableEdit edit;
! 196: if (role != null && role.length > 0)
! 197: edit = new UndoableEdit.changeAttribute(this, new DaxeAttr('class', 'role-' + role));
! 198: else
! 199: edit = new UndoableEdit.changeAttribute(this, new DaxeAttr('class', null));
! 200: doc.doNewEdit(edit);
! 201: });
! 202: }
1.1 damieng 203: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>