/*!
 App Connect Data Traversal
 Version: 1.1.4
 (c) 2022 Wappler.io
 @build 2022-12-09 15:38:45
 */
dmx.Component("data-view",{initialData:{data:[],page:1,pages:1,items:0,sort:{on:"",dir:"asc"},has:{first:!1,prev:!1,next:!1,last:!1}},attributes:{data:{type:[Array,Object],default:null},filter:{type:String,default:""},page:{type:Number,default:1},pageSize:{type:Number,default:0},sortOn:{type:String,default:""},sortDir:{type:String,default:"asc",validate:function(t){return/^(asc|desc)$/i.test(t)}}},methods:{select:function(t){this.setPage(Number(t))},first:function(){this.setPage(1)},prev:function(){var t=this.data.page-1;t<1&&(t=1),t>this.data.pages&&(t=this.data.pages),this.setPage(t)},next:function(){var t=this.data.page+1;t<1&&(t=1),t>this.data.pages&&(t=this.data.pages),this.setPage(t)},last:function(){var t=this.data.pages;this.setPage(t)},sort:function(t,e){this.props.sortOn=t,this.props.sortDir=e&&"desc"==e.toLowerCase()?"desc":"asc",this._update()}},render:function(t){this.items=[],this.filtered=[],this.props.data&&(this.setData(this.props.data),this._update()),this.$parse()},beforeUpdate:function(t){this.props.filter&&(this._needUpdate=!this.$canSkip({idents:dmx.getIdents(this.props.filter)},t))},update:function(t,e){e.has("data")&&this.setData(this.props.data),e.has("page")&&this.setPage(this.props.page),e.size&&(this._needUpdate=!0)},updated:function(){this._needUpdate&&this._update()},_update:function(){if(this._needUpdate=!1,this.filtered=this.items.filter((t=>!this.props.filter||dmx.parse(this.props.filter,dmx.DataScope(t,this)))),this.props.sortOn){let t=this.props.sortOn,e=this.props.sortDir.toLowerCase(),s="desc"==e;this.filtered.sort(((e,i)=>s?e[t]>i[t]?-1:e[t]<i[t]?1:0:e[t]<i[t]?-1:e[t]>i[t]?1:0)),this.set("sort",{on:t,dir:e})}if(this.filtered.length){let t=this.props.pageSize;this.set("pages",t?Math.ceil(this.filtered.length/t):1)}else this.set("pages",1);this.set("items",this.filtered.length),this.setPage(this.props.page)},setData:function(t){this.items=dmx.repeatItems(t).map((t=>(delete(t=dmx.clone(t)).$value,delete t.$index,delete t.$key,t)))},setPage:function(t){const e=+this.props.pageSize,s=this.data.pages,i=((t=t<1?1:t>s?s:t)-1)*e;this.set("page",t),this.set("data",e?this.filtered.slice(i,i+e):this.filtered),this.set("has",{first:t>1,prev:t>1,next:t<s,last:t<s})}}),dmx.Component("data-detail",{initialData:{data:{}},attributes:{data:{type:Array,default:[]},key:{type:String},value:{type:[String,Number]}},methods:{select:function(t){this.props.value=t,this.select(t,!0)}},render:function(t){this.items=[],this.setData(this.props.data),this.select(this.props.value),this.$parse()},update:function(t,e){e.has("data")?(this.setData(this.props.data),this.select(this.props.value)):t.value!==this.props.value&&this.select(this.props.value,!0)},setData:function(t){if(this.items=[],t&&"object"==typeof t)if(Array.isArray(t))this.items=t.map((function(t){return"object"==typeof t?t:{$value:t}}));else for(var e in t)t.hasOwnProperty(e)&&this.items.push({$key:e,$value:t[e]})},select:function(t,e){var s=this.items.findIndex((function(e){return e[this.props.key]===t}),this);if(this.set("data",this.items[s]||null),e){var i=this.$node;requestAnimationFrame((function(){for(var t=i.querySelectorAll("form"),e=0;e<t.length;e++)t[e].reset()}))}}}),dmx.Component("data-iterator",{initialData:{index:-1,value:null,has:{first:!1,prev:!1,next:!1,last:!1}},attributes:{data:{type:Array,default:[]},index:{type:Number,default:0},loop:{type:Boolean,default:!1}},methods:{first:function(){this.first()},prev:function(){this.prev()},next:function(){this.next()},last:function(){this.last()},random:function(){this.random()},select:function(t){this.select(t)}},render:function(t){this.items=[],this.setData(this.props.data),this.select(this.props.index),this.$parse()},update:function(t,e){e.has("data")?(this.setData(this.props.data),this.select(this.props.index)):t.index!==this.props.index&&this.select(this.props.index)},setData:function(t){t&&(Array.isArray(t)?this.items=t:console.warn("Iterator data is not array but a "+typeof t))},first:function(){this.items.length&&this.select(0)},prev:function(){this.items.length&&(index=this.data.index-1,index<0&&this.props.loop&&(index=this.items.length-1),this.select(index))},next:function(){this.items.length&&(index=this.data.index+1,index>=this.items.length&&this.props.loop&&(index=0),this.select(index))},last:function(){this.items.length&&this.select(this.items.length-1)},random:function(){this.items.length&&this.select(Math.floor(this.items.length*Math.random()))},select:function(t){if(t=parseInt(t,10),this.items.length){var e=this.items.length-1;t<0&&(t=0),t>=this.items.length&&(t=e),this.set("index",t),this.set("value",this.items[t]),this.set("has",{first:t>0,prev:t>0,next:t<e,last:t<e})}else this.set("index",-1),this.set("value",null),this.set("has",{first:!1,prev:!1,next:!1,last:!1})}});
//# sourceMappingURL=../maps/dmxDataTraversal.js.map
