aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSilvio <s1lv10@uol.com.br>2009-10-02 18:06:13 -0300
committerSilvio <s1lv10@uol.com.br>2009-10-02 18:06:13 -0300
commit13f0d19c42dc38b35c4ab334c6c9e1f0355ae0cc (patch)
tree2878539965041bf53cfcf018e6fd523b4889c549
parent4474f82dfa5c4bd05b9263766d520b540b079521 (diff)
downloadfinder_menu-13f0d19c42dc38b35c4ab334c6c9e1f0355ae0cc.tar.gz
finder_menu-13f0d19c42dc38b35c4ab334c6c9e1f0355ae0cc.tar.bz2
Changing approach
-rw-r--r--finder_menu.js224
1 files changed, 103 insertions, 121 deletions
diff --git a/finder_menu.js b/finder_menu.js
index ce8e6a9..b6b25db 100644
--- a/finder_menu.js
+++ b/finder_menu.js
@@ -6,126 +6,108 @@
*/
function ul2finder()
{
- // Define variables used and classes to be applied/removed
- var i,uls,als,finder;
- var parentClass='parent';
- var showClass='shown';
- var hideClass='hidden';
- var openClass='open';
+ // Define variables used and classes to be applied/removed
+ var i,uls,als,finder;
+ var parentClass='parent';
+ var showClass='shown';
+ var hideClass='hidden';
+ var openClass='open';
window.finderOpened = new Object();
- // check if our finder list exists, if not, stop all activities
- finder=document.getElementById('finder');
- if(!finder){return;}
+ // check if our finder list exists, if not, stop all activities
+ finder=document.getElementById('finder');
+ if(!finder){return;}
- // add the class domenabled to the body
- cssjs('add',document.body,'domenabled')
+ // add the class domenabled to the body
+ cssjs('add',document.body,'domenabled');
- // loop through all lists inside finder, position and hide them
- // by applying the class hidden
- uls=document.getElementById('finder').getElementsByTagName('ul');
- for(i=0;i<uls.length;i++)
- {
- cssjs('add',uls[i],hideClass);
- }
+ // loop through all lists inside finder, position and hide them
+ // by applying the class hidden
+ uls=finder.getElementsByTagName('ul');
+ for(i=0;i<uls.length;i++)
+ {
+ cssjs('add',uls[i],hideClass);
+ }
+
+ // loop through all links of inside finder
+ lis=finder.getElementsByTagName('li');
+ for(i=0;i<lis.length;i++)
+ {
+ // if the li containing the link has no nested list, skip this one
+ if(!lis[i].getElementsByTagName('ul')[0])
+ {
+ continue;
+ }
+ var newa=document.createElement('a');
+ newa.href='#';
+ newa.appendChild(document.createTextNode(lis[i].firstChild.nodeValue));
+ lis[i].replaceChild(newa,lis[i].firstChild);
+ // otherwise apply the parent class
+ cssjs('add',newa,parentClass);
- // loop through all links of inside finder
- lis=document.getElementById('finder').getElementsByTagName('li');
- for(i=0;i<lis.length;i++)
- {
- // if the li containing the link has no nested list, skip this one
- if(!lis[i].getElementsByTagName('ul')[0])
- {
- continue;
- }
- var newa=document.createElement('a');
- newa.href='#';
- newa.appendChild(document.createTextNode(lis[i].firstChild.nodeValue));
- lis[i].replaceChild(newa,lis[i].firstChild);
- // otherwise apply the parent class
- cssjs('add',newa,parentClass);
-
// setup the link
ref = lis[i].getElementsByTagName('a')[0];
ref.id = 'finder[' + i + ']';
window.finderOpened[ref.id] = false;
- // if the user clicks on the link
- ref.onclick=function()
- {
+ // if the user clicks on the link
+ ref.onclick=function()
+ {
// loop through all lists inside finder
- for(var i=0;i<uls.length;i++)
- {
- // avoid the list connected to this link
- var found=false;
- for(j=0;j<uls[i].getElementsByTagName('ul').length;j++)
- {
- if(uls[i].getElementsByTagName('ul')[j] ==
- this.parentNode.getElementsByTagName('ul')[0])
- {
- found=true;
- break;
- }
- }
+ for(var i=0;i<uls.length;i++)
+ {
+ // check if it's connected to this link
+ var found=false;
+ for(j=0;j<uls[i].getElementsByTagName('ul').length;j++)
+ {
+ if(uls[i].getElementsByTagName('ul')[j] ==
+ this.parentNode.getElementsByTagName('ul')[0])
+ {
+ found=true;
+ break;
+ }
+ }
- // and hide all others
- if(!found)
- {
- cssjs('add',uls[i],hideClass)
- cssjs('remove',uls[i],showClass)
- cssjs('remove',uls[i].parentNode.getElementsByTagName('a')[0],openClass)
- cssjs('add',uls[i].parentNode.getElementsByTagName('a')[0],parentClass)
- }
- }
+ // hide elements
+ if(!found || window.finderOpened[this.id] == true)
+ {
+ ref = uls[i].parentNode.getElementsByTagName('a')[0];
+ if (ref != this) {
+ window.finderOpened[ref.id] = false;
+ ref.innerHTML = false;
+ cssjs('add',uls[i],hideClass);
+ cssjs('remove',uls[i],showClass);
+ cssjs('remove',ref,openClass);
+ cssjs('add',ref,parentClass);
+ }
+ }
+ }
+ uls = this.parentNode.getElementsByTagName('ul');
if (window.finderOpened[this.id] == false) {
window.finderOpened[this.id] = true;
// change the current link from parent to open
// and show the current nested list
cssjs('remove',this,parentClass);
cssjs('add',this,openClass);
- cssjs('add',this.parentNode.getElementsByTagName('ul')[0],showClass);
-
- // unset window.finderNode for all other nodes that aren't children or parent
- children = this.parentNode.childNodes;
- refs = document.getElementById('finder').getElementsByTagName('a');
- for (var i=0;i<refs.length;i++) {
- found = false;
- if (refs[i] == this) {
- found = true;
- } else if (isparent(refs[i].parentNode, this)) {
- found = true;
- } else {
- for(var j=0;j<children.lenght;j++) {
- if (refs[i] == children[j]) {
- found = true;
- break;
- }
- }
- }
- if (!found) {
- window.finderOpened[refs[i].id] = false;
- refs[i].innerHTML = window.finderOpened[refs[i].id];
- }
- }
+ cssjs('add',uls[0],showClass);
} else {
window.finderOpened[this.id] = false;
// change the current link from open to parent
// and hide the current nested list
cssjs('add',this,parentClass);
cssjs('remove',this,openClass);
- children = this.parentNode.getElementsByTagName('ul');
- for(var j=0;j<children.lenght;j++) {
- cssjs('remove',children[j],showClass);
- children[j].innerHTML = window.finderOpened[refs[i].id];
+ // TODO: check why it's not iterating
+ for(i=0;i<uls.lenght;i++) {
+ cssjs('remove',uls[i],showClass);
}
}
this.innerHTML = window.finderOpened[this.id];
- // don't follow the real HREF of the link
- return false;
- }
- }
+ // don't follow the real HREF of the link
+ return false;
+ }
+ }
function isparent(par, child) {
if (par.hasChildNodes()) {
children = par.childNodes;
@@ -139,39 +121,39 @@ function ul2finder()
}
return false;
}
- /*
- * cssjs
- * written by Christian Heilmann (http://icant.co.uk)
- * eases the dynamic application of CSS classes via DOM
- * parameters: action a, object o and class names c1 and c2 (c2 optional)
- * actions: swap exchanges c1 and c2 in object o
- * add adds class c1 to object o
- * remove removes class c1 from object o
- * check tests if class c1 is applied to object o
- * example: cssjs('swap',document.getElementById('foo'),'bar','baz');
- */
- function cssjs(a,o,c1,c2)
- {
- switch (a){
- case 'swap':
- o.className=!cssjs('check',o,c1)?o.className.replace(c2,c1):o.className.replace(c1,c2);
- break;
- case 'add':
- if(!cssjs('check',o,c1)){o.className+=o.className?' '+c1:c1;}
- break;
- case 'remove':
- var rep=o.className.match(' '+c1)?' '+c1:c1;
- o.className=o.className.replace(rep,'');
- break;
- case 'check':
- return new RegExp('\\b'+c1+'\\b').test(o.className)
- break;
- }
- }
+ /*
+ * cssjs
+ * written by Christian Heilmann (http://icant.co.uk)
+ * eases the dynamic application of CSS classes via DOM
+ * parameters: action a, object o and class names c1 and c2 (c2 optional)
+ * actions: swap exchanges c1 and c2 in object o
+ * add adds class c1 to object o
+ * remove removes class c1 from object o
+ * check tests if class c1 is applied to object o
+ * example: cssjs('swap',document.getElementById('foo'),'bar','baz');
+ */
+ function cssjs(a,o,c1,c2)
+ {
+ switch (a){
+ case 'swap':
+ o.className=!cssjs('check',o,c1)?o.className.replace(c2,c1):o.className.replace(c1,c2);
+ break;
+ case 'add':
+ if(!cssjs('check',o,c1)){o.className+=o.className?' '+c1:c1;}
+ break;
+ case 'remove':
+ var rep=o.className.match(' '+c1)?' '+c1:c1;
+ o.className=o.className.replace(rep,'');
+ break;
+ case 'check':
+ return new RegExp('\\b'+c1+'\\b').test(o.className)
+ break;
+ }
+ }
}
// Check if the browser supports DOM, and start the script if it does.
if(document.getElementById && document.createTextNode)
{
- window.onload=ul2finder;
+ window.onload=ul2finder;
}