UNPKG

datatables.net-colreorder

Version:
7 lines 11 kB
/*! ColReorder 2.1.1 * © SpryMedia Ltd - datatables.net/license */ import jQuery from"jquery";import DataTable from"datatables.net";let $=jQuery;function arrayMove(t,e,r,o){var n=t.splice(e,r);n.unshift(0),n.unshift(o<e?o:o-r+1),t.splice.apply(t,n)}function finalise(t){t.rows().invalidate("data"),t.column(0).visible(t.column(0).visible()),t.columns.adjust();var e=t.colReorder.order();t.trigger("columns-reordered",[{order:e,mapping:invertKeyValues(e)}])}function getOrder(t){return t.settings()[0].aoColumns.map(function(t){return t._crOriginalIdx})}function headerUpdate(t,e,r,o){for(var n=[],i=0;i<t.length;i++){var s=t[i];arrayMove(s,r[0],r.length,o);for(var a=0;a<s.length;a++){var l,d=s[a].cell;n.includes(d)||(l=d.getAttribute("data-dt-column").split(",").map(function(t){return e[t]}).join(","),d.setAttribute("data-dt-column",l),n.push(d))}}}function init(t){t.columns().iterator("column",function(t,e){t=t.aoColumns;void 0===t[e]._crOriginalIdx&&(t[e]._crOriginalIdx=e)})}function invertKeyValues(t){for(var e=[],r=0;r<t.length;r++)e[t[r]]=r;return e}function move(t,e,r){var o,n=t.settings()[0],i=n.aoColumns,s=i.map(function(t,e){return e});if(!e.includes(r)){arrayMove(s,e[0],e.length,r);var a=invertKeyValues(s);for(arrayMove(i,e[0],e.length,r),o=0;o<n.aoData.length;o++){var l=n.aoData[o];if(l){var d=l.anCells;if(d)for(arrayMove(d,e[0],e.length,r),c=0;c<d.length;c++)l.nTr&&d[c]&&i[c].bVisible&&l.nTr.appendChild(d[c]),d[c]&&d[c]._DT_CellIndex&&(d[c]._DT_CellIndex.column=c)}}for(o=0;o<i.length;o++){for(var u=i[o],c=0;c<u.aDataSort.length;c++)u.aDataSort[c]=a[u.aDataSort[c]];u.idx=a[u.idx],u.bVisible&&n.colgroup.append(u.colEl)}headerUpdate(n.aoHeader,a,e,r),headerUpdate(n.aoFooter,a,e,r),arrayMove(n.aoPreSearchCols,e[0],e.length,r),orderingIndexes(a,n.aaSorting),Array.isArray(n.aaSortingFixed)?orderingIndexes(a,n.aaSortingFixed):(n.aaSortingFixed.pre||n.aaSortingFixed.post)&&orderingIndexes(a,n.aaSortingFixed.pre),n.aLastSort.forEach(function(t){t.src=a[t.src]}),t.trigger("column-reorder",[t.settings()[0],{from:e,to:r,mapping:a}])}}function orderingIndexes(t,e){if(e)for(var r=0;r<e.length;r++){var o=e[r];"number"==typeof o?e[r]=t[o]:$.isPlainObject(o)&&void 0!==o.idx?o.idx=t[o.idx]:Array.isArray(o)&&"number"==typeof o[0]&&(o[0]=t[o[0]])}}function setOrder(t,e,r){var o=!1;if(e.length!==t.columns().count())t.error("ColReorder - column count mismatch");else{for(var n=invertKeyValues(e=r?transpose(t,e,"toCurrent"):e),i=0;i<n.length;i++){var s=n.indexOf(i);i!==s&&(arrayMove(n,s,1,i),move(t,[s],i),o=!0)}o&&finalise(t)}}function structureFill(t){for(var e=[],r=0;r<t.length;r++){e.push([]);for(var o=0;o<t[r].length;o++){var n=t[r][o];if(n)for(var i=0;i<n.rowspan;i++){e[r+i]||(e[r+i]=[]);for(var s=0;s<n.colspan;s++)e[r+i][o+s]=n.cell}}}return e}function transpose(t,e,r){var o=t.colReorder.order(),n=t.settings()[0].aoColumns;return"toCurrent"===r||"fromOriginal"===r?Array.isArray(e)?e.map(function(t){return o.indexOf(t)}):o.indexOf(e):Array.isArray(e)?e.map(function(t){return n[t]._crOriginalIdx}):n[e]._crOriginalIdx}function validateMove(t,e,r){var o=t.columns().count();return!(e[0]<r&&r<e[e.length]||e[0]<0&&e[e.length-1]>o||r<0&&o<r||!e.includes(r)&&(!validateStructureMove(t.table().header.structure(),e,r)||!validateStructureMove(t.table().footer.structure(),e,r)))}function validateStructureMove(t,e,r){for(var o=structureFill(t),n=0;n<o.length;n++)arrayMove(o[n],e[0],e.length,r);for(n=0;n<o.length;n++)for(var i=[],s=0;s<o[n].length;s++){var a=o[n][s];if(i.includes(a)){if(i[i.length-1]!==a)return!1}else i.push(a)}return!0}var ColReorder=(()=>{function r(o,n){this.dom={drag:null},this.c={columns:null,enable:null,headerRows:null,order:null},this.s={dropZones:[],mouse:{absLeft:-1,offset:{x:-1,y:-1},start:{x:-1,y:-1},target:null,targets:[]},scrollInterval:null};var t,e,i=this;o.settings()[0]._colReorder||((o.settings()[0]._colReorder=this).dt=o,$.extend(this.c,r.defaults,n),init(o),o.on("stateSaveParams",function(t,e,r){r.colReorder=getOrder(o)}),o.on("destroy",function(){o.off(".colReorder"),o.colReorder.reset()}),t=o.state.loaded(),e=this.c.order,(e=t&&t.colReorder&&o.columns().count()===t.colReorder.length?t.colReorder:e)&&o.ready(function(){setOrder(o,e,!0)}),o.table().header.structure().forEach(function(t,e){for(var r=n.headerRows,o=0;o<t.length;o++)r&&!r.includes(e)||t[o]&&t[o].cell&&i._addListener(t[o].cell)}))}return r.prototype.disable=function(){return this.c.enable=!1,this},r.prototype.enable=function(t){return!1===(t=void 0===t?!0:t)?this.disable():(this.c.enable=!0,this)},r.prototype._addListener=function(t){var r=this;$(t).on("selectstart.colReorder",function(){return!1}).on("mousedown.colReorder touchstart.colReorder",function(t){var e;"mousedown"===t.type&&1!==t.which||!r.c.enable||(e=$("button.dtcc-button_reorder",this)).length&&t.target!==e[0]&&0===e.find(t.target).length||r._mouseDown(t,this)})},r.prototype._createDragNode=function(){var t=this.s.mouse.target,e=t.parent(),r=e.parent(),o=r.parent(),n=t.clone();this.dom.drag=$(o[0].cloneNode(!1)).addClass("dtcr-cloned").append($(r[0].cloneNode(!1)).append($(e[0].cloneNode(!1)).append(n[0]))).css({position:"absolute",top:0,left:0,width:$(t).outerWidth(),height:$(t).outerHeight()}).appendTo("body")},r.prototype._cursorPosition=function(t,e){return(-1!==t.type.indexOf("touch")?t.originalEvent.touches[0]:t)[e]},r.prototype._mouseDown=function(t,e){for(var r=this,o=$(t.target).closest("th, td"),n=o.offset(),i=this.dt.columns(this.c.columns).indexes().toArray(),s=$(e).attr("data-dt-column").split(",").map(function(t){return parseInt(t,10)}),a=0;a<s.length;a++)if(!i.includes(s[a]))return!1;this.s.mouse.start.x=this._cursorPosition(t,"pageX"),this.s.mouse.start.y=this._cursorPosition(t,"pageY"),this.s.mouse.offset.x=this._cursorPosition(t,"pageX")-n.left,this.s.mouse.offset.y=this._cursorPosition(t,"pageY")-n.top,this.s.mouse.target=o,this.s.mouse.targets=s;for(var l=0;l<s.length;l++){var d=this.dt.cells(null,s[l],{page:"current"}).nodes().to$(),u="dtcr-moving";0===l&&(u+=" dtcr-moving-first"),l===s.length-1&&(u+=" dtcr-moving-last"),d.addClass(u)}this._regions(s),this._scrollRegions(),$(document).on("mousemove.colReorder touchmove.colReorder",function(t){r._mouseMove(t)}).on("mouseup.colReorder touchend.colReorder",function(t){r._mouseUp(t)})},r.prototype._mouseMove=function(t){if(null===this.dom.drag){if(Math.pow(Math.pow(this._cursorPosition(t,"pageX")-this.s.mouse.start.x,2)+Math.pow(this._cursorPosition(t,"pageY")-this.s.mouse.start.y,2),.5)<5)return;$(document.body).addClass("dtcr-dragging"),this._createDragNode()}this.dom.drag.css({left:this._cursorPosition(t,"pageX")-this.s.mouse.offset.x,top:this._cursorPosition(t,"pageY")-this.s.mouse.offset.y});var e,r=this.dt.table().node(),o=$(r).offset().left,o=this._cursorPosition(t,"pageX")-o,r=(e=this._isRtl()?r.clientWidth-o:o,this.s.dropZones.find(function(t){return t.inlineStart<=e&&e<=t.inlineStart+t.width}));this.s.mouse.absLeft=this._cursorPosition(t,"pageX"),r&&!r.self&&this._move(r,e)},r.prototype._mouseUp=function(t){var e=this;$(document).off(".colReorder"),$(document.body).removeClass("dtcr-dragging"),this.dom.drag&&(this.dom.drag.remove(),this.dom.drag=null,this.s.mouse.target.on("click.dtcr",function(t){return!1}),setTimeout(function(){e.s.mouse.target.off(".dtcr")},10)),this.s.scrollInterval&&clearInterval(this.s.scrollInterval),this.dt.cells(".dtcr-moving").nodes().to$().removeClass("dtcr-moving dtcr-moving-first dtcr-moving-last")},r.prototype._move=function(t,e){var r,o,n=this,i=(this.dt.colReorder.move(this.s.mouse.targets,t.colIdx),this.s.mouse.targets=$(this.s.mouse.target).attr("data-dt-column").split(",").map(function(t){return parseInt(t,10)}),this._regions(this.s.mouse.targets),this.s.mouse.targets.filter(function(t){return n.dt.column(t).visible()})),t=this.s.dropZones.find(function(t){return t.colIdx===i[0]}),s=this.s.dropZones.indexOf(t);t.inlineStart>e&&(o=t.inlineStart-e,r=this.s.dropZones[s-1],t.inlineStart-=o,t.width+=o,r)&&(r.width-=o),(t=this.s.dropZones.find(function(t){return t.colIdx===i[i.length-1]})).inlineStart+t.width<e&&(r=e-(t.inlineStart+t.width),o=this.s.dropZones[s+1],t.width+=r,o)&&(o.inlineStart+=r,o.width-=r)},r.prototype._regions=function(n){var i=this,s=[],a=0,l=0,d=this.dt.columns(this.c.columns).indexes().toArray(),u=this.dt.columns().widths();this.dt.columns().every(function(t,e,r){var o;this.visible()&&(o=u[t],d.includes(t)&&(validateMove(i.dt,n,t)?s.push({colIdx:t,inlineStart:a-l,self:n[0]<=t&&t<=n[n.length-1],width:o+l}):t<n[0]?s.length&&(s[s.length-1].width+=o):t>n[n.length-1]&&(l+=o)),a+=o)}),this.s.dropZones=s},r.prototype._isScrolling=function(){return this.dt.table().body().parentNode!==this.dt.table().header().parentNode},r.prototype._scrollRegions=function(){var e,r,o,n;this._isScrolling()&&(e=this,r=$(this.dt.table().container()).offset().left,o=$(this.dt.table().container()).outerWidth(),n=this.dt.table().body().parentElement.parentElement,this.s.scrollInterval=setInterval(function(){var t=e.s.mouse.absLeft;-1!==t&&(t<r+75&&n.scrollLeft?n.scrollLeft-=5:r+o-75<t&&n.scrollLeft<n.scrollWidth&&(n.scrollLeft+=5))},25))},r.prototype._isRtl=function(){return"rtl"===$(this.dt.table()).css("direction")},r.defaults={columns:"",enable:!0,headerRows:null,order:null},r.version="2.1.1",r})(); /*! ColReorder 2.1.1 * © SpryMedia Ltd - datatables.net/license */DataTable.Api.register("colReorder.enable()",function(e){return this.iterator("table",function(t){t._colReorder&&t._colReorder.enable(e)})}),DataTable.Api.register("colReorder.disable()",function(){return this.iterator("table",function(t){t._colReorder&&t._colReorder.disable()})}),DataTable.Api.register("colReorder.move()",function(t,e){return init(this),validateMove(this,t=Array.isArray(t)?t:[t],e)?this.tables().every(function(){move(this,t,e),finalise(this)}):(this.error("ColReorder - invalid move"),this)}),DataTable.Api.register("colReorder.order()",function(t,e){return init(this),t?this.tables().every(function(){setOrder(this,t,e)}):this.context.length?getOrder(this):null}),DataTable.Api.register("colReorder.reset()",function(){return init(this),this.tables().every(function(){var t=this.columns().every(function(t){return t}).flatten().toArray();setOrder(this,t,!0)})}),DataTable.Api.register("colReorder.transpose()",function(t,e){return init(this),transpose(this,t,e=e||"toCurrent")}),DataTable.ColReorder=ColReorder,$(document).on("stateLoadInit.dt",function(t,e,r){if("dt"===t.namespace){t=new DataTable.Api(e);if(r.colReorder&&t.columns().count()===r.colReorder.length)if(t.ready())setOrder(t,r.colReorder,!0);else if(orderingIndexes(r.colReorder,r.order),r.columns){for(var o=0;o<r.columns.length;o++)r.columns[o]._cr_sort=r.colReorder[o];r.columns.sort(function(t,e){return t._cr_sort-e._cr_sort})}}}),$(document).on("preInit.dt",function(t,e){var r,o;"dt"===t.namespace&&(t=e.oInit.colReorder,o=DataTable.defaults.colReorder,t||o)&&(r=$.extend({},o,t),!1!==t)&&(o=new DataTable.Api(e),new ColReorder(o,r))});export default DataTable;