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) 7.87 kB
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 d,createVNode as a,renderSlot as c,createCommentVNode as h,TransitionGroup as p,withCtx as m,Fragment as u,renderList as f,withDirectives as y}from"vue";var v={name:"OrderList",emits:["update:modelValue","reorder","update:selection","selection-change"],props:{modelValue:{type:Array,default:null},selection:{type:Array,default:null},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}},beforeUnmount(){this.destroyStyle()},updated(){this.reorderDirection&&(this.updateListScroll(),this.reorderDirection=null)},mounted(){this.responsive&&this.createStyle()},methods:{getItemKey(e,i){return this.dataKey?t.resolveFieldData(e,this.dataKey):i},isSelected(e){return-1!=t.findIndexInList(e,this.d_selection)},moveUp(){if(this.d_selection){let e=[...this.modelValue];for(let i=0;i<this.d_selection.length;i++){let l=this.d_selection[i],n=t.findIndexInList(l,e);if(0===n)break;{let t=e[n],i=e[n-1];e[n-1]=t,e[n]=i}}this.reorderDirection="up",this.$emit("update:modelValue",e),this.$emit("reorder",{originalEvent:event,value:e,direction:this.reorderDirection})}},moveTop(){if(this.d_selection){let e=[...this.modelValue];for(let i=0;i<this.d_selection.length;i++){let l=this.d_selection[i],n=t.findIndexInList(l,e);if(0===n)break;{let t=e.splice(n,1)[0];e.unshift(t)}}this.reorderDirection="top",this.$emit("update:modelValue",e),this.$emit("reorder",{originalEvent:event,value:e,direction:this.reorderDirection})}},moveDown(){if(this.d_selection){let e=[...this.modelValue];for(let i=this.d_selection.length-1;i>=0;i--){let l=this.d_selection[i],n=t.findIndexInList(l,e);if(n===e.length-1)break;{let t=e[n],i=e[n+1];e[n+1]=t,e[n]=i}}this.reorderDirection="down",this.$emit("update:modelValue",e),this.$emit("reorder",{originalEvent:event,value:e,direction:this.reorderDirection})}},moveBottom(){if(this.d_selection){let e=[...this.modelValue];for(let i=this.d_selection.length-1;i>=0;i--){let l=this.d_selection[i],n=t.findIndexInList(l,e);if(n===e.length-1)break;{let t=e.splice(n,1)[0];e.push(t)}}this.reorderDirection="bottom",this.$emit("update:modelValue",e),this.$emit("reorder",{originalEvent:event,value:e,direction:this.reorderDirection})}},onItemClick(e,i,l){this.itemTouched=!1;let n=t.findIndexInList(i,this.d_selection),o=-1!=n;if(!this.itemTouched&&this.metaKeySelection){let s=e.metaKey||e.ctrlKey;o&&s?this.d_selection=this.d_selection.filter(((e,t)=>t!==n)):(this.d_selection=s&&this.d_selection?[...this.d_selection]:[],t.insertIntoOrderedArray(i,l,this.d_selection,this.modelValue))}else o?this.d_selection=this.d_selection.filter(((e,t)=>t!==n)):(this.d_selection=this.d_selection?[...this.d_selection]:[],t.insertIntoOrderedArray(i,l,this.d_selection,this.modelValue));this.$emit("update:selection",this.d_selection),this.$emit("selection-change",{originalEvent:e,value:this.d_selection})},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-orderlist-item")?t:this.findNextItem(t):null},findPrevItem(e){let t=e.previousElementSibling;return t?i.hasClass(t,"p-orderlist-item")?t:this.findPrevItem(t):null},updateListScroll(){const e=i.find(this.$refs.list.$el,".p-orderlist-item.p-highlight");if(e&&e.length)switch(this.reorderDirection){case"up":i.scrollInView(this.$refs.list.$el,e[0]);break;case"top":this.$refs.list.$el.scrollTop=0;break;case"down":i.scrollInView(this.$refs.list.$el,e[e.length-1]);break;case"bottom":this.$refs.list.$el.scrollTop=this.$refs.list.$el.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-orderlist[${this.attributeSelector}] {\n flex-direction: column;\n }\n\n .p-orderlist[${this.attributeSelector}] .p-orderlist-controls {\n padding: var(--content-padding);\n flex-direction: row;\n }\n\n .p-orderlist[${this.attributeSelector}] .p-orderlist-controls .p-button {\n margin-right: var(--inline-spacing);\n margin-bottom: 0;\n }\n\n .p-orderlist[${this.attributeSelector}] .p-orderlist-controls .p-button:last-child {\n margin-right: 0;\n }\n}\n`;this.styleElement.innerHTML=e}},destroyStyle(){this.styleElement&&(document.head.removeChild(this.styleElement),this.styleElement=null)}},computed:{attributeSelector:()=>l()},components:{OLButton:e},directives:{ripple:n}};const b={class:"p-orderlist p-component"},g={class:"p-orderlist-controls"},x={class:"p-orderlist-list-container"},k={key:0,class:"p-orderlist-header"};!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-orderlist {\n display: -webkit-box;\n display: -ms-flexbox;\n display: flex;\n}\n.p-orderlist-controls {\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-orderlist-list-container {\n -webkit-box-flex: 1;\n -ms-flex: 1 1 auto;\n flex: 1 1 auto;\n}\n.p-orderlist-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-orderlist-item {\n cursor: pointer;\n overflow: hidden;\n position: relative;\n}\n.p-orderlist.p-state-disabled .p-orderlist-item,\n.p-orderlist.p-state-disabled .p-button {\n cursor: default;\n}\n.p-orderlist.p-state-disabled .p-orderlist-list {\n overflow: hidden;\n}\n"),v.render=function(e,t,i,l,n,v){const I=o("OLButton"),$=s("ripple");return r(),d("div",b,[a("div",g,[a(I,{type:"button",icon:"pi pi-angle-up",onClick:v.moveUp},null,8,["onClick"]),a(I,{type:"button",icon:"pi pi-angle-double-up",onClick:v.moveTop},null,8,["onClick"]),a(I,{type:"button",icon:"pi pi-angle-down",onClick:v.moveDown},null,8,["onClick"]),a(I,{type:"button",icon:"pi pi-angle-double-down",onClick:v.moveBottom},null,8,["onClick"])]),a("div",x,[e.$slots.header?(r(),d("div",k,[c(e.$slots,"header")])):h("",!0),a(p,{ref:"list",name:"p-orderlist-flip",tag:"ul",class:"p-orderlist-list",style:i.listStyle,role:"listbox","aria-multiselectable":"multiple"},{default:m((()=>[(r(!0),d(u,null,f(i.modelValue,((i,l)=>y((r(),d("li",{key:v.getItemKey(i,l),tabindex:"0",class:["p-orderlist-item",{"p-highlight":v.isSelected(i)}],onClick:e=>v.onItemClick(e,i,l),onKeydown:e=>v.onItemKeyDown(e,i,l),onTouchend:t[1]||(t[1]=(...e)=>v.onItemTouchEnd&&v.onItemTouchEnd(...e)),role:"option","aria-selected":v.isSelected(i)},[c(e.$slots,"item",{item:i,index:l})],42,["onClick","onKeydown","aria-selected"])),[[$]]))),128))])),_:3},8,["style"])])])};export default v;