15 lines
No EOL
7.1 KiB
JavaScript
15 lines
No EOL
7.1 KiB
JavaScript
/*!
|
|
* dragtable
|
|
*
|
|
* @Version 2.0.15
|
|
*
|
|
* Copyright (c) 2010-2013, Andres akottr@gmail.com
|
|
* Dual licensed under the MIT (MIT-LICENSE.txt)
|
|
* and GPL (GPL-LICENSE.txt) licenses.
|
|
*
|
|
* Inspired by the the dragtable from Dan Vanderkam (danvk.org/dragtable/)
|
|
* Thanks to the jquery and jqueryui comitters
|
|
*
|
|
* Any comment, bug report, feature-request is welcome
|
|
* Feel free to contact me.
|
|
*/ !function(e){e.widget("akottr.dragtable",{options:{revert:!1,dragHandle:".table-handle",maxMovingRows:40,excludeFooter:!1,onlyHeaderThreshold:100,dragaccept:null,persistState:null,restoreState:null,exact:!0,clickDelay:10,containment:null,cursor:"move",cursorAt:!1,distance:0,tolerance:"pointer",axis:"x",beforeStart:e.noop,beforeMoving:e.noop,beforeReorganize:e.noop,beforeStop:e.noop},originalTable:{el:null,selectedHandle:null,sortOrder:null,startIndex:0,endIndex:0},sortableTable:{el:e(),selectedHandle:e(),movingRow:e()},persistState:function(){var t=this;this.originalTable.el.find("th").each(function(e){""!==this.id&&(t.originalTable.sortOrder[this.id]=e)}),e.ajax({url:this.options.persistState,data:this.originalTable.sortOrder})},_restoreState:function(t){for(var i in t)this.originalTable.startIndex=e("#"+i).closest("th").prevAll().length+1,this.originalTable.endIndex=parseInt(t[i],10)+1,this._bubbleCols()},_bubbleCols:function(){var e,t,i,o,l=this.originalTable.startIndex,a=this.originalTable.endIndex,s=this.originalTable.el.children();if(this.options.excludeFooter&&(s=s.not("tfoot")),l<a)for(e=l;e<a;e++)for(t=0,i=s.find("> tr > td:nth-child("+e+")").add(s.find("> tr > th:nth-child("+e+")")),o=s.find("> tr > td:nth-child("+(e+1)+")").add(s.find("> tr > th:nth-child("+(e+1)+")"));t<i.length;t++)n(i[t],o[t]);else for(e=l;e>a;e--)for(t=0,i=s.find("> tr > td:nth-child("+e+")").add(s.find("> tr > th:nth-child("+e+")")),o=s.find("> tr > td:nth-child("+(e-1)+")").add(s.find("> tr > th:nth-child("+(e-1)+")"));t<i.length;t++)n(i[t],o[t])},_rearrangeTableBackroundProcessing:function(){var n=this;return function(){n._bubbleCols(),n.options.beforeStop(n.originalTable),n.sortableTable.el.remove(),e("#__dragtable_disable_text_selection__").remove(),t?e(document.body).attr("onselectstart",t):e(document.body).removeAttr("onselectstart"),i?e(document.body).attr("unselectable",i):e(document.body).removeAttr("unselectable"),null!==n.options.persistState&&(e.isFunction(n.options.persistState)?n.options.persistState(n.originalTable):n.persistState())}},_rearrangeTable:function(){var e=this;return function(){e.originalTable.selectedHandle.removeClass("dragtable-handle-selected"),e.sortableTable.el.sortable("disable"),e.sortableTable.el.addClass("dragtable-disabled"),e.options.beforeReorganize(e.originalTable,e.sortableTable),e.originalTable.endIndex=e.sortableTable.movingRow.prevAll().length+1,setTimeout(e._rearrangeTableBackroundProcessing(),50)}},_generateSortable:function(t){t.cancelBubble||(t.cancelBubble=!0);for(var i=this,n=this.originalTable.el[0].attributes,o="",l=0;l<n.length;l++)n[l].nodeValue&&"id"!=n[l].nodeName&&"width"!=n[l].nodeName&&(o+=n[l].nodeName+'="'+n[l].nodeValue+'" ');var a=[],s=[];this.originalTable.el.find("tr").slice(0,this.options.maxMovingRows).each(function(t,i){for(var n=this.attributes,o="",l=0;l<n.length;l++)n[l].nodeValue&&"id"!=n[l].nodeName&&(o+=" "+n[l].nodeName+'="'+n[l].nodeValue+'"');a.push(o),s.push(e(this).height())});var r=[],d=0,h=i.originalTable.el.children();if(this.options.excludeFooter&&(h=h.not("tfoot")),h.find("> tr > th").each(function(t,i){var n=e(this).is(":visible")?e(this).outerWidth():0;r.push(n),d+=n}),i.options.exact){var b=d-i.originalTable.el.outerWidth();r[0]-=b}var c='<ul class="dragtable-sortable" style="position:absolute; width:'+(d+=2)+'px;">';h.find("> tr > th").each(function(t,n){c+='<li style="width:'+(e(this).is(":visible")?e(this).outerWidth():0)+'px;">',c+="<table "+o+">";var l=h.find("> tr > th:nth-child("+(t+1)+")");i.options.maxMovingRows>1&&(l=l.add(h.find("> tr > td:nth-child("+(t+1)+")").slice(0,i.options.maxMovingRows-1))),l.each(function(t){var i=e(this).clone().wrap("<div></div>").parent().html();0===i.toLowerCase().indexOf("<th")&&(c+="<thead>"),c+="<tr "+a[t]+'" style="height:'+s[t]+'px;">',c+=i,0===i.toLowerCase().indexOf("<th")&&(c+="</thead>"),c+="</tr>"}),c+="</table>",c+="</li>"}),c+="</ul>",this.sortableTable.el=this.originalTable.el.before(c).prev(),this.sortableTable.el.find("> li > table").each(function(t,i){e(this).css("width",r[t]+"px")}),this.sortableTable.selectedHandle=this.sortableTable.el.find("th .dragtable-handle-selected");var g,p=this.options.dragaccept?"li:has("+this.options.dragaccept+")":"li";this.sortableTable.el.sortable({items:p,stop:this._rearrangeTable(),revert:this.options.revert,tolerance:this.options.tolerance,containment:this.options.containment,cursor:this.options.cursor,cursorAt:this.options.cursorAt,distance:this.options.distance,axis:this.options.axis}),this.originalTable.startIndex=e(t.target).closest("th").prevAll().length+1,this.options.beforeMoving(this.originalTable,this.sortableTable),this.sortableTable.movingRow=this.sortableTable.el.find("> li:nth-child("+this.originalTable.startIndex+")"),g=e('<style id="__dragtable_disable_text_selection__" type="text/css">body { -ms-user-select:none;-moz-user-select:-moz-none;-khtml-user-select:none;-webkit-user-select:none;user-select:none; }</style>'),e(document.head).append(g),e(document.body).attr("onselectstart","return false;").attr("unselectable","on"),window.getSelection?window.getSelection().removeAllRanges():document.selection.empty(),this.sortableTable.movingRow.trigger(e.extend(e.Event(t.type),{which:1,clientX:t.clientX,clientY:t.clientY,pageX:t.pageX,pageY:t.pageY,screenX:t.screenX,screenY:t.screenY}));var u=this.sortableTable.el.find(".ui-sortable-placeholder");0>=!u.height()&&u.css("height",this.sortableTable.el.find(".ui-sortable-helper").height()),u.html('<div class="outer" style="height:100%;"><div class="inner" style="height:100%;"></div></div>')},bindTo:{},_create:function(){this.originalTable={el:this.element,selectedHandle:e(),sortOrder:{},startIndex:0,endIndex:0},this.bindTo=this.originalTable.el.find("th"),this.options.dragaccept&&(this.bindTo=this.bindTo.filter(this.options.dragaccept)),this.bindTo.find(this.options.dragHandle).length>0&&(this.bindTo=this.bindTo.find(this.options.dragHandle)),null!==this.options.restoreState&&(e.isFunction(this.options.restoreState)?this.options.restoreState(this.originalTable):this._restoreState(this.options.restoreState));var t=this;this.bindTo.mousedown(function(i){1===i.which&&!1!==t.options.beforeStart(t.originalTable)&&(clearTimeout(this.downTimer),this.downTimer=setTimeout(function(){t.originalTable.selectedHandle=e(this),t.originalTable.selectedHandle.addClass("dragtable-handle-selected"),t._generateSortable(i)},t.options.clickDelay))}).mouseup(function(e){clearTimeout(this.downTimer)})},redraw:function(){this.destroy(),this._create()},destroy:function(){this.bindTo.unbind("mousedown"),e.Widget.prototype.destroy.apply(this,arguments)}});var t=e(document.body).attr("onselectstart"),i=e(document.body).attr("unselectable");function n(e,t){var i=e.parentNode,n=e.nextSibling===t?e:e.nextSibling;t.parentNode.insertBefore(e,t),i.insertBefore(t,n)}}(jQuery); |