UNPKG

catreact

Version:

Catavolt Core React Components

243 lines (242 loc) 9.94 kB
"use strict"; /** * Created by rburson on 4/29/16. */ var React = require('react'); var catreact_core_1 = require('./catreact-core'); var catavolt_sdk_1 = require('catavolt-sdk'); exports.CvQueryBase = { formContext: function (nextProps, nextContext) { if (nextProps && nextProps.formContext) { return nextProps.formContext; } else if (nextContext) { return this.firstInScope(catavolt_sdk_1.FormContext, nextContext.cvContext.scopeCtx); } else { return this.props.formContext || this.firstInScope(catavolt_sdk_1.FormContext); } }, getChildContext: function () { var ctx = this.getDefaultChildContext(); ctx.cvContext.scopeCtx.scopeObj = this.queryContext(); return ctx; }, reload: function (pageSize, nextProps, nextContext) { var _this = this; var queryContext = this.queryContext(nextProps, nextContext); queryContext.setScroller(pageSize, null, [catavolt_sdk_1.QueryMarkerOption.None]); queryContext.scroller.refresh().onComplete(function (entityRecTry) { if (entityRecTry.isFailure) { var event_1 = { type: catreact_core_1.CvEventType.MESSAGE, eventObj: { message: 'Failed to load query buffer', messageObj: entityRecTry.failure, type: catreact_core_1.CvMessageType.ERROR } }; _this.eventRegistry().publish(event_1, false); } else { //Log.debug(JSON.stringify(queryContext.scroller.buffer)); if (_this.isMounted()) { _this.forceUpdate(); } } }); }, queryContext: function (nextProps, nextContext) { if (nextProps && nextProps.queryContext) { return nextProps.queryContext; } else if (this.props.queryContext) { return this.props.queryContext; } else { var paneRef_1 = nextProps ? nextProps.paneRef : this.props.paneRef; var formContext = this.formContext(nextProps, nextContext); var queryContext_1 = null; formContext.childrenContexts.some(function (childContext) { if (childContext instanceof catavolt_sdk_1.QueryContext && childContext.paneRef == paneRef_1) { queryContext_1 = childContext; return true; } else { return false; } }); return queryContext_1; } }, refresh: function (nextProps, nextContext) { var queryContext = this.queryContext(nextProps, nextContext); //update page size if it has changed - note: this resets the buffer //attempt to detect changes to the queryContext if ((nextProps && nextProps.recordPageSize && nextProps.recordPageSize != this.props.recordPageSize) || (queryContext && queryContext != this.props.queryContext) || (queryContext.scroller.buffer && queryContext.scroller.buffer.length === 0)) { this.reload(nextProps.recordPageSize, nextProps, nextContext); } else { //otherwise, assume an underlying change... if (this.isMounted()) { //if there are 'nextProps' we know that the underlying queryContext has been updated in some way... this.forceUpdate(); } } }, _checkDestroyed: function () { var queryContext = this.queryContext(); if (queryContext && queryContext.isDestroyed) { var event_2 = { type: catreact_core_1.CvEventType.STATE_CHANGE, resourceId: null, eventObj: { source: queryContext, type: catreact_core_1.CvStateChangeType.DESTROYED } }; this.props.stateChangeListeners.forEach(function (listener) { listener(event_2); }); } }, _componentWillMount: function () { this.reload(this.props.recordPageSize); if (this.props.actionProvider) { this.props.actionProvider.subscribe(this._handleAction); } this.eventRegistry() .subscribe(this._dataChangeListener, catreact_core_1.CvEventType.STATE_CHANGE); }, _componentWillReceiveProps: function (nextProps, nextContext) { if (nextProps.actionProvider && nextProps.actionProvider !== this.props.actionProvider) { nextProps.actionProvider.subscribe(this._handleAction); } this.refresh(nextProps, nextContext); }, _componentWillUnmount: function () { this.eventRegistry().unsubscribe(this._dataChangeListener); }, _dataChangeListener: function (dataChangeResult) { if (dataChangeResult.eventObj.type === catreact_core_1.CvStateChangeType.DATA_CHANGE && dataChangeResult.eventObj.source !== this.queryContext()) { this.reload(this.props.recordPageSize); } }, _getCallbackObj: function () { var _this = this; var inProgress = false; return { hasMoreBackward: function () { return _this.queryContext().scroller.hasMoreBackward; }, hasMoreForward: function () { return _this.queryContext().scroller.hasMoreForward; }, pageBackward: function (resultCallback, replaceBuffer) { if (replaceBuffer === void 0) { replaceBuffer = false; } if (!inProgress) { var scroller_1 = _this.queryContext().scroller; if (scroller_1.hasMoreBackward) { inProgress = true; scroller_1.pageBackward().onComplete(function (entityRecTry) { if (entityRecTry.isFailure) { resultCallback(null, Error(entityRecTry.failure)); } else { if (replaceBuffer) { scroller_1.trimLast(scroller_1.buffer.length - entityRecTry.success.length); } _this.refresh(); resultCallback(entityRecTry.success.length); } inProgress = false; }); } else { resultCallback(null, Error('There are no more previous records')); } } else { resultCallback(null, Error('paging already in progress')); } }, pageForward: function (resultCallback, replaceBuffer) { if (replaceBuffer === void 0) { replaceBuffer = false; } if (!inProgress) { var scroller_2 = _this.queryContext().scroller; if (scroller_2.hasMoreForward) { inProgress = true; scroller_2.pageForward().onComplete(function (entityRecTry) { if (entityRecTry.isFailure) { resultCallback(null, Error(entityRecTry.failure)); } else { if (replaceBuffer) { scroller_2.trimFirst(scroller_2.buffer.length - entityRecTry.success.length); } _this.refresh(); resultCallback(entityRecTry.success.length); } inProgress = false; }); } else { resultCallback(null, Error('There are no more records')); } } else { resultCallback(null, Error('paging already in progress')); } }, pageSize: function () { return _this.queryContext().scroller.pageSize; } }; }, _getDefaultProps: function () { return { paneRef: null, formContext: null, queryContext: null, queryRenderer: null, stateChangeListeners: [], actionProvider: null, recordPageSize: 50 }; }, _handleAction: function (params) { var event = params.event; var callback = params.callback; if (event.eventObj.clientAction) { var actionId = event.eventObj.actionId; if (actionId === '#refresh') { this.reload(this.queryContext().scroller.buffer.length); callback(true); } else { callback(null, "Action " + actionId + " not yet implemented."); } } }, _render: function () { var queryContext = this.queryContext(); if (queryContext && !queryContext.isDestroyed) { if (this.props.renderer) { return this.props.renderer(this.getChildContext().cvContext); } else if (this.props.queryRenderer) { return this.props.queryRenderer(this.getChildContext().cvContext, this._getCallbackObj()); } else if (React.Children.count(this.props.children) > 0) { return this.props.children; } else { return null; } } else { return null; } } };