text: "use strict";Object.defineProperty(exports,"__esModule",{value:!0}),exports.default=void 0;var _react=_interopRequireDefault(require("react"));function _interopRequireDefault(e){return e&&e.__esModule?e:{default:e}}function _instanceof(e,t){return null!=t&&"undefined"!=typeof Symbol&&t[Symbol.hasInstance]?!!t[Symbol.hasInstance](e):e instanceof t}function _typeof(e){return(_typeof="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(e){return typeof e}:function(e){return e&&"function"==typeof Symbol&&e.constructor===Symbol&&e!==Symbol.prototype?"symbol":typeof e})(e)}function _classCallCheck(e,t){if(!_instanceof(e,t))throw new TypeError("Cannot call a class as a function")}function _defineProperties(e,t){for(var o=0;o<t.length;o++){var r=t[o];r.enumerable=r.enumerable||!1,r.configurable=!0,"value"in r&&(r.writable=!0),Object.defineProperty(e,r.key,r)}}function _createClass(e,t,o){return t&&_defineProperties(e.prototype,t),o&&_defineProperties(e,o),e}function _possibleConstructorReturn(e,t){return!t||"object"!==_typeof(t)&&"function"!=typeof t?_assertThisInitialized(e):t}function _getPrototypeOf(e){return(_getPrototypeOf=Object.setPrototypeOf?Object.getPrototypeOf:function(e){return e.__proto__||Object.getPrototypeOf(e)})(e)}function _assertThisInitialized(e){if(void 0===e)throw new ReferenceError("this hasn't been initialised - super() hasn't been called");return e}function _inherits(e,t){if("function"!=typeof t&&null!==t)throw new TypeError("Super expression must either be null or a function");e.prototype=Object.create(t&&t.prototype,{constructor:{value:e,writable:!0,configurable:!0}}),t&&_setPrototypeOf(e,t)}function _setPrototypeOf(e,t){return(_setPrototypeOf=Object.setPrototypeOf||function(e,t){return e.__proto__=t,e})(e,t)}var SortableTable=function(){function o(e){var t;return _classCallCheck(this,o),(t=_possibleConstructorReturn(this,_getPrototypeOf(o).call(this,e))).sortColumn=t.sortColumn.bind(_assertThisInitialized(t)),t.table=_react.default.createRef(),t}return _inherits(o,_react.default.Component),_createClass(o,[{key:"componentDidMount",value:function(){this.setupTable()}},{key:"setupTable",value:function(){var e=this.table.current;if(null===e.tHead)return console.warn("Unable to setup sorting for table because there is no <thead> element"),void console.log(e);if(0===e.tHead.rows.length)return console.warn("Unable to setup sorting for table because the <thead> element contained now rows"),void console.log(e);if(0===e.tBodies.length)return console.warn("Unable to setup sorting for table because the <tbody> element is missing"),void console.log(e);if(1!==e.tBodies.length)return console.warn("Unable to setup sorting for table because there can only be one <tbody> element for the table"),void console.log(e);for(var t=e.tHead.rows[e.tHead.rows.length-1],o=0,r=t.cells.length;o<r;o++)t.cells[o].addEventListener("click",this.sortColumn),t.cells[o].style.cursor="pointer"}},{key:"sortColumn",value:function(e){var t=e.target,o=t&&t.parentElement&&t.parentElement.parentElement?t.parentElement.parentElement.parentElement:null;if("TH"!==t.tagName&&"TD"!==t.tagName)return console.warn("SortableTable.sortColumn() was called with an invalid element. If called manually the cell needs to be passed."),void console.log(t);if(null!=o&&"TABLE"!==o.tagName)return console.warn("SortableTable.sortColumn() was called with an invalid element. If called manually the table cell of the last row from the table header should be used."),void console.log(t);var r=o.getAttribute("data-sort-column"),n=t.cellIndex,a="asc";null!==r&&parseInt(r,10)===n&&(a="asc"===o.getAttribute("data-sort-order")?"desc":"asc");for(var l=o.tBodies[0],s=l.rows,u=[],i=0,c=s.length;i<c;i++){var p=s[i].cells[n];if(void 0!==p){var f=p.getAttribute("data-value"),d=null!==f?f.trim():p.textContent.trim(),b=_typeof(d);if(""===d)d=null;else if(isNaN(d)){var y=new Date(d);isNaN(y.getTime())?d=void 0!==d.toLocaleLowerCase?d.toLocaleLowerCase():d.toLowerCase():(d=y,b="date")}else d=parseFloat(d);u.push({row:s[i],value:d,type:null===d?null:b})}}u.sort(this.sortCompare),"desc"===a&&u.reverse();for(var m=o.getAttribute("data-sort-class-odd"),v=o.getAttribute("data-sort-class-even"),h=m&&v,_=0,g=0,w=u.length;g<w;g++){var C=u[g].row;h&&"none"!==C.style.display&&(++_%2==0?(C.classList.add(v),C.classList.remove(m)):(C.classList.add(m),C.classList.remove(v))),l.appendChild(C)}o.setAttribute("data-sort-column",n),o.setAttribute("data-sort-order",a)}},{key:"sortCompare",value:function(e,t){return null===e.type&&null!==t.type?-1:null===t.type&&null!==e.type?1:null===e.type&&null===t.type?0:"number"===e.type&&"number"!==t.type?-1:"number"===t.type&&"number"!==e.type?1:"number"===e.type&&"number"===t.type?e.value-t.value:"date"===e.type&&"date"!==t.type?-1:"date"===t.type&&"date"!==e.type?1:"date"===e.type&&"date"===t.type?e.value<t.value?-1:e.value>t.value?1:0:"string"===e.type&&"string"===t.type?void 0!==e.value.localeCompare?e.value.localeCompare(t.value):e.value<t.value?-1:e.value>t.value?1:0:(console.warn("A code change from [SortableTable] impacted sorting and caused an expected error so the data may appear out of order. Please review your code changes"),0)}},{key:"render",value:function(){return _react.default.createElement("table",Object.assign({},this.props,{ref:this.table}),this.props.children)}}]),o}();exports.default=SortableTable;