UNPKG

primevue

Version:

[![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT) [![npm version](https://badge.fury.io/js/primevue.svg)](https://badge.fury.io/js/primevue) [![Discord Chat](https://img.shields.io/discord/55794023

2 lines (1 loc) 13.8 kB
"use strict";var e=require("primevue/basecomponent"),t=require("primevue/overlayeventbus"),i=require("primevue/portal"),n=require("primevue/utils"),s=require("primevue/ripple"),o=require("vue");function l(e){return e&&"object"==typeof e&&"default"in e?e:{default:e}}var r=l(e),a=l(t),d=l(i),c=l(s),p={name:"Menuitem",extends:r.default,inheritAttrs:!1,emits:["item-click"],props:{item:null,templates:null,exact:null,id:null,focusedOptionId:null},methods:{getItemProp:(e,t)=>e&&e.item?n.ObjectUtils.getItemValue(e.item[t]):void 0,getPTOptions(e){return this.ptm(e,{context:{focused:this.isItemFocused()}})},isItemFocused(){return this.focusedOptionId===this.id},onItemActionClick(e,t){t&&t(e)},onItemClick(e){const t=this.getItemProp(this.item,"command");t&&t({originalEvent:e,item:this.item.item}),this.$emit("item-click",{originalEvent:e,item:this.item,id:this.id})},containerClass(){return["p-menuitem",this.item.class,{"p-focus":this.id===this.focusedOptionId,"p-disabled":this.disabled()}]},linkClass(e){return["p-menuitem-link",{"router-link-active":e&&e.isActive,"router-link-active-exact":this.exact&&e&&e.isExactActive}]},visible(){return"function"==typeof this.item.visible?this.item.visible():!1!==this.item.visible},disabled(){return"function"==typeof this.item.disabled?this.item.disabled():this.item.disabled},label(){return"function"==typeof this.item.label?this.item.label():this.item.label}},computed:{iconClass(){return["p-menuitem-icon",this.item.icon]}},directives:{ripple:c.default}};const m=["id","aria-label","aria-disabled"],u=["href","onClick"],h=["href","target"];p.render=function(e,t,i,n,s,l){const r=o.resolveComponent("router-link"),a=o.resolveDirective("ripple");return l.visible()?(o.openBlock(),o.createElementBlock("li",o.mergeProps({key:0,id:i.id,class:l.containerClass(),role:"menuitem",style:i.item.style,"aria-label":l.label(),"aria-disabled":l.disabled()},l.getPTOptions("menuitem")),[o.createElementVNode("div",o.mergeProps({class:"p-menuitem-content",onClick:t[0]||(t[0]=e=>l.onItemClick(e))},l.getPTOptions("content")),[i.templates.item?(o.openBlock(),o.createBlock(o.resolveDynamicComponent(i.templates.item),{key:1,item:i.item},null,8,["item"])):(o.openBlock(),o.createElementBlock(o.Fragment,{key:0},[i.item.to&&!l.disabled()?(o.openBlock(),o.createBlock(r,{key:0,to:i.item.to,custom:""},{default:o.withCtx((({navigate:e,href:t,isActive:n,isExactActive:s})=>[o.withDirectives((o.openBlock(),o.createElementBlock("a",o.mergeProps({href:t,class:l.linkClass({isActive:n,isExactActive:s}),tabindex:"-1","aria-hidden":"true",onClick:t=>l.onItemActionClick(t,e)},l.getPTOptions("action")),[i.templates.itemicon?(o.openBlock(),o.createBlock(o.resolveDynamicComponent(i.templates.itemicon),{key:0,item:i.item,class:o.normalizeClass(l.iconClass)},null,8,["item","class"])):i.item.icon?(o.openBlock(),o.createElementBlock("span",o.mergeProps({key:1,class:l.iconClass},l.getPTOptions("icon")),null,16)):o.createCommentVNode("",!0),o.createElementVNode("span",o.mergeProps({class:"p-menuitem-text"},l.getPTOptions("label")),o.toDisplayString(l.label()),17)],16,u)),[[a]])])),_:1},8,["to"])):o.withDirectives((o.openBlock(),o.createElementBlock("a",o.mergeProps({key:1,href:i.item.url,class:l.linkClass(),target:i.item.target,tabindex:"-1","aria-hidden":"true"},l.getPTOptions("action")),[i.templates.itemicon?(o.openBlock(),o.createBlock(o.resolveDynamicComponent(i.templates.itemicon),{key:0,item:i.item,class:o.normalizeClass(l.iconClass)},null,8,["item","class"])):i.item.icon?(o.openBlock(),o.createElementBlock("span",o.mergeProps({key:1,class:l.iconClass},l.getPTOptions("icon")),null,16)):o.createCommentVNode("",!0),o.createElementVNode("span",o.mergeProps({class:"p-menuitem-text"},l.getPTOptions("label")),o.toDisplayString(l.label()),17)],16,h)),[[a]])],64))],16)],16,m)):o.createCommentVNode("",!0)};var b={name:"Menu",extends:r.default,inheritAttrs:!1,emits:["show","hide","focus","blur"],props:{popup:{type:Boolean,default:!1},model:{type:Array,default:null},appendTo:{type:String,default:"body"},autoZIndex:{type:Boolean,default:!0},baseZIndex:{type:Number,default:0},exact:{type:Boolean,default:!0},tabindex:{type:Number,default:0},"aria-label":{type:String,default:null},"aria-labelledby":{type:String,default:null}},data(){return{id:this.$attrs.id,overlayVisible:!1,focused:!1,focusedOptionIndex:-1,selectedOptionIndex:-1}},watch:{"$attrs.id":function(e){this.id=e||n.UniqueComponentId()}},target:null,outsideClickListener:null,scrollHandler:null,resizeListener:null,container:null,list:null,mounted(){this.id=this.id||n.UniqueComponentId(),this.popup||(this.bindResizeListener(),this.bindOutsideClickListener())},beforeUnmount(){this.unbindResizeListener(),this.unbindOutsideClickListener(),this.scrollHandler&&(this.scrollHandler.destroy(),this.scrollHandler=null),this.target=null,this.container&&this.autoZIndex&&n.ZIndexUtils.clear(this.container),this.container=null},methods:{itemClick(e){const t=e.item;this.disabled(t)||(t.command&&t.command(e),t.to&&e.navigate&&e.navigate(e.originalEvent),this.overlayVisible&&this.hide(),this.popup||this.focusedOptionIndex===e.id||(this.focusedOptionIndex=e.id))},onListFocus(e){this.focused=!0,this.popup||(-1!==this.selectedOptionIndex?(this.changeFocusedOptionIndex(this.selectedOptionIndex),this.selectedOptionIndex=-1):this.changeFocusedOptionIndex(0)),this.$emit("focus",e)},onListBlur(e){this.focused=!1,this.focusedOptionIndex=-1,this.$emit("blur",e)},onListKeyDown(e){switch(e.code){case"ArrowDown":this.onArrowDownKey(e);break;case"ArrowUp":this.onArrowUpKey(e);break;case"Home":this.onHomeKey(e);break;case"End":this.onEndKey(e);break;case"Enter":this.onEnterKey(e);break;case"Space":this.onSpaceKey(e);break;case"Escape":this.popup&&(n.DomHandler.focus(this.target),this.hide());case"Tab":this.overlayVisible&&this.hide()}},onArrowDownKey(e){const t=this.findNextOptionIndex(this.focusedOptionIndex);this.changeFocusedOptionIndex(t),e.preventDefault()},onArrowUpKey(e){if(e.altKey&&this.popup)n.DomHandler.focus(this.target),this.hide(),e.preventDefault();else{const t=this.findPrevOptionIndex(this.focusedOptionIndex);this.changeFocusedOptionIndex(t),e.preventDefault()}},onHomeKey(e){this.changeFocusedOptionIndex(0),e.preventDefault()},onEndKey(e){this.changeFocusedOptionIndex(n.DomHandler.find(this.container,"li.p-menuitem:not(.p-disabled)").length-1),e.preventDefault()},onEnterKey(e){const t=n.DomHandler.findSingle(this.list,`li[id="${this.focusedOptionIndex}"]`),i=t&&n.DomHandler.findSingle(t,".p-menuitem-link");this.popup&&n.DomHandler.focus(this.target),i?i.click():t&&t.click(),e.preventDefault()},onSpaceKey(e){this.onEnterKey(e)},findNextOptionIndex(e){const t=[...n.DomHandler.find(this.container,"li.p-menuitem:not(.p-disabled)")].findIndex((t=>t.id===e));return t>-1?t+1:0},findPrevOptionIndex(e){const t=[...n.DomHandler.find(this.container,"li.p-menuitem:not(.p-disabled)")].findIndex((t=>t.id===e));return t>-1?t-1:0},changeFocusedOptionIndex(e){const t=n.DomHandler.find(this.container,"li.p-menuitem:not(.p-disabled)");let i=e>=t.length?t.length-1:e<0?0:e;i>-1&&(this.focusedOptionIndex=t[i].getAttribute("id"))},toggle(e){this.overlayVisible?this.hide():this.show(e)},show(e){this.overlayVisible=!0,this.target=e.currentTarget},hide(){this.overlayVisible=!1,this.target=null},onEnter(e){this.alignOverlay(),this.bindOutsideClickListener(),this.bindResizeListener(),this.bindScrollListener(),this.autoZIndex&&n.ZIndexUtils.set("menu",e,this.baseZIndex+this.$primevue.config.zIndex.menu),this.popup&&(n.DomHandler.focus(this.list),this.changeFocusedOptionIndex(0)),this.$emit("show")},onLeave(){this.unbindOutsideClickListener(),this.unbindResizeListener(),this.unbindScrollListener(),this.$emit("hide")},onAfterLeave(e){this.autoZIndex&&n.ZIndexUtils.clear(e)},alignOverlay(){n.DomHandler.absolutePosition(this.container,this.target),this.container.style.minWidth=n.DomHandler.getOuterWidth(this.target)+"px"},bindOutsideClickListener(){this.outsideClickListener||(this.outsideClickListener=e=>{const t=this.container&&!this.container.contains(e.target),i=!(this.target&&(this.target===e.target||this.target.contains(e.target)));this.overlayVisible&&t&&i?this.hide():!this.popup&&t&&i&&(this.focusedOptionIndex=-1)},document.addEventListener("click",this.outsideClickListener))},unbindOutsideClickListener(){this.outsideClickListener&&(document.removeEventListener("click",this.outsideClickListener),this.outsideClickListener=null)},bindScrollListener(){this.scrollHandler||(this.scrollHandler=new n.ConnectedOverlayScrollHandler(this.target,(()=>{this.overlayVisible&&this.hide()}))),this.scrollHandler.bindScrollListener()},unbindScrollListener(){this.scrollHandler&&this.scrollHandler.unbindScrollListener()},bindResizeListener(){this.resizeListener||(this.resizeListener=()=>{this.overlayVisible&&!n.DomHandler.isTouchDevice()&&this.hide()},window.addEventListener("resize",this.resizeListener))},unbindResizeListener(){this.resizeListener&&(window.removeEventListener("resize",this.resizeListener),this.resizeListener=null)},visible:e=>"function"==typeof e.visible?e.visible():!1!==e.visible,disabled:e=>"function"==typeof e.disabled?e.disabled():e.disabled,label:e=>"function"==typeof e.label?e.label():e.label,separatorClass:e=>["p-menuitem-separator",e.class],onOverlayClick(e){a.default.emit("overlay-click",{originalEvent:e,target:this.target})},containerRef(e){this.container=e},listRef(e){this.list=e}},computed:{containerClass(){return["p-menu p-component",{"p-menu-overlay":this.popup,"p-input-filled":"filled"===this.$primevue.config.inputStyle,"p-ripple-disabled":!1===this.$primevue.config.ripple}]},focusedOptionId(){return-1!==this.focusedOptionIndex?this.focusedOptionIndex:null}},components:{PVMenuitem:p,Portal:d.default}};const k=["id"],f=["id","tabindex","aria-activedescendant","aria-label","aria-labelledby"],v=["id"];!function(e,t){void 0===t&&(t={});var i=t.insertAt;if(e&&"undefined"!=typeof document){var n=document.head||document.getElementsByTagName("head")[0],s=document.createElement("style");s.type="text/css","top"===i&&n.firstChild?n.insertBefore(s,n.firstChild):n.appendChild(s),s.styleSheet?s.styleSheet.cssText=e:s.appendChild(document.createTextNode(e))}}("\n.p-menu-overlay {\n position: absolute;\n top: 0;\n left: 0;\n}\n.p-menu ul {\n margin: 0;\n padding: 0;\n list-style: none;\n}\n.p-menu .p-menuitem-link {\n cursor: pointer;\n display: flex;\n align-items: center;\n text-decoration: none;\n overflow: hidden;\n position: relative;\n}\n.p-menu .p-menuitem-text {\n line-height: 1;\n}\n"),b.render=function(e,t,i,n,s,l){const r=o.resolveComponent("PVMenuitem"),a=o.resolveComponent("Portal");return o.openBlock(),o.createBlock(a,{appendTo:i.appendTo,disabled:!i.popup},{default:o.withCtx((()=>[o.createVNode(o.Transition,{name:"p-connected-overlay",onEnter:l.onEnter,onLeave:l.onLeave,onAfterLeave:l.onAfterLeave},{default:o.withCtx((()=>[!i.popup||s.overlayVisible?(o.openBlock(),o.createElementBlock("div",o.mergeProps({key:0,ref:l.containerRef,id:s.id,class:l.containerClass,onClick:t[3]||(t[3]=(...e)=>l.onOverlayClick&&l.onOverlayClick(...e))},{...e.$attrs,...e.ptm("root")}),[e.$slots.start?(o.openBlock(),o.createElementBlock("div",o.mergeProps({key:0,class:"p-menu-start"},e.ptm("start")),[o.renderSlot(e.$slots,"start")],16)):o.createCommentVNode("",!0),o.createElementVNode("ul",o.mergeProps({ref:l.listRef,id:s.id+"_list",class:"p-menu-list p-reset",role:"menu",tabindex:i.tabindex,"aria-activedescendant":s.focused?l.focusedOptionId:void 0,"aria-label":e.ariaLabel,"aria-labelledby":e.ariaLabelledby,onFocus:t[0]||(t[0]=(...e)=>l.onListFocus&&l.onListFocus(...e)),onBlur:t[1]||(t[1]=(...e)=>l.onListBlur&&l.onListBlur(...e)),onKeydown:t[2]||(t[2]=(...e)=>l.onListKeyDown&&l.onListKeyDown(...e))},e.ptm("menu")),[(o.openBlock(!0),o.createElementBlock(o.Fragment,null,o.renderList(i.model,((t,n)=>(o.openBlock(),o.createElementBlock(o.Fragment,{key:l.label(t)+n.toString()},[t.items&&l.visible(t)&&!t.separator?(o.openBlock(),o.createElementBlock(o.Fragment,{key:0},[t.items?(o.openBlock(),o.createElementBlock("li",o.mergeProps({key:0,id:s.id+"_"+n,class:"p-submenu-header",role:"none"},e.ptm("submenuHeader")),[o.renderSlot(e.$slots,"item",{item:t},(()=>[o.createTextVNode(o.toDisplayString(l.label(t)),1)]))],16,v)):o.createCommentVNode("",!0),(o.openBlock(!0),o.createElementBlock(o.Fragment,null,o.renderList(t.items,((a,d)=>(o.openBlock(),o.createElementBlock(o.Fragment,{key:a.label+n+"_"+d},[l.visible(a)&&!a.separator?(o.openBlock(),o.createBlock(r,{key:0,id:s.id+"_"+n+"_"+d,item:a,templates:e.$slots,exact:i.exact,focusedOptionId:l.focusedOptionId,onItemClick:l.itemClick,pt:e.pt},null,8,["id","item","templates","exact","focusedOptionId","onItemClick","pt"])):l.visible(a)&&a.separator?(o.openBlock(),o.createElementBlock("li",o.mergeProps({key:"separator"+n+d,class:l.separatorClass(t),style:a.style,role:"separator"},e.ptm("separator")),null,16)):o.createCommentVNode("",!0)],64)))),128))],64)):l.visible(t)&&t.separator?(o.openBlock(),o.createElementBlock("li",o.mergeProps({key:"separator"+n.toString(),class:l.separatorClass(t),style:t.style,role:"separator"},e.ptm("separator")),null,16)):(o.openBlock(),o.createBlock(r,{key:l.label(t)+n.toString(),id:s.id+"_"+n,item:t,templates:e.$slots,exact:i.exact,focusedOptionId:l.focusedOptionId,onItemClick:l.itemClick,pt:e.pt},null,8,["id","item","templates","exact","focusedOptionId","onItemClick","pt"]))],64)))),128))],16,f),e.$slots.end?(o.openBlock(),o.createElementBlock("div",o.mergeProps({key:1,class:"p-menu-end"},e.ptm("end")),[o.renderSlot(e.$slots,"end")],16)):o.createCommentVNode("",!0)],16,k)):o.createCommentVNode("",!0)])),_:3},8,["onEnter","onLeave","onAfterLeave"])])),_:3},8,["appendTo","disabled"])},module.exports=b;