-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjquery.nestedlist.js
1 lines (1 loc) · 2.76 KB
/
jquery.nestedlist.js
1
jQuery.fn.shift=function(t,n){var i=parseInt(this.css("left")),e=isNaN(i)?t:i+t;n?this.animate({left:e},n):this.css({left:e})},jQuery.fn.nestedList=function(t){function n(){return!1}function i(t,n){for(var i,e=t.find(n||"li:not(.leaf) > a:first-of-type"),o=e.length;o--;)i=e.eq(o),i.replaceWith(i.html())}function e(t,n){t.find(n||".leaf a:first-of-type").each(function(){var t=$(this);t.parents(".leaf:first").click(function(){return document.location.href=t.attr("href"),!1}).css("cursor","pointer")})}function o(o){function s(t,n){for(var i=n.children(),e=i.filter("li"),s=0,r=0;r<e.length;r+=1)s+=e.eq(r).outerHeight();return o.height(s),e}function l(){return u(),!1}function a(){d.off("click",l),d.click(n)}function f(t){v&&v.removeClass("active"),v=t?t.addClass("active"):!1}function c(n){if(nextList=n.children("ul"),t.onBeforeChange(1,nextList,o),f(n),nextList.length){nextList.show(),t.upButtonShow(d,nextList);var i=n.siblings(),e=nextList.find("li");i.length+1>e.length&&i.slice(-0,i.length-e.length).hide(),a(),t.shiftAnimation.complete=function(){d.click(l),t.onAfterChange(1,nextList,o)},o.shift(-L,t.shiftAnimation),s(o,nextList)}}function u(n){if(v){var i=v.children("ul"),e=v.parent("ul");return t.onBeforeChange(-1,e,o),v.siblings().show(),e.hasClass(p)?(f(!1),d.hide()):(t.upButtonShow(d,e),f(e.parent("li"))),s(o,e),a(),!n&&t.shiftAnimation?(t.shiftAnimation.complete=function(){d.click(l),i.hide(),t.onAfterChange(-1,e,o)},o.shift(L,t.shiftAnimation)):(o.shift(L,!1),i.hide()),!0}return!1}function h(t){var n=$(this);return n.hasClass("active")||o.is(":animated")?!1:(c(n),void t.stopPropagation())}var p=r();o.addClass(p);var d,v=!1;t&&t.upButtonSelector?(d="string"==typeof t.upButtonSelector?o.nextAll(t.upButtonSelector):t.upButtonSelector,d.hide()):(d=o.nextAll(".nestedlist-up"),1==d.length?d.remove():d=$("<li>back</li>").addClass("nestedlist-up"));var g={upButtonShow:function(t,n){n.prepend(t)},onBeforeChange:function(){},onAfterChange:function(){},shiftAnimation:{},noScriptLinks:!1,linkListItems:!0};t="object"==typeof t?$.extend(g,t):g;var L=o.outerWidth();return o.css({position:"relative"}),o.find("ul").css({position:"absolute",left:L+"px",top:"0px"}).hide(),o.find("li:not(:has(ul))").addClass("leaf").click(function(t){t.stopPropagation()}),o.find("li:not(.leaf)").hover(function(t){$(this).addClass("hover"),t.stopPropagation()},function(t){$(this).removeClass("hover"),t.stopPropagation()}).click(h),t.noScriptLinks&&i(o,"string"==typeof t.noScriptLinks&&t.noScriptLinks),t.linkListItems&&e(o,t&&t.leafItemLink),{container:o,toplevelClass:p,closeList:u,openList:c}}function s(){for(var t=0;t<l.length;t+=1)for(;l[t].closeList(!0););}var r=function(){var t=0;return function(){return t+=1,"nestedlist-"+t}}(),l=$.map(this,function(t){return o($(t))});return{nestedLists:l,closeAll:s}};