Annotation of modules/damieng/graphical_editor/loncapa_daxe/web/nodes/itemgroup.dart, revision 1.1
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: list.add('none');
! 66: return list;
! 67: }
! 68:
! 69: @override
! 70: bool simpleUIPossible() {
! 71: if (attributes.length != 0)
! 72: throw new SimpleUIException('itemgroup: ' + LCDStrings.get('no_attribute'));
! 73: List<int> values = new List<int>();
! 74: for (DaxeNode dn=firstChild; dn!= null; dn=dn.nextSibling) {
! 75: if (dn is MatchItem) {
! 76: if (!dn.simpleUIPossible())
! 77: return false;
! 78: } else if (dn.nodeType != DaxeNode.TEXT_NODE) {
! 79: String title = doc.cfg.elementTitle(dn.ref);
! 80: throw new SimpleUIException(LCDStrings.get('element_prevents') + ' ' + dn.nodeName +
! 81: (title != null ? " ($title)" : ''));
! 82: } else if (dn.nodeValue.trim() != '') {
! 83: return false;
! 84: }
! 85: }
! 86: return true;
! 87: }
! 88:
! 89: @override
! 90: void setupSimpleUI() {
! 91: simpleUI = true;
! 92: setupRestrictions();
! 93: if (getItems().length == 0) {
! 94: addFirstItem();
! 95: }
! 96: }
! 97:
! 98: void setupRestrictions() {
! 99: if (restrictedInserts == null)
! 100: restrictedInserts = ['item'];
! 101: }
! 102:
! 103: @override
! 104: h.Element html() {
! 105: simpleUI = parent is MatchFoilgroup && (parent as MatchFoilgroup).simpleUI;
! 106: if (!simpleUI)
! 107: return super.html();
! 108: setupRestrictions();
! 109:
! 110: h.DivElement div = new h.DivElement();
! 111: div.id = id;
! 112: div.classes.add('itemgroup');
! 113: div.appendText(LCDStrings.get('item_to_match'));
! 114: h.TableElement table = new h.TableElement();
! 115: for (MatchItem item in getItems()) {
! 116: table.append(item.html());
! 117: }
! 118: div.append(table);
! 119: h.ButtonElement bAdd = new h.ButtonElement();
! 120: bAdd.attributes['type'] = 'button';
! 121: bAdd.text = LCDStrings.get('add_item');
! 122: bAdd.onClick.listen((h.MouseEvent event) => addUndoableItem());
! 123: div.append(bAdd);
! 124: return div;
! 125: }
! 126:
! 127: /**
! 128: * Adds an item (not using an UndoableEdit)
! 129: */
! 130: void addFirstItem() {
! 131: MatchItem item = new MatchItem.fromRef(itemRef);
! 132: item.state = 1;
! 133: item.simpleUI = true;
! 134: appendChild(item);
! 135: }
! 136:
! 137: /**
! 138: * Adds an item at the end (using an UndoableEdit)
! 139: */
! 140: void addUndoableItem() {
! 141: MatchItem item = new MatchItem.fromRef(itemRef);
! 142: item.state = 1;
! 143: item.simpleUI = true;
! 144: Position pos = new Position(this, offsetLength);
! 145: doc.insertNode(item, pos);
! 146: page.moveCursorTo(new Position(item, 0));
! 147: page.updateAfterPathChange();
! 148: }
! 149:
! 150: /**
! 151: * Removes the given item and updates the foils if necessary (called when the user click on the button).
! 152: */
! 153: void removeItem(MatchItem item) {
! 154: MatchFoilgroup foilgroup = parent;
! 155: UndoableEdit compound = new UndoableEdit.compound(Strings.get('undo.remove_element'));
! 156: UndoableEdit foilsEdit = foilgroup.updateFoilsAfterItemRemovalEdit(item.getAttribute('name'));
! 157: if (foilsEdit != null)
! 158: compound.addSubEdit(foilsEdit);
! 159: compound.addSubEdit(new UndoableEdit.removeNode(item));
! 160: doc.doNewEdit(compound);
! 161: }
! 162:
! 163: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>