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) 14.9 kB
"use strict";var e=require("primevue/basecomponent"),t=require("primevue/button"),i=require("primevue/icons/angledoubledown"),o=require("primevue/icons/angledoubleup"),n=require("primevue/icons/angledown"),l=require("primevue/icons/angleup"),s=require("primevue/ripple"),r=require("primevue/utils"),d=require("vue");function a(e){return e&&"object"==typeof e&&"default"in e?e:{default:e}}var c=a(e),p=a(t),u=a(i),m=a(o),h=a(n),f=a(l),v=a(s),b={name:"OrderList",extends:c.default,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{id:this.$attrs.id,d_selection:this.selection,focused:!1,focusedOptionIndex:-1}},watch:{"$attrs.id":function(e){this.id=e||r.UniqueComponentId()}},beforeUnmount(){this.destroyStyle()},updated(){this.reorderDirection&&(this.updateListScroll(),this.reorderDirection=null)},mounted(){this.id=this.id||r.UniqueComponentId(),this.responsive&&this.createStyle()},methods:{getItemKey(e,t){return this.dataKey?r.ObjectUtils.resolveFieldData(e,this.dataKey):t},getPTOptions(e,t){return this.ptm(t,{context:{active:this.isSelected(e),focused:this.id===this.focusedOptionId}})},isSelected(e){return-1!=r.ObjectUtils.findIndexInList(e,this.d_selection)},onListFocus(e){const t=r.DomHandler.findSingle(this.list,"li.p-orderlist-item.p-highlight"),i=r.ObjectUtils.findIndexInList(t,this.list.children);this.focused=!0;const o=-1!==this.focusedOptionIndex?this.focusedOptionIndex:t?i:-1;this.changeFocusedOptionIndex(o),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=r.DomHandler.find(this.list,"li.p-orderlist-item"),t=r.DomHandler.findSingle(this.list,`li.p-orderlist-item[id=${this.focusedOptionIndex}]`),i=[...e].findIndex((e=>e===t));this.d_selection=[...this.modelValue].slice(0,i+1),this.$emit("update:selection",this.d_selection)}else this.changeFocusedOptionIndex(0);e.preventDefault()},onEndKey(e){if(e.ctrlKey&&e.shiftKey){const e=r.DomHandler.find(this.list,"li.p-orderlist-item"),t=r.DomHandler.findSingle(this.list,`li.p-orderlist-item[id=${this.focusedOptionIndex}]`),i=[...e].findIndex((e=>e===t));this.d_selection=[...this.modelValue].slice(i,e.length),this.$emit("update:selection",this.d_selection)}else this.changeFocusedOptionIndex(r.DomHandler.find(this.list,"li.p-orderlist-item").length-1);e.preventDefault()},onEnterKey(e){const t=r.DomHandler.find(this.list,"li.p-orderlist-item"),i=r.DomHandler.findSingle(this.list,`li.p-orderlist-item[id=${this.focusedOptionIndex}]`),o=[...t].findIndex((e=>e===i));this.onItemClick(e,this.modelValue[o],o),e.preventDefault()},onSpaceKey(e){if(e.shiftKey){const e=r.DomHandler.find(this.list,"li.p-orderlist-item"),t=r.ObjectUtils.findIndexInList(this.d_selection[0],[...this.modelValue]),i=r.DomHandler.findSingle(this.list,`li.p-orderlist-item[id=${this.focusedOptionIndex}]`),o=[...e].findIndex((e=>e===i));this.d_selection=[...this.modelValue].slice(Math.min(t,o),Math.max(t,o)+1),this.$emit("update:selection",this.d_selection)}else this.onEnterKey(e)},findNextOptionIndex(e){const t=[...r.DomHandler.find(this.list,"li.p-orderlist-item")].findIndex((t=>t.id===e));return t>-1?t+1:0},findPrevOptionIndex(e){const t=[...r.DomHandler.find(this.list,"li.p-orderlist-item")].findIndex((t=>t.id===e));return t>-1?t-1:0},changeFocusedOptionIndex(e){const t=r.DomHandler.find(this.list,"li.p-orderlist-item");let i=e>=t.length?t.length-1:e<0?0:e;this.focusedOptionIndex=t[i]?t[i].getAttribute("id"):-1,this.scrollInView(this.focusedOptionIndex)},scrollInView(e){const t=r.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 i=this.d_selection[e],o=r.ObjectUtils.findIndexInList(i,t);if(0===o)break;{let e=t[o],i=t[o-1];t[o-1]=e,t[o]=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 i=this.d_selection[e],o=r.ObjectUtils.findIndexInList(i,t);if(0===o)break;{let e=t.splice(o,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 i=this.d_selection[e],o=r.ObjectUtils.findIndexInList(i,t);if(o===t.length-1)break;{let e=t[o],i=t[o+1];t[o+1]=e,t[o]=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 i=this.d_selection[e],o=r.ObjectUtils.findIndexInList(i,t);if(o===t.length-1)break;{let e=t.splice(o,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,i){this.itemTouched=!1;const o=r.ObjectUtils.findIndexInList(t,this.d_selection),n=-1!=o,l=!this.itemTouched&&this.metaKeySelection,s=r.DomHandler.find(this.list,".p-orderlist-item")[i].getAttribute("id");if(this.focusedOptionIndex=s,l){const l=e.metaKey||e.ctrlKey;n&&l?this.d_selection=this.d_selection.filter(((e,t)=>t!==o)):(this.d_selection=l&&this.d_selection?[...this.d_selection]:[],r.ObjectUtils.insertIntoOrderedArray(t,i,this.d_selection,this.modelValue))}else n?this.d_selection=this.d_selection.filter(((e,t)=>t!==o)):(this.d_selection=this.d_selection?[...this.d_selection]:[],r.ObjectUtils.insertIntoOrderedArray(t,i,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?r.DomHandler.hasClass(t,"p-orderlist-item")?t:this.findNextItem(t):null},findPrevItem(e){let t=e.previousElementSibling;return t?r.DomHandler.hasClass(t,"p-orderlist-item")?t:this.findPrevItem(t):null},findFirstItem(){return r.DomHandler.findSingle(this.list,".p-orderlist-item")},findLastItem(){const e=r.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=r.DomHandler.find(this.list,".p-orderlist-item.p-highlight");if(e&&e.length)switch(this.reorderDirection){case"up":r.DomHandler.scrollInView(this.list,e[0]);break;case"top":this.list.scrollTop=0;break;case"down":r.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}]},attributeSelector:()=>r.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:p.default,AngleUpIcon:f.default,AngleDownIcon:h.default,AngleDoubleUpIcon:m.default,AngleDoubleDownIcon:u.default},directives:{ripple:v.default}};const g=["id","onClick","aria-selected","onMousedown"];!function(e,t){void 0===t&&(t={});var i=t.insertAt;if(e&&"undefined"!=typeof document){var o=document.head||document.getElementsByTagName("head")[0],n=document.createElement("style");n.type="text/css","top"===i&&o.firstChild?o.insertBefore(n,o.firstChild):o.appendChild(n),n.styleSheet?n.styleSheet.cssText=e:n.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"),b.render=function(e,t,i,o,n,l){const s=d.resolveComponent("AngleUpIcon"),r=d.resolveComponent("OLButton"),a=d.resolveComponent("AngleDoubleUpIcon"),c=d.resolveComponent("AngleDownIcon"),p=d.resolveComponent("AngleDoubleDownIcon"),u=d.resolveDirective("ripple");return d.openBlock(),d.createElementBlock("div",d.mergeProps({class:l.containerClass},e.ptm("root")),[d.createElementVNode("div",d.mergeProps({class:"p-orderlist-controls"},e.ptm("controls")),[d.renderSlot(e.$slots,"controlsstart"),d.createVNode(r,d.mergeProps({type:"button",onClick:l.moveUp,"aria-label":l.moveUpAriaLabel,disabled:l.moveDisabled()},i.moveUpButtonProps,{pt:e.ptm("moveUpButton")}),{icon:d.withCtx((()=>[d.renderSlot(e.$slots,"moveupicon",{},(()=>[d.createVNode(s,d.normalizeProps(d.guardReactiveProps(e.ptm("moveUpButton").icon)),null,16)]))])),_:3},16,["onClick","aria-label","disabled","pt"]),d.createVNode(r,d.mergeProps({type:"button",onClick:l.moveTop,"aria-label":l.moveTopAriaLabel,disabled:l.moveDisabled()},i.moveTopButtonProps,{pt:e.ptm("moveTopButton")}),{icon:d.withCtx((()=>[d.renderSlot(e.$slots,"movetopicon",{},(()=>[d.createVNode(a,d.normalizeProps(d.guardReactiveProps(e.ptm("moveTopButton").icon)),null,16)]))])),_:3},16,["onClick","aria-label","disabled","pt"]),d.createVNode(r,d.mergeProps({type:"button",onClick:l.moveDown,"aria-label":l.moveDownAriaLabel,disabled:l.moveDisabled()},i.moveDownButtonProps,{pt:e.ptm("moveDownButton")}),{icon:d.withCtx((()=>[d.renderSlot(e.$slots,"movedownicon",{},(()=>[d.createVNode(c,d.normalizeProps(d.guardReactiveProps(e.ptm("moveDownButton").icon)),null,16)]))])),_:3},16,["onClick","aria-label","disabled","pt"]),d.createVNode(r,d.mergeProps({type:"button",onClick:l.moveBottom,"aria-label":l.moveBottomAriaLabel,disabled:l.moveDisabled()},i.moveBottomButtonProps,{pt:e.ptm("moveBottomButton")}),{icon:d.withCtx((()=>[d.renderSlot(e.$slots,"movebottomicon",{},(()=>[d.createVNode(p,d.normalizeProps(d.guardReactiveProps(e.ptm("moveBottomButton").icon)),null,16)]))])),_:3},16,["onClick","aria-label","disabled","pt"]),d.renderSlot(e.$slots,"controlsend")],16),d.createElementVNode("div",d.mergeProps({class:"p-orderlist-list-container"},e.ptm("container")),[e.$slots.header?(d.openBlock(),d.createElementBlock("div",d.mergeProps({key:0,class:"p-orderlist-header"},e.ptm("header")),[d.renderSlot(e.$slots,"header")],16)):d.createCommentVNode("",!0),d.createVNode(d.TransitionGroup,d.mergeProps({ref:l.listRef,id:n.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":n.focused?l.focusedOptionId:void 0,"aria-label":e.ariaLabel,"aria-labelledby":e.ariaLabelledby,onFocus:l.onListFocus,onBlur:l.onListBlur,onKeydown:l.onListKeyDown},{...i.listProps,...e.ptm("list")}),{default:d.withCtx((()=>[(d.openBlock(!0),d.createElementBlock(d.Fragment,null,d.renderList(i.modelValue,((i,o)=>d.withDirectives((d.openBlock(),d.createElementBlock("li",d.mergeProps({key:l.getItemKey(i,o),id:n.id+"_"+o,role:"option",class:l.itemClass(i,`${n.id}_${o}`),onClick:e=>l.onItemClick(e,i,o),onTouchend:t[0]||(t[0]=(...e)=>l.onItemTouchEnd&&l.onItemTouchEnd(...e)),"aria-selected":l.isSelected(i),onMousedown:e=>l.onOptionMouseDown(o)},l.getPTOptions(i,"item")),[d.renderSlot(e.$slots,"item",{item:i,index:o})],16,g)),[[u]]))),128))])),_:3},16,["id","style","tabindex","aria-activedescendant","aria-label","aria-labelledby","onFocus","onBlur","onKeydown"])],16)],16)},module.exports=b;