UNPKG

8.38 kBJavaScriptView Raw
1function _classCallCheck(a,b){if(!(a instanceof b))throw new TypeError("Cannot call a class as a function")}function _defineProperties(a,b){for(var c,d=0;d<b.length;d++)c=b[d],c.enumerable=c.enumerable||!1,c.configurable=!0,"value"in c&&(c.writable=!0),Object.defineProperty(a,c.key,c)}function _createClass(a,b,c){return b&&_defineProperties(a.prototype,b),c&&_defineProperties(a,c),a}function _extends(){return _extends=Object.assign||function(a){for(var b,c=1;c<arguments.length;c++)for(var d in b=arguments[c],b)Object.prototype.hasOwnProperty.call(b,d)&&(a[d]=b[d]);return a},_extends.apply(this,arguments)}function _inherits(a,b){if("function"!=typeof b&&null!==b)throw new TypeError("Super expression must either be null or a function");a.prototype=Object.create(b&&b.prototype,{constructor:{value:a,writable:!0,configurable:!0}}),b&&_setPrototypeOf(a,b)}function _getPrototypeOf(a){return _getPrototypeOf=Object.setPrototypeOf?Object.getPrototypeOf:function(a){return a.__proto__||Object.getPrototypeOf(a)},_getPrototypeOf(a)}function _setPrototypeOf(a,b){return _setPrototypeOf=Object.setPrototypeOf||function(a,b){return a.__proto__=b,a},_setPrototypeOf(a,b)}function _assertThisInitialized(a){if(void 0===a)throw new ReferenceError("this hasn't been initialised - super() hasn't been called");return a}function _possibleConstructorReturn(a,b){return b&&("object"==typeof b||"function"==typeof b)?b:_assertThisInitialized(a)}const{React}=window,PureComponent=React.PureComponent,Component=React.Component,useState=React.useState,useEffect=React.useEffect;var events=["AllowDeleteChanged","AllowUpdateChanged","AllowInsertChanged","SaveFailed","PartialDataLoaded","DataLoadFailed","FieldChanged","RecordCreated","RecordRefreshed","RecordDeleting","RecordDeleted","AfterSave","BeforeLoad","BeforeSave","CancelEdit","CurrentIndexChanged","DataLoaded","DirtyChanged"];function dataObjectConnect(a){var b=!!(1<arguments.length&&arguments[1]!==void 0)&&arguments[1];return function(c){function d(){return"string"==typeof a?window[a]:a}var e=function(a){function e(a){var c;_classCallCheck(this,e),c=_possibleConstructorReturn(this,_getPrototypeOf(e).call(this,a));var f={},g=d();if(b){var h=!0,i=!1,j=void 0;try{for(var k,l,m=g.getFields()[Symbol.iterator]();!(h=(k=m.next()).done);h=!0)l=k.value,f[l.name]=null}catch(a){i=!0,j=a}finally{try{h||null==m.return||m.return()}finally{if(i)throw j}}}else f.data=[];return c.state=Object.assign({canDelete:g.isDeleteAllowed(),canUpdate:g.isUpdateAllowed(),canInsert:g.isInsertAllowed(),currentIndex:g.getCurrentIndex(),isDirty:g.isDirty(),isDeleting:!1,isLoading:!0===g.isDataLoading(),isSaving:!1,loadError:null,saveFailed:!1},f),c.handleAfterSave=c.handleAfterSave.bind(_assertThisInitialized(_assertThisInitialized(c))),c.handleAllowDeleteChanged=c.handleAllowDeleteChanged.bind(_assertThisInitialized(_assertThisInitialized(c))),c.handleAllowInsertChanged=c.handleAllowInsertChanged.bind(_assertThisInitialized(_assertThisInitialized(c))),c.handleAllowUpdateChanged=c.handleAllowUpdateChanged.bind(_assertThisInitialized(_assertThisInitialized(c))),c.handleBeforeLoad=c.handleBeforeLoad.bind(_assertThisInitialized(_assertThisInitialized(c))),c.handleBeforeSave=c.handleBeforeSave.bind(_assertThisInitialized(_assertThisInitialized(c))),c.handleCancelEdit=c.handleCancelEdit.bind(_assertThisInitialized(_assertThisInitialized(c))),c.handleCurrentIndexChanged=c.handleCurrentIndexChanged.bind(_assertThisInitialized(_assertThisInitialized(c))),c.handleDataLoaded=c.handleDataLoaded.bind(_assertThisInitialized(_assertThisInitialized(c))),c.handleDataLoadFailed=c.handleDataLoadFailed.bind(_assertThisInitialized(_assertThisInitialized(c))),c.handleDirtyChanged=c.handleDirtyChanged.bind(_assertThisInitialized(_assertThisInitialized(c))),c.handlePartialDataLoaded=c.handlePartialDataLoaded.bind(_assertThisInitialized(_assertThisInitialized(c))),c.handleRecordDeleting=c.handleRecordDeleting.bind(_assertThisInitialized(_assertThisInitialized(c))),c.handleRecordDeleted=c.handleRecordDeleted.bind(_assertThisInitialized(_assertThisInitialized(c))),c.handleSaveFailed=c.handleSaveFailed.bind(_assertThisInitialized(_assertThisInitialized(c))),c.setFieldValue=c.setFieldValue.bind(_assertThisInitialized(_assertThisInitialized(c))),c.setFieldValues=c.setFieldValues.bind(_assertThisInitialized(_assertThisInitialized(c))),c.updateData=c.updateData.bind(_assertThisInitialized(_assertThisInitialized(c))),c.handleFieldChanged=c.updateData,c.handleRecordCreated=c.updateData,c.handleRecordRefreshed=c.updateData,c}return _inherits(e,a),_createClass(e,[{key:"componentDidMount",value:function(){for(var a,b=d(),c=0;c<events.length;c++)a=events[c],b.attachEvent("on"+a,this["handle"+a]);this.updateData()}},{key:"componentWillUnmount",value:function(){for(var a,b=d(),c=0;c<events.length;c++)a=events[c],b.detachEvent("on"+a,this["handle"+a])}},{key:"cancelEdit",value:function(){d().cancelEdit()}},{key:"deleteRow",value:function(a){var c=d();return new Promise(function(d){var e=function(a,b){d({data:b,error:a})};b?c.deleteCurrentRow(e):c.deleteRow(a,e)})}},{key:"endEdit",value:function(a){var b=d();return new Promise(function(c){b.endEdit(function(b,d){"function"==typeof a&&a(b,d),c({data:d,error:b})})})}},{key:"updateData",value:function(){var a=0<arguments.length&&void 0!==arguments[0]?arguments[0]:{},c=d();if(b){var e=c.currentRow();this.setState(Object.assign(e,a))}else{var f=c.getData(),g=c.currentRow();this.setState(Object.assign({current:g,data:f},a))}}},{key:"handleAllowDeleteChanged",value:function(a){this.setState({canDelete:a})}},{key:"handleAllowUpdateChanged",value:function(a){this.setState({canUpdate:a})}},{key:"handleAllowInsertChanged",value:function(a){this.setState({canInsert:a})}},{key:"handleSaveFailed",value:function(){this.setState({saveFailed:!0})}},{key:"handlePartialDataLoaded",value:function(){return null}},{key:"handleDataLoadFailed",value:function(a){a?this.setState({isLoading:!1,loadError:a}):this.setState({isLoading:!1})}},{key:"handleRecordDeleting",value:function(){this.setState({isDeleting:!0})}},{key:"handleRecordDeleted",value:function(){this.updateData({isDeleting:!1})}},{key:"handleAfterSave",value:function(){this.updateData({isSaving:!1})}},{key:"handleBeforeLoad",value:function(){this.setState({isLoading:!0})}},{key:"handleBeforeSave",value:function(){this.setState({isSaving:!0,saveFailed:!1})}},{key:"handleCancelEdit",value:function(){this.updateData({isSaving:!1})}},{key:"handleCurrentIndexChanged",value:function(a){b?this.updateData():(this.updateData(),this.setState({currentIndex:a}))}},{key:"handleDataLoaded",value:function(){this.updateData({isLoading:!1,isSaving:!1,isDeleting:!1,saveFailed:!1})}},{key:"handleDirtyChanged",value:function(){this.setState({isDirty:d().isDirty()})}},{key:"refreshData",value:function(a){var b=d();return new Promise(function(c){b.refreshDataSource(function(b,d){"function"==typeof a&&a(b,d),c({data:d,error:b})})})}},{key:"refreshRow",value:function(a){var b=d();return new Promise(function(c){b.refreshCurrentRow(function(b,d){"function"==typeof a&&a(b,d),c({data:d,error:b})})})}},{key:"setFieldValue",value:function(a,b){d().currentRow(a,b),this.updateData()}},{key:"setFieldValues",value:function(a){var b=d();for(var c in a)a.hasOwnProperty(c)&&b.currentRow(c,a[c]);this.updateData()}},{key:"setCurrentIndex",value:function(a){d().setCurrentIndex(a)}},{key:"setParameter",value:function(){var a;(a=d()).setParameter.apply(a,arguments)}},{key:"render",value:function(){return React.createElement(c,_extends({},this.state,{onCancelEdit:this.cancelEdit,onCurrentIndexChange:this.setCurrentIndex,onDeleteRow:this.deleteRow,onEndEdit:this.endEdit,onFieldChange:this.setFieldValue,onFieldsChange:this.setFieldValues,onRefreshData:this.refreshData,onRefreshRow:this.refreshRow,onSetParameter:this.setParameter},this.props))}}]),e}(React.Component);return e.displayName="string"==typeof a?a:a.getDataSourceId(),e.displayName+="(".concat(function(){return c.displayName||c.name||"Component"}(),")"),e}}var properties=["onCancelEdit","onCurrentIndexChange","onEndEdit","onDeleteRow","onFieldChange","onFieldsChange","onRefreshData","onRefreshRow","onSetParameter","canDelete","canUpdate","canInsert","currentIndex","isDirty","isDeleting","isLoading","isSaving","loadError","saveFailed"];export{dataObjectConnect,properties};