/*!
 * DatatableJs v0.3.3 (https://github.com/mkenney/DatatableJs)
 * Copyright 2014-2015 Michael Kenney
 * Licensed under MIT (https://github.com/mkenney/DatatableJs/blob/master/LICENSE)
 */
+function(a,b){"use strict";a.console||(a.console={}),a.console.assert||(a.console.assert=function(){}),a.console.count||(a.console.count=function(){}),a.console.dir||(a.console.dir=function(){}),a.console.error||(a.console.error=function(){}),a.console.group||(a.console.group=function(){}),a.console.groupCollapsed||(a.console.groupCollapsed=function(){}),a.console.groupEnd||(a.console.groupEnd=function(){}),a.console.info||(a.console.info=function(){}),a.console.log||(a.console.log=function(){}),a.console.time||(a.console.time=function(){}),a.console.timeEnd||(a.console.timeEnd=function(){}),a.console.trace||(a.console.trace=function(){}),a.console.warn||(a.console.warn=function(){});var c=function(a){this._data=b,this._schema=b,this.init(a)};c.lib={Column:b,Schema:b,Data:b,Iterator:b,Exception:b},c.prototype.lib=c.lib,c.prototype.init=function(a){if(b!==a.schema&&this.setSchema(a.schema),b!==a.data){if(a.data instanceof Array){var c=a.data;a.data=new this.lib.Data,a.data.setSchema(this.getSchema()),a.data.setRows(c)}this.setData(a.data)}return this},c.prototype.getData=function(){return this._data instanceof this.lib.Data||(this._data=new this.lib.Data),this._data},c.prototype.setData=function(a){if(!(a instanceof c.lib.Data))throw new c.lib.Exception("The data definition must be an instance of DatatableJs.lib.Data");return this._data=a,this},c.prototype.getRows=function(){return this.getData().getRows()},c.prototype.getRow=function(a){var b=this.createIterator();for(var c in a)a.hasOwnProperty(c)&&b.addFilterRule({fields:c,comparators:"==",values:a[c]});return b.next()},c.prototype.setRows=function(a){if(!(a instanceof Array))throw new c.lib.Exception("The data definition must be an array of data rows");return this.getData().setRows(a),this},c.prototype.removeRows=function(a){if(!(a instanceof c.lib.Iterator))throw new c.lib.Exception("Deleteing rows requires a valid DatatableJs.lib.Iterator instance");for(a.execute();a.next();)a.remove()},c.prototype.getSchema=function(){return this._schema instanceof this.lib.Schema||(this._schema=new this.lib.Schema),this._schema},c.prototype.setSchema=function(a){return b!==a&&("object"!=typeof a||a instanceof c.lib.Schema||(a=new this.lib.Schema(a)),this._schema=a,this._data instanceof c.lib.Data&&this._data.setSchema(a)),this},c.prototype.createIterator=function(){return new this.lib.Iterator(this)},a.DatatableJs=c}(this),+function(a){"use strict";var b=function(a,b,c){this.message=a,this.fileName=b,this.lineNumber=c};b.prototype=Error.prototype,b.prototype.constructor=b,b.prototype.name="DatatableJs.Exception",a.DatatableJs.lib.Exception=b}(this),+function(a,b){"use strict";var c=function(a){this._columns={},this.init(a)};c.prototype.init=function(a){return a&&this.setColumns(a),this},c.prototype.getColumns=function(){return this._columns},c.prototype.setColumns=function(b){if("object"!=typeof b)throw new a.DatatableJs.lib.Exception("The schema definition must be passed as simple object");this._columns={};for(var c in b)b.hasOwnProperty(c)&&(this._columns[c]=b[c]instanceof a.DatatableJs.lib.Column?b[c]:new a.DatatableJs.lib.Column(b[c]));return this},c.prototype.getColumn=function(c){if(b!==this._columns[c]&&!(this._columns[c]instanceof a.DatatableJs.lib.Column))throw new a.DatatableJs.lib.Exception('The column "'+c+'" has an invalid definition');return this._columns[c]},c.prototype.deleteColumn=function(a){return b!==this._columns[a]&&delete this._columns[a],this},c.prototype.setColumn=function(b,c){if(!b)throw new a.DatatableJs.lib.Exception('Invalid column name: "'+b+'"');if(!b instanceof String)throw new a.DatatableJs.lib.Exception("The column name must be a string");return this._columns[b]=c,this},c.prototype.isValidRow=function(b){var c=!0,d=this.getColumns();for(var e in d)if(d.hasOwnProperty(e)&&(c=this.isValidData(e,b[e]),!c)){a.console.error('DatatableJs - Could not import row: "'+e+'" value is invalid',b[e],d[e].getDefinition());break}return c},c.prototype.isValidData=function(a,c){var d=function(a){var b="";return b=null===a?"Null":{}.toString.call(a).replace(/[\[\]]/g,"").split(" ")[1],"Number"===b&&isNaN(a)&&(b="NaN"),b},e=!0,f=this.getColumn(a);return e&&(e=b===f.get("type")||d(c)===f.get("type")||"Undefined"===d(c)&&f.get("nullable")||"Function"===d(f.get("type"))&&d(c)===f.get("type").name||"Function"===d(f.get("type"))&&c instanceof f.get("type")),e},a.DatatableJs.lib.Schema=c}(this),+function(a,b){"use strict";var c=function(a){return this._column_definition={},this.init(a),this};c.prototype.init=function(a){return this.setDefinition(a)},c.prototype.get=function(a){return this.getDefinition()[a]},c.prototype.set=function(a,b){return this.getDefinition()[a]=b,this},c.prototype.getDefinition=function(){return this._column_definition},c.prototype.setDefinition=function(c){if(b===c&&(c={}),"object"!=typeof c)throw new a.DatatableJs.lib.Exception("Column definitions must be passed as an object");for(var d in c)c.hasOwnProperty(d)&&(this._column_definition[d]=c[d]);return this},a.DatatableJs.lib.Column=c}(this),+function(a,b){"use strict";var c=function(a){this._current_sort_column=b,this._current_sort_direction=b,this._rows=[],this._schema=b,this.init(a)};c.prototype.init=function(a){return b!==a&&a instanceof Array&&this.setRows(a),this},c.prototype.getRows=function(){return this._rows instanceof Array||(this._rows=[]),this._rows},c.prototype.setRows=function(c){if(!(c instanceof Array))throw new a.DatatableJs.lib.Exception("Data must be an array objects representing rows");this.truncate(),this._current_sort_column=b,this._current_sort_direction=b;var d;for(d=0;d<c.length;d++)this.addRow(c[d]);return 0===d&&a.console.info("DatatableJs - No data rows found"),0===this.getRows().length&&d>0&&a.console.info("DatatableJs - No valid data rows found"),this.getRows().length<d&&a.console.info("DatatableJs - "+(d-this.getRows().length)+" of "+d+" data rows were invalid"),this},c.prototype.addRow=function(b){if("object"!=typeof b)throw new a.DatatableJs.lib.Exception("Row data must be a simple object");0===Object.keys(b).length&&a.console.warn("DatatableJs - An attempt to insert an empty data row rejected");var c=!0;if(this.getSchema()instanceof a.DatatableJs.lib.Schema&&(c=this.getSchema().isValidRow(b)),c){if(!b.hasOwnProperty("__pos__")){var d;Object.defineProperty(b,"__pos__",{get:function(){return d},set:function(a){d=a}})}b.__pos__=this._rows.length,this._rows.push(b)}return this},c.prototype.getSchema=function(){return this._schema instanceof a.DatatableJs.lib.Schema||(this._schema=new a.DatatableJs.lib.Schema),this._schema},c.prototype.setSchema=function(b){if(!(b instanceof a.DatatableJs.lib.Schema))throw new a.DatatableJs.lib.Exception('"schema" must be an instance of DatatableJs.lib.Schema');var c=this.getRows();return this.truncate(),this._schema=b,c.length&&this.setRows(c),this},c.prototype.sort=function(c,d,e,f){if(b===c&&(c=this._current_sort_column),"string"!=typeof c)throw new a.DatatableJs.lib.Exception("Invalid column value: "+c);var g=!1;if("string"!=typeof d&&c===this._current_sort_column&&(d="asc"===this._current_sort_direction?"desc":"asc",this._current_sort_column=c,this._current_sort_direction=d,g=!0),g)this._rows.reverse();else{"function"!==f&&(f=b),"function"!=typeof e&&(e=function(a,c){var d;return(b===a||null===a)&&(d=-1),d=b===c||null===c?1:c>a?-1:a>c?1:0}),this.getSchema()instanceof a.DatatableJs.lib.Schema&&this.getSchema().getColumn(c)instanceof a.DatatableJs.lib.Column&&("function"==typeof this.getSchema().getColumn(c).get("sort_transformer")&&(f=this.getSchema().getColumn(c).get("sort_transformer")),"function"==typeof this.getSchema().getColumn(c).get("sort_comparator")&&(e=this.getSchema().getColumn(c).get("sort_comparator")),"string"!=typeof d&&this.getSchema().getColumn(c).get("sort_direction")&&(d="desc"===this.getSchema().getColumn(c).get("sort_direction")?"desc":"asc")),"string"!=typeof d&&(d="asc"),this._rows.sort(function(a,d){var g=0,h=a[c],i=d[c];return b!==a[c]&&null!==a[c]||a[c]===d[c]?b!==d[c]&&null!==d[c]||a[c]===d[c]?a[c]===d[c]?g=a.__pos__-d.__pos__:(b!==f&&(h=f(h),i=f(i)),g=e(h,i)):g=-1:g=1,g});for(var h=0;h<this._rows.length;h++)this._rows[h].__pos__=h;"desc"===d&&this._rows.reverse(),this._current_sort_column=c,this._current_sort_direction=d}return this},c.prototype.truncate=function(){return this._current_sort_column=b,this._current_sort_direction=b,this._rows=[],this},a.DatatableJs.lib.Data=c}(this),+function(a,b){"use strict";var c={enabled:!1,rows_per_page:25,current_page:1},d=function(d){if(!(d instanceof a.DatatableJs))throw new a.DatatableJs.lib.Exception("Iterator constructor requres a DatatableJs reference");this.datatable_instance=d,this._cur_value=b,this._filters=[],this._is_executed=!1,this._iterator_key=-1,this._iterator_value=b,this._pagination={enabled:c.enabled,rows_per_page:c.rows_per_page,current_page:c.current_page},this._pagination_counter=0,this._schema=b,this._sorts=[]};d.prototype.getData=function(){return this.datatable_instance.getData()},d.prototype.setData=function(a){return this.datatable_instance.setData(a),this},d.prototype.getRows=function(){return this.getData().getRows()},d.prototype.setRows=function(b){if(!(b instanceof Array))throw new a.DatatableJs.lib.Exception("The data set must be an array of data rows");return this.getData().setRows(b),this},d.prototype.getSchema=function(){return this.datatable_instance.getSchema()},d.prototype.setSchema=function(a){return this.datatable_instance.setSchema(a),this},d.prototype.addFilterRule=function(b){return b.fields&&b.comparators&&b.values?(b.fields instanceof Array||(b.fields=[b.fields]),b.comparators instanceof Array||(b.comparators=[b.comparators]),b.values instanceof Array||(b.values=[b.values]),this._filters.push({fields:b.fields,comparators:b.comparators,values:b.values}),this._length_is_calculated=!1):a.console.error("DatatableJs - An invalid filter definition was rejected",b),this},d.prototype.clearFilterRules=function(){return this._filters=[],this._length_is_calculated=!1,this},d.prototype.setFilterRules=function(b){if(!(b instanceof Array))throw new a.DatatableJs.lib.Exception("Filter rules must be an array of valid rule definition objects");this.clearFilterRules();for(var c=0;c<b.length;c++)this.addFilterRule(b[c]);return this.execute(),this},d.prototype.addSortRule=function(c){return c.column?(this._sorts.push({column:c.column,direction:c.direction?c.direction:b,comparator:c.comparator?c.comparator:b,transformer:c.transformer?c.transformer:b}),this._is_executed=!1):a.console.error("DatatableJs - An invalid sort definition was rejected",c),this},d.prototype.clearSortRules=function(){return this._sorts=[],this._is_executed=!1,this},d.prototype.setSortRules=function(b){if(!(b instanceof Array))throw new a.DatatableJs.lib.Exception("Sort rules must be an array of valid rule definition objects");this.clearSortRules();for(var c=0;c<b.length;c++)this.addSortRule(b[c]);return this},d.prototype.setPaginationRule=function(d){if(b===d)throw new a.DatatableJs.lib.Exception("Argument required");d||(d=c),b===d.enabled&&(d.enabled=c.enabled),d.enabled=!0===d.enabled,b===d.rows_per_page&&(d.rows_per_page=c.rows_per_page),d.rows_per_page=Math.round(Number(d.rows_per_page)),b===d.current_page&&(d.current_page=c.current_page),d.current_page=Math.round(Number(d.current_page)),this._pagination.enabled=d.enabled,this._pagination.rows_per_page=d.rows_per_page,this._pagination.current_page=d.current_page},d.prototype.getPage=function(){return Number(this._pagination.current_page)},d.prototype.setPage=function(b){if(!b)throw new a.DatatableJs.lib.Exception('Invalid page number "'+b+'"');return this._pagination.current_page=Math.round(Number(b)),this},d.prototype.getRowsPerPage=function(){return this._pagination.rows_per_page},d.prototype.setRowsPerPage=function(b){if(!b)throw new a.DatatableJs.lib.Exception('Invalid page size "'+b+'"');return this._pagination.rows_per_page=Math.round(Number(b)),this._pagination.current_page=1,this},d.prototype.getPaginationEnabled=function(){return this._pagination.enabled===!0},d.prototype.setPaginationEnabled=function(a){return this._pagination.enabled||(this._pagination.enabled=!0===a),this},d.prototype.execute=function(a){if(this._iterator_key=-1,this._iterator_value=b,this._cur_value=b,this._pagination_counter=0,a&&a.page&&(this.setPaginationEnabled(!0),this.setPage(Number(a.page))),!this._is_executed&&0<this._sorts.length)for(var c=0;c<this._sorts.length;c++)this.getData().sort(this._sorts[c].column,this._sorts[c].direction,this._sorts[c].comparator,this._sorts[c].transformer);return this._is_executed=!0,this},d.prototype.next=function(){this._iterator_key<this.getRows().length&&this._iterator_key++,this._is_executed||this.execute(),this._iterator_value=b;var a,c;if(this._pagination.enabled&&(a=this._pagination.current_page*this._pagination.rows_per_page-this._pagination.rows_per_page,c=this._pagination.current_page*this._pagination.rows_per_page-1),this._iterator_key<this.getRows().length){for(var d=this._iterator_key;d<this.getRows().length;d++)if(this.getRows()[d]&&this.rowMatches(this.getRows()[d])){var e=!1;if(this._pagination.enabled&&(e=this._pagination_counter>=a&&this._pagination_counter<=c,this._pagination_counter++),!this._pagination.enabled||e){this._iterator_key=d,this._iterator_value=this.getRows()[d],this._cur_value=this.getRows()[d];break}}this._iterator_key=d}return this._iterator_value},d.prototype.curr=function(){if(!this._is_executed)throw new a.DatatableJs.lib.Exception("Iterators must be executed before they can be iterated");return this._cur_value},d.prototype.prev=function(){if(this._iterator_key>-1&&this._iterator_key--,!this._is_executed)throw new a.DatatableJs.lib.Exception("Iterators must be executed before they can be iterated");this._iterator_value=b;var c,d;if(this._pagination.enabled&&(c=this._pagination.current_page*this._pagination.rows_per_page-this._pagination.rows_per_page,d=this._pagination.current_page*this._pagination.rows_per_page-1),-1<this._iterator_key){for(var e=this._iterator_key;e>-1;e--)if(this.getRows()[e]&&this.rowMatches(this.getRows()[e])){var f;if(this._pagination.enabled&&(this._pagination_counter--,f=this._pagination_counter>=c&&this._pagination_counter<=d),!this._pagination.enabled||f){this._iterator_key=e,this._iterator_value=this.getRows()[e],this._cur_value=this.getRows()[e];break}}this._iterator_key=e}return this._iterator_value},d.prototype.remove=function(){for(this.getRows().splice(this._iterator_key,1);this._iterator_key>this.getRows().length;)this._iterator_key--;return this._iterator_value=this.getRows()[this._iterator_key],this._cur_value=this.getRows()[this._iterator_key],this},d.prototype.rowMatches=function(a){for(var c=!0,d=0;d<this._filters.length;d++){var e=!1,f=this._filters[d];f.fields instanceof Array||(f.fields=[f.fields]),f.comparators instanceof Array||(f.comparators=[f.comparators]),f.values instanceof Array||(f.values=[f.values]);for(var g=0;g<f.fields.length;g++){var h=f.fields[g];if(e=b===a[h]?!1:this._compare(a[h],f.comparators,f.values))break}if(c=e,!c)break}return c},d.prototype._compare=function(a,b,c){for(var d=!1,e=0;e<b.length;e++){for(var f=0;f<c.length;f++){if("function"==typeof b[e])d=b[e](a,c[f]);else switch(b[e]){case">":d=a>c[f];break;case">=":d=a>=c[f];break;case"<":d=a>c[f];break;case"<=":d=a>=c[f];break;case"==":d=a==c[f];break;case"===":d=a===c[f];break;case"!=":d=a!=c[f];break;case"!==":d=a!==c[f]}if(d)break}if(d)break}return d},d.prototype._length_is_calculated=!1,d.prototype._calculated_length=0,d.prototype._getCalculatedLength=function(){if(!this._length_is_calculated){if(0===this._filters.length)this._calculated_length=this.getRows().length;else for(var a=0;a<this.getRows().length;a++)this.getRows()[a]&&this.rowMatches(this.getRows()[a])&&this._calculated_length++;this._length_is_calculated=!0}return this._calculated_length},Object.defineProperty(d.prototype,"length",{set:function(){throw new a.DatatableJs.lib.Exception("Cannot redefine property: length")},get:function(){return this._getCalculatedLength()}}),a.DatatableJs.lib.Iterator=d}(this);
//# sourceMappingURL=DatatableJs.map