catreact
Version:
Catavolt Core React Components
331 lines (330 loc) • 15.4 kB
JavaScript
"use strict";
/**
* Created by rburson on 2/10/16.
*/
Object.defineProperty(exports, "__esModule", { value: true });
var React = require("react");
var catreact_core_1 = require("./catreact-core");
var catavolt_sdk_1 = require("catavolt-sdk");
exports.CvActionBase = {
/*
Find a PaneContext in the hierarchy that has a MenuDef with this actionId
*/
findPaneContextWithActionId: function (actionId) {
var paneContext = null;
if (this.props.paneContext) {
var menuDef = this.props.paneContext.findMenuDefAt(actionId);
if (menuDef) {
paneContext = this.props.paneContext;
}
}
else {
var scopeCtx = this.findFirstScopeCtx(function (scopeCtx) {
if (scopeCtx.scopeObj instanceof catavolt_sdk_1.PaneContext) {
return scopeCtx.scopeObj.findMenuDefAt(actionId) != null;
}
else {
return false;
}
});
if (scopeCtx) {
paneContext = scopeCtx.scopeObj;
}
}
return paneContext;
},
getSelectedTargets: function () {
return this.state.selectedTargets || [];
},
paneContext: function () {
var paneContext = null;
if (this.props.paneContext) {
paneContext = this.props.paneContext;
}
else if (this.props.actionId) {
paneContext = this.findPaneContextWithActionId(this.props.actionId);
}
return paneContext ? paneContext : this.findPaneContext();
},
menuDef: function () {
if (this.props.menuDef) {
return this.props.menuDef;
}
else {
var paneContext = this.paneContext();
return paneContext.findMenuDefAt(this.props.actionId);
}
},
performAction: function (resultCallback, presaveExecuted, destroyRequested) {
var _this = this;
var paneContext = this.paneContext();
if (paneContext) {
var menuDef_1 = this.menuDef();
if (menuDef_1) {
var result = null;
/*
* Only navigate if the action is a not a 'client-side' action.
* If it is, the 'action' will still be published so that the client can handle it appropriately.
*/
var actionMeta_1 = this._getActionMeta(menuDef_1.actionId);
if (!actionMeta_1.isClientAction) {
if (paneContext instanceof catavolt_sdk_1.QueryContext) {
result = paneContext.performMenuAction(menuDef_1, this.getSelectedTargets());
}
else if (paneContext instanceof catavolt_sdk_1.EditorContext) {
var editorContext_1 = paneContext;
var entityRec = editorContext_1.entityRec;
/* @TODO maybe we want to save changes here, maybe not... they are currently discarded */
var pendingWrites = catavolt_sdk_1.EntityRecUtil.newEntityRec(entityRec.objectId, []);
if (editorContext_1.isWriteMode && menuDef_1.isPresaveDirective && !presaveExecuted) {
// Perform the write if needed. By design, if there is a navigation as a result
// of the write it is ignored. The purpose of the write is to setup the action.
var writeResult = editorContext_1.write({ "followupAction": menuDef_1.actionId });
writeResult.onComplete(function (writeTry) {
if (writeTry.isFailure) {
var event_1 = {
type: catreact_core_1.CvEventType.MESSAGE,
eventObj: {
message: 'Save changes failed',
messageObj: writeTry.failure,
type: catreact_core_1.CvMessageType.ERROR
}
};
_this.eventRegistry().publish(event_1, false);
if (resultCallback && typeof resultCallback === 'function') {
resultCallback(null, Error(writeTry.failure));
}
}
else {
// With a successful write, proceed with executing the action.
_this.performAction(resultCallback, true, editorContext_1.isDestroyRequested);
}
});
}
else {
result = editorContext_1.performMenuAction(menuDef_1, pendingWrites);
}
}
else if (paneContext instanceof catavolt_sdk_1.FormContext) {
result = paneContext.performMenuAction(menuDef_1);
}
}
/*
* Client Action - no server involvement
* Note: client actions are handled supplying an 'actionHandler' propery to the CvAction
* resultCallbacks will only be called for successful or failed 'Navigations' (non-client actions)
*/
if (actionMeta_1.isClientAction) {
/* let the designated handler perform the action */
if (this.props.actionHandler) {
/* Publish the action fired */
var event_2 = this._publishActionStarted(menuDef_1.actionId, paneContext, actionMeta_1.isClientAction, this.props.actionListeners, this.eventRegistry());
this.props.actionHandler({ event: event_2, callback: function (success, error) {
if (error) {
var de = new catavolt_sdk_1.DialogException();
de.message = '' + error;
var event_3 = {
type: catreact_core_1.CvEventType.MESSAGE, eventObj: { message: '' + error, messageObj: de, type: catreact_core_1.CvMessageType.ERROR }
};
_this.eventRegistry().publish(event_3, false);
}
/* Publish the action finished */
_this._publishActionFinished(menuDef_1.actionId, paneContext, _this.props.actionListeners, _this.eventRegistry());
} });
}
else {
/* If there's no handler for a client action, publish both start and finish events */
this._publishActionStarted(menuDef_1.actionId, paneContext, actionMeta_1.isClientAction, this.props.actionListeners, this.eventRegistry());
this._publishActionFinished(menuDef_1.actionId, paneContext, this.props.actionListeners, this.eventRegistry());
}
/*
* Otherwise the server has supplied us with a result
*/
}
else if (result) {
if (destroyRequested) {
paneContext.destroy();
}
/* Publish the action fired */
var event_4 = this._publishActionStarted(menuDef_1.actionId, paneContext, actionMeta_1.isClientAction, this.props.actionListeners, this.eventRegistry());
/* Publish the navigation */
result.onComplete(function (navRequestTry) {
/* Publish the action finished */
_this._publishActionFinished(menuDef_1.actionId, paneContext, _this.props.actionListeners, _this.eventRegistry());
if (navRequestTry.isSuccess) {
var e = catreact_core_1.CvNavigationResultUtil.publishNavigation(_this.catavolt(), _this.eventRegistry(), navRequestTry.success, _this.props.actionId, null, _this.props.navTarget, _this.props.navigationListeners, paneContext.isDestroyed, actionMeta_1.noTransition);
if (resultCallback && typeof resultCallback === 'function') {
resultCallback(e.eventObj);
}
_this._checkDestroyed(paneContext);
}
else {
var event_5 = { type: catreact_core_1.CvEventType.MESSAGE, eventObj: { message: 'Navigation failed',
messageObj: navRequestTry.failure, type: catreact_core_1.CvMessageType.ERROR } };
if (resultCallback && typeof resultCallback === 'function') {
resultCallback(null, navRequestTry.failure);
}
_this.eventRegistry().publish(event_5, false);
if (destroyRequested) {
_this._checkDestroyed(paneContext);
}
}
});
}
}
}
else {
catavolt_sdk_1.Log.info("Failed to find menuDef for action: " + this.props.actionId);
}
},
_checkDestroyed: function (paneContext) {
if (paneContext && paneContext.isDestroyed) {
var event_6 = {
type: catreact_core_1.CvEventType.STATE_CHANGE,
resourceId: null,
eventObj: { source: paneContext, type: catreact_core_1.CvStateChangeType.DESTROYED }
};
this.props.stateChangeListeners.forEach(function (listener) {
listener(event_6);
});
}
},
_componentDidMount: function () {
if (this.props.selectionProvider) {
this.props.selectionProvider.subscribe(this._setSelection);
}
if (this.props.fireOnLoad) {
this._getCallbackObj().fireAction(this.props.fireOnLoad);
}
},
_componentWillReceiveProps: function (nextProps) {
if (nextProps.selectionProvider && (nextProps.selectionProvider != this.props.selectionProvider)) {
nextProps.selectionProvider.subscribe(this._setSelection);
}
if (nextProps.fireOnUpdate) {
this._getCallbackObj().fireAction(nextProps.fireOnUpdate);
}
},
_shouldComponentUpdate: function (nextProps, nextState) {
//don't update for a selection change
if (this.state.selectedTargets != null && nextState.selectedTargets != null) {
if (this.state.selectedTargets.length != nextState.selectedTargets.length) {
return false;
}
}
},
_getCallbackObj: function () {
var _this = this;
return {
fireAction: function (resultCallback) {
_this.performAction(resultCallback);
}
};
},
_getInitialState: function () {
return { selectedTargets: [] };
},
_setSelection: function (selectedTargets) {
this.setState({ selectedTargets: selectedTargets });
},
/** static methods */
/* static */
/* intended for resuse by other components */
_getActionMeta: function (actionId) {
//treat search like a server action
//searchQuery currently has a server-side bug if null is submitted for 'keyword'
//if(actionId === "#search" || actionId === 'searchQuery') {
if (actionId === "#search") {
return { isClientAction: false, noTransition: true };
}
else {
var isClientAction = actionId === 'clear' || (actionId.substr(0, 1) === "#");
return { isClientAction: isClientAction, noTransition: false };
}
},
/* static */
/* intended for resuse by other components */
_publishActionStarted: function (actionId, source, clientAction, actionListeners, eventRegistry) {
var e = {
type: catreact_core_1.CvEventType.ACTION_FIRED,
eventObj: {
actionId: actionId,
type: catreact_core_1.CvActionFiredResultType.ACTION_STARTED,
source: source,
clientAction: clientAction
}
};
actionListeners.forEach(function (listener) {
listener(e);
});
eventRegistry.publish(e, false);
return e;
},
/* static */
/* intended for resuse by other components */
_publishActionFinished: function (actionId, source, actionListeners, eventRegistry) {
var e = {
type: catreact_core_1.CvEventType.ACTION_FIRED,
eventObj: { actionId: actionId, type: catreact_core_1.CvActionFiredResultType.ACTION_COMPLETED, source: source, clientAction: false }
};
actionListeners.forEach(function (listener) {
listener(e);
});
eventRegistry.publish(e, false);
return e;
},
};
/*
***************************************************
* A catavolt action
***************************************************
*/
exports.CvAction = React.createClass({
mixins: [catreact_core_1.CvBaseMixin, exports.CvActionBase],
componentDidMount: function () {
this._componentDidMount();
},
componentWillReceiveProps: function (nextProps) {
this._componentWillReceiveProps(nextProps);
},
getChildContext: function () {
var ctx = this.getDefaultChildContext();
ctx.cvContext.scopeCtx.scopeObj = this.menuDef();
return ctx;
},
getDefaultProps: function () {
return {
actionId: null,
fireOnLoad: null,
paneContext: null,
navTarget: null,
menuDef: null,
actionListeners: [],
navigationListeners: [],
stateChangeListeners: [],
selectionProvider: null,
renderer: null,
wrapperElemName: 'span',
wrapperElemProps: {},
wrapperEventHandlerName: 'onClick',
actionHandlerProvider: null
};
},
getInitialState: function () {
return this._getInitialState();
},
render: function () {
var _this = this;
if (this.props.renderer) {
return this.props.renderer(this.getChildContext().cvContext, this._getCallbackObj());
}
else if (this.props.children) {
var props = catavolt_sdk_1.ObjUtil.addAllProps(this.props.wrapperElemProps, {});
props[this.props.wrapperEventHandlerName] = function () { _this.performAction(); };
return React.createElement(this.props.wrapperElemName, props, this.props.children);
}
else {
return null;
}
}
});