@ecl/ec-preset-website
Version:
ECL EC Website preset
3 lines (2 loc) • 3.84 kB
JavaScript
/* @ecl/ec-preset-website - 2.0.0-alpha.2 Built on 2018-10-15T14:42:30.131Z */
!function(){"use strict";var t=function(t){var e=1<arguments.length&&void 0!==arguments[1]?arguments[1]:document;return[].slice.call(e.querySelectorAll(t))},e=function(t){return(1<arguments.length&&void 0!==arguments[1]?arguments[1]:document).querySelector(t)};!function(){function x(t){var e=1<arguments.length&&void 0!==arguments[1]?arguments[1]:{},i=e.ellipsisButtonSelector,n=void 0===i?"[data-ecl-breadcrumb-ellipsis-button]":i,l=e.ellipsisSelector,s=void 0===l?"[data-ecl-breadcrumb-ellipsis]":l,a=e.segmentSelector,r=void 0===a?"[data-ecl-breadcrumb-item]":a,o=e.expandableItemsSelector,c=void 0===o?'[data-ecl-breadcrumb-item="expandable"]':o,h=e.staticItemsSelector,d=void 0===h?'[data-ecl-breadcrumb-item="static"]':h,u=e.onPartialExpand,p=void 0===u?null:u,m=e.onFullExpand,E=void 0===m?null:m,f=e.attachClickListener,b=void 0===f||f;if(function(t,e){if(!(t instanceof e))throw new TypeError("Cannot call a class as a function")}(this,x),!t||t.nodeType!==Node.ELEMENT_NODE)throw new TypeError("DOM element should be given to initialize this widget.");this.element=t,this.ellipsisButtonSelector=n,this.ellipsisSelector=s,this.segmentSelector=r,this.expandableItemsSelector=c,this.staticItemsSelector=d,this.onPartialExpand=p,this.onFullExpand=E,this.attachClickListener=b,this.ellipsisButton=null,this.itemsElements=null,this.staticElements=null,this.expandableElements=null,this.handleClickOnEllipsis=this.handleClickOnEllipsis.bind(this)}x.prototype.init=function(){this.ellipsisButton=e(this.ellipsisButtonSelector,this.element),this.attachClickListener&&this.ellipsisButton&&this.ellipsisButton.addEventListener("click",this.handleClickOnEllipsis),this.itemsElements=t(this.segmentSelector,this.element),this.staticElements=t(this.staticItemsSelector,this.element),this.expandableElements=t(this.expandableItemsSelector,this.element),this.check()},x.prototype.destroy=function(){this.attachClickListener&&this.ellipsisButton&&this.ellipsisButton.removeEventListener("click",this.handleClickOnEllipsis)},x.prototype.handleClickOnEllipsis=function(){return this.handleFullExpand()},x.prototype.check=function(){var t=this.computeVisibilityMap();t&&(!0===t.expanded?this.handleFullExpand():this.handlePartialExpand(t))},x.prototype.hideEllipsis=function(){var t=e(this.ellipsisSelector,this.element);t&&t.setAttribute("aria-hidden","true"),this.attachClickListener&&this.ellipsisButton&&this.ellipsisButton.removeEventListener("click",this.handleClickOnEllipsis)},x.prototype.showAllItems=function(){this.expandableElements.forEach(function(t){return t.setAttribute("aria-hidden","false")})},x.prototype.handlePartialExpand=function(t){if(t){var i=t.isItemVisible;i&&Array.isArray(i)&&(this.onPartialExpand?this.onPartialExpand(i):this.expandableElements.forEach(function(t,e){t.setAttribute("aria-hidden",i[e]?"false":"true")}))}},x.prototype.handleFullExpand=function(){this.onFullExpand?this.onFullExpand():(this.hideEllipsis(),this.showAllItems())},x.prototype.computeVisibilityMap=function(){if(!this.expandableElements||0===this.expandableElements.length)return{expanded:!0};var i=Math.floor(this.element.getBoundingClientRect().width);if(this.itemsElements.map(function(t){return t.getBoundingClientRect().width}).reduce(function(t,e){return t+e})<=i)return{expanded:!0};var n=e(this.ellipsisSelector,this.element).getBoundingClientRect().width+this.staticElements.reduce(function(t,e){return t+e.getBoundingClientRect().width},0);if(i<=n)return{expanded:!1,isItemVisible:[].concat(this.expandableElements.map(function(){return!1}))};var l=0,s=!0;return{expanded:!1,isItemVisible:[].concat(this.expandableElements).reverse().map(function(t){if(!s)return!1;var e=(l+=t.getBoundingClientRect().width)+n<=i;return e||(s=!1),e}).reverse()}}}()}();