// jquery menu tree plugin // // version 1.0 // // usage: $('.menutree').menutree( options, callback ) // // options: // menuevent - event to trigger expand/collapse; default = click // expandspeed - default = 500 (ms); use -1 for no animation // collapsespeed - default = 500 (ms); use -1 for no animation // expandeasing - easing function to use on expand (optional) // collapseeasing - easing function to use on collapse (optional) // multiopenedsubmenu - whether or not to limit to one opened menu at a time; default = false; // parentmenutriggercallback - whether or not parent menu triggers callback; default = false; // expandednode - default node to expand (optional) // // callback: // a callback function triggered with 'rel' parameter. // // history: // // 1.0 - first release (28 may 2009) // if (jquery) (function($) { $.extend($.fn, { menutree: function(o, callback) { // default parameters if (!o) var o = {}; o.data = this.html(); if (o.menuevent == undefined) o.menuevent = 'click'; if (o.expandspeed == undefined) o.expandspeed = 500; if (o.collapsespeed == undefined) o.collapsespeed = 500; if (o.expandeasing == undefined) o.expandeasing = null; if (o.collapseeasing == undefined) o.collapseeasing = null; if (o.multiopenedsubmenu == undefined) o.multiopenedsubmenu = false; if (o.parentmenutriggercallback == undefined) o.parentmenutriggercallback = false; if (o.expandednode == undefined) o.expandednode = null; $(this).each(function() { function bindtree(t) { var liclickedselector = callback != undefined ? 'li > i' : 'li.parent > i'; $(t).find(liclickedselector).bind(o.menuevent, function() { currentitem = $(this); if ($(this).parent().hasclass('parent')) { if ($(this).parent().hasclass('expanded')) { // collapse $(this).parent().find('ul').slideup({ duration: o.collapsespeed, easing: o.collapseeasing }); $(this).parent().removeclass('expanded').addclass('collapsed'); } else { // expand if (!o.multiopenedsubmenu) { $(this).parent().parent().find('ul').slideup({ duration: o.collapsespeed, easing: o.collapseeasing }); $(this).parent().parent().find('li.parent').removeclass('expanded').addclass('collapsed'); } $($(this).parent().find("ul")[0]).slidedown({ duration: o.expandspeed, easing: o.expandeasing }); $(this).parent().removeclass('collapsed').addclass('expanded'); $(this).parent().find('li.parent').removeclass('expanded').addclass('collapsed'); if (o.parentmenutriggercallback) callback($(this).attr('rel')); } } else { callback($(this).attr('rel')); } return false; }); // prevent a from triggering the # on non-click events if (o.menuevent.tolowercase != 'click') $(t).find(liclickedselector).bind('click', function() { return false; }); } // initialization $($(this)).find(":first").show(); bindtree($(this)); // expend default node if (o.expandednode) { var elementtoexpend = $($(this)).find("a[rel=" + o.expandednode + "]").parent().parent(); // collect all ul items that need to be extended var ulmenuelements = new array(); var i = 0; while (elementtoexpend && elementtoexpend.find('div').length == 0) { if (elementtoexpend[0].tagname == "ul") { ulmenuelements[i] = elementtoexpend; i++; } elementtoexpend = elementtoexpend.parent(); } ulmenuelements = ulmenuelements.reverse() // extend all collected item (recursive) var i = 0; var openmenu = function() { i++; // skip first ul(root) if (i >= ulmenuelements.length) return; ulmenuelements[i].removeclass('collapsed').addclass('expanded'); ulmenuelements[i].slidedown({ duration: o.expandspeed, easing: o.expandeasing, complete: openmenu }); } openmenu(ulmenuelements); } }); } }); })(jquery);