File:  [LON-CAPA] / modules / damieng / graphical_editor / loncapa_daxe / web / nodes / hintgroup.dart
Revision 1.6: download - view: text, annotated - select for diffs
Mon Sep 26 21:47:23 2016 UTC (7 years, 8 months ago) by damieng
Branches: MAIN
CVS tags: HEAD
removed unnecessary casts

/*
  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 RadioResponse for simple UI.
 */
class Hintgroup extends LCDBlock {

  Hintgroup.fromRef(x.Element elementRef) : super.fromRef(elementRef) {
  }

  Hintgroup.fromNode(x.Node node, DaxeNode parent) : super.fromNode(node, parent) {
    if (parent is LCDBlock && parent.simpleUI)
      simpleUI = true;
  }

  @override
  bool simpleUIPossible() {
    for (DaxeAttr att in attributes)
      if (att.name != 'showoncorrect' || att.value != 'no')
        throw new SimpleUIException('hintgroup: ' + LCDStrings.get('attribute_problem') + att.name);
    // check if there is a single hintpart with on="default" and simple content inside
    bool hintpartok = false;
    bool othernode = false;
    for (DaxeNode dn in childNodes) {
      if (dn.nodeType == DaxeNode.ELEMENT_NODE && dn.nodeName == 'hintpart' &&
          dn.getAttribute('on') == 'default' && dn.attributes.length == 1 &&
          SimpleUIText.checkNodeContents(dn))
        hintpartok = true;
      else if (dn.nodeType != DaxeNode.TEXT_NODE || dn.nodeValue.trim() != '')
        othernode = true;
    }
    if (hintpartok && !othernode)
      return true;
    // otherwise just check the contents
    return SimpleUIText.checkNodeContents(this);
  }

  @override
  h.Element html() {
    simpleUI = simpleUIPossibleNoThrow() && parent is LCDBlock && (parent as LCDBlock).simpleUI;
    if (!simpleUI)
      return super.html();
    setupRestrictions();
    h.TableElement table = new h.TableElement();
    table.id = id;
    table.classes.add('hintgroup');
    h.TableRowElement tr = new h.TableRowElement();
    h.TableCellElement td = new h.TableCellElement();
    h.SpanElement titleSpan = new h.SpanElement();
    titleSpan.appendText(LCDStrings.get('hint') + ' ');
    td.append(titleSpan);
    tr.append(td);
    td = new h.TableCellElement();
    td.id = 'contents-' + id;
    h.SpanElement contentsSpan = new h.SpanElement();
    for (DaxeNode dn=firstChild; dn!= null; dn=dn.nextSibling) {
      contentsSpan.append(dn.html());
    }
    td.append(contentsSpan);
    tr.append(td);
    table.append(tr);
    return table;
  }

  @override
  h.Element getHTMLContentsNode() {
    if (!simpleUI)
      return super.getHTMLContentsNode();
    return(h.document.getElementById('contents-' + id));
  }

  void setupRestrictions() {
    if (restrictedInserts == null)
      restrictedInserts = SimpleUIText.possibleDescendants;
  }

  @override
  void setupSimpleUI() {
    if (simpleUIPossibleNoThrow()) {
      simpleUI = true;
      setupRestrictions();
      fixChildrenForSimpleUI();
    } else {
      simpleUI = false;
    }
  }

  void fixChildrenForSimpleUI() {
    // remove hintpart (but keep its content)
    for (DaxeNode dn in childNodes) {
      if (dn.nodeType == DaxeNode.ELEMENT_NODE && dn.nodeName == 'hintpart') {
        removeChild(dn);
        // remove spaces inside this node
        normalize();
        if (firstChild is DNText && firstChild.nodeValue.trim() == '' &&
            firstChild.nextSibling == null)
          removeChild(firstChild);
        // remove left and right spaces inside hintpart
        if (dn.firstChild.nodeType == DaxeNode.TEXT_NODE) {
          if (dn.firstChild.nodeValue.trim() == '')
            dn.removeChild(dn.firstChild);
          else
            dn.firstChild.nodeValue = dn.firstChild.nodeValue.trimLeft();
        }
        if (dn.lastChild.nodeType == DaxeNode.TEXT_NODE) {
          if (dn.lastChild.nodeValue.trim() == '')
            dn.removeChild(dn.lastChild);
          else
            dn.lastChild.nodeValue = dn.lastChild.nodeValue.trimRight();
        }
        // append hintpart content
        for (DaxeNode dn2=dn.firstChild; dn2!=null; dn2=dn.firstChild) {
          dn.removeChild(dn2);
          appendChild(dn2);
        }
        break;
      }
    }
  }

}

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