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

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 {
1.6     ! damieng    24: 
1.1       damieng    25:   Hintgroup.fromRef(x.Element elementRef) : super.fromRef(elementRef) {
                     26:   }
1.6     ! damieng    27: 
1.1       damieng    28:   Hintgroup.fromNode(x.Node node, DaxeNode parent) : super.fromNode(node, parent) {
1.6     ! damieng    29:     if (parent is LCDBlock && parent.simpleUI)
1.1       damieng    30:       simpleUI = true;
                     31:   }
1.6     ! damieng    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:   }
1.6     ! damieng    54: 
1.2       damieng    55:   @override
                     56:   h.Element html() {
1.5       damieng    57:     simpleUI = simpleUIPossibleNoThrow() && parent is LCDBlock && (parent as LCDBlock).simpleUI;
1.2       damieng    58:     if (!simpleUI)
                     59:       return super.html();
                     60:     setupRestrictions();
1.4       damieng    61:     h.TableElement table = new h.TableElement();
                     62:     table.id = id;
                     63:     table.classes.add('hintgroup');
                     64:     h.TableRowElement tr = new h.TableRowElement();
                     65:     h.TableCellElement td = new h.TableCellElement();
1.2       damieng    66:     h.SpanElement titleSpan = new h.SpanElement();
                     67:     titleSpan.appendText(LCDStrings.get('hint') + ' ');
1.4       damieng    68:     td.append(titleSpan);
                     69:     tr.append(td);
                     70:     td = new h.TableCellElement();
                     71:     td.id = 'contents-' + id;
1.2       damieng    72:     h.SpanElement contentsSpan = new h.SpanElement();
                     73:     for (DaxeNode dn=firstChild; dn!= null; dn=dn.nextSibling) {
                     74:       contentsSpan.append(dn.html());
                     75:     }
1.4       damieng    76:     td.append(contentsSpan);
                     77:     tr.append(td);
                     78:     table.append(tr);
                     79:     return table;
1.2       damieng    80:   }
1.6     ! damieng    81: 
1.2       damieng    82:   @override
                     83:   h.Element getHTMLContentsNode() {
                     84:     if (!simpleUI)
                     85:       return super.getHTMLContentsNode();
1.4       damieng    86:     return(h.document.getElementById('contents-' + id));
1.2       damieng    87:   }
1.6     ! damieng    88: 
1.2       damieng    89:   void setupRestrictions() {
                     90:     if (restrictedInserts == null)
                     91:       restrictedInserts = SimpleUIText.possibleDescendants;
                     92:   }
1.6     ! damieng    93: 
1.2       damieng    94:   @override
                     95:   void setupSimpleUI() {
1.5       damieng    96:     if (simpleUIPossibleNoThrow()) {
                     97:       simpleUI = true;
                     98:       setupRestrictions();
                     99:       fixChildrenForSimpleUI();
                    100:     } else {
                    101:       simpleUI = false;
                    102:     }
1.2       damieng   103:   }
1.6     ! damieng   104: 
1.2       damieng   105:   void fixChildrenForSimpleUI() {
1.1       damieng   106:     // remove hintpart (but keep its content)
                    107:     for (DaxeNode dn in childNodes) {
                    108:       if (dn.nodeType == DaxeNode.ELEMENT_NODE && dn.nodeName == 'hintpart') {
                    109:         removeChild(dn);
                    110:         // remove spaces inside this node
                    111:         normalize();
                    112:         if (firstChild is DNText && firstChild.nodeValue.trim() == '' &&
                    113:             firstChild.nextSibling == null)
                    114:           removeChild(firstChild);
                    115:         // remove left and right spaces inside hintpart
                    116:         if (dn.firstChild.nodeType == DaxeNode.TEXT_NODE) {
                    117:           if (dn.firstChild.nodeValue.trim() == '')
                    118:             dn.removeChild(dn.firstChild);
                    119:           else
                    120:             dn.firstChild.nodeValue = dn.firstChild.nodeValue.trimLeft();
                    121:         }
                    122:         if (dn.lastChild.nodeType == DaxeNode.TEXT_NODE) {
                    123:           if (dn.lastChild.nodeValue.trim() == '')
                    124:             dn.removeChild(dn.lastChild);
                    125:           else
                    126:             dn.lastChild.nodeValue = dn.lastChild.nodeValue.trimRight();
                    127:         }
                    128:         // append hintpart content
                    129:         for (DaxeNode dn2=dn.firstChild; dn2!=null; dn2=dn.firstChild) {
                    130:           dn.removeChild(dn2);
                    131:           appendChild(dn2);
                    132:         }
                    133:         break;
                    134:       }
                    135:     }
                    136:   }
1.6     ! damieng   137: 
1.1       damieng   138: }

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