catreact
Version:
Catavolt Core React Components
295 lines (294 loc) • 13.3 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
/**
* 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");
var CvQueryMode;
(function (CvQueryMode) {
CvQueryMode[CvQueryMode["NORMAL"] = 0] = "NORMAL";
CvQueryMode[CvQueryMode["DEFERRED"] = 1] = "DEFERRED";
CvQueryMode[CvQueryMode["MANUAL"] = 2] = "MANUAL";
})(CvQueryMode = exports.CvQueryMode || (exports.CvQueryMode = {}));
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;
//we allow multiple refresh calls - the sdk chains Futures so they are safe
this.refreshInProgress = this.refreshInProgress ? ++this.refreshInProgress : 1;
var queryContext = this.queryContext(nextProps, nextContext);
queryContext.setScroller(pageSize, null, [catavolt_sdk_1.QueryMarkerOption.None]);
catreact_core_1.CvActionBase._publishActionStarted('#refresh', queryContext, false, this.props.actionListeners, this.eventRegistry());
queryContext.scroller.refresh().onComplete(function (entityRecTry) {
_this.refreshInProgress--;
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();
}
}
catreact_core_1.CvActionBase._publishActionFinished('#refresh', queryContext, _this.props.actionListeners, _this.eventRegistry());
});
},
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;
}
},
isInProgress: function () {
return !!this.refreshInProgress;
},
checkReloadNeeded: 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
//is this an update to recordPageSize?
if (nextProps && nextProps.recordPageSize && nextProps.recordPageSize != this.props.recordPageSize) {
this.reload(nextProps.recordPageSize, nextProps, nextContext);
//is this a 'new' queryContext object
}
else if (queryContext && this.props.queryContext && (queryContext != this.props.queryContext)) {
this.reload(this.props.recordPageSize, nextProps, nextContext);
// is the queryContext's buffer empty? @TODO - removing this for now, not sure it's necessary
//} else if (queryContext.scroller.buffer && queryContext.scroller.buffer.length === 0) {
//this.reload(this.props.recordPageSize, nextProps, nextContext);
//is the pane out of date?
}
else if (queryContext.isRefreshNeeded) {
this.reload(this.props.recordPageSize, nextProps, nextContext);
}
},
_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 () {
if (this.props.queryMode === CvQueryMode.NORMAL) {
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);
}
if (this.props.queryMode === CvQueryMode.NORMAL) {
this.checkReloadNeeded(nextProps, nextContext);
}
},
_componentWillUnmount: function () {
this.eventRegistry().unsubscribe(this._dataChangeListener);
},
_dataChangeListener: function (dataChangeResult) {
//reload if someone else ('source' !== me) published a state change
if (dataChangeResult.eventObj.type === catreact_core_1.CvStateChangeType.DATA_CHANGE &&
dataChangeResult.eventObj.source !== this.queryContext()) {
if (this.props.queryMode === CvQueryMode.NORMAL ||
this.props.queryMode === CvQueryMode.DEFERRED) {
this.reload(this.props.recordPageSize);
}
}
},
_getCallbackObj: function () {
var _this = this;
var inProgress = false;
var suppressEvents = false;
return {
hasMoreBackward: function () {
return _this.queryContext().scroller.hasMoreBackward;
},
hasMoreForward: function () {
return _this.queryContext().scroller.hasMoreForward;
},
isInProgress: function () {
return inProgress || _this.isInProgress();
},
pageBackward: function (resultCallback, replaceBuffer) {
if (replaceBuffer === void 0) { replaceBuffer = false; }
if (!inProgress) {
var queryContext_2 = _this.queryContext();
var scroller_1 = queryContext_2.scroller;
if (scroller_1.hasMoreBackward) {
inProgress = true;
if (!suppressEvents)
catreact_core_1.CvActionBase._publishActionStarted('#pageBackward', queryContext_2, false, _this.props.actionListeners, _this.eventRegistry());
scroller_1.pageBackward().onComplete(function (entityRecTry) {
inProgress = false;
if (entityRecTry.isFailure) {
resultCallback(null, Error(entityRecTry.failure));
}
else {
if (replaceBuffer) {
scroller_1.trimLast(scroller_1.buffer.length - entityRecTry.success.length);
}
if (!suppressEvents)
_this.forceUpdate();
resultCallback(entityRecTry.success.length);
}
if (!suppressEvents)
catreact_core_1.CvActionBase._publishActionFinished('#pageBackward', queryContext_2, _this.props.actionListeners, _this.eventRegistry());
});
}
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 queryContext_3 = _this.queryContext();
var scroller_2 = queryContext_3.scroller;
if (scroller_2.hasMoreForward) {
inProgress = true;
if (!suppressEvents)
catreact_core_1.CvActionBase._publishActionStarted('#pageForward', queryContext_3, false, _this.props.actionListeners, _this.eventRegistry());
scroller_2.pageForward().onComplete(function (entityRecTry) {
inProgress = false;
if (entityRecTry.isFailure) {
resultCallback(null, Error(entityRecTry.failure));
}
else {
if (replaceBuffer) {
scroller_2.trimFirst(scroller_2.buffer.length - entityRecTry.success.length);
}
if (!suppressEvents)
_this.forceUpdate();
resultCallback(entityRecTry.success.length);
}
if (!suppressEvents)
catreact_core_1.CvActionBase._publishActionFinished('#pageForward', queryContext_3, _this.props.actionListeners, _this.eventRegistry());
});
}
else {
resultCallback(null, Error('There are no more records'));
}
}
else {
resultCallback(null, Error('paging already in progress'));
}
},
pageSize: function () {
return _this.queryContext().scroller.pageSize;
},
refresh: function () {
_this.reload(_this.props.recordPageSize);
},
suppressEvents: function (val) { suppressEvents = val; }
};
},
_getDefaultProps: function () {
return {
paneRef: null,
formContext: null,
queryContext: null,
queryRenderer: null,
errorRenderer: null,
stateChangeListeners: [],
actionListeners: [],
actionProvider: null,
recordPageSize: 50,
queryMode: CvQueryMode.NORMAL
};
},
_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 (queryContext.hasError) {
return this.props.errorRenderer ? this.props.errorRenderer(this.getChildContext().cvContext, queryContext.error) : null;
}
else 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;
}
}
};