fullts
Version:
Full stack framework in TypeScript, based on TSRPC.
127 lines (126 loc) • 4.74 kB
JavaScript
var __extends = (this && this.__extends) || (function () {
var extendStatics = function (d, b) {
extendStatics = Object.setPrototypeOf ||
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
return extendStatics(d, b);
}
return function (d, b) {
extendStatics(d, b);
function __() { this.constructor = d; }
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
})();
import * as React from 'react';
import * as ReactDOM from 'react-dom';
import { DefaultFulltsAppConfig } from './FulltsAppConfig';
import TsrpcClient from 'tsrpc-browser';
import * as PropTypes from 'prop-types';
import { BrowserRouter } from 'react-router-dom';
import FulltsRouteSwitch from './FulltsRouteSwitch';
import 'k8w-extend-native';
var FulltsApp = /** @class */ (function () {
function FulltsApp(config) {
/**
* params from parse query string
*/
this.query = {};
this._apiRequests = {};
this.config = Object.merge({}, DefaultFulltsAppConfig, config);
this.rpcClient = new TsrpcClient({
serverUrl: this.config.serverUrl
});
}
FulltsApp.prototype.callApi = function (ptl, req) {
var _this = this;
var conn = this.rpcClient.callApi(ptl, req);
var sn = TsrpcClient.getLastReqSn();
this._apiRequests[sn] = conn;
//pop when complete
conn.always(function () {
delete _this._apiRequests[sn];
}).onCancel(function () {
delete _this._apiRequests[sn];
});
return conn;
};
FulltsApp.prototype.renderTo = function (domTarget) {
this._domTarget = domTarget;
ReactDOM.render(React.createElement(FulltsAppContainer, { app: this }), domTarget);
};
FulltsApp.prototype.dispose = function () {
this._domTarget && ReactDOM.unmountComponentAtNode(this._domTarget);
this._domTarget = undefined;
this.cancelAllApiRequest();
};
FulltsApp.prototype.cancelAllApiRequest = function () {
for (var sn in this._apiRequests) {
if (this._apiRequests.hasOwnProperty(sn)) {
this._apiRequests[sn].cancel();
}
}
};
/**
* 设置页面标题
* @param title
*/
FulltsApp.prototype.setTitle = function (title) {
var titleNode = document.head.querySelector('title');
if (!titleNode) {
titleNode = document.createElement('title');
document.head.appendChild(titleNode);
}
titleNode.innerText = title;
};
/**
* 设置SEO用到的Meta信息
* @param meta
*/
FulltsApp.prototype.setSeoMeta = function (meta) {
var keywordsNode = document.head.querySelector('meta[name=keywords]');
if (meta.keywords && meta.keywords.length) {
if (!keywordsNode) {
keywordsNode = document.createElement('meta');
keywordsNode.name = 'keywords';
document.head.appendChild(keywordsNode);
}
keywordsNode.content = meta.keywords.join();
}
else {
keywordsNode && keywordsNode.remove();
}
var descNode = document.head.querySelector('meta[name=description]');
if (meta.description) {
if (!descNode) {
descNode = document.createElement('meta');
descNode.name = 'description';
document.head.appendChild(descNode);
}
descNode.content = meta.description;
}
else {
descNode && descNode.remove();
}
};
return FulltsApp;
}());
export default FulltsApp;
var FulltsAppContainer = /** @class */ (function (_super) {
__extends(FulltsAppContainer, _super);
function FulltsAppContainer() {
return _super !== null && _super.apply(this, arguments) || this;
}
FulltsAppContainer.prototype.getChildContext = function () {
return {
fullTsApp: this.props.app
};
};
FulltsAppContainer.prototype.render = function () {
return (React.createElement(BrowserRouter, null,
React.createElement(FulltsRouteSwitch, { routes: this.props.app.config.routes, app: this.props.app })));
};
FulltsAppContainer.childContextTypes = {
fullTsApp: PropTypes.instanceOf(FulltsApp)
};
return FulltsAppContainer;
}(React.Component));