Annotation of modules/damieng/graphical_editor/loncapa_daxe/web/nodes/itemgroup.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: * This is used by MatchResponse for simple UI.
22: */
23: class Itemgroup extends LCDBlock {
24:
25: x.Element itemRef;
26: MatchItem draggedItem;
27:
28: Itemgroup.fromRef(x.Element elementRef) : super.fromRef(elementRef) {
29: initItemgroup();
30: }
31:
32: Itemgroup.fromNode(x.Node node, DaxeNode parent) : super.fromNode(node, parent) {
33: initItemgroup();
34: if ((parent as MatchFoilgroup).simpleUI && simpleUIPossibleNoThrow())
35: simpleUI = true;
36: if (simpleUI)
37: setupSimpleUI();
38: }
39:
40: void initItemgroup() {
41: List<x.Element> itemRefs = doc.cfg.elementReferences('item');
42: itemRef = doc.cfg.findSubElement(ref, itemRefs);
43: }
44:
45: /**
46: * Returns the Item children
47: */
48: List<MatchItem> getItems() {
49: List<MatchItem> list = new List<MatchItem>();
50: for (DaxeNode dn in childNodes) {
51: if (dn is MatchItem)
52: list.add(dn);
53: }
54: return list;
55: }
56:
57: /**
58: * Returns the names of the items.
59: */
60: List<String> getItemNames() {
61: List<String> list = new List<String>();
62: for (MatchItem item in getItems()) {
63: list.add(item.getAttribute('name'));
64: }
65: return list;
66: }
67:
68: @override
69: bool simpleUIPossible() {
70: if (attributes.length != 0)
71: throw new SimpleUIException('itemgroup: ' + LCDStrings.get('no_attribute'));
72: for (DaxeNode dn=firstChild; dn!= null; dn=dn.nextSibling) {
73: if (dn is MatchItem) {
74: if (!dn.simpleUIPossible())
75: return false;
76: } else if (dn.nodeType != DaxeNode.TEXT_NODE) {
77: String title = doc.cfg.elementTitle(dn.ref);
78: throw new SimpleUIException(LCDStrings.get('element_prevents') + ' ' + dn.nodeName +
79: (title != null ? " ($title)" : ''));
80: } else if (dn.nodeValue.trim() != '') {
81: return false;
82: }
83: }
84: return true;
85: }
86:
87: @override
88: void setupSimpleUI() {
89: simpleUI = true;
90: setupRestrictions();
91: if (getItems().length == 0) {
92: addFirstItem();
93: }
94: }
95:
96: void setupRestrictions() {
97: if (restrictedInserts == null)
98: restrictedInserts = ['item'];
99: }
100:
101: @override
102: h.Element html() {
103: simpleUI = parent is MatchFoilgroup && (parent as MatchFoilgroup).simpleUI;
104: if (!simpleUI)
105: return super.html();
106: setupRestrictions();
107:
108: h.DivElement div = new h.DivElement();
109: div.id = id;
110: div.classes.add('itemgroup');
111: div.appendText(LCDStrings.get('item_to_match'));
112: h.TableElement table = new h.TableElement();
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>