Annotation of modules/damieng/graphical_editor/loncapa_daxe/web/nodes/itemgroup.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: * This is used by MatchResponse for simple UI.
22: */
23: class Itemgroup extends LCDBlock {
24:
25: x.Element itemRef;
26:
27: Itemgroup.fromRef(x.Element elementRef) : super.fromRef(elementRef) {
28: initItemgroup();
29: }
30:
31: Itemgroup.fromNode(x.Node node, DaxeNode parent) : super.fromNode(node, parent) {
32: initItemgroup();
33: if ((parent as MatchFoilgroup).simpleUI && simpleUIPossibleNoThrow())
34: simpleUI = true;
35: if (simpleUI)
36: setupSimpleUI();
37: }
38:
39: void initItemgroup() {
40: List<x.Element> itemRefs = doc.cfg.elementReferences('item');
41: itemRef = doc.cfg.findSubElement(ref, itemRefs);
42: }
43:
44: /**
45: * Returns the Item children
46: */
47: List<MatchItem> getItems() {
48: List<MatchItem> list = new List<MatchItem>();
49: for (DaxeNode dn in childNodes) {
50: if (dn is MatchItem)
51: list.add(dn);
52: }
53: return list;
54: }
55:
56: /**
57: * Returns the names of the items.
58: */
59: List<String> getItemNames() {
60: List<String> list = new List<String>();
61: for (MatchItem item in getItems()) {
62: list.add(item.getAttribute('name'));
63: }
64: return list;
65: }
66:
67: @override
68: bool simpleUIPossible() {
69: if (attributes.length != 0)
70: throw new SimpleUIException('itemgroup: ' + LCDStrings.get('no_attribute'));
71: for (DaxeNode dn=firstChild; dn!= null; dn=dn.nextSibling) {
72: if (dn is MatchItem) {
73: if (!dn.simpleUIPossible())
74: return false;
75: } else if (dn.nodeType != DaxeNode.TEXT_NODE) {
76: String title = doc.cfg.elementTitle(dn.ref);
77: throw new SimpleUIException(LCDStrings.get('element_prevents') + ' ' + dn.nodeName +
78: (title != null ? " ($title)" : ''));
79: } else if (dn.nodeValue.trim() != '') {
80: return false;
81: }
82: }
83: return true;
84: }
85:
86: @override
87: void setupSimpleUI() {
88: simpleUI = true;
89: setupRestrictions();
90: if (getItems().length == 0) {
91: addFirstItem();
92: }
93: }
94:
95: void setupRestrictions() {
96: if (restrictedInserts == null)
97: restrictedInserts = ['item'];
98: }
99:
100: @override
101: h.Element html() {
102: simpleUI = parent is MatchFoilgroup && (parent as MatchFoilgroup).simpleUI;
103: if (!simpleUI)
104: return super.html();
105: setupRestrictions();
106:
107: h.DivElement div = new h.DivElement();
108: div.id = id;
109: div.classes.add('itemgroup');
110: div.appendText(LCDStrings.get('item_to_match'));
111: h.TableElement table = new h.TableElement();
1.5 ! damieng 112: table.id = "contents-$id";
1.1 damieng 113: for (MatchItem item in getItems()) {
114: table.append(item.html());
115: }
116: div.append(table);
117: h.ButtonElement bAdd = new h.ButtonElement();
118: bAdd.attributes['type'] = 'button';
119: bAdd.text = LCDStrings.get('add_item');
120: bAdd.onClick.listen((h.MouseEvent event) => addUndoableItem());
121: div.append(bAdd);
122: return div;
123: }
124:
125: /**
126: * Adds an item (not using an UndoableEdit)
127: */
128: void addFirstItem() {
129: MatchItem item = new MatchItem.fromRef(itemRef);
130: item.state = 1;
131: item.simpleUI = true;
132: appendChild(item);
133: }
134:
135: /**
136: * Adds an item at the end (using an UndoableEdit)
137: */
138: void addUndoableItem() {
139: MatchItem item = new MatchItem.fromRef(itemRef);
140: item.state = 1;
141: item.simpleUI = true;
142: Position pos = new Position(this, offsetLength);
143: doc.insertNode(item, pos);
144: page.moveCursorTo(new Position(item, 0));
145: page.updateAfterPathChange();
146: }
147:
148: /**
149: * Removes the given item and updates the foils if necessary (called when the user click on the button).
150: */
151: void removeItem(MatchItem item) {
152: MatchFoilgroup foilgroup = parent;
153: UndoableEdit compound = new UndoableEdit.compound(Strings.get('undo.remove_element'));
154: UndoableEdit foilsEdit = foilgroup.updateFoilsAfterItemRemovalEdit(item.getAttribute('name'));
155: if (foilsEdit != null)
156: compound.addSubEdit(foilsEdit);
157: compound.addSubEdit(new UndoableEdit.removeNode(item));
158: doc.doNewEdit(compound);
159: }
160:
161: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>