javascript-data-set
Version:
powerfull recordset class, supporting sorting, filtering, subsets, loading data from json, array and html markup
10 lines • 5.33 kB
JavaScript
/*!
* DataSet | Powerfull dataset for your app
* http://francodacosta.com/data-set/
*
* Copyright 2013-2014, Nuno Costa <nuno@francodacosta.com>
* Released under the MIT license
* https://github.com/francodacosta/data-set/blob/master/LICENSE
*
*/
var window,__bind=function(a,b){return function(){return a.apply(b,arguments)}};window.Francodacosta=window.Francodacosta||{},window.Francodacosta.DataSet=window.Francodacosta.DataSet||{},window.Francodacosta.DataSet.Column=function(){function a(a,b,c){if(this.name=a,this.title=b,this.options=c,this.title=this.title||this.name,this.options=this.options||{},"[object Object]"!==Object.prototype.toString.call(this.options))throw new Error("Options should be an object");if(!this.name&&0!==this.name)throw new Error("Column needs a name")}return a.prototype.getName=function(){return this.name},a.prototype.getTitle=function(){return this.title},a.prototype.getOptions=function(){return this.options},a}(),window=window||{},window.Francodacosta=window.Francodacosta||{},window.Francodacosta.DataSet=window.Francodacosta.DataSet||{},window.Francodacosta.DataSet.dataset=function(){function a(a,b){this.getColumns=__bind(this.getColumns,this),a=a||[],b=b||[],this.setColumns(b),this.setData(a),this.sortingInfo=[],this.filters=[]}return a.prototype.clearFilters=function(){return this.filters=[],this.data=void 0},a.prototype.clearFiltersForColumn=function(a){return delete this.filters[a],this.data=void 0},a.prototype.addFilter=function(a,b,c){if("function"!=typeof b)throw new Error("Filter: Expecting function, got "+b);if(!this.hasColumn(a))throw new Error("Column does not exist "+a);return a in this.filters||(this.filters[a]=[]),this.filters[a].push([b,c]),this.data=void 0},a.prototype.clearSorting=function(){return this.sortingInfo=[],this.data=void 0},a.prototype.filter=function(a){var b,c,d,e,f,g,h,i,j,k,l,m,n;if(0===Object.keys(this.filters).length)return a;for(g=[],k=0,m=a.length;m>k;k++){i=a[k];for(b in this.filters)for(h=!1,c=this.filters[b],l=0,n=c.length;n>l&&(e=c[l],d=e[0],f=e[1]||null,j=i.get(b),d(j,f));l++)h=!0;h&&g.push(i)}return g},a.prototype.setSorting=function(a,b){if(!this.hasColumn(a))throw new Error("Unknown column "+a);return b=(""+b).toUpperCase(),"DESC"!==b&&(b="ASC"),this.sortingInfo.push([a,b]),this.data=void 0},a.prototype.sort=function(a){var b,c,d,e;return this.sortingInfo&&(b=this.sortingInfo.length)?(c=function(a,b){return a===b?0:a>b?1:-1},d=function(a,b){return a===b?0:b>a?1:-1},e=this,a.sort(function(a,f){var g,h,i;for(i=0,g=0;0===i&&b>g;)h=e.sortingInfo[g][0],i="DESC"===e.sortingInfo[g][1]?d(a.get(h),f.get(h)):c(a.get(h),f.get(h)),g+=1;return i}),a):a},a.prototype.hasColumn=function(a){var b,c,d,e;for(e=this.getColumns(),c=0,d=e.length;d>c;c++)if(b=e[c],b.getName()===a)return!0;return!1},a.prototype.getColumn=function(a){var b,c,d,e;for(e=this.getColumns(),c=0,d=e.length;d>c;c++)if(b=e[c],b.getName()===a)return b;throw new Error("Unknown column "+a)},a.prototype.setColumns=function(a){var b,c,d,e,f,g;if(this.columns=[],"[object Array]"!==Object.prototype.toString.call(a))throw nwe(Error("Column definition should be an array of objects or strings"));for(f=0,g=a.length;g>f;f++)b=a[f],"[object Object]"===Object.prototype.toString.call(b)?(c=b.name,e=b.title||b.name,d=b.options||{}):(c=b,e=void 0,d=void 0),this.addColumn(c,e,d);return this.data=void 0},a.prototype.addColumn=function(a,b,c){return this.columns.push(new window.Francodacosta.DataSet.Column(a,b,c)),this.data=void 0},a.prototype.getColumns=function(){return this.columns},a.prototype.setData=function(a){var b,c,d,e;for(this.originalData=[],this.data=void 0,e=[],c=0,d=a.length;d>c;c++){if(b=a[c],"[object Array]"===Object.prototype.toString.call(b))b=this._normalizeDataArray(b);else{if("[object Object]"!==Object.prototype.toString.call(b))throw new Error("Could not parse data");b=this._normalizeDataObject(b)}e.push(this.add(b))}return e},a.prototype._normalizeDataArray=function(a){var b,c,d,e,f,g,h;for(d={},h=this.getColumns(),c=f=0,g=h.length;g>f;c=++f)b=h[c],e=a[c]?a[c]:void 0,d[b.getName()]=e;return new window.Francodacosta.DataSet.Record(d)},a.prototype._normalizeDataObject=function(a){var b,c,d,e,f,g;for(c={},g=this.getColumns(),e=0,f=g.length;f>e;e++)b=g[e],d=!b.getName()in a?void 0:a[b.getName()],c[b.getName()]=d;return new window.Francodacosta.DataSet.Record(c)},a.prototype.getData=function(a,b){return a=a||0,b=b||this.originalData.length,this.data||(this.data=this._processData(a,b)),this.data},a.prototype._processData=function(a,b){return this.data=this.originalData,this.data=this.filter(this.data),this.data=this.sort(this.data),this.data.slice(a,a+b)},a.prototype.add=function(a){if(!a instanceof window.Francodacosta.DataSet.Record)throw new Error("record must be a instance of Record");return this.originalData.push(a),this.data=void 0},a}(),window.Francodacosta=window.Francodacosta||{},window.Francodacosta.DataSet=window.Francodacosta.DataSet||{},window.Francodacosta.DataSet.Record=function(){function a(a){this.data=a,this.dirty=!1}return a.prototype.getData=function(){return this.data},a.prototype.get=function(a){return a in this.data?this.data[a]:void 0},a.prototype.set=function(a,b){return this.get(a)!==b?(this.data[a]=b,this.dirty=!0):void 0},a.prototype.isDirty=function(){return this.dirty},a}();