UNPKG

angular4-material-table

Version:

Angular 4 table based on @angular/cdk table structure, to allow row insertion, edition, validation and deletion.

1 lines 7.31 kB
!function(t,e){"object"==typeof exports&&"undefined"!=typeof module?e(exports,require("@angular/common"),require("@angular/core"),require("lodash.clonedeep"),require("@angular/cdk/collections"),require("rxjs")):"function"==typeof define&&define.amd?define(["exports","@angular/common","@angular/core","lodash.clonedeep","@angular/cdk/collections","rxjs"],e):e((t.ng=t.ng||{},t.ng["angular4-material-table"]={}),t.ng.common,t.ng.core,t.index,t.ng.cdk.collections,null)}(this,function(t,e,r,o,n,a){"use strict";o=o&&o.hasOwnProperty("default")?o.default:o;var i,c,u,s=function(){function t(){}return t.decorators=[{type:r.NgModule,args:[{declarations:[],imports:[e.CommonModule],exports:[]}]}],t}(),l=function(){function t(){}return t.decorators=[{type:r.Injectable}],t}(),d=function(){function t(){}return t.prototype.getRowValidator=function(){return null},t.decorators=[{type:r.Injectable}],t}(),p=function(){function t(){}return t.prototype.delete=function(){this.source.delete(this.id)},t.prototype.confirmEditCreate=function(){return-1==this.id?this.source.confirmCreate(this):this.source.confirmEdit(this)},t.prototype.startEdit=function(){this.originalData=o(this.currentData),this.editing=!0},t.prototype.cancelOrDelete=function(){-1!=this.id&&this.editing?(this.currentData=this.originalData,this.editing=!1):this.delete()},t}(),f=(i=Object.setPrototypeOf||{__proto__:[]}instanceof Array&&function(t,e){t.__proto__=e}||function(t,e){for(var r in e)e.hasOwnProperty(r)&&(t[r]=e[r])},function(t,e){function r(){this.constructor=t}i(t,e),t.prototype=null===e?Object.create(e):(r.prototype=e.prototype,new r)}),h=function(r){function t(t){var e=r.call(this)||this;return e.validator=t.validator,Object.assign(e,t),e}return f(t,r),Object.defineProperty(t.prototype,"currentData",{get:function(){return this.validator.getRawValue()},set:function(t){this.validator.patchValue(t)},enumerable:!0,configurable:!0}),Object.defineProperty(t.prototype,"editing",{get:function(){return this.validator.enabled},set:function(t){t?this.validator.enable():this.validator.disable()},enumerable:!0,configurable:!0}),t.prototype.isValid=function(){return this.validator.valid},t}(p),g=(c=Object.setPrototypeOf||{__proto__:[]}instanceof Array&&function(t,e){t.__proto__=e}||function(t,e){for(var r in e)e.hasOwnProperty(r)&&(t[r]=e[r])},function(t,e){function r(){this.constructor=t}c(t,e),t.prototype=null===e?Object.create(e):(r.prototype=e.prototype,new r)}),w=function(r){function t(t){var e=r.call(this)||this;return Object.assign(e,t),e}return g(t,r),Object.defineProperty(t.prototype,"validator",{get:function(){return null},set:function(t){},enumerable:!0,configurable:!0}),Object.defineProperty(t.prototype,"currentData",{get:function(){return this._currentData},set:function(t){this._currentData=t},enumerable:!0,configurable:!0}),Object.defineProperty(t.prototype,"editing",{get:function(){return this._editing},set:function(t){this._editing=t},enumerable:!0,configurable:!0}),t.prototype.isValid=function(){return!0},t}(p),y=function(){function t(){}return t.createTableElement=function(t){return this.isValidatorDefined(t)?new h(t):new w(t)},t.isValidatorDefined=function(t){return null!=t.validator&&null!=t.validator},t}(),b=(u=Object.setPrototypeOf||{__proto__:[]}instanceof Array&&function(t,e){t.__proto__=e}||function(t,e){for(var r in e)e.hasOwnProperty(r)&&(t[r]=e[r])},function(t,e){function r(){this.constructor=t}u(t,e),t.prototype=null===e?Object.create(e):(r.prototype=e.prototype,new r)}),m=function(i){function t(t,e,r,o){void 0===o&&(o={prependNewElements:!1,suppressErrors:!1});var n=i.call(this)||this;if(n.validatorService=r,n.config=o,r||(n.validatorService=new d),e)n.dataConstructor=e;else{if(!(t&&0<t.length))throw new Error("You must define either a non empty array, or an associated class to build the table.");n.dataKeys=Object.keys(t[0])}return n.checkValidatorFields(n.validatorService),n.rowsSubject=new a.BehaviorSubject(n.getRowsFromData(t)),n.datasourceSubject=new a.Subject,n}return b(t,i),t.prototype.checkValidatorFields=function(t){var r=this,e=t.getRowValidator();if(null!=e){var o=Object.keys(this.createNewObject());Object.keys(e.controls).forEach(function(e){o.some(function(t){return t===e})&&r.logError("Validator form control keys must match row object keys.")})}},t.prototype.logError=function(t){this.config.suppressErrors||console.error(t)},t.prototype.createNew=function(){var t=this.rowsSubject.getValue();if(!this.existsNewElement(t)){var e=y.createTableElement({id:-1,editing:!0,currentData:this.createNewObject(),source:this,validator:this.validatorService.getRowValidator()});this.config.prependNewElements?this.rowsSubject.next([e].concat(t)):(t.push(e),this.rowsSubject.next(t))}},t.prototype.confirmCreate=function(t){if(!t.isValid())return!1;var e=this.rowsSubject.getValue();return t.id=e.length-1,this.rowsSubject.next(e),t.editing=!1,this.updateDatasourceFromRows(e),!0},t.prototype.confirmEdit=function(t){if(!t.isValid())return!1;var e=this.rowsSubject.getValue();return e[this.getIndexFromRowId(t.id,e)]=t,this.rowsSubject.next(e),t.originalData=void 0,t.editing=!1,this.updateDatasourceFromRows(e),!0},t.prototype.delete=function(t){var e=this.rowsSubject.getValue(),r=this.getIndexFromRowId(t,e);e.splice(r,1),this.updateRowIds(r,e),this.rowsSubject.next(e),-1!=t&&this.updateDatasourceFromRows(e)},t.prototype.getRow=function(t){var e=this.rowsSubject.getValue(),r=this.getIndexFromRowId(t,e);return 0<=r&&r<e.length?e[r]:null},t.prototype.updateDatasource=function(t,e){void 0===e&&(e={emitEvent:!0}),this.currentData!==t&&(this.currentData=t,this.rowsSubject.next(this.getRowsFromData(t)),e.emitEvent&&this.datasourceSubject.next(t))},t.prototype.existsNewElement=function(t){return!(0==t.length||-1<t[this.getNewRowIndex(t)].id)},t.prototype.getNewRowIndex=function(t){return this.config.prependNewElements?0:t.length-1},t.prototype.getRowIdFromIndex=function(t,e){return this.config.prependNewElements?e-1-t:t},t.prototype.getIndexFromRowId=function(t,e){return-1==t?this.existsNewElement(e)?this.getNewRowIndex(e):-1:this.config.prependNewElements?e.length-1-t:t},t.prototype.updateRowIds=function(t,e){for(var r=this.config.prependNewElements?-1:1,o=t;o<e.length&&0<=o;o+=r)-1!=e[o].id&&(e[o].id=this.getRowIdFromIndex(o,e.length))},t.prototype.getDataFromRows=function(t){return t.filter(function(t){return-1!=t.id}).map(function(t){return t.originalData?t.originalData:t.currentData})},t.prototype.updateDatasourceFromRows=function(t){this.currentData=this.getDataFromRows(t),this.datasourceSubject.next(this.currentData)},t.prototype.getRowsFromData=function(r){var o=this;return r.map(function(t,e){return y.createTableElement({id:o.getRowIdFromIndex(e,r.length),editing:!1,currentData:t,source:o,validator:o.validatorService.getRowValidator()})})},t.prototype.createNewObject=function(){return this.dataConstructor?new this.dataConstructor:this.dataKeys.reduce(function(t,e){return t[e]=void 0,t},{})},t.prototype.connect=function(){return this.rowsSubject.asObservable()},t.prototype.disconnect=function(){},t}(n.DataSource);t.AppModule=s,t.ValidatorService=l,t.DefaultValidatorService=d,t.TableDataSource=m,t.TableElement=p,t.TableElementTemplateDriven=w,t.TableElementReactiveForms=h,Object.defineProperty(t,"__esModule",{value:!0})});