catreact
Version:
Catavolt Core React Components
99 lines (98 loc) • 4.11 kB
JavaScript
"use strict";
/**
* Created by rburson on 12/23/15.
*/
Object.defineProperty(exports, "__esModule", { value: true });
var React = require("react");
var catreact_core_1 = require("./catreact-core");
var catavolt_sdk_1 = require("catavolt-sdk");
/*
***************************************************
* Render a NavRequest
***************************************************
*/
exports.CvNavigation = React.createClass({
mixins: [catreact_core_1.CvBaseMixin],
componentDidMount: function () {
if (this.props.navigationResult) {
this.setState({ navRequest: this.props.navigationResult.navRequest, visible: true });
}
else if (this.props.navigationId) {
this._handleRetrieveNavigation(this.props.navigationId);
}
this.eventRegistry().subscribe(this._navListener, catreact_core_1.CvEventType.NAVIGATION);
},
componentWillUnmount: function () {
this.eventRegistry().unsubscribe(this._navListener);
},
componentWillReceiveProps: function (nextProps) {
if (nextProps.navigationResult) {
this.setState({ navRequest: nextProps.navigationResult.navRequest, visible: true });
}
else if (nextProps.navigationId) {
this._handleRetrieveNavigation(nextProps.navigationId);
}
},
getChildContext: function () {
var ctx = this.getDefaultChildContext();
ctx.cvContext.scopeCtx.scopeObj = this.state.navRequest;
return ctx;
},
getDefaultProps: function () {
return { persistent: false, targetId: null, navigationResult: null, navigationId: null };
},
getInitialState: function () {
return { visible: false, navRequest: null };
},
render: function () {
if (this.state.visible && this.state.navRequest) {
if (this.props.renderer) {
return this.props.renderer(this.getChildContext().cvContext);
}
else if (React.Children.count(this.props.children) > 0) {
return this.props.children;
}
else {
return null;
}
}
else {
return null;
}
},
_handleRetrieveNavigation: function (navigationId) {
var _this = this;
var event = this.eventRegistry().getEventByKey(navigationId);
if (!event) {
var _a = catreact_core_1.CvResourceManager.deserializeRedirection(navigationId), redirection = _a.redirection, actionSource_1 = _a.actionSource;
catavolt_sdk_1.NavRequestUtil.fromRedirection(redirection, actionSource_1, this.catavolt().sessionContextTry.success).onComplete(function (navRequestTry) {
if (navRequestTry.isSuccess) {
catreact_core_1.CvNavigationResultUtil.publishNavigation(_this.catavolt(), _this.eventRegistry(), navRequestTry.success, actionSource_1.actionId, actionSource_1.workbenchId, null, null, false, false);
_this.setState({ navRequest: navRequestTry.success, visible: true });
}
else {
var event_1 = { type: catreact_core_1.CvEventType.MESSAGE, eventObj: { message: 'Navigation failed',
messageObj: navRequestTry.failure, type: catreact_core_1.CvMessageType.ERROR } };
_this.eventRegistry().publish(event_1, false);
}
});
}
else {
this.setState({ navRequest: event.eventObj.navRequest, visible: true });
}
},
_navListener: function (navEvent) {
var navRequest = navEvent.eventObj.navRequest;
if (navRequest && this.isMounted()) {
if (navEvent.eventObj.navTarget) {
if (this.props.targetId === navEvent.eventObj.navTarget) {
this.setState({ navRequest: navRequest, visible: true });
}
else {
if (!this.props.persistent)
this.setState({ visible: false });
}
}
}
},
});