123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983 |
- (function($, window, document) {
- function fnInvertKeyValues( aIn )
- {
- var aRet=[];
- for ( var i=0, iLen=aIn.length ; i<iLen ; i++ )
- {
- aRet[ aIn[i] ] = i;
- }
- return aRet;
- }
- function fnArraySwitch( aArray, iFrom, iTo )
- {
- var mStore = aArray.splice( iFrom, 1 )[0];
- aArray.splice( iTo, 0, mStore );
- }
- function fnDomSwitch( nParent, iFrom, iTo )
- {
- var anTags = [];
- for ( var i=0, iLen=nParent.childNodes.length ; i<iLen ; i++ )
- {
- if ( nParent.childNodes[i].nodeType == 1 )
- {
- anTags.push( nParent.childNodes[i] );
- }
- }
- var nStore = anTags[ iFrom ];
-
- if ( iTo !== null )
- {
- nParent.insertBefore( nStore, anTags[iTo] );
- }
- else
- {
- nParent.appendChild( nStore );
- }
- }
- $.fn.dataTableExt.oApi.fnColReorder = function ( oSettings, iFrom, iTo )
- {
- var i, iLen, j, jLen, iCols=oSettings.aoColumns.length, nTrs, oCol;
-
-
- if ( iFrom == iTo )
- {
-
- return;
- }
-
- if ( iFrom < 0 || iFrom >= iCols )
- {
- this.oApi._fnLog( oSettings, 1, "ColReorder 'from' index is out of bounds: "+iFrom );
- return;
- }
-
- if ( iTo < 0 || iTo >= iCols )
- {
- this.oApi._fnLog( oSettings, 1, "ColReorder 'to' index is out of bounds: "+iTo );
- return;
- }
-
-
- var aiMapping = [];
- for ( i=0, iLen=iCols ; i<iLen ; i++ )
- {
- aiMapping[i] = i;
- }
- fnArraySwitch( aiMapping, iFrom, iTo );
- var aiInvertMapping = fnInvertKeyValues( aiMapping );
-
-
-
-
- for ( i=0, iLen=oSettings.aaSorting.length ; i<iLen ; i++ )
- {
- oSettings.aaSorting[i][0] = aiInvertMapping[ oSettings.aaSorting[i][0] ];
- }
-
-
- if ( oSettings.aaSortingFixed !== null )
- {
- for ( i=0, iLen=oSettings.aaSortingFixed.length ; i<iLen ; i++ )
- {
- oSettings.aaSortingFixed[i][0] = aiInvertMapping[ oSettings.aaSortingFixed[i][0] ];
- }
- }
-
-
- for ( i=0, iLen=iCols ; i<iLen ; i++ )
- {
- oCol = oSettings.aoColumns[i];
- for ( j=0, jLen=oCol.aDataSort.length ; j<jLen ; j++ )
- {
- oCol.aDataSort[j] = aiInvertMapping[ oCol.aDataSort[j] ];
- }
- }
-
-
- for ( i=0, iLen=iCols ; i<iLen ; i++ )
- {
- oCol = oSettings.aoColumns[i];
- if ( typeof oCol.mData == 'number' ) {
- oCol.mData = aiInvertMapping[ oCol.mData ];
- oCol.fnGetData = oSettings.oApi._fnGetObjectDataFn( oCol.mData );
- oCol.fnSetData = oSettings.oApi._fnSetObjectDataFn( oCol.mData );
- }
- }
-
-
-
- if ( oSettings.aoColumns[iFrom].bVisible )
- {
-
- var iVisibleIndex = this.oApi._fnColumnIndexToVisible( oSettings, iFrom );
- var iInsertBeforeIndex = null;
-
- i = iTo < iFrom ? iTo : iTo + 1;
- while ( iInsertBeforeIndex === null && i < iCols )
- {
- iInsertBeforeIndex = this.oApi._fnColumnIndexToVisible( oSettings, i );
- i++;
- }
-
-
- nTrs = oSettings.nTHead.getElementsByTagName('tr');
- for ( i=0, iLen=nTrs.length ; i<iLen ; i++ )
- {
- fnDomSwitch( nTrs[i], iVisibleIndex, iInsertBeforeIndex );
- }
-
-
- if ( oSettings.nTFoot !== null )
- {
- nTrs = oSettings.nTFoot.getElementsByTagName('tr');
- for ( i=0, iLen=nTrs.length ; i<iLen ; i++ )
- {
- fnDomSwitch( nTrs[i], iVisibleIndex, iInsertBeforeIndex );
- }
- }
-
-
- for ( i=0, iLen=oSettings.aoData.length ; i<iLen ; i++ )
- {
- if ( oSettings.aoData[i].nTr !== null )
- {
- fnDomSwitch( oSettings.aoData[i].nTr, iVisibleIndex, iInsertBeforeIndex );
- }
- }
- }
-
-
-
-
- fnArraySwitch( oSettings.aoColumns, iFrom, iTo );
-
-
- fnArraySwitch( oSettings.aoPreSearchCols, iFrom, iTo );
-
-
- for ( i=0, iLen=oSettings.aoData.length ; i<iLen ; i++ )
- {
- if ( $.isArray( oSettings.aoData[i]._aData ) ) {
- fnArraySwitch( oSettings.aoData[i]._aData, iFrom, iTo );
- }
- fnArraySwitch( oSettings.aoData[i]._anHidden, iFrom, iTo );
- }
-
-
- for ( i=0, iLen=oSettings.aoHeader.length ; i<iLen ; i++ )
- {
- fnArraySwitch( oSettings.aoHeader[i], iFrom, iTo );
- }
-
- if ( oSettings.aoFooter !== null )
- {
- for ( i=0, iLen=oSettings.aoFooter.length ; i<iLen ; i++ )
- {
- fnArraySwitch( oSettings.aoFooter[i], iFrom, iTo );
- }
- }
-
-
-
-
-
- for ( i=0, iLen=iCols ; i<iLen ; i++ )
- {
- $(oSettings.aoColumns[i].nTh).unbind('click');
- this.oApi._fnSortAttachListener( oSettings, oSettings.aoColumns[i].nTh, i );
- }
-
-
-
- $(oSettings.oInstance).trigger( 'column-reorder', [ oSettings, {
- "iFrom": iFrom,
- "iTo": iTo,
- "aiInvertMapping": aiInvertMapping
- } ] );
-
- if ( typeof oSettings.oInstance._oPluginFixedHeader != 'undefined' )
- {
- oSettings.oInstance._oPluginFixedHeader.fnUpdate();
- }
- };
- ColReorder = function( oDTSettings, oOpts )
- {
-
- if ( !this.CLASS || this.CLASS != "ColReorder" )
- {
- alert( "Warning: ColReorder must be initialised with the keyword 'new'" );
- }
-
- if ( typeof oOpts == 'undefined' )
- {
- oOpts = {};
- }
-
-
-
-
-
- this.s = {
-
- "dt": null,
-
-
- "init": oOpts,
-
-
- "fixed": 0,
-
-
- "dropCallback": null,
-
-
- "mouse": {
- "startX": -1,
- "startY": -1,
- "offsetX": -1,
- "offsetY": -1,
- "target": -1,
- "targetIndex": -1,
- "fromIndex": -1
- },
-
-
- "aoTargets": []
- };
-
-
-
- this.dom = {
-
- "drag": null,
-
-
- "pointer": null
- };
-
-
-
- this.s.dt = oDTSettings.oInstance.fnSettings();
- this._fnConstruct();
-
- oDTSettings.oApi._fnCallbackReg(oDTSettings, 'aoDestroyCallback', jQuery.proxy(this._fnDestroy, this), 'ColReorder');
-
- ColReorder.aoInstances.push( this );
- return this;
- };
- ColReorder.prototype = {
-
-
- "fnReset": function ()
- {
- var a = [];
- for ( var i=0, iLen=this.s.dt.aoColumns.length ; i<iLen ; i++ )
- {
- a.push( this.s.dt.aoColumns[i]._ColReorder_iOrigCol );
- }
-
- this._fnOrderColumns( a );
- },
-
-
-
-
-
- "_fnConstruct": function ()
- {
- var that = this;
- var i, iLen;
-
-
- if ( typeof this.s.init.iFixedColumns != 'undefined' )
- {
- this.s.fixed = this.s.init.iFixedColumns;
- }
-
-
- if ( typeof this.s.init.fnReorderCallback != 'undefined' )
- {
- this.s.dropCallback = this.s.init.fnReorderCallback;
- }
-
-
- for ( i=0, iLen=this.s.dt.aoColumns.length ; i<iLen ; i++ )
- {
- if ( i > this.s.fixed-1 )
- {
- this._fnMouseListener( i, this.s.dt.aoColumns[i].nTh );
- }
-
-
- this.s.dt.aoColumns[i]._ColReorder_iOrigCol = i;
- }
-
-
- this.s.dt.oApi._fnCallbackReg( this.s.dt, 'aoStateSaveParams', function (oS, oData) {
- that._fnStateSave.call( that, oData );
- }, "ColReorder_State" );
-
-
- var aiOrder = null;
- if ( typeof this.s.init.aiOrder != 'undefined' )
- {
- aiOrder = this.s.init.aiOrder.slice();
- }
-
-
- if ( this.s.dt.oLoadedState && typeof this.s.dt.oLoadedState.ColReorder != 'undefined' &&
- this.s.dt.oLoadedState.ColReorder.length == this.s.dt.aoColumns.length )
- {
- aiOrder = this.s.dt.oLoadedState.ColReorder;
- }
-
-
- if ( aiOrder )
- {
-
- if ( !that.s.dt._bInitComplete )
- {
- var bDone = false;
- this.s.dt.aoDrawCallback.push( {
- "fn": function () {
- if ( !that.s.dt._bInitComplete && !bDone )
- {
- bDone = true;
- var resort = fnInvertKeyValues( aiOrder );
- that._fnOrderColumns.call( that, resort );
- }
- },
- "sName": "ColReorder_Pre"
- } );
- }
- else
- {
- var resort = fnInvertKeyValues( aiOrder );
- that._fnOrderColumns.call( that, resort );
- }
- }
- },
-
-
-
- "_fnOrderColumns": function ( a )
- {
- if ( a.length != this.s.dt.aoColumns.length )
- {
- this.s.dt.oInstance.oApi._fnLog( this.s.dt, 1, "ColReorder - array reorder does not "+
- "match known number of columns. Skipping." );
- return;
- }
-
- for ( var i=0, iLen=a.length ; i<iLen ; i++ )
- {
- var currIndex = $.inArray( i, a );
- if ( i != currIndex )
- {
-
- fnArraySwitch( a, currIndex, i );
-
-
- this.s.dt.oInstance.fnColReorder( currIndex, i );
- }
- }
-
-
- if ( this.s.dt.oScroll.sX !== "" || this.s.dt.oScroll.sY !== "" )
- {
- this.s.dt.oInstance.fnAdjustColumnSizing();
- }
-
-
- this.s.dt.oInstance.oApi._fnSaveState( this.s.dt );
- },
-
-
-
- "_fnStateSave": function ( oState )
- {
- var i, iLen, aCopy, iOrigColumn;
- var oSettings = this.s.dt;
-
- for ( i=0 ; i<oState.aaSorting.length ; i++ )
- {
- oState.aaSorting[i][0] = oSettings.aoColumns[ oState.aaSorting[i][0] ]._ColReorder_iOrigCol;
- }
- aSearchCopy = $.extend( true, [], oState.aoSearchCols );
- oState.ColReorder = [];
- for ( i=0, iLen=oSettings.aoColumns.length ; i<iLen ; i++ )
- {
- iOrigColumn = oSettings.aoColumns[i]._ColReorder_iOrigCol;
-
- oState.aoSearchCols[ iOrigColumn ] = aSearchCopy[i];
-
- oState.abVisCols[ iOrigColumn ] = oSettings.aoColumns[i].bVisible;
-
-
- oState.ColReorder.push( iOrigColumn );
- }
- },
-
-
-
-
-
- "_fnMouseListener": function ( i, nTh )
- {
- var that = this;
- $(nTh).bind( 'mousedown.ColReorder', function (e) {
- e.preventDefault();
- that._fnMouseDown.call( that, e, nTh );
- } );
- },
-
-
-
- "_fnMouseDown": function ( e, nTh )
- {
- var
- that = this,
- aoColumns = this.s.dt.aoColumns;
-
-
- var nThTarget = e.target.nodeName == "TH" ? e.target : $(e.target).parents('TH')[0];
- var offset = $(nThTarget).offset();
- this.s.mouse.startX = e.pageX;
- this.s.mouse.startY = e.pageY;
- this.s.mouse.offsetX = e.pageX - offset.left;
- this.s.mouse.offsetY = e.pageY - offset.top;
- this.s.mouse.target = nTh;
- this.s.mouse.targetIndex = $('th', nTh.parentNode).index( nTh );
- this.s.mouse.fromIndex = this.s.dt.oInstance.oApi._fnVisibleToColumnIndex( this.s.dt,
- this.s.mouse.targetIndex );
-
-
- this.s.aoTargets.splice( 0, this.s.aoTargets.length );
-
- this.s.aoTargets.push( {
- "x": $(this.s.dt.nTable).offset().left,
- "to": 0
- } );
-
- var iToPoint = 0;
- for ( var i=0, iLen=aoColumns.length ; i<iLen ; i++ )
- {
-
- if ( i != this.s.mouse.fromIndex )
- {
- iToPoint++;
- }
-
- if ( aoColumns[i].bVisible )
- {
- this.s.aoTargets.push( {
- "x": $(aoColumns[i].nTh).offset().left + $(aoColumns[i].nTh).outerWidth(),
- "to": iToPoint
- } );
- }
- }
-
-
- if ( this.s.fixed !== 0 )
- {
- this.s.aoTargets.splice( 0, this.s.fixed );
- }
-
-
- $(document).bind( 'mousemove.ColReorder', function (e) {
- that._fnMouseMove.call( that, e );
- } );
-
- $(document).bind( 'mouseup.ColReorder', function (e) {
- that._fnMouseUp.call( that, e );
- } );
- },
-
-
-
- "_fnMouseMove": function ( e )
- {
- var that = this;
-
- if ( this.dom.drag === null )
- {
-
- if ( Math.pow(
- Math.pow(e.pageX - this.s.mouse.startX, 2) +
- Math.pow(e.pageY - this.s.mouse.startY, 2), 0.5 ) < 5 )
- {
- return;
- }
- this._fnCreateDragNode();
- }
-
-
- this.dom.drag.style.left = (e.pageX - this.s.mouse.offsetX) + "px";
- this.dom.drag.style.top = (e.pageY - this.s.mouse.offsetY) + "px";
-
-
- var bSet = false;
- for ( var i=1, iLen=this.s.aoTargets.length ; i<iLen ; i++ )
- {
- if ( e.pageX < this.s.aoTargets[i-1].x + ((this.s.aoTargets[i].x-this.s.aoTargets[i-1].x)/2) )
- {
- this.dom.pointer.style.left = this.s.aoTargets[i-1].x +"px";
- this.s.mouse.toIndex = this.s.aoTargets[i-1].to;
- bSet = true;
- break;
- }
- }
-
-
- if ( !bSet )
- {
- this.dom.pointer.style.left = this.s.aoTargets[this.s.aoTargets.length-1].x +"px";
- this.s.mouse.toIndex = this.s.aoTargets[this.s.aoTargets.length-1].to;
- }
- },
-
-
-
- "_fnMouseUp": function ( e )
- {
- var that = this;
-
- $(document).unbind( 'mousemove.ColReorder' );
- $(document).unbind( 'mouseup.ColReorder' );
-
- if ( this.dom.drag !== null )
- {
-
- document.body.removeChild( this.dom.drag );
- document.body.removeChild( this.dom.pointer );
- this.dom.drag = null;
- this.dom.pointer = null;
-
-
- this.s.dt.oInstance.fnColReorder( this.s.mouse.fromIndex, this.s.mouse.toIndex );
-
-
- if ( this.s.dt.oScroll.sX !== "" || this.s.dt.oScroll.sY !== "" )
- {
- this.s.dt.oInstance.fnAdjustColumnSizing();
- }
-
- if ( this.s.dropCallback !== null )
- {
- this.s.dropCallback.call( this );
- }
-
-
- this.s.dt.oInstance.oApi._fnSaveState( this.s.dt );
- }
- },
-
-
-
- "_fnCreateDragNode": function ()
- {
- var that = this;
-
- this.dom.drag = $(this.s.dt.nTHead.parentNode).clone(true)[0];
- this.dom.drag.className += " DTCR_clonedTable";
- while ( this.dom.drag.getElementsByTagName('caption').length > 0 )
- {
- this.dom.drag.removeChild( this.dom.drag.getElementsByTagName('caption')[0] );
- }
- while ( this.dom.drag.getElementsByTagName('tbody').length > 0 )
- {
- this.dom.drag.removeChild( this.dom.drag.getElementsByTagName('tbody')[0] );
- }
- while ( this.dom.drag.getElementsByTagName('tfoot').length > 0 )
- {
- this.dom.drag.removeChild( this.dom.drag.getElementsByTagName('tfoot')[0] );
- }
-
- $('thead tr:eq(0)', this.dom.drag).each( function () {
- $('th', this).eq(that.s.mouse.targetIndex).siblings().remove();
- } );
- $('tr', this.dom.drag).height( $('tr:eq(0)', that.s.dt.nTHead).height() );
-
- $('thead tr:gt(0)', this.dom.drag).remove();
-
- $('thead th:eq(0)', this.dom.drag).each( function (i) {
- this.style.width = $('th:eq('+that.s.mouse.targetIndex+')', that.s.dt.nTHead).width()+"px";
- } );
-
- this.dom.drag.style.position = "absolute";
- this.dom.drag.style.top = "0px";
- this.dom.drag.style.left = "0px";
- this.dom.drag.style.width = $('th:eq('+that.s.mouse.targetIndex+')', that.s.dt.nTHead).outerWidth()+"px";
-
-
- this.dom.pointer = document.createElement( 'div' );
- this.dom.pointer.className = "DTCR_pointer";
- this.dom.pointer.style.position = "absolute";
-
- if ( this.s.dt.oScroll.sX === "" && this.s.dt.oScroll.sY === "" )
- {
- this.dom.pointer.style.top = $(this.s.dt.nTable).offset().top+"px";
- this.dom.pointer.style.height = $(this.s.dt.nTable).height()+"px";
- }
- else
- {
- this.dom.pointer.style.top = $('div.dataTables_scroll', this.s.dt.nTableWrapper).offset().top+"px";
- this.dom.pointer.style.height = $('div.dataTables_scroll', this.s.dt.nTableWrapper).height()+"px";
- }
-
- document.body.appendChild( this.dom.pointer );
- document.body.appendChild( this.dom.drag );
- },
-
- "_fnDestroy": function ()
- {
- for ( var i=0, iLen=ColReorder.aoInstances.length ; i<iLen ; i++ )
- {
- if ( ColReorder.aoInstances[i] === this )
- {
- ColReorder.aoInstances.splice( i, 1 );
- break;
- }
- }
- $(this.s.dt.nTHead).find( '*' ).unbind( '.ColReorder' );
- this.s.dt.oInstance._oPluginColReorder = null;
- this.s = null;
- }
- };
- ColReorder.aoInstances = [];
- ColReorder.fnReset = function ( oTable )
- {
- for ( var i=0, iLen=ColReorder.aoInstances.length ; i<iLen ; i++ )
- {
- if ( ColReorder.aoInstances[i].s.dt.oInstance == oTable )
- {
- ColReorder.aoInstances[i].fnReset();
- }
- }
- };
- ColReorder.prototype.CLASS = "ColReorder";
- ColReorder.VERSION = "1.0.8";
- ColReorder.prototype.VERSION = ColReorder.VERSION;
- if ( typeof $.fn.dataTable == "function" &&
- typeof $.fn.dataTableExt.fnVersionCheck == "function" &&
- $.fn.dataTableExt.fnVersionCheck('1.9.3') )
- {
- $.fn.dataTableExt.aoFeatures.push( {
- "fnInit": function( oDTSettings ) {
- var oTable = oDTSettings.oInstance;
- if ( typeof oTable._oPluginColReorder == 'undefined' ) {
- var opts = typeof oDTSettings.oInit.oColReorder != 'undefined' ?
- oDTSettings.oInit.oColReorder : {};
- oTable._oPluginColReorder = new ColReorder( oDTSettings, opts );
- } else {
- oTable.oApi._fnLog( oDTSettings, 1, "ColReorder attempted to initialise twice. Ignoring second" );
- }
-
- return null;
- },
- "cFeature": "R",
- "sFeature": "ColReorder"
- } );
- }
- else
- {
- alert( "Warning: ColReorder requires DataTables 1.9.3 or greater - www.datatables.net/download");
- }
- })(jQuery, window, document);
|