v-accordion.min.js 5.6 KB

123456789
  1. /**
  2. * vAccordion - AngularJS multi-level accordion component
  3. * @version v1.2.1
  4. * @link http://lukaszwatroba.github.io/v-accordion
  5. * @author Łukasz Wątroba <l@lukaszwatroba.com>
  6. * @license MIT License, http://www.opensource.org/licenses/MIT
  7. */
  8. !function(e){"use strict";function n(){return{restrict:"E",transclude:!0,controller:t,scope:{control:"=?",allowMultiple:"=?multiple",expandCb:"&?onexpand",collapseCb:"&?oncollapse"},link:function(n,t,o,a,i){function l(){e.forEach(d,function(e){if(n.control[e])throw new Error("The `"+e+"` method can not be overwritten")})}i(n.$parent,function(e){t.append(e)});var d=["toggle","expand","collapse","expandAll","collapseAll"];if(e.isDefined(n.allowMultiple)||(n.allowMultiple=e.isDefined(o.multiple)),o.$set("role","tablist"),n.allowMultiple&&o.$set("aria-multiselectable","true"),e.isDefined(n.control)){l();var c=e.extend({},n.internalControl,n.control);n.control=n.internalControl=c}else n.control=n.internalControl}}}function t(n){var t=this,o=!1;n.panes=[],t.hasExpandedPane=function(){for(var e=!1,t=0,o=n.panes.length;o>t;t++){var a=n.panes[t];if(a.isExpanded){e=!0;break}}return e},t.getPaneByIndex=function(e){return n.panes[e]},t.getPaneIndex=function(e){return n.panes.indexOf(e)},t.disable=function(){o=!0},t.enable=function(){o=!1},t.addPane=function(e){if(!n.allowMultiple&&t.hasExpandedPane()&&e.isExpanded)throw new Error("The `multiple` attribute can't be found");n.panes.push(e),e.isExpanded&&n.expandCb({index:t.getPaneIndex(e),target:e})},t.focusNext=function(){for(var e=n.panes.length,t=0;e>t;t++){var o=n.panes[t];if(o.isFocused){var a=t+1;a>n.panes.length-1&&(a=0);var i=n.panes[a];i.paneElement.find("v-pane-header")[0].focus();break}}},t.focusPrevious=function(){for(var e=n.panes.length,t=0;e>t;t++){var o=n.panes[t];if(o.isFocused){var a=t-1;0>a&&(a=n.panes.length-1);var i=n.panes[a];i.paneElement.find("v-pane-header")[0].focus();break}}},t.toggle=function(e){!o&&e&&(n.allowMultiple||t.collapseAll(e),e.isExpanded=!e.isExpanded,e.isExpanded?n.expandCb({index:t.getPaneIndex(e)}):n.collapseCb({index:t.getPaneIndex(e)}))},t.expand=function(e){!o&&e&&(n.allowMultiple||t.collapseAll(e),e.isExpanded||(e.isExpanded=!0,n.expandCb({index:t.getPaneIndex(e)})))},t.collapse=function(e){!o&&e&&e.isExpanded&&(e.isExpanded=!1,n.collapseCb({index:t.getPaneIndex(e)}))},t.expandAll=function(){if(!o){if(!n.allowMultiple)throw new Error("The `multiple` attribute can't be found");e.forEach(n.panes,function(e){t.expand(e)})}},t.collapseAll=function(a){o||e.forEach(n.panes,function(e){e!==a&&t.collapse(e)})},n.internalControl={toggle:function(e){t.toggle(t.getPaneByIndex(e))},expand:function(e){t.expand(t.getPaneByIndex(e))},collapse:function(e){t.collapse(t.getPaneByIndex(e))},expandAll:t.expandAll,collapseAll:t.collapseAll}}function o(){return{restrict:"E",require:"^vPane",transclude:!0,template:"<div ng-transclude></div>",scope:{},link:function(e,n,t){t.$set("role","tabpanel")}}}function a(){return{restrict:"E",require:["^vPane","^vAccordion"],transclude:!0,template:"<div ng-transclude></div>",scope:{},link:function(e,n,t,o){t.$set("role","tab");var a=o[0],i=o[1];n.on("click",function(){e.$apply(function(){a.toggle()})}),n[0].onfocus=function(){a.focusPane()},n[0].onblur=function(){a.blurPane()},n.on("keydown",function(n){32===n.keyCode||13===n.keyCode?(e.$apply(function(){a.toggle()}),n.preventDefault()):39===n.keyCode?(e.$apply(function(){i.focusNext()}),n.preventDefault()):37===n.keyCode&&(e.$apply(function(){i.focusPrevious()}),n.preventDefault())})}}}function i(n,t,o){return{restrict:"E",require:"^vAccordion",transclude:!0,controller:l,scope:{isExpanded:"=?expanded"},link:function(a,i,l,d,c){function r(){d.disable(),f[0].style.maxHeight="0px",u.attr({"aria-selected":"true",tabindex:"0"}),a.$emit("vAccordion:onExpand"),n(function(){t.addClass(i,p.expanded).then(function(){d.enable(),f[0].style.maxHeight="none",a.$emit("vAccordion:onExpandAnimationEnd")}),setTimeout(function(){f[0].style.maxHeight=x[0].offsetHeight+"px"},0)},0)}function s(){d.disable(),f[0].style.maxHeight=x[0].offsetHeight+"px",u.attr({"aria-selected":"false",tabindex:"-1"}),a.$emit("vAccordion:onCollapse"),n(function(){t.removeClass(i,p.expanded).then(function(){d.enable(),a.$emit("vAccordion:onCollapseAnimationEnd")}),setTimeout(function(){f[0].style.maxHeight="0px"},0)},0)}c(a.$parent,function(e){i.append(e)}),e.isDefined(a.isExpanded)||(a.isExpanded=e.isDefined(l.expanded));var p=o.states,u=i.find("v-pane-header"),f=i.find("v-pane-content"),x=f.find("div");if(!u[0])throw new Error("The `v-pane-header` directive can't be found");if(!f[0])throw new Error("The `v-pane-content` directive can't be found");d.addPane(a),a.paneElement=i,a.paneContentElement=f,a.paneInnerElement=x,a.accordionCtrl=d,a.isExpanded?(i.addClass(p.expanded),f[0].style.maxHeight="none",u.attr({"aria-selected":"true",tabindex:"0"})):(f[0].style.maxHeight="0px",u.attr({"aria-selected":"false",tabindex:"-1"})),a.$watch("isExpanded",function(e,n){return e===n?!0:(e?r():s(),void 0)})}}}function l(e){var n=this;n.toggle=function(){e.isAnimating||e.accordionCtrl.toggle(e)},n.focusPane=function(){e.isFocused=!0},n.blurPane=function(){e.isFocused=!1}}e.module("vAccordion.config",[]).constant("accordionConfig",{states:{expanded:"is-expanded"}}),e.module("vAccordion.directives",[]),e.module("vAccordion",["vAccordion.config","vAccordion.directives"]),e.module("vAccordion.directives").directive("vAccordion",n),t.$inject=["$scope"],e.module("vAccordion.directives").directive("vPaneContent",o),e.module("vAccordion.directives").directive("vPaneHeader",a),e.module("vAccordion.directives").directive("vPane",i),i.$inject=["$timeout","$animate","accordionConfig"],l.$inject=["$scope"]}(angular);