catreact
Version:
Catavolt Core React Components
118 lines (117 loc) • 4.8 kB
JavaScript
;
/**
* Created by rburson on 12/23/15.
*/
Object.defineProperty(exports, "__esModule", { value: true });
var React = require("react");
var catreact_core_1 = require("../core/catreact-core");
/*
***************************************************
* Provide Login Support to other components
***************************************************
*/
exports.CvLogin = React.createClass({
mixins: [catreact_core_1.CvBaseMixin],
componentDidMount: function () {
},
getChildContext: function () {
return this.getDefaultChildContext();
},
getDefaultProps: function () {
return {
actionListeners: [],
loginListeners: [],
renderer: null
};
},
render: function () {
if (this.props.renderer) {
return this.props.renderer(this.getChildContext().cvContext, this._getCallbackObject());
}
else {
return null;
}
},
_getCallbackObject: function () {
var _this = this;
return {
isLoggedIn: function () {
return _this.catavolt().isLoggedIn;
},
changePasswordAndLogin: function (url, tenantId, clientType, userId, existingPassword, newPassword, resultCallback) {
_this._publishActionStarted('#login');
_this._postLogin(_this.catavolt().changePasswordAndLogin(url, tenantId, clientType, userId, existingPassword, newPassword), resultCallback);
},
login: function (gatewayHost, tenantId, clientType, userId, password, resultCallback) {
_this._publishActionStarted('#login');
_this._postLogin(_this.catavolt().login(gatewayHost, tenantId, clientType, userId, password), resultCallback);
},
loginDirectly: function (url, tenantId, clientType, userId, password, resultCallback) {
_this._publishActionStarted('#login');
_this._postLogin(_this.catavolt().loginDirectly(url, tenantId, clientType, userId, password), resultCallback);
}
};
},
_postLogin: function (loginResult, resultCallback) {
var _this = this;
loginResult.onComplete(function (appWinDefTry) {
_this._publishActionFinished('#login');
if (appWinDefTry.isSuccess) {
catreact_core_1.CvSessionManager.storeSession(_this.catavolt().sessionContextTry.success);
var eventRegistry = _this.eventRegistry();
var event_1 = {
type: catreact_core_1.CvEventType.LOGIN,
resourceId: catreact_core_1.CvResourceManager.resourceIdForObject(appWinDefTry.success, _this.catavolt()),
eventObj: { appWinDef: appWinDefTry.success }
};
if (resultCallback && typeof resultCallback === 'function') {
resultCallback(event_1.eventObj);
}
eventRegistry.publish(event_1, catreact_core_1.CvResourceManager.shouldCacheResult(_this.eventRegistry()));
_this.props.loginListeners.forEach(function (listener) {
listener(event_1);
});
}
else {
var event_2 = {
type: catreact_core_1.CvEventType.MESSAGE,
eventObj: { message: 'Login failed', messageObj: appWinDefTry.failure, type: catreact_core_1.CvMessageType.ERROR }
};
if (resultCallback && typeof resultCallback === 'function') {
resultCallback(null, appWinDefTry.failure);
}
_this.eventRegistry().publish(event_2, false);
}
});
},
_publishActionStarted: function (actionId) {
var e = {
type: catreact_core_1.CvEventType.ACTION_FIRED,
eventObj: {
actionId: actionId,
type: catreact_core_1.CvActionFiredResultType.ACTION_STARTED,
source: null,
clientAction: false
}
};
this.props.actionListeners.forEach(function (listener) {
listener(e);
});
this.eventRegistry().publish(e, false);
},
_publishActionFinished: function (actionId) {
var e = {
type: catreact_core_1.CvEventType.ACTION_FIRED,
eventObj: {
actionId: actionId,
type: catreact_core_1.CvActionFiredResultType.ACTION_COMPLETED,
source: null,
clientAction: false
}
};
this.props.actionListeners.forEach(function (listener) {
listener(e);
});
this.eventRegistry().publish(e, false);
}
});