(function(undefined) { /* local var and method */ /* object prototype */ var treeRef_proto = { id: '', expandCollapse : function (domObj) { var oc = $(domObj).attr('src').indexOf('list-')<0?'open':'close'; var curLevel = parseInt($(domObj).attr('data-livello')); $(domObj).attr('src', $(domObj).attr('src').replace(oc=='open'?'list+':'list-', oc=='open'?'list-':'list+')); // ciclo sui sibling fino a che finiscono o ne incontro uno con livello inferiore o uguale $(domObj).parent().nextAll().each(function() { if($(this).get(0).nodeType == 1 && $(this).get(0).nodeName == 'LI') { if (parseInt($(this).attr('data-livello')) <= curLevel) return false; if (oc=='open' && parseInt($(this).attr('data-livello')) == (curLevel+1)) $(this).css('display', 'block'); if (oc=='close' && parseInt($(this).attr('data-livello')) > curLevel) { $(this).css('display', 'none'); $(this).find('> img').each(function() { $(this).attr('src', $(this).attr('src').replace('list-', 'list+')); }); } } }); return this; }, expandAll: function() { $("#"+this.id).find('li').each(function() { $(this).find('> img').each(function() { $(this).attr('src', $(this).attr('src').replace('list+', 'list-')); }); $(this).css('display', 'block'); }); }, collapseAll: function() { $("#"+this.id).find('li').each(function() { $(this).find('> img').each(function() { $(this).attr('src', $(this).attr('src').replace('list-', 'list+')); }); if($(this).attr('data-livello') != '0') $(this).css('display', 'none'); }); } } var treeRef = {}; /* public method and attribute */ /** * connect dom with tree function trought tag id */ $$.tree = function(treeID, config) { // tree in $$ global namespace config = $.extend(config||{}, { }); // creo un oggetto la prima volta, uso quello già definito nei riferimenti successivi if(treeRef[treeID] == undefined) { treeRef[treeID] = Object.create(treeRef_proto); treeRef[treeID].id = treeID; } return treeRef[treeID]; } })();