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

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