@govbr-ds/core
Version:
Padrão Digital de Governo
2 lines • 8.19 kB
JavaScript
!function e(t,o){"object"==typeof exports&&"object"==typeof module?module.exports=o():"function"==typeof define&&define.amd?define("core",[],o):"object"==typeof exports?exports.core=o():t.core=o()}(self,(()=>(()=>{var e=()=>{},t,o=()=>{},s={d:(e,t)=>{for(var o in t)s.o(t,o)&&!s.o(e,o)&&Object.defineProperty(e,o,{enumerable:!0,get:t[o]})},o:(e,t)=>Object.prototype.hasOwnProperty.call(e,t)};((e,t,o)=>{class s{constructor({name:e,component:t,json:o,lang:s,mode:i="default",callback:r}){this.name=e,this.component=t,this.data=new CookiebarData(o,s),this.templates=new CookiebarTemplates(this.data),this.mode=i,this.callback=r,this._setUp()}_setUp(){this._buildCookiebar(),this._setAccessibility(),this._setBehavior(),this._showCookiebar()}_buildCookiebar(){this.component.innerHTML=this.templates.setGlobalContentArea()}_setAccessibility(){this.component.setAttribute("role","dialog"),this.component.setAttribute("aria-modal",!0),this.component.setAttribute("aria-describedby","info-t"),this.component.setAttribute("aria-label","Componente para definição de Cookies")}_setBehavior(){this._setAcceptButtonBehavior(),this._setPoliticsButtonBehavior(),this._setCloseButtonBehavior(),this._setToggleGroupBehavior(),this._setCheckboxBehavior(),this._setSelectionBehavior(),this._setWindowResizeBehavior()}_setAcceptButtonBehavior(){const e=this.component.querySelector(selectors.ACCEPT_BUTTON);e.addEventListener("click",(()=>{this._hideCookiebar(),document.body.style.overflowY="auto",this.callback(this._setOutputJSON())})),e.addEventListener("keydown",(e=>{"Tab"===e.key&&(this.component.classList.contains("default")||this.component.focus())})),this._setActionButtonResponsive(e)}_setPoliticsButtonBehavior(){this.component.querySelectorAll(selectors.POLITICS_BUTTON).forEach((e=>{e.addEventListener("click",(()=>{e.classList.add("d-none"),this.component.classList.remove("default"),this.component.focus(),document.body.style.overflowY="hidden",this._setOpenView()})),this._setActionButtonResponsive(e)}))}_setCloseButtonBehavior(){this.component.querySelectorAll(selectors.CLOSE_BUTTON).forEach((e=>{e.addEventListener("click",(()=>{if(this.component.classList.add("default"),"open"===this.mode)this._hideCookiebar();this.component.querySelector(selectors.POLITICS_BUTTON).classList.remove("d-none"),document.body.style.overflowY="auto",this._setDefaultView()}))}))}_setWindowResizeBehavior(){window.addEventListener("resize",(()=>{this.component.classList.contains("default")||this._setOpenView(),this.component.querySelectorAll(selectors.ACTION_BUTTONS).forEach((e=>{this._setActionButtonResponsive(e)}))}))}_setActionButtonResponsive(e){window.matchMedia("(max-width: 574px)").matches&&e.classList.add("block"),window.matchMedia("(min-width: 575px)").matches&&e.classList.remove("block")}_setToggleGroupBehavior(){this.component.querySelectorAll(`${selectors.GROUP_BUTTON}, ${selectors.GROUP_NAME}, ${selectors.COOKIES_CHECKED}, ${selectors.GROUP_SIZE}`).forEach((e=>{e.addEventListener("click",this._handleToggleGroupClick.bind(this))}))}_handleToggleGroupClick(e){const t=this._getParentElementByClass(e.currentTarget,"br-item");t.classList.contains("open")?(t.classList.remove("open"),t.nextElementSibling.querySelectorAll(selectors.BR_SWITCH).forEach((e=>{e.setAttribute("tabindex",-1)})),this._setGroupAttributes(t,`Expandir o grupo de Cookies ${t.querySelector(".group-name").innerText}`),this._toggleIcon(t,"fa-angle-up","fa-angle-down")):(t.classList.add("open"),t.nextElementSibling.querySelectorAll(selectors.BR_SWITCH).forEach((e=>{e.setAttribute("tabindex",0)})),this._setGroupAttributes(t,`Retrair o grupo de Cookies ${t.querySelector(".group-name").innerText}`),this._toggleIcon(t,"fa-angle-down","fa-angle-up"),this._scrollUp(t))}_setCheckboxBehavior(){this.component.querySelectorAll(selectors.PARENT_CHECKBOX).forEach((e=>{this.checkgroupBehavior=new Checkgroup(e),this.checkgroupBehavior.setBehavior()}))}_setSelectionBehavior(){this.component.querySelectorAll(selectors.CHECKBOX).forEach((e=>{e.addEventListener("change",this._controlSelection.bind(this))}))}_controlSelection(e){const t=e.currentTarget.id.split("-");switch(t[1]){case"all":this._setCheckAllBehavior(e.currentTarget);break;case"group":this._setCheckgroupBehavior(e.currentTarget,t[2]);break;case"cookie":this._setCheckCookieBehavior(e.currentTarget,t[2],t[3])}}_setCheckAllBehavior(e){this.data.selectAll=e.checked,this.data.allIndeterminated=!!e.hasAttribute("indeterminate"),this._displayBroadAlertMessage()}_setCheckgroupBehavior(e,t){this.data.cookieGroups[t].groupSelected=e.checked,this.data.cookieGroups[t].groupIndeterminated=!!e.hasAttribute("indeterminate"),this.data.cookieGroups[t].cookieList.forEach(((o,s)=>{o.cookieOptOut||(o.cookieSelected=e.checked,this._displayCookieAlertMessage(t,s))})),this._displayGroupAlertMessage(t)}_setCheckCookieBehavior(e,t,o){this.data.cookieGroups[t].cookieList[o].cookieSelected=e.checked,this._displayCookieAlertMessage(t,o)}_displayBroadAlertMessage(){this.component.querySelectorAll(selectors.BROAD_ALERT).forEach((e=>{!this.data.allAlertMessage||this.data.selectAll&&!this.data.allIndeterminated?e.classList.add("d-none"):e.classList.remove("d-none")}))}_displayGroupAlertMessage(e){const t=undefined;this.component.querySelectorAll(selectors.GROUP_INFO)[e].querySelectorAll(selectors.GROUP_ALERT).forEach((t=>{!this.data.cookieGroups[e].groupAlertMessage||this.data.cookieGroups[e].groupSelected&&!this.data.cookieGroups[e].groupIndeterminated?t.classList.add("d-none"):t.classList.remove("d-none")}))}_displayCookieAlertMessage(e,t){const o=undefined,s=undefined;this.component.querySelectorAll(selectors.GROUP_INFO)[e].nextElementSibling.querySelectorAll(selectors.COOKIE_CARD)[t].querySelectorAll(selectors.COOKIE_ALERT).forEach((o=>{this.data.cookieGroups[e].cookieList[t].alertMessage&&!this.data.cookieGroups[e].cookieList[t].cookieSelected?o.classList.remove("d-none"):o.classList.add("d-none")}))}_getParentElementByClass(e,t){for(parent=e.parentNode;!parent.classList.contains(t);)parent=parent.parentNode;return parent}_toggleIcon(e,t,o){e.querySelectorAll(selectors.BUTTON_ICON).forEach((e=>{e.classList.remove(t),e.classList.add(o)}))}_setGroupAttributes(e,t){e.querySelectorAll(`${selectors.GROUP_BUTTON}, ${selectors.GROUP_NAME}, ${selectors.COOKIES_CHECKED}, ${selectors.GROUP_SIZE}`).forEach((e=>{e.setAttribute("aria-label",t)}))}_scrollUp(e){setTimeout((()=>{this.component.querySelectorAll(selectors.WRAPPER).forEach((()=>{setTimeout((()=>{e.scrollIntoView({behavior:"smooth",block:"start"})}),150)}),5e3)}))}_showCookiebar(){if(this.component.classList.remove("d-none"),this.component.focus(),"open"===this.mode)this.component.classList.remove("default"),this.component.querySelectorAll(selectors.POLITICS_BUTTON).forEach((e=>{e.classList.add("d-none")})),document.body.style.overflowY="hidden",this._setOpenView()}_hideCookiebar(){this.component.classList.add("d-none")}_setOpenView(){const e=this.component.querySelector(selectors.WRAPPER),t=this.component.querySelector(selectors.CONTAINER_FLUID),o=this.component.querySelector(selectors.MODAL_FOOTER),s=window.getComputedStyle(t,null).getPropertyValue("padding-top").match(/\d+/),i=window.innerHeight-2*s-o.offsetHeight+"px";e.style.height=i}_setDefaultView(){this.component.querySelector(selectors.WRAPPER).removeAttribute("style")}_setOutputJSON(){return this.output={},this.output.selectAll=this.data.allIndeterminated?"indeterminated":this.data.selectAll,this.output.cookieGroups=[],this.data.cookieGroups.forEach((e=>{const t=[];e.cookieList.forEach((e=>{t.push({cookieId:e.cookieId,cookieSelected:e.cookieSelected})})),this.output.cookieGroups.push({cookieList:t,groupId:e.groupId,groupSelected:e.groupIndeterminated?"indeterminated":e.groupSelected})})),JSON.stringify(this.output)}static createCookiebar(e,t){const o=document.createElement("div");o.classList.add("br-cookiebar","default","d-none"),o.setAttribute("tabindex",1),document.body.appendChild(o);const i=undefined;return new s({callback:t,component:o,json:e,lang:"pt-br",mode:"default",name:"br-cookiebar"})}}})(0,{},s),e(0,{},s);var i={};return o(0,i,s),i})()));
//# sourceMappingURL=cookiebar.min.js.map