Annotation of modules/damieng/graphical_editor/loncapa_daxe/web/nodes/hintgroup.dart, revision 1.3

1.1       damieng     1: /*
                      2:   This file is part of LONCAPA-Daxe.
                      3: 
                      4:   LONCAPA-Daxe is free software: you can redistribute it and/or modify
                      5:   it under the terms of the GNU General Public License as published by
                      6:   the Free Software Foundation, either version 3 of the License, or
                      7:   (at your option) any later version.
                      8: 
                      9:   LONCAPA-Daxe is distributed in the hope that it will be useful,
                     10:   but WITHOUT ANY WARRANTY; without even the implied warranty of
                     11:   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
                     12:   GNU General Public License for more details.
                     13: 
                     14:   You should have received a copy of the GNU General Public License
                     15:   along with Daxe.  If not, see <http://www.gnu.org/licenses/>.
                     16: */
                     17: 
                     18: part of loncapa_daxe;
                     19: 
                     20: /**
                     21:  * This is used by RadioResponse for simple UI.
                     22:  */
                     23: class Hintgroup extends LCDBlock {
                     24:   
                     25:   Hintgroup.fromRef(x.Element elementRef) : super.fromRef(elementRef) {
                     26:   }
                     27:   
                     28:   Hintgroup.fromNode(x.Node node, DaxeNode parent) : super.fromNode(node, parent) {
                     29:     if (parent is LCDBlock && (parent as LCDBlock).simpleUI)
                     30:       simpleUI = true;
                     31:   }
                     32:   
1.2       damieng    33:   @override
1.1       damieng    34:   bool simpleUIPossible() {
1.3     ! damieng    35:     for (DaxeAttr att in attributes)
        !            36:       if (att.name != 'showoncorrect' || att.value != 'no')
        !            37:         throw new SimpleUIException('hintgroup: ' + LCDStrings.get('attribute_problem') + att.name);
1.1       damieng    38:     // check if there is a single hintpart with on="default" and simple content inside
                     39:     bool hintpartok = false;
                     40:     bool othernode = false;
                     41:     for (DaxeNode dn in childNodes) {
                     42:       if (dn.nodeType == DaxeNode.ELEMENT_NODE && dn.nodeName == 'hintpart' &&
                     43:           dn.getAttribute('on') == 'default' && dn.attributes.length == 1 &&
                     44:           SimpleUIText.checkNodeContents(dn))
                     45:         hintpartok = true;
                     46:       else if (dn.nodeType != DaxeNode.TEXT_NODE || dn.nodeValue.trim() != '')
                     47:         othernode = true;
                     48:     }
                     49:     if (hintpartok && !othernode)
                     50:       return true;
                     51:     // otherwise just check the contents
                     52:     return SimpleUIText.checkNodeContents(this);
                     53:   }
                     54:   
1.2       damieng    55:   @override
                     56:   h.Element html() {
                     57:     simpleUI = parent is LCDBlock && (parent as LCDBlock).simpleUI;
                     58:     if (!simpleUI)
                     59:       return super.html();
                     60:     setupRestrictions();
                     61:     h.DivElement div = new h.DivElement();
                     62:     div.id = id;
                     63:     h.SpanElement titleSpan = new h.SpanElement();
                     64:     titleSpan.appendText(LCDStrings.get('hint') + ' ');
                     65:     div.append(titleSpan);
                     66:     h.SpanElement contentsSpan = new h.SpanElement();
                     67:     for (DaxeNode dn=firstChild; dn!= null; dn=dn.nextSibling) {
                     68:       contentsSpan.append(dn.html());
                     69:     }
                     70:     div.append(contentsSpan);
                     71:     return div;
                     72:   }
                     73:   
                     74:   @override
                     75:   h.Element getHTMLContentsNode() {
                     76:     if (!simpleUI)
                     77:       return super.getHTMLContentsNode();
                     78:     return(getHTMLNode().nodes[1]);
                     79:   }
                     80:   
                     81:   void setupRestrictions() {
                     82:     if (restrictedInserts == null)
                     83:       restrictedInserts = SimpleUIText.possibleDescendants;
                     84:   }
                     85:   
                     86:   @override
                     87:   void setupSimpleUI() {
                     88:     simpleUI = true;
                     89:     setupRestrictions();
                     90:     fixChildrenForSimpleUI();
                     91:   }
                     92:   
                     93:   void fixChildrenForSimpleUI() {
1.1       damieng    94:     // remove hintpart (but keep its content)
                     95:     for (DaxeNode dn in childNodes) {
                     96:       if (dn.nodeType == DaxeNode.ELEMENT_NODE && dn.nodeName == 'hintpart') {
                     97:         removeChild(dn);
                     98:         // remove spaces inside this node
                     99:         normalize();
                    100:         if (firstChild is DNText && firstChild.nodeValue.trim() == '' &&
                    101:             firstChild.nextSibling == null)
                    102:           removeChild(firstChild);
                    103:         // remove left and right spaces inside hintpart
                    104:         if (dn.firstChild.nodeType == DaxeNode.TEXT_NODE) {
                    105:           if (dn.firstChild.nodeValue.trim() == '')
                    106:             dn.removeChild(dn.firstChild);
                    107:           else
                    108:             dn.firstChild.nodeValue = dn.firstChild.nodeValue.trimLeft();
                    109:         }
                    110:         if (dn.lastChild.nodeType == DaxeNode.TEXT_NODE) {
                    111:           if (dn.lastChild.nodeValue.trim() == '')
                    112:             dn.removeChild(dn.lastChild);
                    113:           else
                    114:             dn.lastChild.nodeValue = dn.lastChild.nodeValue.trimRight();
                    115:         }
                    116:         // append hintpart content
                    117:         for (DaxeNode dn2=dn.firstChild; dn2!=null; dn2=dn.firstChild) {
                    118:           dn.removeChild(dn2);
                    119:           appendChild(dn2);
                    120:         }
                    121:         break;
                    122:       }
                    123:     }
                    124:   }
                    125:   
                    126: }

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