primevue
Version:
[](https://opensource.org/licenses/MIT) [](https://badge.fury.io/js/primevue) [ • 13.3 kB
JavaScript
this.primevue=this.primevue||{},this.primevue.orderlist=function(e,t,i,n){"use strict";function l(e){return e&&"object"==typeof e&&"default"in e?e:{default:e}}var o=l(e),s=l(t),r={name:"OrderList",emits:["update:modelValue","reorder","update:selection","selection-change","focus","blur"],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"},stripedRows:{type:Boolean,default:!1},tabindex:{type:Number,default:0},listProps:{type:null,default:null},moveUpButtonProps:{type:null,default:null},moveTopButtonProps:{type:null,default:null},moveDownButtonProps:{type:null,default:null},moveBottomButtonProps:{type:null,default:null},"aria-labelledby":{type:String,default:null},"aria-label":{type:String,default:null}},itemTouched:!1,reorderDirection:null,styleElement:null,list:null,data(){return{d_selection:this.selection,focused:!1,focusedOptionIndex:-1}},beforeUnmount(){this.destroyStyle()},updated(){this.reorderDirection&&(this.updateListScroll(),this.reorderDirection=null)},mounted(){this.responsive&&this.createStyle()},methods:{getItemKey(e,t){return this.dataKey?i.ObjectUtils.resolveFieldData(e,this.dataKey):t},isSelected(e){return-1!=i.ObjectUtils.findIndexInList(e,this.d_selection)},onListFocus(e){const t=i.DomHandler.findSingle(this.list,"li.p-orderlist-item.p-highlight"),n=i.ObjectUtils.findIndexInList(t,this.list.children);this.focused=!0;const l=-1!==this.focusedOptionIndex?this.focusedOptionIndex:t?n:-1;this.changeFocusedOptionIndex(l),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"KeyA":e.ctrlKey&&(this.d_selection=[...this.modelValue],this.$emit("update:selection",this.d_selection))}},onOptionMouseDown(e){this.focused=!0,this.focusedOptionIndex=e},onArrowDownKey(e){const t=this.findNextOptionIndex(this.focusedOptionIndex);this.changeFocusedOptionIndex(t),e.shiftKey&&this.onEnterKey(e),e.preventDefault()},onArrowUpKey(e){const t=this.findPrevOptionIndex(this.focusedOptionIndex);this.changeFocusedOptionIndex(t),e.shiftKey&&this.onEnterKey(e),e.preventDefault()},onHomeKey(e){if(e.ctrlKey&&e.shiftKey){const e=i.DomHandler.find(this.list,"li.p-orderlist-item"),t=i.DomHandler.findSingle(this.list,`li.p-orderlist-item[id=${this.focusedOptionIndex}]`),n=[...e].findIndex((e=>e===t));this.d_selection=[...this.modelValue].slice(0,n+1),this.$emit("update:selection",this.d_selection)}else this.changeFocusedOptionIndex(0);e.preventDefault()},onEndKey(e){if(e.ctrlKey&&e.shiftKey){const e=i.DomHandler.find(this.list,"li.p-orderlist-item"),t=i.DomHandler.findSingle(this.list,`li.p-orderlist-item[id=${this.focusedOptionIndex}]`),n=[...e].findIndex((e=>e===t));this.d_selection=[...this.modelValue].slice(n,e.length),this.$emit("update:selection",this.d_selection)}else this.changeFocusedOptionIndex(i.DomHandler.find(this.list,"li.p-orderlist-item").length-1);e.preventDefault()},onEnterKey(e){const t=i.DomHandler.find(this.list,"li.p-orderlist-item"),n=i.DomHandler.findSingle(this.list,`li.p-orderlist-item[id=${this.focusedOptionIndex}]`),l=[...t].findIndex((e=>e===n));this.onItemClick(e,this.modelValue[l],l),e.preventDefault()},onSpaceKey(e){if(e.shiftKey){const e=i.DomHandler.find(this.list,"li.p-orderlist-item"),t=i.ObjectUtils.findIndexInList(this.d_selection[0],[...this.modelValue]),n=i.DomHandler.findSingle(this.list,`li.p-orderlist-item[id=${this.focusedOptionIndex}]`),l=[...e].findIndex((e=>e===n));this.d_selection=[...this.modelValue].slice(Math.min(t,l),Math.max(t,l)+1),this.$emit("update:selection",this.d_selection)}else this.onEnterKey(e)},findNextOptionIndex(e){const t=[...i.DomHandler.find(this.list,"li.p-orderlist-item")].findIndex((t=>t.id===e));return t>-1?t+1:0},findPrevOptionIndex(e){const t=[...i.DomHandler.find(this.list,"li.p-orderlist-item")].findIndex((t=>t.id===e));return t>-1?t-1:0},changeFocusedOptionIndex(e){const t=i.DomHandler.find(this.list,"li.p-orderlist-item");let n=e>=t.length?t.length-1:e<0?0:e;this.focusedOptionIndex=t[n].getAttribute("id"),this.scrollInView(t[n].getAttribute("id"))},scrollInView(e){const t=i.DomHandler.findSingle(this.list,`li[id="${e}"]`);t&&t.scrollIntoView&&t.scrollIntoView({block:"nearest",inline:"start"})},moveUp(e){if(this.d_selection){let t=[...this.modelValue];for(let e=0;e<this.d_selection.length;e++){let n=this.d_selection[e],l=i.ObjectUtils.findIndexInList(n,t);if(0===l)break;{let e=t[l],i=t[l-1];t[l-1]=e,t[l]=i}}this.reorderDirection="up",this.$emit("update:modelValue",t),this.$emit("reorder",{originalEvent:e,value:t,direction:this.reorderDirection})}},moveTop(e){if(this.d_selection){let t=[...this.modelValue];for(let e=0;e<this.d_selection.length;e++){let n=this.d_selection[e],l=i.ObjectUtils.findIndexInList(n,t);if(0===l)break;{let e=t.splice(l,1)[0];t.unshift(e)}}this.reorderDirection="top",this.$emit("update:modelValue",t),this.$emit("reorder",{originalEvent:e,value:t,direction:this.reorderDirection})}},moveDown(e){if(this.d_selection){let t=[...this.modelValue];for(let e=this.d_selection.length-1;e>=0;e--){let n=this.d_selection[e],l=i.ObjectUtils.findIndexInList(n,t);if(l===t.length-1)break;{let e=t[l],i=t[l+1];t[l+1]=e,t[l]=i}}this.reorderDirection="down",this.$emit("update:modelValue",t),this.$emit("reorder",{originalEvent:e,value:t,direction:this.reorderDirection})}},moveBottom(e){if(this.d_selection){let t=[...this.modelValue];for(let e=this.d_selection.length-1;e>=0;e--){let n=this.d_selection[e],l=i.ObjectUtils.findIndexInList(n,t);if(l===t.length-1)break;{let e=t.splice(l,1)[0];t.push(e)}}this.reorderDirection="bottom",this.$emit("update:modelValue",t),this.$emit("reorder",{originalEvent:e,value:t,direction:this.reorderDirection})}},onItemClick(e,t,n){this.itemTouched=!1;const l=i.ObjectUtils.findIndexInList(t,this.d_selection),o=-1!=l,s=!this.itemTouched&&this.metaKeySelection,r=i.DomHandler.find(this.list,".p-orderlist-item")[n].getAttribute("id");if(this.focusedOptionIndex=r,s){const s=e.metaKey||e.ctrlKey;o&&s?this.d_selection=this.d_selection.filter(((e,t)=>t!==l)):(this.d_selection=s&&this.d_selection?[...this.d_selection]:[],i.ObjectUtils.insertIntoOrderedArray(t,n,this.d_selection,this.modelValue))}else o?this.d_selection=this.d_selection.filter(((e,t)=>t!==l)):(this.d_selection=this.d_selection?[...this.d_selection]:[],i.ObjectUtils.insertIntoOrderedArray(t,n,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},findNextItem(e){let t=e.nextElementSibling;return t?i.DomHandler.hasClass(t,"p-orderlist-item")?t:this.findNextItem(t):null},findPrevItem(e){let t=e.previousElementSibling;return t?i.DomHandler.hasClass(t,"p-orderlist-item")?t:this.findPrevItem(t):null},findFirstItem(){return i.DomHandler.findSingle(this.list,".p-orderlist-item")},findLastItem(){const e=i.DomHandler.find(this.list,".p-orderlist-item");return e[e.length-1]},itemClass(e,t){return["p-orderlist-item",{"p-highlight":this.isSelected(e),"p-focus":t===this.focusedOptionId}]},updateListScroll(){const e=i.DomHandler.find(this.list,".p-orderlist-item.p-highlight");if(e&&e.length)switch(this.reorderDirection){case"up":i.DomHandler.scrollInView(this.list,e[0]);break;case"top":this.list.scrollTop=0;break;case"down":i.DomHandler.scrollInView(this.list,e[e.length-1]);break;case"bottom":this.list.scrollTop=this.list.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)},moveDisabled(){if(!this.d_selection||!this.d_selection.length)return!0},listRef(e){this.list=e?e.$el:void 0}},computed:{containerClass(){return["p-orderlist p-component",{"p-orderlist-striped":this.stripedRows}]},id(){return this.$attrs.id||i.UniqueComponentId()},attributeSelector:()=>i.UniqueComponentId(),focusedOptionId(){return-1!==this.focusedOptionIndex?this.focusedOptionIndex:null},moveUpAriaLabel(){return this.$primevue.config.locale.aria?this.$primevue.config.locale.aria.moveUp:void 0},moveTopAriaLabel(){return this.$primevue.config.locale.aria?this.$primevue.config.locale.aria.moveTop:void 0},moveDownAriaLabel(){return this.$primevue.config.locale.aria?this.$primevue.config.locale.aria.moveDown:void 0},moveBottomAriaLabel(){return this.$primevue.config.locale.aria?this.$primevue.config.locale.aria.moveBottom:void 0}},components:{OLButton:o.default},directives:{ripple:s.default}};const d={class:"p-orderlist-controls"},a={class:"p-orderlist-list-container"},c={key:0,class:"p-orderlist-header"},h=["id","onClick","aria-selected","onMousedown"];return function(e,t){void 0===t&&(t={});var i=t.insertAt;if(e&&"undefined"!=typeof document){var n=document.head||document.getElementsByTagName("head")[0],l=document.createElement("style");l.type="text/css","top"===i&&n.firstChild?n.insertBefore(l,n.firstChild):n.appendChild(l),l.styleSheet?l.styleSheet.cssText=e:l.appendChild(document.createTextNode(e))}}("\n.p-orderlist {\n display: flex;\n}\n.p-orderlist-controls {\n display: flex;\n flex-direction: column;\n justify-content: center;\n}\n.p-orderlist-list-container {\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"),r.render=function(e,t,i,l,o,s){const r=n.resolveComponent("OLButton"),u=n.resolveDirective("ripple");return n.openBlock(),n.createElementBlock("div",{class:n.normalizeClass(s.containerClass)},[n.createElementVNode("div",d,[n.renderSlot(e.$slots,"controlsstart"),n.createVNode(r,n.mergeProps({type:"button",icon:"pi pi-angle-up",onClick:s.moveUp,"aria-label":s.moveUpAriaLabel,disabled:s.moveDisabled()},i.moveUpButtonProps),null,16,["onClick","aria-label","disabled"]),n.createVNode(r,n.mergeProps({type:"button",icon:"pi pi-angle-double-up",onClick:s.moveTop,"aria-label":s.moveTopAriaLabel,disabled:s.moveDisabled()},i.moveTopButtonProps),null,16,["onClick","aria-label","disabled"]),n.createVNode(r,n.mergeProps({type:"button",icon:"pi pi-angle-down",onClick:s.moveDown,"aria-label":s.moveDownAriaLabel,disabled:s.moveDisabled()},i.moveDownButtonProps),null,16,["onClick","aria-label","disabled"]),n.createVNode(r,n.mergeProps({type:"button",icon:"pi pi-angle-double-down",onClick:s.moveBottom,"aria-label":s.moveBottomAriaLabel,disabled:s.moveDisabled()},i.moveBottomButtonProps),null,16,["onClick","aria-label","disabled"]),n.renderSlot(e.$slots,"controlsend")]),n.createElementVNode("div",a,[e.$slots.header?(n.openBlock(),n.createElementBlock("div",c,[n.renderSlot(e.$slots,"header")])):n.createCommentVNode("",!0),n.createVNode(n.TransitionGroup,n.mergeProps({ref:s.listRef,id:s.id+"_list",name:"p-orderlist-flip",tag:"ul",class:"p-orderlist-list",style:i.listStyle,role:"listbox","aria-multiselectable":"true",tabindex:i.tabindex,"aria-activedescendant":o.focused?s.focusedOptionId:void 0,"aria-label":e.ariaLabel,"aria-labelledby":e.ariaLabelledby,onFocus:s.onListFocus,onBlur:s.onListBlur,onKeydown:s.onListKeyDown},i.listProps),{default:n.withCtx((()=>[(n.openBlock(!0),n.createElementBlock(n.Fragment,null,n.renderList(i.modelValue,((i,l)=>n.withDirectives((n.openBlock(),n.createElementBlock("li",{key:s.getItemKey(i,l),id:s.id+"_"+l,role:"option",class:n.normalizeClass(s.itemClass(i,`${s.id}_${l}`)),onClick:e=>s.onItemClick(e,i,l),onTouchend:t[0]||(t[0]=(...e)=>s.onItemTouchEnd&&s.onItemTouchEnd(...e)),"aria-selected":s.isSelected(i),onMousedown:e=>s.onOptionMouseDown(l)},[n.renderSlot(e.$slots,"item",{item:i,index:l})],42,h)),[[u]]))),128))])),_:3},16,["id","style","tabindex","aria-activedescendant","aria-label","aria-labelledby","onFocus","onBlur","onKeydown"])])],2)},r}(primevue.button,primevue.ripple,primevue.utils,Vue);