File:  [LON-CAPA] / modules / damieng / graphical_editor / loncapa_daxe / web / nodes / math_answer.dart
Revision 1.4: 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 MathResponse for simple UI.
 */
class MathAnswer extends LCDBlock {

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

  MathAnswer.fromNode(x.Node node, DaxeNode parent) : super.fromNode(node, parent) {
    if (parent is LCDBlock && parent.simpleUI)
      simpleUI = true;
    if (firstChild is DNText && firstChild.nextSibling == null) {
      String text = firstChild.nodeValue;
      // remove one newline after <![CDATA[ and before ]]>
      if (text.startsWith('\n'))
        text = text.substring(1);
      if (text.endsWith('\n'))
        text = text.substring(0, text.length - 1);
      firstChild.nodeValue = text;
    }
  }

  @override
  bool simpleUIPossible() {
    for (DaxeAttr att in attributes)
      throw new SimpleUIException('answer: ' + LCDStrings.get('attribute_problem') + att.name);
    // there should only be one text node
    for (DaxeNode dn in childNodes) {
      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)" : ''));
      }
    }
    if (firstChild != null && firstChild.nextSibling != null)
      return false;
    return true;
  }

  @override
  h.Element html() {
    simpleUI = parent is LCDBlock && (parent as LCDBlock).simpleUI;
    if (!simpleUI) {
      h.Element div = super.html();
      if (state != 2 && hasContent) {
        h.DivElement contents = div.lastChild;
        contents.style.fontFamily = 'monospace';
      }
      return div;
    }
    h.TableElement table = new h.TableElement();
    table.id = id;
    table.classes.add('math-answer');
    h.TableRowElement tr = new h.TableRowElement();
    h.TableCellElement td = new h.TableCellElement();
    h.SpanElement titleSpan = new h.SpanElement();
    titleSpan.appendText(LCDStrings.get('algorithm') + ' ');
    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));
  }

  @override
  void setupSimpleUI() {
    simpleUI = true;
  }

  /**
   * Override to use a CDATA section with added newlines.
   */
  @override
  x.Node toDOMNode(x.Document domDocument) {
    if (firstChild == null || firstChild is! DNText || firstChild.nextSibling != null)
      return super.toDOMNode(domDocument);
    String text = '\n' + firstChild.nodeValue + '\n';
    x.Element el = domDocument.createElementNS(namespaceURI, nodeName);
    for (DaxeAttr att in attributes)
      el.setAttributeNS(att.namespaceURI, att.name, att.value);
    x.CDATASection cdata = domDocument.createCDATASection(text);
    el.appendChild(cdata);
    return(el);
  }
}

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