File:  [LON-CAPA] / modules / damieng / graphical_editor / loncapa_daxe / web / nodes / match_item.dart
Revision 1.2: download - view: text, annotated - select for diffs
Thu Nov 17 02:47:16 2016 UTC (7 years, 7 months ago) by damieng
Branches: MAIN
CVS tags: HEAD
using Daxe's new builtin drag&drop for elements instead of the custom drag&drop for foils

/*
  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;

/**
 * Display for an item inside a matchresponse.
 * Uses a simplified UI if the parent (Itemgroup) is.
 */
class MatchItem extends LCDBlock {
  
  MatchItem.fromRef(x.Element elementRef) : super.fromRef(elementRef) {
  }
  
  MatchItem.fromNode(x.Node node, DaxeNode parent) : super.fromNode(node, parent) {
  }
  
  @override
  bool simpleUIPossible() {
    for (DaxeAttr att in attributes) {
      if (!(att.name == 'name' ||
          (att.name == 'location' && att.value == 'random')))
        throw new SimpleUIException('item: ' + LCDStrings.get('attribute_problem') + att.name);
    }
    return SimpleUIText.checkNodeContents(this);
  }
  
  void setupRestrictions() {
    if (restrictedInserts == null)
      restrictedInserts = SimpleUIText.possibleDescendants;
  }
  
  @override
  h.Element html() {
    // this is called just after a new node is inserted, and we need to know if we should use simple UI
    simpleUI = parent is Itemgroup && (parent as Itemgroup).simpleUI;
    if (!simpleUI)
      return super.html();
    setupRestrictions();
    
    h.TableRowElement tr = new h.TableRowElement();
    tr.id = id;
    Itemgroup group = parent;
    h.TableCellElement td = new h.TableCellElement();
    h.ImageElement grip = new h.ImageElement(src:'images/grip.png', width:8, height:16);
    grip.classes.add('grip-icon');
    setupDrag(grip);
    td.append(grip);
    tr.append(td);
    td = new h.TableCellElement();
    h.TextInputElement textfield = new h.TextInputElement();
    textfield.size = 15;
    if (getAttribute('name') != null)
      textfield.value = getAttribute('name');
    textfield.onInput.listen((h.Event e) {
      UndoableEdit compound = new UndoableEdit.compound(LCDStrings.get('item_name_change'));
      compound.addSubEdit(new UndoableEdit.changeAttribute(this,
          new DaxeAttr('name', textfield.value), updateDisplay:false));
      UndoableEdit foilsEdit = (group.parent as MatchFoilgroup).updateFoilsAfterItemNameChangeEdit(
          getAttribute('name'), textfield.value);
      if (foilsEdit != null)
        compound.addSubEdit(foilsEdit);
      doc.doNewEdit(compound);
      (group.parent as MatchFoilgroup).updateFoilsHtml();
    });
    td.append(textfield);
    tr.append(td);
    td = new h.TableCellElement();
    for (DaxeNode dn=firstChild; dn!= null; dn=dn.nextSibling) {
      td.append(dn.html());
    }
    tr.append(td);
    td = new h.TableCellElement();
    h.ImageElement deleteImg = new h.ImageElement(src:'images/delete.png', width:16, height:16);
    deleteImg.classes.add('delete-icon');
    deleteImg.onClick.listen((h.MouseEvent e) => (parent as Itemgroup).removeItem(this));
    td.append(deleteImg);
    tr.append(td);
    return tr;
  }
  
  @override
  h.Element getHTMLContentsNode() {
    if (!simpleUI)
      return super.getHTMLContentsNode();
    return(getHTMLNode().nodes[2]);
  }
  
  @override
  void updateAttributes() {
    updateHTML();
  }
  
}

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