File:  [LON-CAPA] / modules / damieng / graphical_editor / loncapa_daxe / web / nodes / itemgroup.dart
Revision 1.5: download - view: text, annotated - select for diffs
Wed Mar 8 20:31:22 2017 UTC (7 years, 7 months ago) by damieng
Branches: MAIN
CVS tags: HEAD
making sure that getHTMLContentsNode() returns something for foilgroups

/*
  This file is part of LONCAPA-Daxe.

  LONCAPA-Daxe is free software: you can redistribute it and/or modify
  it under the terms of the GNU General Public License as published by
  the Free Software Foundation, either version 3 of the License, or
  (at your option) any later version.

  LONCAPA-Daxe is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  GNU General Public License for more details.

  You should have received a copy of the GNU General Public License
  along with Daxe.  If not, see <http://www.gnu.org/licenses/>.
*/

part of loncapa_daxe;

/**
 * This is used by MatchResponse for simple UI.
 */
class Itemgroup extends LCDBlock {
  
  x.Element itemRef;
  
  Itemgroup.fromRef(x.Element elementRef) : super.fromRef(elementRef) {
    initItemgroup();
  }
  
  Itemgroup.fromNode(x.Node node, DaxeNode parent) : super.fromNode(node, parent) {
    initItemgroup();
    if ((parent as MatchFoilgroup).simpleUI && simpleUIPossibleNoThrow())
      simpleUI = true;
    if (simpleUI)
      setupSimpleUI();
  }
  
  void initItemgroup() {
    List<x.Element> itemRefs = doc.cfg.elementReferences('item');
    itemRef = doc.cfg.findSubElement(ref, itemRefs);
  }
  
  /**
   * Returns the Item children
   */
  List<MatchItem> getItems() {
    List<MatchItem> list = new List<MatchItem>();
    for (DaxeNode dn in childNodes) {
      if (dn is MatchItem)
        list.add(dn);
    }
    return list;
  }
  
  /**
   * Returns the names of the items.
   */
  List<String> getItemNames() {
    List<String> list = new List<String>();
    for (MatchItem item in getItems()) {
      list.add(item.getAttribute('name'));
    }
    return list;
  }
  
  @override
  bool simpleUIPossible() {
    if (attributes.length != 0)
      throw new SimpleUIException('itemgroup: ' + LCDStrings.get('no_attribute'));
    for (DaxeNode dn=firstChild; dn!= null; dn=dn.nextSibling) {
      if (dn is MatchItem) {
        if (!dn.simpleUIPossible())
          return false;
      } else if (dn.nodeType != DaxeNode.TEXT_NODE) {
        String title = doc.cfg.elementTitle(dn.ref);
        throw new SimpleUIException(LCDStrings.get('element_prevents') + ' ' + dn.nodeName +
            (title != null ? " ($title)" : ''));
      } else if (dn.nodeValue.trim() != '') {
        return false;
      }
    }
    return true;
  }
  
  @override
  void setupSimpleUI() {
    simpleUI = true;
    setupRestrictions();
    if (getItems().length == 0) {
      addFirstItem();
    }
  }
  
  void setupRestrictions() {
    if (restrictedInserts == null)
      restrictedInserts = ['item'];
  }
  
  @override
  h.Element html() {
    simpleUI = parent is MatchFoilgroup && (parent as MatchFoilgroup).simpleUI;
    if (!simpleUI)
      return super.html();
    setupRestrictions();
    
    h.DivElement div = new h.DivElement();
    div.id = id;
    div.classes.add('itemgroup');
    div.appendText(LCDStrings.get('item_to_match'));
    h.TableElement table = new h.TableElement();
    table.id = "contents-$id";
    for (MatchItem item in getItems()) {
      table.append(item.html());
    }
    div.append(table);
    h.ButtonElement bAdd = new h.ButtonElement();
    bAdd.attributes['type'] = 'button';
    bAdd.text = LCDStrings.get('add_item');
    bAdd.onClick.listen((h.MouseEvent event) => addUndoableItem());
    div.append(bAdd);
    return div;
  }
  
  /**
   * Adds an item (not using an UndoableEdit)
   */
  void addFirstItem() {
    MatchItem item = new MatchItem.fromRef(itemRef);
    item.state = 1;
    item.simpleUI = true;
    appendChild(item);
  }
  
  /**
   * Adds an item at the end (using an UndoableEdit)
   */
  void addUndoableItem() {
    MatchItem item = new MatchItem.fromRef(itemRef);
    item.state = 1;
    item.simpleUI = true;
    Position pos = new Position(this, offsetLength);
    doc.insertNode(item, pos);
    page.moveCursorTo(new Position(item, 0));
    page.updateAfterPathChange();
  }
  
  /**
   * Removes the given item and updates the foils if necessary (called when the user click on the button).
   */
  void removeItem(MatchItem item) {
    MatchFoilgroup foilgroup = parent;
    UndoableEdit compound = new UndoableEdit.compound(Strings.get('undo.remove_element'));
    UndoableEdit foilsEdit = foilgroup.updateFoilsAfterItemRemovalEdit(item.getAttribute('name'));
    if (foilsEdit != null)
      compound.addSubEdit(foilsEdit);
    compound.addSubEdit(new UndoableEdit.removeNode(item));
    doc.doNewEdit(compound);
  }
  
}

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