primevue
Version:
[](https://opensource.org/licenses/MIT) [](https://badge.fury.io/js/primevue) [ • 12.4 kB
JavaScript
import e from"primevue/button";import{ObjectUtils as t,DomHandler as i,UniqueComponentId as l}from"primevue/utils";import n from"primevue/ripple";import{resolveComponent as o,resolveDirective as s,openBlock as r,createBlock as c,createVNode as a,renderSlot as d,createCommentVNode as p,TransitionGroup as u,withCtx as h,Fragment as m,renderList as f,withDirectives as k}from"vue";var g={name:"PickList",emits:["update:modelValue","reorder","update:selection","selection-change","move-to-target","move-to-source","move-all-to-target","move-all-to-source"],props:{modelValue:{type:Array,default:()=>[[],[]]},selection:{type:Array,default:()=>[[],[]]},dataKey:{type:String,default:null},listStyle:{type:null,default:null},metaKeySelection:{type:Boolean,default:!0},responsive:{type:Boolean,default:!0},breakpoint:{type:String,default:"960px"}},itemTouched:!1,reorderDirection:null,styleElement:null,data(){return{d_selection:this.selection}},updated(){this.reorderDirection&&(this.updateListScroll(this.$refs.sourceList.$el),this.updateListScroll(this.$refs.targetList.$el),this.reorderDirection=null)},beforeUnmount(){this.destroyStyle()},mounted(){this.responsive&&this.createStyle()},watch:{selection(e){this.d_selection=e}},methods:{getItemKey(e,i){return this.dataKey?t.resolveFieldData(e,this.dataKey):i},isSelected(e,i){return-1!=t.findIndexInList(e,this.d_selection[i])},moveUp(e,i){if(this.d_selection&&this.d_selection[i]){let l=[...this.modelValue[i]],n=this.d_selection[i];for(let e=0;e<n.length;e++){let i=n[e],o=t.findIndexInList(i,l);if(0===o)break;{let e=l[o],t=l[o-1];l[o-1]=e,l[o]=t}}let o=[...this.modelValue];o[i]=l,this.reorderDirection="up",this.$emit("update:modelValue",o),this.$emit("reorder",{originalEvent:e,value:o,direction:this.reorderDirection,listIndex:i})}},moveTop(e,i){if(this.d_selection){let l=[...this.modelValue[i]],n=this.d_selection[i];for(let e=0;e<n.length;e++){let i=n[e],o=t.findIndexInList(i,l);if(0===o)break;{let e=l.splice(o,1)[0];l.unshift(e)}}let o=[...this.modelValue];o[i]=l,this.reorderDirection="top",this.$emit("update:modelValue",o),this.$emit("reorder",{originalEvent:e,value:o,direction:this.reorderDirection,listIndex:i})}},moveDown(e,i){if(this.d_selection){let l=[...this.modelValue[i]],n=this.d_selection[i];for(let e=n.length-1;e>=0;e--){let i=n[e],o=t.findIndexInList(i,l);if(o===l.length-1)break;{let e=l[o],t=l[o+1];l[o+1]=e,l[o]=t}}let o=[...this.modelValue];o[i]=l,this.reorderDirection="down",this.$emit("update:modelValue",o),this.$emit("reorder",{originalEvent:e,value:o,direction:this.reorderDirection,listIndex:i})}},moveBottom(e,i){if(this.d_selection){let l=[...this.modelValue[i]],n=this.d_selection[i];for(let e=n.length-1;e>=0;e--){let i=n[e],o=t.findIndexInList(i,l);if(o===l.length-1)break;{let e=l.splice(o,1)[0];l.push(e)}}let o=[...this.modelValue];o[i]=l,this.reorderDirection="bottom",this.$emit("update:modelValue",o),this.$emit("reorder",{originalEvent:e,value:o,direction:this.reorderDirection,listIndex:i})}},moveToTarget(e){let i=this.d_selection&&this.d_selection[0]?this.d_selection[0]:null,l=[...this.modelValue[0]],n=[...this.modelValue[1]];if(i){for(let e=0;e<i.length;e++){let o=i[e];-1==t.findIndexInList(o,n)&&n.push(l.splice(t.findIndexInList(o,l),1)[0])}let o=[...this.modelValue];o[0]=l,o[1]=n,this.$emit("update:modelValue",o),this.$emit("move-to-target",{originalEvent:e,items:i}),this.d_selection[0]=[],this.$emit("update:selection",this.d_selection),this.$emit("selection-change",{originalEvent:e,value:this.d_selection})}},moveAllToTarget(e){if(this.modelValue[0]){let t=[...this.modelValue[0]],i=[...this.modelValue[1]];this.$emit("move-all-to-target",{originalEvent:e,items:t}),i=[...i,...t],t=[];let l=[...this.modelValue];l[0]=t,l[1]=i,this.$emit("update:modelValue",l),this.d_selection[0]=[],this.$emit("update:selection",this.d_selection),this.$emit("selection-change",{originalEvent:e,value:this.d_selection})}},moveToSource(e){let i=this.d_selection&&this.d_selection[1]?this.d_selection[1]:null,l=[...this.modelValue[0]],n=[...this.modelValue[1]];if(i){for(let e=0;e<i.length;e++){let o=i[e];-1==t.findIndexInList(o,l)&&l.push(n.splice(t.findIndexInList(o,n),1)[0])}let o=[...this.modelValue];o[0]=l,o[1]=n,this.$emit("update:modelValue",o),this.$emit("move-to-source",{originalEvent:e,items:i}),this.d_selection[1]=[],this.$emit("update:selection",this.d_selection),this.$emit("selection-change",{originalEvent:e,value:this.d_selection})}},moveAllToSource(e){if(this.modelValue[1]){let t=[...this.modelValue[0]],i=[...this.modelValue[1]];this.$emit("move-all-to-source",{originalEvent:e,items:i}),t=[...t,...i],i=[];let l=[...this.modelValue];l[0]=t,l[1]=i,this.$emit("update:modelValue",l),this.d_selection[1]=[],this.$emit("update:selection",this.d_selection),this.$emit("selection-change",{originalEvent:e,value:this.d_selection})}},onItemClick(e,i,l){this.itemTouched=!1;const n=this.d_selection[l],o=t.findIndexInList(i,n),s=-1!=o;let r;if(!this.itemTouched&&this.metaKeySelection){let t=e.metaKey||e.ctrlKey;s&&t?r=n.filter(((e,t)=>t!==o)):(r=t&&n?[...n]:[],r.push(i))}else s?r=n.filter(((e,t)=>t!==o)):(r=n?[...n]:[],r.push(i));let c=[...this.d_selection];c[l]=r,this.d_selection=c,this.$emit("update:selection",this.d_selection),this.$emit("selection-change",{originalEvent:e,value:this.d_selection})},onItemDblClick(e,t,i){0===i?this.moveToTarget(e):1===i&&this.moveToSource(e)},onItemTouchEnd(){this.itemTouched=!0},onItemKeyDown(e,t,i){let l=e.currentTarget;switch(e.which){case 40:var n=this.findNextItem(l);n&&n.focus(),e.preventDefault();break;case 38:var o=this.findPrevItem(l);o&&o.focus(),e.preventDefault();break;case 13:this.onItemClick(e,t,i),e.preventDefault()}},findNextItem(e){let t=e.nextElementSibling;return t?i.hasClass(t,"p-picklist-item")?t:this.findNextItem(t):null},findPrevItem(e){let t=e.previousElementSibling;return t?i.hasClass(t,"p-picklist-item")?t:this.findPrevItem(t):null},updateListScroll(e){const t=i.find(e,".p-picklist-item.p-highlight");if(t&&t.length)switch(this.reorderDirection){case"up":i.scrollInView(e,t[0]);break;case"top":e.scrollTop=0;break;case"down":i.scrollInView(e,t[t.length-1]);break;case"bottom":e.scrollTop=e.scrollHeight}},createStyle(){if(!this.styleElement){this.$el.setAttribute(this.attributeSelector,""),this.styleElement=document.createElement("style"),this.styleElement.type="text/css",document.head.appendChild(this.styleElement);let e=`\n@media screen and (max-width: ${this.breakpoint}) {\n .p-picklist[${this.attributeSelector}] {\n flex-direction: column;\n }\n\n .p-picklist[${this.attributeSelector}] .p-picklist-buttons {\n padding: var(--content-padding);\n flex-direction: row;\n }\n\n .p-picklist[${this.attributeSelector}] .p-picklist-buttons .p-button {\n margin-right: var(--inline-spacing);\n margin-bottom: 0;\n }\n\n .p-picklist[${this.attributeSelector}] .p-picklist-buttons .p-button:last-child {\n margin-right: 0;\n }\n\n .p-picklist[${this.attributeSelector}] .pi-angle-right:before {\n content: "\\e930"\n }\n\n .p-picklist[${this.attributeSelector}] .pi-angle-double-right:before {\n content: "\\e92c"\n }\n\n .p-picklist[${this.attributeSelector}] .pi-angle-left:before {\n content: "\\e933"\n }\n\n .p-picklist[${this.attributeSelector}] .pi-angle-double-left:before {\n content: "\\e92f"\n }\n}\n`;this.styleElement.innerHTML=e}},destroyStyle(){this.styleElement&&(document.head.removeChild(this.styleElement),this.styleElement=null)}},computed:{sourceList(){return this.modelValue&&this.modelValue[0]?this.modelValue[0]:null},targetList(){return this.modelValue&&this.modelValue[1]?this.modelValue[1]:null},attributeSelector:()=>l()},components:{PLButton:e},directives:{ripple:n}};const v={class:"p-picklist p-component"},b={class:"p-picklist-buttons p-picklist-source-controls"},y={class:"p-picklist-list-wrapper p-picklist-source-wrapper"},x={key:0,class:"p-picklist-header"},I={class:"p-picklist-buttons p-picklist-transfer-buttons"},$={class:"p-picklist-list-wrapper p-picklist-target-wrapper"},V={key:0,class:"p-picklist-header"},w={class:"p-picklist-buttons p-picklist-target-controls"};!function(e,t){void 0===t&&(t={});var i=t.insertAt;if(e&&"undefined"!=typeof document){var l=document.head||document.getElementsByTagName("head")[0],n=document.createElement("style");n.type="text/css","top"===i&&l.firstChild?l.insertBefore(n,l.firstChild):l.appendChild(n),n.styleSheet?n.styleSheet.cssText=e:n.appendChild(document.createTextNode(e))}}("\n.p-picklist {\n display: -webkit-box;\n display: -ms-flexbox;\n display: flex;\n}\n.p-picklist-buttons {\n display: -webkit-box;\n display: -ms-flexbox;\n display: flex;\n -webkit-box-orient: vertical;\n -webkit-box-direction: normal;\n -ms-flex-direction: column;\n flex-direction: column;\n -webkit-box-pack: center;\n -ms-flex-pack: center;\n justify-content: center;\n}\n.p-picklist-list-wrapper {\n -webkit-box-flex: 1;\n -ms-flex: 1 1 50%;\n flex: 1 1 50%;\n}\n.p-picklist-list {\n list-style-type: none;\n margin: 0;\n padding: 0;\n overflow: auto;\n min-height: 12rem;\n max-height: 24rem;\n}\n.p-picklist-item {\n cursor: pointer;\n overflow: hidden;\n position: relative;\n}\n.p-picklist-item.p-picklist-flip-enter-active.p-picklist-flip-enter-to,\n.p-picklist-item.p-picklist-flip-leave-active.p-picklist-flip-leave-to {\n -webkit-transition: none !important;\n transition: none !important;\n}\n"),g.render=function(e,t,i,l,n,g){const S=o("PLButton"),_=s("ripple");return r(),c("div",v,[a("div",b,[a(S,{type:"button",icon:"pi pi-angle-up",onClick:t[1]||(t[1]=e=>g.moveUp(e,0))}),a(S,{type:"button",icon:"pi pi-angle-double-up",onClick:t[2]||(t[2]=e=>g.moveTop(e,0))}),a(S,{type:"button",icon:"pi pi-angle-down",onClick:t[3]||(t[3]=e=>g.moveDown(e,0))}),a(S,{type:"button",icon:"pi pi-angle-double-down",onClick:t[4]||(t[4]=e=>g.moveBottom(e,0))})]),a("div",y,[e.$slots.sourceHeader?(r(),c("div",x,[d(e.$slots,"sourceHeader")])):p("",!0),a(u,{ref:"sourceList",name:"p-picklist-flip",tag:"ul",class:"p-picklist-list p-picklist-source",style:i.listStyle,role:"listbox","aria-multiselectable":"multiple"},{default:h((()=>[(r(!0),c(m,null,f(g.sourceList,((i,l)=>k((r(),c("li",{key:g.getItemKey(i,l),tabindex:"0",class:["p-picklist-item",{"p-highlight":g.isSelected(i,0)}],onClick:e=>g.onItemClick(e,i,0),onDblclick:e=>g.onItemDblClick(e,i,0),onKeydown:e=>g.onItemKeyDown(e,i,0),onTouchend:t[5]||(t[5]=(...e)=>g.onItemTouchEnd&&g.onItemTouchEnd(...e)),role:"option","aria-selected":g.isSelected(i,0)},[d(e.$slots,"item",{item:i,index:l})],42,["onClick","onDblclick","onKeydown","aria-selected"])),[[_]]))),128))])),_:3},8,["style"])]),a("div",I,[a(S,{type:"button",icon:"pi pi-angle-right",onClick:g.moveToTarget},null,8,["onClick"]),a(S,{type:"button",icon:"pi pi-angle-double-right",onClick:g.moveAllToTarget},null,8,["onClick"]),a(S,{type:"button",icon:"pi pi-angle-left",onClick:g.moveToSource},null,8,["onClick"]),a(S,{type:"button",icon:"pi pi-angle-double-left",onClick:g.moveAllToSource},null,8,["onClick"])]),a("div",$,[e.$slots.targetHeader?(r(),c("div",V,[d(e.$slots,"targetHeader")])):p("",!0),a(u,{ref:"targetList",name:"p-picklist-flip",tag:"ul",class:"p-picklist-list p-picklist-target",style:i.listStyle,role:"listbox","aria-multiselectable":"multiple"},{default:h((()=>[(r(!0),c(m,null,f(g.targetList,((i,l)=>k((r(),c("li",{key:g.getItemKey(i,l),tabindex:"0",class:["p-picklist-item",{"p-highlight":g.isSelected(i,1)}],onClick:e=>g.onItemClick(e,i,1),onDblclick:e=>g.onItemDblClick(e,i,1),onKeydown:e=>g.onItemKeyDown(e,i,1),onTouchend:t[6]||(t[6]=(...e)=>g.onItemTouchEnd&&g.onItemTouchEnd(...e)),role:"option","aria-selected":g.isSelected(i,1)},[d(e.$slots,"item",{item:i,index:l})],42,["onClick","onDblclick","onKeydown","aria-selected"])),[[_]]))),128))])),_:3},8,["style"])]),a("div",w,[a(S,{type:"button",icon:"pi pi-angle-up",onClick:t[7]||(t[7]=e=>g.moveUp(e,1))}),a(S,{type:"button",icon:"pi pi-angle-double-up",onClick:t[8]||(t[8]=e=>g.moveTop(e,1))}),a(S,{type:"button",icon:"pi pi-angle-down",onClick:t[9]||(t[9]=e=>g.moveDown(e,1))}),a(S,{type:"button",icon:"pi pi-angle-double-down",onClick:t[10]||(t[10]=e=>g.moveBottom(e,1))})])])};export default g;