/* file KLEIDER/web/src/kalender/treelist.js */ // -*- coding: utf-8 -*- /* Verschachtelte XHTML-Listen als Baum 2015-01-28 Herbert Schiemann */ // Konstruktor function Treelist () { // Elementname des "Containers", z.B. div, p, ol this.le = "ul"; // Elementname des "Knotens", z.B. p, a, span this.li = "li"; // Name des "Schalters" this.se = "span"; // Attributwort, das einen "eingefalteten" Eintrag (li) kennzeichnet this.kc = "c"; // Attributwort, das einen "expandierten" Eintrag (li) kennzeichnet this.kx = "x"; // Attributwort, das einen "unsensiblen" Blatt-Eintrag (li) kennzeichnet // oder leer this.kl = ""; // Default-Anzeige von faltbaren Einträgen (li) ohne class-Attribut // "x", "c" oder "" (irgend ein anderes Wort) this.df = "c"; // Name des Attributs, das den Zustand der Knoten kennzeichnet this.an = "class"; // Attributwort, das "Nicht-Knoten" kennzeichnet this.kn = "nofld"; // Attributwort, das einen "Toplevel-Container" kennzeichnet this.kt = "tree"; // Ereignis, auf das der "Schalter" reagiert. this.ev = "click"; } // Treelist Treelist.prototype.mk = function (l) { var rc = new RegExp ("\\b" + this.kc + "\\b"); var rx = new RegExp ("\\b" + this.kx + "\\b"); var rl = new RegExp ("\\b" + this.kl + "\\b"); var rn = new RegExp ( "\\b(?:" + this.kn + "|" + this.kt + ")\\b" ); var s = this; var c; var cl; var hnd = function (e) { c = e.currentTarget; while (c && c.nodeType != Node.ELEMENT_NODE) c = c.parentNode; if (!c || c.localName == "a") return; while (c && c.localName != s.li) c = c.parentNode; if (!c) return; cl = c.getAttribute (s.an); c.setAttribute ( s.an, !cl || cl == "" ? cl = s.kc : cl.search (rc) >= 0 ? cl.replace (rc, s.kx) : cl.search (rx) >= 0 ? cl.replace (rx, s.kc) : cl + " " + s.kc ); }; var es; // erwarte "sensitives" Kindelement des Elements es var mt = function () { es = null; for ( c = l.firstChild; c && c != l; c = n ) { n = null; if (c.nodeType == Node.ELEMENT_NODE) { // "sensitives Element" if (es && c.localName == s.se) { c.addEventListener (s.ev, hnd, 1); if (s.df) { if (!es.getAttribute (s.an)) es.setAttribute (s.an, s.df); } es = null; } // Knoten oder Container else if (c.localName == s.li || c.localName == s.le) { cl = c.getAttribute (s.an); if (!cl || cl.search (rn) < 0) { if (es && s.kl) { // Knoten als nicht faltbar kennzeichnen cl = es.getAttribute (s.an); if (!cl || cl.search (rl) < 0) es.setAttribute (s.an, cl ? cl + " " + s.kl : s.kl); } if (c.localName == s.li) es = c; // es = c.localName == s.li ? c : null; n = c.firstChild; } } } if (!n) { n = c.nextSibling; if (!n && es) { if (s.kl) { // Knoten als nicht faltbar kennzeichnen cl = es.getAttribute (s.an); if (!cl || cl.search (rl) < 0) es.setAttribute (s.an, cl ? cl + " " + s.kl : s.kl); } es = null; } } while (!n) { c = c.parentNode ; n = c.nextSibling ; } } }; // mt if (l) mt (); else { var i; var nl = document.getElementsByTagName (s.le); for (i = 0; i < nl.length; ++i) { l = nl [i]; if (l.getAttribute (s.an) == this.kt) mt (); } } }; // Treelist.prototype.mk onload = function () { new Treelist () .mk (); }; /* end of file KLEIDER/web/src/kalender/treelist.js */