Annotation of modules/damieng/graphical_editor/loncapa_daxe/web/nodes/itemgroup.dart, revision 1.4

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();
                    112:     for (MatchItem item in getItems()) {
                    113:       table.append(item.html());
                    114:     }
                    115:     div.append(table);
                    116:     h.ButtonElement bAdd = new h.ButtonElement();
                    117:     bAdd.attributes['type'] = 'button';
                    118:     bAdd.text = LCDStrings.get('add_item');
                    119:     bAdd.onClick.listen((h.MouseEvent event) => addUndoableItem());
                    120:     div.append(bAdd);
                    121:     return div;
                    122:   }
                    123:   
                    124:   /**
                    125:    * Adds an item (not using an UndoableEdit)
                    126:    */
                    127:   void addFirstItem() {
                    128:     MatchItem item = new MatchItem.fromRef(itemRef);
                    129:     item.state = 1;
                    130:     item.simpleUI = true;
                    131:     appendChild(item);
                    132:   }
                    133:   
                    134:   /**
                    135:    * Adds an item at the end (using an UndoableEdit)
                    136:    */
                    137:   void addUndoableItem() {
                    138:     MatchItem item = new MatchItem.fromRef(itemRef);
                    139:     item.state = 1;
                    140:     item.simpleUI = true;
                    141:     Position pos = new Position(this, offsetLength);
                    142:     doc.insertNode(item, pos);
                    143:     page.moveCursorTo(new Position(item, 0));
                    144:     page.updateAfterPathChange();
                    145:   }
                    146:   
                    147:   /**
                    148:    * Removes the given item and updates the foils if necessary (called when the user click on the button).
                    149:    */
                    150:   void removeItem(MatchItem item) {
                    151:     MatchFoilgroup foilgroup = parent;
                    152:     UndoableEdit compound = new UndoableEdit.compound(Strings.get('undo.remove_element'));
                    153:     UndoableEdit foilsEdit = foilgroup.updateFoilsAfterItemRemovalEdit(item.getAttribute('name'));
                    154:     if (foilsEdit != null)
                    155:       compound.addSubEdit(foilsEdit);
                    156:     compound.addSubEdit(new UndoableEdit.removeNode(item));
                    157:     doc.doNewEdit(compound);
                    158:   }
                    159:   
                    160: }

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>