"use strict";(function(a,b){if(typeof define==="function"&&define.amd){define(["react"],b)}else{if(typeof exports==="object"){module.exports=b(require("react"))}else{a.Reactable=b(a.React)}}}(this,function(b){var e={};if(!Array.prototype.map){Array.prototype.map=function(y,x){var r,q,s;if(this==null){throw new TypeError(" this is null or not defined")}var u=Object(this);var v=u.length>>>0;if(typeof y!=="function"){throw new TypeError(y+" is not a function")}if(arguments.length>1){r=x}q=new Array(v);s=0;while(s<v){var t,w;if(s in u){t=u[s];w=y.call(r,t,s,u);q[s]=w}s++}return q}}if(!Array.prototype.indexOf){Array.prototype.indexOf=function(r){var q=this.length>>>0;var s=Number(arguments[1])||0;s=(s<0)?Math.ceil(s):Math.floor(s);if(s<0){s+=q}for(;s<q;s++){if(s in this&&this[s]===r){return s}}return -1}}if(!Array.prototype.find){Object.defineProperty(Array.prototype,"find",{enumerable:false,configurable:true,writable:true,value:function(q){if(this==null){throw new TypeError("Array.prototype.find called on null or undefined")}if(typeof q!=="function"){throw new TypeError("predicate must be a function")}var v=Object(this);var t=v.length>>>0;var r=arguments[1];var u;for(var s=0;s<t;s++){if(s in v){u=v[s];if(q.call(r,u,s,v)){return u}}}return undefined}})}if(!Array.isArray){Array.isArray=function(q){return Object.prototype.toString.call(q)==="[object Array]"}}if(!Object.assign){Object.defineProperty(Object,"assign",{enumerable:false,configurable:true,writable:true,value:function(x,q){if(x===undefined||x===null){throw new TypeError("Cannot convert first argument to object")}var z=Object(x);for(var r=1;r<arguments.length;r++){var u=arguments[r];if(u===undefined||u===null){continue}var s=Object.keys(Object(u));for(var t=0,w=s.length;t<w;t++){var y=s[t];var v=Object.getOwnPropertyDescriptor(u,y);if(v!==undefined&&v.enumerable){z[y]=u[y]}}}return z}})}function l(q){this.content=q}l.prototype.toString=function(){return this.content};function n(q){return q!==null&&typeof(q)!=="undefined"&&typeof(q.toString==="function")}function h(q){return q!==null&&typeof(q)==="object"&&typeof(q.props)!=="undefined"}b.Children.children=function(q){return b.Children.map(q,function(r){return r})||[]};e.unsafe=function(q){return new l(q)};e.Sort={Numeric:function(s,q){var t=parseFloat(s.toString().replace(",",""));var r=parseFloat(q.toString().replace(",",""));if(isNaN(t)&&isNaN(r)){t=s;r=q}else{if(isNaN(t)){return 1}if(isNaN(r)){return -1}}if(t<r){return -1}if(t>r){return 1}return 0},Currency:function(r,q){if(r[0]==="$"){r=r.substring(1)}if(q[0]==="$"){q=q.substring(1)}return e.Sort.Numeric(r,q)},Date:function(s,q){var t=Date.parse(s);var r=Date.parse(q);if(isNaN(t)||isNaN(r)){return e.Sort.Numeric(s,q)}if(t>r){return 1}if(r>t){return -1}return 0},CaseInsensitive:function(s,q){var t=s.toLowerCase();var r=q.toLowerCase();if(t>r){return 1}if(r>t){return -1}return 0}};var g=e.Td=b.createClass({displayName:"Td",handleClick:function(q){if(typeof this.props.handleClick!=="undefined"){return this.props.handleClick(q,this)}},render:function(){var s={"data-column":this.props.column.key,className:this.props.className,onClick:this.handleClick};for(var q in this.props.column){if(q!=="key"&&q!=="name"){s[q]=this.props.column[q]}}var r=this.props.data;if(typeof(this.props.children)!=="undefined"){if(h(this.props.children)){r=this.props.children}else{if(typeof(this.props.data)==="undefined"&&n(this.props.children)){r=this.props.children.toString()}}if(this.props.children instanceof l){s.dangerouslySetInnerHTML={__html:this.props.children.toString()}}else{s.children=r}}return b.DOM.td(s)}});var m=e.Tr=b.createClass({displayName:"Tr",statics:{childNode:g,dataType:"object"},render:function(){var q=d(b.Children.children(this.props.children));if(this.props.data&&this.props.columns&&typeof this.props.columns.map==="function"){if(typeof(q.concat)==="undefined"){console.log(q)}q=q.concat(this.props.columns.map(function(t,s){if(this.props.data.hasOwnProperty(t.key)){return g({column:t,key:t.key},this.props.data[t.key])}else{return g({column:t,key:t.key})}}.bind(this)))}var r=o(this.props);return b.DOM.tr(r,q)}});var f=e.Thead=b.createClass({displayName:"Thead",getColumns:function(){return b.Children.map(this.props.children,function(q){if(typeof q.props.children==="string"){return q.props.children}else{throw new TypeError("<th> must have a string child")}})},handleClickTh:function(q){this.props.onSort(q.key)},render:function(){var u=[];for(var r=0;r<this.props.columns.length;r++){var t=this.props.columns[r];var q="";if(this.props.sortableColumns[t.key]){q+="reactable-header-sortable "}if(this.props.sort.column===t.key){q+="reactable-header-sort";if(this.props.sort.direction===1){q+="-asc"}else{q+="-desc"}}u.push(c({className:q,key:r,onClick:this.handleClickTh.bind(this,t)},t.label))}var s=o(this.props);return b.DOM.thead(s,(this.props.filtering===true?k({colSpan:this.props.columns.length,onFilter:this.props.onFilter,value:this.props.currentFilter}):""),b.DOM.tr({className:"reactable-column-header"},u))}});var c=e.Th=b.createClass({displayName:"Th",render:function(){if(this.props.children instanceof l){return this.transferPropsTo(b.DOM.th({dangerouslySetInnerHTML:{__html:this.props.children.toString()}}))}else{return this.transferPropsTo(b.DOM.th(null,this.props.children))}}});var p=b.createClass({displayName:"FiltererInput",onChange:function(){this.props.onFilter(this.getDOMNode().value)},render:function(){return(b.DOM.input({type:"text",className:"reactable-filter-input",value:this.props.value,onKeyUp:this.onChange,onChange:this.onChange}))}});var k=b.createClass({displayName:"Filterer",render:function(){if(typeof this.props.colSpan==="undefined"){throw new TypeError("Must pass a colSpan argument to Filterer")}return(b.DOM.tr({className:"reactable-filterer"},b.DOM.td({colSpan:this.props.colSpan},p({onFilter:this.props.onFilter,value:this.props.value}))))}});var a=b.createClass({displayName:"Paginator",render:function(){if(typeof this.props.colSpan==="undefined"){throw new TypeError("Must pass a colSpan argument to Paginator")}if(typeof this.props.numPages==="undefined"){throw new TypeError("Must pass a non-zero numPages argument to Paginator")}if(typeof this.props.currentPage==="undefined"){throw new TypeError("Must pass a currentPage argument to Paginator")}var s=[];for(var q=0;q<this.props.numPages;q++){var t=q;var r="reactable-page-button";if(this.props.currentPage===q){r+=" reactable-current-page"}s.push(b.DOM.a({className:r,key:q,onClick:(function(u){return function(){this.props.onPageChange(u)}.bind(this)}.bind(this))(q)},q+1))}return(b.DOM.tbody({className:"reactable-pagination"},b.DOM.tr(null,b.DOM.td({colSpan:this.props.colSpan},s))))}});var j=e.Table=b.createClass({displayName:"Table",translateColumnsArray:function(q){return q.map(function(t,s){if(typeof(t)==="string"){return{key:t,label:t}}else{if(typeof(t.sortable)!=="undefined"){var r=t.sortable===true?"default":t.sortable;this._sortable[t.key]=r}return t}}.bind(this))},parseChildData:function(q){var r=[];if(typeof(q.children)!=="undefined"){b.Children.forEach(q.children,function(t){var s=t.props.data||{};b.Children.forEach(t.props.children,function(u){if(true){if(typeof(u.props.column)!=="undefined"){if(typeof(u.props.data)!=="undefined"){s[u.props.column]=u.props.data}else{if(typeof(u.props.children)!=="undefined"){s[u.props.column]=u.props.children}else{console.warn("exports.Td specified without a `data` property or children, ignoring")}}}else{console.warn("exports.Td specified without a `column` property, ignoring")}}});r.push({data:s,props:o(t.props),__reactableMeta:true})}.bind(this))}return r},initialize:function(q){this.data=q.data||[];this.data=this.data.concat(this.parseChildData(q));this.initializeSorts(q)},initializeSorts:function(){this._sortable={};for(var s in this.props.sortable){var t=this.props.sortable[s];var r,q;if(t instanceof Object){if(typeof(t.column)!=="undefined"){r=t.column}else{console.warn("Sortable column specified without column name");return}if(typeof(t.sortFunction)==="function"){q=t.sortFunction}else{q="default"}}else{r=t;q="default"}this._sortable[r]=q}},getDefaultProps:function(){var q={sortBy:false,defaultSort:false,itemsPerPage:0};return q},getInitialState:function(){var q={currentPage:0,currentSort:{column:null,direction:1},filter:""};if(this.props.sortBy!==false||this.props.defaultSort!==false){var r=this.props.sortBy||this.props.defaultSort;q.currentSort=this.getCurrentSort(r)}return q},getCurrentSort:function(r){if(r instanceof Object){var q,s;if(typeof(r.column)!=="undefined"){q=r.column}else{console.warn("Default column specified without column name");return}if(typeof(r.direction)!=="undefined"){if(r.direction===1||r.direction==="asc"){s=1}else{if(r.direction===-1||r.direction==="desc"){s=-1}else{console.warn("Invalid default sort specified.  Defaulting to ascending");s=1}}}else{s=1}}else{q=r;s=1}return{column:q,direction:s}},updateCurrentSort:function(q){if(q!==false&&q.column!==this.state.currentSort.column&&q.direction!==this.state.currentSort.direction){this.setState({currentSort:this.getCurrentSort(q)})}},componentWillMount:function(){this.initialize(this.props);this.sortByCurrentSort()},componentWillReceiveProps:function(q){this.initialize(q);this.updateCurrentSort(q.sortBy);this.sortByCurrentSort()},onPageChange:function(q){this.setState({currentPage:q})},filterBy:function(q){this.setState({filter:q})},applyFilter:function(t,s){var t=t.toLowerCase();var u=[];for(var r=0;r<s.length;r++){var v=s[r].props.data;for(var q=0;q<this.props.filterable.length;q++){var w=this.props.filterable[q];if(typeof(v[w])!=="undefined"&&v[w].toString().toLowerCase().indexOf(t)>-1){u.push(s[r]);break}}}return u},sortByCurrentSort:function(){var q=this.state.currentSort;if(q.column===null){return}this.data.sort(function(u,s){var t=u[q.column];t=n(t)?t.toString():"";var r=s[q.column];r=n(r)?r.toString():"";if(typeof(this._sortable[q.column])==="undefined"||this._sortable[q.column]==="default"){if(t<r){return -1*q.direction}if(t>r){return 1*q.direction}return 0}else{if(q.direction===1){return this._sortable[q.column](t,r)}else{return this._sortable[q.column](r,t)}}}.bind(this))},onSort:function(r){if(typeof(this._sortable[r])==="undefined"){return}var q=this.state.currentSort;if(q.column===r){q.direction*=-1}else{q.column=r;q.direction=1}this.setState({currentSort:q});this.sortByCurrentSort()},render:function(){var r=[];var u;var y=false;if(this.props.children&&this.props.children.length>0&&this.props.children[0].type.ConvenienceConstructor===f){u=this.props.children[0].getColumns()}else{u=this.props.columns||[]}if(u.length>0){y=true;u=this.translateColumnsArray(u)}if(this.data&&typeof this.data.map==="function"){r=r.concat(this.data.map(function(H,D){var G=H;var F={};if(H.__reactableMeta===true){G=H.data;F=H.props}for(var C in G){if(G.hasOwnProperty(C)){if(y===false){var E={key:C,label:C};if(u.find(function(I){return I.key===E.key})===undefined){u.push(E)}}}}return(m(Object.assign({columns:u,key:D,data:G},F)))}.bind(this)))}if(this.props.sortable===true){for(var v=0;v<u.length;v++){this._sortable[u[v].key]="default"}}var B=false;if(this.props.filterable&&Array.isArray(this.props.filterable)&&this.props.filterable.length>0){B=true}var q=r;if(this.state.filter!==""){q=this.applyFilter(this.state.filter,q)}var s=0;var z=false;var t;var w=this.state.currentPage;var A=q;if(this.props.itemsPerPage>0){s=this.props.itemsPerPage;t=Math.ceil(q.length/s);if(w>t-1){w=t-1}z=true;A=q.slice(w*s,(w+1)*s)}var x=o(this.props);return b.DOM.table(Object.assign({},x),[(u&&u.length>0?f({columns:u,filtering:B,onFilter:this.filterBy,currentFilter:this.state.filter,sort:this.state.currentSort,sortableColumns:this._sortable,onSort:this.onSort,key:"thead"}):null),b.DOM.tbody({className:"reactable-data",key:"tbody"},A),(z===true?a({colSpan:u.length,numPages:t,currentPage:w,onPageChange:this.onPageChange,key:"paginator"}):null)])}});function d(s){var r=[];for(var q in s){r[q]=s}return r}function o(q){q=q||{};var s={};for(var r in q){if(!(r in i)){s[r]=q[r]}}return s}var i={columns:true,sortable:true,filterable:true,sortBy:true,defaultSort:true,itemsPerPage:true,childNode:true,data:true};return e}));