File:  [LON-CAPA] / modules / damieng / graphical_editor / loncapa_daxe / web / nodes / rank_foil.dart
Revision 1.8: download - view: text, annotated - select for diffs
Thu Nov 17 02:47:16 2016 UTC (7 years, 6 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 a foil inside a rankresponse.
 * Uses a simplified UI if the parent (RankFoilgroup) is.
 */
class RankFoil extends LCDBlock {
  
  RankFoil.fromRef(x.Element elementRef) : super.fromRef(elementRef) {
  }
  
  RankFoil.fromNode(x.Node node, DaxeNode parent) : super.fromNode(node, parent) {
  }
  
  @override
  bool simpleUIPossible() {
    final RegExp numberedFoil = new RegExp("^[fF]oil[0-9]+\$");
    final RegExp positiveInteger = new RegExp("^[0-9]+\$");
    for (DaxeAttr att in attributes) {
      if (!(att.name == 'value' && positiveInteger.hasMatch(att.value) ||
          (att.name == 'name' && numberedFoil.hasMatch(att.value)) ||
          (att.name == 'location' && att.value == 'random')))
        throw new SimpleUIException('foil: ' + LCDStrings.get('attribute_problem') + att.name);
    }
    return SimpleUIText.checkNodeContents(this);
  }
  
  void setupRestrictions() {
    if (restrictedInserts == null)
      restrictedInserts = SimpleUIText.possibleDescendants;
  }
  
  int getRank() {
    int nb = 1;
    for (DaxeNode dn=previousSibling; dn!=null; dn=dn.previousSibling) {
      if (dn is RankFoil)
        nb++;
    }
    return nb;
  }
  
  @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 RankFoilgroup && (parent as RankFoilgroup).simpleUI;
    if (!simpleUI)
      return super.html();
    setupRestrictions();
    
    h.TableRowElement tr = new h.TableRowElement();
    tr.id = id;
    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);
    h.SpanElement rankNumberSpan = new h.SpanElement();
    rankNumberSpan.classes.add('rank-number');
    rankNumberSpan.appendText("${getRank()}");
    td.append(rankNumberSpan);
    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 RankFoilgroup).removeFoil(this));
    td.append(deleteImg);
    tr.append(td);
    return tr;
  }
  
  @override
  h.Element getHTMLContentsNode() {
    if (!simpleUI)
      return super.getHTMLContentsNode();
    return(getHTMLNode().nodes[1]);
  }
  
  @override
  void updateAttributes() {
    updateHTML();
  }
  
  // NOTE: see RadioFoil for explanations about these 2 overrides
  
  @override
  String getAttribute(String name) {
    if (!simpleUI || (name != 'name' && name != 'value'))
      return super.getAttribute(name);
    // for validation, return a value
    RankFoilgroup group = parent as RankFoilgroup;
    String prefix = group.simpleFoilNamePrefix;
    int nb = getRank();
    if (name == 'name')
      return "$prefix$nb";
    else
      return "$nb";
  }
  
  @override
  x.Node toDOMNode(x.Document domDocument) {
    x.Node node = super.toDOMNode(domDocument);
    RadioFoil.replaceSingleParagraphByContents(node);
    // set name and value attributes if necessary
    if (!simpleUI || parent == null)
      return node;
    x.Element el = node as x.Element;
    RankFoilgroup group = parent as RankFoilgroup;
    String prefix = group.simpleFoilNamePrefix;
    int nb = getRank();
    el.setAttribute('name', "$prefix$nb");
    el.setAttribute('value', "$nb");
    return el;
  }
  
}

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