File:  [LON-CAPA] / modules / damieng / graphical_editor / loncapa_daxe / web / nodes / textline.dart
Revision 1.8: download - view: text, annotated - select for diffs
Tue Feb 28 20:40:59 2017 UTC (7 years, 8 months ago) by damieng
Branches: MAIN
CVS tags: HEAD
updates following changes in Daxe Config API

/*
  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 the textline element, for simple UI (the textline size is displayed like an attribute).
 * Jaxe display type: 'textline'.
 */
class Textline extends LCDBlock {
  
  Textline.fromRef(x.Element elementRef) : super.fromRef(elementRef);
  
  Textline.fromNode(x.Node node, DaxeNode parent) : super.fromNode(node, parent) {
    if (simpleParent())
      simpleUI = true;
  }
  
  @override
  bool simpleUIPossible() {
    if (attributes.length != 0) {
      // accept any size and readonly=no
      // accept spellcheck only with value 'none'
      for (DaxeAttr att in attributes) {
        if (!((att.name == 'size' && parent is! OrganicResponse) ||
            (att.name == 'readonly' && (att.value == 'no' || parent is OrganicResponse ||
            parent is ReactionResponse)) ||
            (att.name == 'spellcheck' && (att.value == '' || att.value == 'none')))) {
          throw new SimpleUIException('textline: ' + LCDStrings.get('attribute_problem') + att.name);
        }
      }
    }
    if (firstChild != null)
      return false;
    return true;
  }
  
  bool simpleParent() {
    return (parent is LCDBlock && (parent as LCDBlock).simpleUI) &&
        (parent is NumericalResponse || parent is FormulaResponse || parent is StringResponse ||
            parent is MathResponse || parent is OrganicResponse || parent is ReactionResponse);
  }
  
  @override
  h.Element html() {
    simpleUI = simpleParent();
    if (!simpleUI)
      return super.html();
    
    // the whole textline node is reduced to its size attribute
    // or readonly attribute for organicresponse
    // or size+readonly for reactionresponse
    h.TableElement table = new h.TableElement();
    if (parent is! OrganicResponse) {
      x.Element sizeRef = null;
      for (x.Element refAttr in attRefs) {
        String name = doc.cfg.attributeName(refAttr);
        if (name == 'size') {
          sizeRef = refAttr;
          break;
        }
      }
      assert(sizeRef != null);
      
      h.TableRowElement tr = attributeHTML(sizeRef);
      tr.id = id;
      tr.childNodes[1].text = LCDStrings.get('field_size');
      table.append(tr);
    }
    if (parent is OrganicResponse || parent is ReactionResponse) {
      x.Element readonlyRef = null;
      for (x.Element refAttr in attRefs) {
        String name = doc.cfg.attributeName(refAttr);
        if (name == 'readonly') {
          readonlyRef = refAttr;
          break;
        }
      }
      assert(readonlyRef != null);
      
      h.TableRowElement tr = attributeHTML(readonlyRef);
      tr.id = id;
      tr.childNodes[1].text = LCDStrings.get('readonly_field');
      table.append(tr);
    }
    if (table.childNodes.length == 1)
      return table.firstChild;
    else
      return(table);
  }
  
  @override
  void updateAttributes() {
    if (!simpleUI) {
      super.updateAttributes();
      return;
    }
    parent.updateHTML();
  }
}

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