Rev 8588 | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 8588 | Rev 18309 | ||
---|---|---|---|
Line 7... | Line 7... | ||
7 | 7 | ||
8 | /* |
8 | /* |
9 | * Initialise all trees identified by <ul class="tree"> |
9 | * Initialise all trees identified by <ul class="tree"> |
10 | */ |
10 | */ |
11 | 11 | ||
12 | /* |
12 | /** |
13 | * Usefull function for popup window displaying |
13 | * Usefull function for popup window displaying |
- | 14 | * @param {*} mylink |
|
- | 15 | * @param {*} windowname |
|
- | 16 | * @returns |
|
14 | */ |
17 | */ |
15 | function popup(mylink, windowname){ |
18 | function popup(mylink, windowname){ |
16 | if (! window.focus)return true; |
19 | if (! window.focus)return true; |
17 | var href; |
20 | var href; |
18 | if (typeof(mylink) == 'string') |
21 | if (typeof(mylink) == 'string') |
Line 21... | Line 24... | ||
21 | href=mylink.href; |
24 | href=mylink.href; |
22 | window.open(href, windowname, 'width=800,height=500,scrollbars=yes'); |
25 | window.open(href, windowname, 'width=800,height=500,scrollbars=yes'); |
23 | return false; |
26 | return false; |
24 | } |
27 | } |
25 | 28 | ||
- | 29 | /** |
|
- | 30 | * autoInit_trees |
|
- | 31 | */ |
|
26 | function autoInit_trees() { |
32 | function autoInit_trees() { |
- | 33 | //console.log("autoInit_trees"); |
|
27 |
|
34 | var candidates = document.getElementsByTagName('ul'); |
28 |
|
35 | for(var i=0;i<candidates.length;i++) { |
29 |
|
36 | if(candidates[i].className && candidates[i].className.indexOf('tree') != -1) { |
30 |
|
37 | initTree(candidates[i]); |
31 |
|
38 | candidates[i].className = candidates[i].className.replace(/ ?unformatted ?/, ' '); |
32 |
|
39 | } |
33 |
|
40 | } |
34 | } |
41 | } |
35 | 42 | ||
36 | /* |
43 | /** |
37 | * Initialise a tree node, converting all its LIs appropriately |
44 | * Initialise a tree node, converting all its LIs appropriately |
- | 45 | * @param {*} el |
|
- | 46 | * @returns |
|
38 | */ |
47 | */ |
39 | function initTree(el) { |
48 | function initTree(el) { |
- | 49 | //console.log("initTree"); |
|
40 |
|
50 | var i,j; |
41 |
|
51 | var spanA, spanB, spanC; |
42 |
|
52 | var startingPoint, stoppingPoint, childUL; |
43 | 53 | ||
44 |
|
54 | // Find all LIs to process |
45 |
|
55 | for(i=0;i<el.childNodes.length;i++) { |
46 |
|
56 | if(el.childNodes[i].tagName && el.childNodes[i].tagName.toLowerCase() == 'li') { |
47 |
|
57 | var li = el.childNodes[i]; |
48 | 58 | ||
49 |
|
59 | // Create our extra spans |
50 |
|
60 | spanA = document.createElement('span'); |
51 |
|
61 | spanB = document.createElement('span'); |
52 |
|
62 | spanC = document.createElement('span'); |
53 |
|
63 | spanA.appendChild(spanB); |
54 |
|
64 | spanB.appendChild(spanC); |
55 |
|
65 | spanA.className = 'a ' + li.className.replace('closed','spanClosed'); |
56 |
|
66 | spanA.onMouseOver = function() {} |
57 |
|
67 | spanB.className = 'b'; |
58 |
|
68 | spanB.onclick = treeToggle; |
59 |
|
69 | spanC.className = 'c'; |
60 | - | ||
61 | 70 | ||
62 |
|
71 | // Find the UL within the LI, if it exists |
63 |
|
72 | stoppingPoint = li.childNodes.length; |
64 |
|
73 | startingPoint = 0; |
65 |
|
74 | childUL = null; |
66 |
|
75 | for(j=0;j<li.childNodes.length;j++) { |
67 |
|
76 | if(li.childNodes[j].tagName && li.childNodes[j].tagName.toLowerCase() == 'div') { |
68 |
|
77 | startingPoint = j + 1; |
69 |
|
78 | continue; |
70 |
|
79 | } |
71 | 80 | ||
72 |
|
81 | if(li.childNodes[j].tagName && li.childNodes[j].tagName.toLowerCase() == 'ul') { |
73 |
|
82 | childUL = li.childNodes[j]; |
74 |
|
83 | stoppingPoint = j; |
75 |
|
84 | break; |
76 |
|
85 | } |
77 |
|
86 | } |
78 | 87 | ||
79 |
|
88 | // Move all the nodes up until that point into spanC |
80 |
|
89 | for(j=startingPoint;j<stoppingPoint;j++) { |
81 |
|
90 | spanC.appendChild(li.childNodes[startingPoint]); |
82 |
|
91 | } |
83 | 92 | ||
84 |
|
93 | // Insert the outermost extra span into the tree |
- | 94 | if(li.childNodes.length > startingPoint) { |
|
85 |
|
95 | li.insertBefore(spanA, li.childNodes[startingPoint]); |
- | 96 | } else { |
|
86 |
|
97 | li.appendChild(spanA); |
- | 98 | } |
|
87 | 99 | ||
88 |
|
100 | // Process the children |
89 |
|
101 | if(childUL != null) { |
90 |
|
102 | if(initTree(childUL)) { |
91 |
|
103 | li.classList.add('children'); |
92 |
|
104 | spanA.classList.add('children'); |
93 |
|
105 | } |
94 |
|
106 | } |
95 |
|
107 | } |
96 |
|
108 | } |
97 | 109 | ||
98 |
|
110 | if(li) { |
99 |
|
111 | // li and spanA will still be set to the last item |
100 | - | ||
101 |
|
112 | li.classList.add('last'); |
102 |
|
113 | spanA.classList.add('last'); |
103 |
|
114 | return true; |
104 |
|
115 | } else { |
105 |
|
116 | return false; |
106 |
|
117 | } |
107 | - | ||
108 | } |
118 | } |
109 | 119 | ||
110 | 120 | /** |
|
- | 121 | * +/- toggle the tree |
|
111 |
|
122 | * |
112 | * |
123 | * @param {*} el the <span class="b"> node |
113 | * |
124 | * @param {*} force will force it to "open" or "close" |
114 | */ |
125 | */ |
115 | function treeToggle(el, force) { |
126 | function treeToggle(el, force) { |
- | 127 | //console.log("treeToggle"); |
|
116 |
|
128 | el = this; |
117 |
|
129 | while(el != null && (!el.tagName || el.tagName.toLowerCase() != "li")) el = el.parentNode; |
118 | 130 | ||
119 |
|
131 | // Get UL within the LI |
120 |
|
132 | var childSet = findChildWithTag(el, 'ul'); |
121 |
|
133 | var topSpan = findChildWithTag(el, 'span'); |
122 | 134 | ||
123 |
|
135 | if( force != null ){ |
124 | - | ||
125 |
|
136 | if( force == "open"){ |
126 |
|
137 | treeOpen( topSpan, el ) |
127 | } |
- | |
128 |
|
138 | } else if( force == "close" ){ |
129 |
|
139 | treeClose( topSpan, el ) |
130 |
|
140 | } |
131 | - | ||
132 | } |
- | |
133 | - | ||
134 |
|
141 | } else if( childSet != null) { |
135 |
|
142 | // Is open, close it |
136 |
|
143 | if(!el.className.match(/(^| )closed($| )/)) { |
137 |
|
144 | treeClose( topSpan, el ) |
138 |
|
145 | // Is closed, open it |
139 |
|
146 | } else { |
140 |
|
147 | treeOpen( topSpan, el ) |
141 |
|
148 | } |
142 |
|
149 | } |
143 | } |
150 | } |
144 | 151 | ||
- | 152 | /** |
|
- | 153 | * treeOpen |
|
- | 154 | * @param {*} a |
|
- | 155 | * @param {*} b |
|
145 | 156 | */ |
|
146 | function treeOpen(a, b ){ |
157 | function treeOpen(a, b ){ |
- | 158 | //console.log("treeOpen"); |
|
147 |
|
159 | a.classList.remove('spanClosed'); |
148 |
|
160 | b.classList.remove('closed'); |
149 | } |
161 | } |
150 | 162 | ||
151 | /* |
163 | /** |
152 | * [Jquery] treeToggleAll : Open/close all tree inside "elem" |
164 | * * [Jquery] treeToggleAll : Open/close all tree inside "elem" |
153 | * elem can be a css id (#ident) or a css class (.classname) |
165 | * elem can be a css id (#ident) or a css class (.classname) |
154 | * added by obado |
166 | * added by obado |
- | 167 | * @param {*} elem |
|
155 | */ |
168 | */ |
156 | function treeToggleAll( elem ){ |
169 | function treeToggleAll( elem ){ |
- | 170 | //console.log("treeToggleAll"); |
|
157 |
|
171 | //$(elem+" .children").toggleClass("closed"); |
- | 172 | if ($(elem).hasClass("all_open")){ |
|
- | 173 | $(elem+" .children").addClass("closed"); |
|
- | 174 | $(elem).removeClass("all_open"); |
|
- | 175 | } |
|
- | 176 | else{ |
|
- | 177 | $(elem+" .children").removeClass("closed"); |
|
- | 178 | $(elem).addClass("all_open"); |
|
- | 179 | } |
|
158 | } |
180 | } |
159 | 181 | ||
- | 182 | /** |
|
- | 183 | * treeClose |
|
- | 184 | * @param {*} a |
|
- | 185 | * @param {*} b |
|
160 | 186 | */ |
|
161 | function treeClose(a, b ){ |
187 | function treeClose(a, b ){ |
- | 188 | //console.log("treeClose"); |
|
162 |
|
189 | a.classList.add('spanClosed'); |
163 |
|
190 | b.classList.add('closed'); |
164 | } |
191 | } |
165 | 192 | ||
166 | /* |
193 | /** |
167 | * Find the a child of el of type tag |
194 | * Find the a child of el of type tag |
- | 195 | * @param {*} el |
|
- | 196 | * @param {*} tag |
|
- | 197 | * @returns |
|
168 | */ |
198 | */ |
169 | function findChildWithTag(el, tag) { |
199 | function findChildWithTag(el, tag) { |
- | 200 | //console.log("findChildWithTag"); |
|
170 |
|
201 | for(var i=0;i<el.childNodes.length;i++) { |
171 |
|
202 | if(el.childNodes[i].tagName != null && el.childNodes[i].tagName.toLowerCase() == tag) return el.childNodes[i]; |
172 |
|
203 | } |
173 |
|
204 | return null; |
174 | } |
205 | } |
175 | 206 | ||
176 | /* |
207 | /** |
177 | * Functions to add and remove class names |
208 | * Functions to add and remove class names |
178 | * Mac IE hates unnecessary spaces |
209 | * Mac IE hates unnecessary spaces |
- | 210 | * @param {*} el |
|
- | 211 | * @param {*} cls |
|
- | 212 | * @param {*} forceBefore |
|
179 | */ |
213 | */ |
180 | function addClass(el, cls, forceBefore) { |
214 | function addClass(el, cls, forceBefore) { |
- | 215 | //console.log(`addClass ${cls}`); |
|
181 |
|
216 | if(forceBefore != null && el.className.match(new RegExp('(^| )' + forceBefore))) { |
182 |
|
217 | el.className = el.className.replace(new RegExp("( |^)" + forceBefore), '$1' + cls + ' ' + forceBefore); |
183 | 218 | ||
184 |
|
219 | } else if(!el.className.match(new RegExp('(^| )' + cls + '($| )'))) { |
185 |
|
220 | el.className += ' ' + cls; |
186 |
|
221 | el.className = el.className.replace(/(^ +)|( +$)/g, ''); |
187 |
|
222 | } |
188 | } |
223 | } |
- | 224 | ||
- | 225 | /** |
|
- | 226 | * removeClass |
|
- | 227 | * @param {*} el |
|
- | 228 | * @param {*} cls |
|
- | 229 | */ |
|
189 | function removeClass(el, cls) { |
230 | function removeClass(el, cls) { |
- | 231 | //console.log("removeClass"); |
|
190 |
|
232 | var old = el.className; |
191 |
|
233 | var newCls = ' ' + el.className + ' '; |
192 |
|
234 | newCls = newCls.replace(new RegExp(' (' + cls + ' +)+','g'), ' '); |
193 |
|
235 | el.className = newCls.replace(/(^ +)|( +$)/g, ''); |
194 | } |
236 | } |
195 | 237 | ||
196 | /* |
238 | /* |
197 | * Handlers for automated loading |
239 | * Handlers for automated loading |
198 | */ |
240 | */ |
199 |
|
241 | _LOADERS = Array(); |
200 | 242 | ||
- | 243 | /** |
|
- | 244 | * callAllLoaders |
|
- | 245 | */ |
|
201 | function callAllLoaders() { |
246 | function callAllLoaders() { |
202 |
|
247 | var i, loaderFunc; |
203 |
|
248 | for(i=0;i<_LOADERS.length;i++) { |
204 |
|
249 | loaderFunc = _LOADERS[i]; |
205 |
|
250 | if(loaderFunc != callAllLoaders) loaderFunc(); |
206 |
|
251 | } |
207 | } |
252 | } |
208 | 253 | ||
- | 254 | /** |
|
- | 255 | * appendLoader |
|
- | 256 | * @param {*} loaderFunc |
|
- | 257 | */ |
|
209 | function appendLoader(loaderFunc) { |
258 | function appendLoader(loaderFunc) { |
210 |
|
259 | if(window.onload && window.onload != callAllLoaders) |
211 |
|
260 | _LOADERS[_LOADERS.length] = window.onload; |
212 | 261 | ||
213 |
|
262 | window.onload = callAllLoaders; |
214 | 263 | ||
215 |
|
264 | _LOADERS[_LOADERS.length] = loaderFunc; |
216 | } |
265 | } |
217 | 266 | ||
218 | appendLoader(autoInit_trees); |
267 | appendLoader(autoInit_trees); |