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) 8.2 kB
"use strict";var e=require("primevue/button"),t=require("primevue/utils"),i=require("primevue/ripple"),l=require("vue");function n(e){return e&&"object"==typeof e&&"default"in e?e:{default:e}}var o=n(e),r=n(i),s={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.ObjectUtils.resolveFieldData(e,this.dataKey):i},isSelected(e){return-1!=t.ObjectUtils.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.ObjectUtils.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.ObjectUtils.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.ObjectUtils.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.ObjectUtils.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.ObjectUtils.findIndexInList(i,this.d_selection),o=-1!=n;if(!this.itemTouched&&this.metaKeySelection){let r=e.metaKey||e.ctrlKey;o&&r?this.d_selection=this.d_selection.filter(((e,t)=>t!==n)):(this.d_selection=r&&this.d_selection?[...this.d_selection]:[],t.ObjectUtils.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.ObjectUtils.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 i=e.nextElementSibling;return i?t.DomHandler.hasClass(i,"p-orderlist-item")?i:this.findNextItem(i):null},findPrevItem(e){let i=e.previousElementSibling;return i?t.DomHandler.hasClass(i,"p-orderlist-item")?i:this.findPrevItem(i):null},updateListScroll(){const e=t.DomHandler.find(this.$refs.list.$el,".p-orderlist-item.p-highlight");if(e&&e.length)switch(this.reorderDirection){case"up":t.DomHandler.scrollInView(this.$refs.list.$el,e[0]);break;case"top":this.$refs.list.$el.scrollTop=0;break;case"down":t.DomHandler.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:()=>t.UniqueComponentId()},components:{OLButton:o.default},directives:{ripple:r.default}};const d={class:"p-orderlist p-component"},c={class:"p-orderlist-controls"},a={class:"p-orderlist-list-container"},h={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"),s.render=function(e,t,i,n,o,r){const s=l.resolveComponent("OLButton"),u=l.resolveDirective("ripple");return l.openBlock(),l.createBlock("div",d,[l.createVNode("div",c,[l.createVNode(s,{type:"button",icon:"pi pi-angle-up",onClick:r.moveUp},null,8,["onClick"]),l.createVNode(s,{type:"button",icon:"pi pi-angle-double-up",onClick:r.moveTop},null,8,["onClick"]),l.createVNode(s,{type:"button",icon:"pi pi-angle-down",onClick:r.moveDown},null,8,["onClick"]),l.createVNode(s,{type:"button",icon:"pi pi-angle-double-down",onClick:r.moveBottom},null,8,["onClick"])]),l.createVNode("div",a,[e.$slots.header?(l.openBlock(),l.createBlock("div",h,[l.renderSlot(e.$slots,"header")])):l.createCommentVNode("",!0),l.createVNode(l.TransitionGroup,{ref:"list",name:"p-orderlist-flip",tag:"ul",class:"p-orderlist-list",style:i.listStyle,role:"listbox","aria-multiselectable":"multiple"},{default:l.withCtx((()=>[(l.openBlock(!0),l.createBlock(l.Fragment,null,l.renderList(i.modelValue,((i,n)=>l.withDirectives((l.openBlock(),l.createBlock("li",{key:r.getItemKey(i,n),tabindex:"0",class:["p-orderlist-item",{"p-highlight":r.isSelected(i)}],onClick:e=>r.onItemClick(e,i,n),onKeydown:e=>r.onItemKeyDown(e,i,n),onTouchend:t[1]||(t[1]=(...e)=>r.onItemTouchEnd&&r.onItemTouchEnd(...e)),role:"option","aria-selected":r.isSelected(i)},[l.renderSlot(e.$slots,"item",{item:i,index:n})],42,["onClick","onKeydown","aria-selected"])),[[u]]))),128))])),_:3},8,["style"])])])},module.exports=s;