-
Notifications
You must be signed in to change notification settings - Fork 1
/
selecTable.min.js
1 lines (1 loc) · 3.81 KB
/
selecTable.min.js
1
!function(e,t,s){function l(t,s){this.element=t,this.options=e.extend({},v,s),this._defaults=v,this._name=w,this.init()}var n="string",i=1,o=16,c=37,r=38,u=39,a=40,h=[o,c,r,u,a],d="cell",f="selecTable",p="selected",m="cursored",w="selecTable",b=t.document,v={};l.prototype.clearSelectedCells=function(){e(this.selectedCells).removeClass(p),this.selectedCells=[]},l.prototype.updateSelectedCells=function(t){this.clearSelectedCells();var s,l,n,i,o=this.$cellA,c=this.$cellB,r=o.data(d).split(","),u=c.data(d).split(","),a=Number(r[0]),h=Number(u[0]),f=Number(r[1]),m=Number(u[1]);a>h?(s=h,l=a):(s=a,l=h),f>m?(n=m,i=f):(n=f,i=m);for(var w=[],b=s;l>=b;++b)for(var v=e(this.$rows[b]),C=v.find("td"),g=n;i>=g;++g){var y=C[g];this.selectedCells.push(y),w.push({row:b,col:g}),e(y).addClass(p)}var $={selectedCells:e.extend([],this.selectedCells),selectedCellsNo:w,lastSelectedCell:c[0]};t&&t.pageX&&t.pageY&&($.pageX=t.pageX,$.pageY=t.pageY),e(this.element).trigger("selectionupdate",$)};var C="plugin_"+w,g="table",y=function(t){return e(t).data(C)},$=function(t){var s;return e(t).parents(g).each(function(){var e=y(this);return e?(s=e,!1):void 0}),s},k=function(){return!1},S=function(t){e(t).attr("unselectable","on").css({"-moz-user-select":"none","-o-user-select":"none","-khtml-user-select":"none","-webkit-user-select":"none","-ms-user-select":"none","user-select":"none"}).bind("selectstart",k)},B=function(t){e(t).attr("unselectable","").css({"-moz-user-select":"","-o-user-select":"","-khtml-user-select":"","-webkit-user-select":"","-ms-user-select":"","user-select":""}).unbind("selectstart",k)};l.prototype.set$cellB=function(e){this.$cellB.removeClass(m),this.$cellB=e,this.$cellB.addClass(m)},l.prototype.mousedown=function(t){if(t.which!==i)return!0;var s=$(this);s.isShiftDown?s.selectedCells=[]:s.$cellA=e(t.target),s.isMouseDown=!0,S(b)},l.prototype.mouseenter=function(t){var s=this;setTimeout(function(){var l=$(s);return l.isMouseDown?(l.set$cellB(e(t.target)),l.updateSelectedCells(),void 0):!0},0)},l.prototype.mouseup=function(t){if(t.which!==i)return!0;var s=$(this);s.isMouseDown=!1,s.set$cellB(e(t.target)),s.updateSelectedCells(t),B(b)},l.prototype.keydown=function(t){var s=y(this),l=t.which,n=!1;if(e.each(h,function(e,t){return l===t?(n=!0,!1):void 0}),!n)return!0;var i,f,p=s.$cellB,m=p.data(d).split(","),w=Number(m[0]),v=Number(m[1]);if(l===o)s.isShiftDown=!0;else{switch(l){case c:i=w,f=v-1,f=f>=0?f:0;break;case u:i=w,f=v+1,f=f<s.numOfCols?f:s.numOfCols-1;break;case r:i=w-1,i=i>=0?i:0,f=v;break;case a:i=w+1,i=i<s.numOfRows?i:s.numOfRows-1,f=v}var C=e(s.$rows[i]),g=e(C.find("td")[f]);s.isShiftDown||(s.$cellA=g),s.set$cellB(g),s.updateSelectedCells()}S(b)},l.prototype.keyup=function(e){var t=y(this);e.which===o&&(t.isShiftDown=!1),B(b)},l.prototype.blur=function(){var t=y(this);t.isMouseDown=!1,t.$cellA=e(),t.clearSelectedCells(),e(t.element).trigger("selectionupdate",{selectedCells:[],selectedCellsNo:[]})};var D=function(t){var s=e(t).find("tr");s.each(function(t){var s=e(this).find("td");s.each(function(s){e(this).data(d,t+","+s)})})};l.prototype.update=function(){var t=e(this.element),s=t.find("tr"),l=t.find("td");this.numOfRows=s.length,this.numOfCols=e(s[0]).find("td").length,this.$rows=s,D(this.element),this.$cellA=e(),this.$cellB=e(),this.isMouseDown=!1,this.isShiftDown=!1,this.selectedCells=[],l.off(".selecTable").on("mousedown.selecTable",this.mousedown).on("mouseenter.selecTable",this.mouseenter).on("mouseup.selecTable",this.mouseup)},l.prototype.init=function(){var t=e(this.element);t.on("keydown.selecTable",this.keydown).on("keyup.selecTable",this.keyup).on("blur.selecTable",this.blur),S(t),t.addClass(f),this.update()},e.fn[w]=function(t,i){return typeof t===n&&(i=t,t=s),this.each(function(){if(e.data(this,"plugin_"+w))switch(i){case"update":e.data(this,"plugin_"+w).update()}else e.data(this,"plugin_"+w,new l(this,t))})}}(jQuery,window);