1 | /* this is not directly used - compress it as rvtree-min.js on http://javascriptcompressor.com/ or http://closure-compiler.appspot.com |
---|
2 | Based on Matt Kruse <matt@mattkruse.com> http://www.mattkruse.com/ */ |
---|
3 | var XElement = { |
---|
4 | hasClass: function(element, className) { |
---|
5 | if (!element) return false; |
---|
6 | var elementClassName = element.className; |
---|
7 | return ( elementClassName.length > 0 && (elementClassName == className || |
---|
8 | new RegExp("(^|\\s)" + className + "(\\s|$)").test(elementClassName)) ); |
---|
9 | }, |
---|
10 | |
---|
11 | addClass: function(element, className) { |
---|
12 | if (!element) return; |
---|
13 | if (!XElement.hasClass(element, className)) |
---|
14 | element.className += (element.className ? ' ' : '') + className; |
---|
15 | return element; |
---|
16 | }, |
---|
17 | |
---|
18 | toggleClasses: function(element, class1, class2) { |
---|
19 | if (!element) return; |
---|
20 | var classNames = element.className.split(/\s+/); |
---|
21 | var i1=-1, i2=-1; |
---|
22 | for (var i=0; i<classNames.length; i++) |
---|
23 | { |
---|
24 | if (classNames[i]==class1) i1=i; |
---|
25 | if (classNames[i]==class2) i2=i; |
---|
26 | } |
---|
27 | if (i1>=0) |
---|
28 | { |
---|
29 | classNames.splice(i1,1); |
---|
30 | if (i2>=i1) |
---|
31 | i2--; |
---|
32 | } |
---|
33 | else |
---|
34 | classNames.push(class1); |
---|
35 | |
---|
36 | if (i2>=0) |
---|
37 | classNames.splice(i2,1); |
---|
38 | else |
---|
39 | classNames.push(class2); |
---|
40 | element.className = classNames.join(' '); |
---|
41 | return element; |
---|
42 | } |
---|
43 | }; |
---|
44 | |
---|
45 | var RVTree = |
---|
46 | { |
---|
47 | options : |
---|
48 | { |
---|
49 | nodeClosedClass: "liClosed", |
---|
50 | nodeOpenClass: "liOpen", |
---|
51 | nodeBulletClass: "liBullet", |
---|
52 | nodeLinkClass: "bullet" |
---|
53 | }, |
---|
54 | |
---|
55 | convertTree: function (root) |
---|
56 | { |
---|
57 | if (!document.createElement) |
---|
58 | return; // Without createElement, we can't do anything |
---|
59 | if (window.attachEvent && !window.opera) |
---|
60 | { |
---|
61 | if ( new RegExp("MSIE ([0-9]{1,}[\.0-9]{0,})").exec( navigator.userAgent ) != null ) |
---|
62 | RVTree.options.IEVersion = parseFloat( RegExp.$1 ); |
---|
63 | } |
---|
64 | RVTree._processList(root, true); |
---|
65 | }, |
---|
66 | |
---|
67 | _processList: function(ul, isRoot) |
---|
68 | { |
---|
69 | if (!ul.childNodes || ul.childNodes.length==0) |
---|
70 | return; |
---|
71 | |
---|
72 | for (var i=0; i<ul.childNodes.length; i++) |
---|
73 | { //Iterate LIs |
---|
74 | var item = ul.childNodes[i]; |
---|
75 | if (item.nodeName != "LI") |
---|
76 | continue; |
---|
77 | |
---|
78 | // Iterate things in this LI |
---|
79 | var subLists = false; |
---|
80 | for (var sitemi=0;sitemi<item.childNodes.length;sitemi++) |
---|
81 | { |
---|
82 | var sitem = item.childNodes[sitemi]; |
---|
83 | if (sitem.nodeName=="UL") |
---|
84 | { |
---|
85 | subLists = true; |
---|
86 | RVTree._processList(sitem, false); |
---|
87 | } |
---|
88 | } |
---|
89 | |
---|
90 | var s= document.createElement("SPAN"); |
---|
91 | var t= '\u00A0'; // |
---|
92 | s.className = RVTree.options.nodeLinkClass; |
---|
93 | if (subLists) |
---|
94 | { |
---|
95 | // This LI has UL's in it, so it's a +/- node |
---|
96 | if ( !XElement.hasClass(item, RVTree.options.nodeOpenClass) ) |
---|
97 | XElement.addClass(item, RVTree.options.nodeClosedClass); |
---|
98 | |
---|
99 | // If it's just text, make the text work as the link also |
---|
100 | if (item.firstChild.nodeName=="#text") |
---|
101 | { |
---|
102 | t = t+item.firstChild.nodeValue; |
---|
103 | item.removeChild(item.firstChild); |
---|
104 | } |
---|
105 | s.onclick = function () |
---|
106 | { |
---|
107 | XElement.toggleClasses(this.parentNode, RVTree.options.nodeOpenClass, RVTree.options.nodeClosedClass ); |
---|
108 | return false; |
---|
109 | } |
---|
110 | } |
---|
111 | else |
---|
112 | {// No sublists |
---|
113 | if ( XElement.hasClass(item, RVTree.options.nodeClosedClass) ) |
---|
114 | { |
---|
115 | s.onclick = function() |
---|
116 | { |
---|
117 | var nodes = this.parentNode.getElementsByTagName("A"); |
---|
118 | if (nodes.length) |
---|
119 | document.location = nodes[0].href; |
---|
120 | } |
---|
121 | } |
---|
122 | else |
---|
123 | XElement.addClass(item, RVTree.options.nodeBulletClass); |
---|
124 | } |
---|
125 | |
---|
126 | item.style.listStyleType='none'; |
---|
127 | if (isRoot && RVTree.options.IEVersion && RVTree.options.IEVersion < 8) |
---|
128 | item.style.marginLeft="-16px"; // thats IE6,IE7 ; IE8 seems more compliant |
---|
129 | if (s) |
---|
130 | { |
---|
131 | s.appendChild(document.createTextNode(t)); |
---|
132 | item.insertBefore(s,item.firstChild); |
---|
133 | } |
---|
134 | } |
---|
135 | ul.style.marginLeft = 0; |
---|
136 | ul.style.paddingLeft = isRoot ? 0 : "8px"; |
---|
137 | } |
---|
138 | }; |
---|
139 | |
---|
140 | (function() { |
---|
141 | if (typeof _rvTreeAutoQueue != "undefined" && _rvTreeAutoQueue.length) |
---|
142 | { |
---|
143 | for (var i=0; i<_rvTreeAutoQueue.length; i++) |
---|
144 | RVTree.convertTree(_rvTreeAutoQueue[i]); |
---|
145 | } |
---|
146 | _rvTreeAutoQueue = { |
---|
147 | push: function(elt) { |
---|
148 | RVTree.convertTree(elt); |
---|
149 | } |
---|
150 | } |
---|
151 | })(); |
---|