forked from marcrobledo/retroarch-playlist-editor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMarcTableSelect.min.js
2 lines (2 loc) · 7.48 KB
/
MarcTableSelect.min.js
1
2
/* MarcTableSelect.js v20190328 - Marc Robledo 2014-2019 - http://www.marcrobledo.com/license */
var MarcTableSelect=function(){function a(a,b,c){l?a.attachEvent("on"+b,c):a.addEventListener(b,c,!1)}function b(a,b,c){l?a.detachEvent("on"+b,c):a.removeEventListener(b,c,!1)}function c(a){"undefined"==typeof a.stopPropagation?a.cancelBubble=!0:a.stopPropagation()}function d(a){a.preventDefault?a.preventDefault():a.returnValue=!1}function e(){document.body.className=document.body.className.replace(/ moving-rows$/g,""),g(),p&&(p.element.className=p.element.className.replace(/ moving-rows$/g,""),p=null)}function f(a){if(c(a),p){if(this.movableRows&&!this.isSelected(this.getRowIndex(m))){var b=p.getSelectedRows();p.removeSelected();var d=this.getRowIndex(m);-1<d&&(!q&&d++,this.appendRows(b,d,!0),this.focus(d),"function"==typeof this.onRowsMove&&this.onRowsMove(d,b.length))}e()}else if(!a.shiftKey&&!a.ctrlKey){var f=i(a,this);this.isSelected(f)&&(this.unselectAll(),this.select(f),this.focus(f))}n=0,o=0}function g(){m&&(m.className=m.className.replace(/ row-over row-over-\w+/g,""))}function h(a){if(p){g(),m=this.element.children[i(a,this)];var b=m.getBoundingClientRect();q=a.clientY-b.top<b.height/2,this.movableRows&&!this.isSelected(this.getRowIndex(m))&&(q?m.className+=" row-over row-over-top":m.className+=" row-over row-over-bottom")}else n&&o&&(16<Math.abs(n-a.clientX)||16<Math.abs(o-a.clientY))&&(p=this,document.body.className+=" moving-rows",this.element.className+=" moving-rows",n=0,o=0)}function i(a,b){for(var c=a.target||a.srcElement,d=[].slice.call(b.element.children),e=-1;-1===e&&"undefined"!=typeof c&&c&&c!==document.body;)c.parentElement===b.element&&(e=d.indexOf(c)),c=c.parentElement;return e}function j(a){c(a),d(a),a=a||window.event,this.movableRows&&(n=a.clientX,o=a.clientY);var b=i(a,this);if(-1!==b){if(a.shiftKey){var e=0;this.selectedRows.length&&this.focusedRow&&(e=this.getRowIndex(this.rangeMode?this.selectedRows[0]:this.focusedRow)),this.unselectAll(),this.selectRange(e,b)}else a.ctrlKey?this.toggle(b):this.isSelected(b)||(this.unselectAll(),this.select(b));this.focus(b),s=this}s!==this&&MarcTableSelect.blur()}function k(b){this.element=b,this.onRowsMove=!1,this.onSelect=!1,this.onBlur=!1,this.onFocus=!1,this.movableRows=!1,a(this.element,"click",c),a(this.element,"mousedown",j.bind(this)),a(this.element,"mouseup",f.bind(this)),this.selectedRows=[],this.rangeMode=!1,this.unselectAll(!0),this.blur(!0)}var l=/MSIE 8/.test(navigator.userAgent);Function.prototype.bind||(Function.prototype.bind=function(a){if("function"!=typeof this)throw new TypeError("Function.prototype.bind - what is trying to be bound is not callable");var b=Array.prototype.slice.call(arguments,1),c=this,d=function(){},e=function(){return c.apply(this instanceof d&&a?this:a,b.concat(Array.prototype.slice.call(arguments)))};return d.prototype=this.prototype,e.prototype=new d,e});var m,n=0,o=0,p=null,q=!1;k.prototype.setMovable=function(c){this.movableRows||!c?this.movableRows&&!c&&(b(this.element,"mouseout",this.element.onmouseout),b(this.element,"mousemove",this.element.onmousemove)):(a(this.element,"mouseout",g),a(this.element,"mousemove",h.bind(this))),this.movableRows=!!c},k.prototype.isSelected=function(a){return 0<=this.selectedRows.indexOf(this.element.children[a])},k.prototype.isFocused=function(){return MarcTableSelect.getFocusedTable()===this},k.prototype.select=function(a,b){!this.isSelected(a)&&-1<a&&(this.selectedRows.push(this.element.children[a]),this.element.children[a].className+=" row-selected",!b&&"function"==typeof this.onSelect&&this.onSelect(this.getSelectedIndexes(),this.selectedRows))},k.prototype.toggle=function(a){-1<a&&(this.isSelected(a)?this.unselect(a):this.select(a),this.rangeMode=!1)},k.prototype.focus=function(a,b){this.focusedRow&&(this.focusedRow.className=this.focusedRow.className.replace(" row-focused","")),-1<a&&(this.focusedRow=this.element.children[a],this.focusedRow.className+=" row-focused",!b&&"function"==typeof this.onFocus&&this.onFocus(a,this.focusedRow))},k.prototype.getFocusIndex=function(){return this.focusedRow?this.getRowIndex(this.focusedRow):-1},k.prototype.selectRange=function(a,b,c){if(a<b)for(var d=a;d<=b;d++)this.select(d,!0);else for(var d=a;d>=b;d--)this.select(d,!0);this.rangeMode=!0,c||"function"!=typeof this.onSelect||this.onSelect(this.getSelectedIndexes(),this.selectedRows)},k.prototype.selectAll=function(a){this.element.children.length&&this.selectRange(0,this.element.children.length-1,a)},k.prototype.removeSelected=function(a){for(var b=this.getFocusIndex(),c=0;c<this.selectedRows.length;c++)this.selectedRows[c].className=this.selectedRows[c].className.replace(" row-selected",""),this.element.removeChild(this.selectedRows[c]);this.blur(),b>=this.element.children.length&&(b=this.element.children.length-1),this.focus(b),this.selectedRows=[],this.rangeMode=!1,a&&"function"==typeof this.onSelect&&this.onSelect(this.getSelectedIndexes(),this.selectedRows)},k.prototype.blur=function(a){this.focusedRow&&(this.focusedRow.className=this.focusedRow.className.replace(" row-focused",""),!a&&"function"==typeof this.onBlur&&this.onSelect(this.getSelectedIndexes(),this.selectedRows))},k.prototype.unselect=function(a,b){this.isSelected(a)&&(this.element.children[a].className=this.element.children[a].className.replace(" row-selected",""),this.selectedRows.splice(this.selectedRows.indexOf(this.element.children[a]),1),!b&&"function"==typeof this.onSelect&&this.onSelect(this.getSelectedIndexes(),this.selectedRows))},k.prototype.unselectAll=function(a){for(var b=0;b<this.selectedRows.length;b++)this.selectedRows[b].className=this.selectedRows[b].className.replace(" row-selected","");this.selectedRows=[],this.rangeMode=!1,a||"function"!=typeof this.onSelect||this.onSelect(this.getSelectedIndexes(),this.selectedRows)},k.prototype.getRowIndex=function(a){return[].slice.call(this.element.children).indexOf(a)},k.prototype.getSelectedIndexes=function(){for(var a=[],b=[].slice.call(this.element.children),c=0;c<this.selectedRows.length;c++)a.push(b.indexOf(this.selectedRows[c]));return a},k.prototype.getSelectedRows=function(){for(var a=this.getSelectedIndexes().sort(function(c,a){return c-a}),b=[],c=0;c<a.length;c++)b.push(this.element.children[a[c]]);return b},k.prototype.appendRows=function(a,b,c){"undefined"==typeof b&&(b=this.element.children.length),c&&this.unselectAll(!0);for(var d=0;d<a.length;d++)b<this.element.children.length?this.element.insertBefore(a[d],this.element.children[b+d]):this.element.appendChild(a[d]);return c&&this.selectRange(b,b+d-1),b};var r=[],s=null;return a(window,"load",function(){a(window,"click",MarcTableSelect.unselect)}),a(window,"mouseup",e),a(window,"keydown",function(a){if(document.activeElement===document.body)if(27===a.keyCode||9===a.keyCode)MarcTableSelect.blur();else if(38!==a.keyCode&&40!==a.keyCode)32===a.keyCode&&s&&(s.toggle(s.getFocusIndex()),d(a));else if(s){var b=s.getFocusIndex();if(38===a.keyCode&&0<b||40===a.keyCode&&b<s.element.children.length-1){var c=s.getRowIndex(s.focusedRow)+(38===a.keyCode?-1:1);if(a.shiftKey){var e=s.getRowIndex(s.rangeMode?s.selectedRows[0]:s.focusedRow);s.unselectAll(),s.selectRange(e,c)}s.focus(c),d(a)}}}),{setSelectable:function(a){return"string"==typeof a&&(a=document.getElementById(a)),"TBODY"===a.tagName&&(a=a.parentElement),r.push(new k(a.tBodies[0])),r[r.length-1]},removeSelectable:function(a){a.blur(),a.unselect(),s===a&&(s=null),r.splice(r.indexOf(a,1))},getFocusedSelectable:function(){return s},removeAll:function(){for(var a=0;a<r.length;a++)this.removeSelectable(r[a])},blur:function(){s&&(s.blur(),s=null)}}}();