UNPKG

@nitro-ui/component-collapse

Version:

Collapse & Accordian styling for Nitro UI.

1 lines 7.73 kB
"use strict";function _interopDefault(e){return e&&"object"==typeof e&&"default"in e?e.default:e}var $=_interopDefault(require("jquery"));const TRANSITION_END="transitionend",MAX_UID=1e6,MILLISECONDS_MULTIPLIER=1e3;function toType(e){return{}.toString.call(e).match(/\s([a-z]+)/i)[1].toLowerCase()}function getSpecialTransitionEndEvent(){return{bindType:TRANSITION_END,delegateType:TRANSITION_END,handle(e){return $(e.target).is(this)?e.handleObj.handler.apply(this,arguments):void 0}}}function transitionEndEmulator(e){let t=!1;return $(this).one(Util.TRANSITION_END,()=>{t=!0}),setTimeout(()=>{t||Util.triggerTransitionEnd(this)},e),this}function setTransitionEndSupport(){$.fn.emulateTransitionEnd=transitionEndEmulator,$.event.special[Util.TRANSITION_END]=getSpecialTransitionEndEvent()}const Util={TRANSITION_END:"bsTransitionEnd",getUID(e){do{e+=~~(1e6*Math.random())}while(document.getElementById(e));return e},getSelectorFromElement(e){let t=e.getAttribute("data-target");if(!t||"#"===t){const n=e.getAttribute("href");t=n&&"#"!==n?n.trim():""}try{return document.querySelector(t)?t:null}catch(e){return null}},getTransitionDurationFromElement(e){if(!e)return 0;let t=$(e).css("transition-duration"),n=$(e).css("transition-delay");const s=parseFloat(t),i=parseFloat(n);return s||i?(t=t.split(",")[0],n=n.split(",")[0],1e3*(parseFloat(t)+parseFloat(n))):0},reflow:e=>e.offsetHeight,triggerTransitionEnd(e){$(e).trigger(TRANSITION_END)},supportsTransitionEnd:()=>!0,isElement:e=>(e[0]||e).nodeType,typeCheckConfig(e,t,n){for(const s in n)if(Object.prototype.hasOwnProperty.call(n,s)){const i=n[s],l=t[s],a=l&&Util.isElement(l)?"element":toType(l);if(!new RegExp(i).test(a))throw new Error(`${e.toUpperCase()}: `+`Option "${s}" provided type "${a}" `+`but expected type "${i}".`)}},findShadowRoot(e){if(!document.documentElement.attachShadow)return null;if("function"==typeof e.getRootNode){const t=e.getRootNode();return t instanceof ShadowRoot?t:null}return e instanceof ShadowRoot?e:e.parentNode?Util.findShadowRoot(e.parentNode):null}};setTransitionEndSupport();const NAME="collapse",VERSION="4.3.1",DATA_KEY="bs.collapse",EVENT_KEY=`.${DATA_KEY}`,DATA_API_KEY=".data-api",JQUERY_NO_CONFLICT=$.fn.collapse,Default={toggle:!0,parent:""},DefaultType={toggle:"boolean",parent:"(string|element)"},Event={SHOW:`show${EVENT_KEY}`,SHOWN:`shown${EVENT_KEY}`,HIDE:`hide${EVENT_KEY}`,HIDDEN:`hidden${EVENT_KEY}`,CLICK_DATA_API:`click${EVENT_KEY}.data-api`},ClassName={SHOW:"show",COLLAPSE:"collapse",COLLAPSING:"collapsing",COLLAPSED:"collapsed"},Dimension={WIDTH:"width",HEIGHT:"height"},Selector={ACTIVES:".show, .collapsing",DATA_TOGGLE:'[data-toggle="collapse"]'};class Collapse{constructor(e,t){this._isTransitioning=!1,this._element=e,this._config=this._getConfig(t),this._triggerArray=[].slice.call(document.querySelectorAll(`[data-toggle="collapse"][href="#${e.id}"],`+`[data-toggle="collapse"][data-target="#${e.id}"]`));const n=[].slice.call(document.querySelectorAll(Selector.DATA_TOGGLE));for(let t=0,s=n.length;t<s;t++){const s=n[t],i=Util.getSelectorFromElement(s),l=[].slice.call(document.querySelectorAll(i)).filter(t=>t===e);null!==i&&0<l.length&&(this._selector=i,this._triggerArray.push(s))}this._parent=this._config.parent?this._getParent():null,this._config.parent||this._addAriaAndCollapsedClass(this._element,this._triggerArray),this._config.toggle&&this.toggle()}static get VERSION(){return VERSION}static get Default(){return Default}toggle(){$(this._element).hasClass(ClassName.SHOW)?this.hide():this.show()}show(){if(this._isTransitioning||$(this._element).hasClass(ClassName.SHOW))return;let e,t;if(this._parent&&(0===(e=[].slice.call(this._parent.querySelectorAll(Selector.ACTIVES)).filter(e=>"string"==typeof this._config.parent?e.getAttribute("data-parent")===this._config.parent:e.classList.contains(ClassName.COLLAPSE))).length&&(e=null)),e&&((t=$(e).not(this._selector).data(DATA_KEY))&&t._isTransitioning))return;const n=$.Event(Event.SHOW);if($(this._element).trigger(n),n.isDefaultPrevented())return;e&&(Collapse._jQueryInterface.call($(e).not(this._selector),"hide"),!t&&$(e).data(DATA_KEY,null));const s=this._getDimension();$(this._element).removeClass(ClassName.COLLAPSE).addClass(ClassName.COLLAPSING),this._element.style[s]=0,this._triggerArray.length&&$(this._triggerArray).removeClass(ClassName.COLLAPSED).attr("aria-expanded",!0),this.setTransitioning(!0);const i=s[0].toUpperCase()+s.slice(1),l=Util.getTransitionDurationFromElement(this._element);$(this._element).one(Util.TRANSITION_END,()=>{$(this._element).removeClass(ClassName.COLLAPSING).addClass(ClassName.COLLAPSE).addClass(ClassName.SHOW),this._element.style[s]="",this.setTransitioning(!1),$(this._element).trigger(Event.SHOWN)}).emulateTransitionEnd(l),this._element.style[s]=`${this._element[`scroll${i}`]}px`}hide(){if(!this._isTransitioning&&$(this._element).hasClass(ClassName.SHOW)){const e=$.Event(Event.HIDE);if($(this._element).trigger(e),!e.isDefaultPrevented()){const e=this._getDimension();this._element.style[e]=`${this._element.getBoundingClientRect()[e]}px`,Util.reflow(this._element),$(this._element).addClass(ClassName.COLLAPSING).removeClass(ClassName.COLLAPSE).removeClass(ClassName.SHOW);const t=this._triggerArray.length;if(0<t)for(let e=0;e<t;e++){const t=this._triggerArray[e],n=Util.getSelectorFromElement(t);if(null!==n){$([].slice.call(document.querySelectorAll(n))).hasClass(ClassName.SHOW)||$(t).addClass(ClassName.COLLAPSED).attr("aria-expanded",!1)}}this.setTransitioning(!0),this._element.style[e]="";const n=Util.getTransitionDurationFromElement(this._element);$(this._element).one(Util.TRANSITION_END,()=>{this.setTransitioning(!1),$(this._element).removeClass(ClassName.COLLAPSING).addClass(ClassName.COLLAPSE).trigger(Event.HIDDEN)}).emulateTransitionEnd(n)}}}setTransitioning(e){this._isTransitioning=e}dispose(){$.removeData(this._element,DATA_KEY),this._config=null,this._parent=null,this._element=null,this._triggerArray=null,this._isTransitioning=null}_getConfig(e){return(e={...Default,...e}).toggle=!!e.toggle,Util.typeCheckConfig(NAME,e,DefaultType),e}_getDimension(){return $(this._element).hasClass(Dimension.WIDTH)?Dimension.WIDTH:Dimension.HEIGHT}_getParent(){let e;Util.isElement(this._config.parent)?(e=this._config.parent,"undefined"!=typeof this._config.parent.jquery&&(e=this._config.parent[0])):e=document.querySelector(this._config.parent);const t=`[data-toggle="collapse"][data-parent="${this._config.parent}"]`,n=[].slice.call(e.querySelectorAll(t));return $(n).each((e,t)=>{this._addAriaAndCollapsedClass(Collapse._getTargetFromElement(t),[t])}),e}_addAriaAndCollapsedClass(e,t){const n=$(e).hasClass(ClassName.SHOW);t.length&&$(t).toggleClass(ClassName.COLLAPSED,!n).attr("aria-expanded",n)}static _getTargetFromElement(e){const t=Util.getSelectorFromElement(e);return t?document.querySelector(t):null}static _jQueryInterface(e){return this.each((function(){const t=$(this);let n=t.data(DATA_KEY);const s={...Default,...t.data(),..."object"==typeof e&&e?e:{}};if(!n&&s.toggle&&/show|hide/.test(e)&&(s.toggle=!1),n||(n=new Collapse(this,s),t.data(DATA_KEY,n)),"string"==typeof e){if("undefined"==typeof n[e])throw new TypeError(`No method named "${e}"`);n[e]()}}))}}$(document).on(Event.CLICK_DATA_API,Selector.DATA_TOGGLE,(function(e){"A"===e.currentTarget.tagName&&e.preventDefault();const t=$(this),n=Util.getSelectorFromElement(this),s=[].slice.call(document.querySelectorAll(n));$(s).each((function(){const e=$(this),n=e.data(DATA_KEY)?"toggle":t.data();Collapse._jQueryInterface.call(e,n)}))})),$.fn.collapse=Collapse._jQueryInterface,$.fn.collapse.Constructor=Collapse,$.fn.collapse.noConflict=()=>($.fn[NAME]=JQUERY_NO_CONFLICT,Collapse._jQueryInterface);