cyra
Version:
single page application view engine
86 lines (85 loc) • 3.02 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
var Router_1 = require("./Router");
var Page_1 = require("./Page");
var config_1 = require("../config");
var Cyra = (function () {
function Cyra() {
}
/**
* 初始化 Cyra
* @param {InitAppObject} obj
*/
Cyra.initApp = function (obj) {
var root = obj.root || config_1.CyraConfig.DEFAULT_ROOT;
var mode = obj.mode;
if (!mode) {
var checkHistoryApi = !!(window.history && window.history.pushState);
mode = checkHistoryApi ? 'history' : 'multipage';
}
Cyra.router = new Router_1.default(root, obj.index, mode);
if (obj.dataSplit) {
config_1.CyraConfig.URL_DATA_SPLIT = obj.dataSplit;
}
if (obj.alwaysReload) {
config_1.CyraConfig.ALWAYS_RELOAD = true;
}
if (obj.appRoot) {
config_1.CyraConfig.APP_ROOT = obj.appRoot;
// 修正 URL 前缀,使其最后以 '/' 结尾
if (config_1.CyraConfig.APP_ROOT[config_1.CyraConfig.APP_ROOT.length - 1] !== '/') {
config_1.CyraConfig.APP_ROOT += '/';
}
}
if (obj.paramPrefix) {
config_1.CyraConfig.PARAM_PREFIX = obj.paramPrefix;
}
if (obj.copyIndex) {
config_1.CyraConfig.COPYINDEX = obj.copyIndex;
}
if (obj.animation) {
config_1.CyraConfig.ANIMATION = obj.animation;
var ua = navigator.userAgent, versionObj = ua.match(/\sOS\s(\d+)_\d/i), version = versionObj && Number(versionObj[1]);
if (!/ios|iphone|ipod|ipad/i.test(ua)) {
// let version: string[] = ua.match(/Android (\d\.\d)/i);
// if (!version || Number(version[1]) < 6.0) {
// CyraConfig.ANIMATION = false;
// }
config_1.CyraConfig.ANIMATION = false;
}
else if (version < 9) {
config_1.CyraConfig.ANIMATION = false;
}
}
if (obj.switchDuration) {
config_1.CyraConfig.SWITCH_DURATION = obj.switchDuration;
}
if (obj.animationPrefix) {
config_1.CyraConfig.ANIMATION_PREFIX = obj.animationPrefix;
}
if (obj.animationFunc) {
config_1.CyraConfig.ANIMATION_FUNC = obj.animationFunc;
}
Cyra.router.defineRoute(obj.routes);
Cyra.router.startRouting();
};
/**
* Page 扩展工具方法
* @param {Object} obj
* @return {Object}
*/
Cyra.extendBasePage = function (obj) {
function UserPage() {
Page_1.Page.apply(this, arguments);
}
var proto = Object.create(Page_1.Page.prototype);
for (var key in obj) {
proto[key] = obj[key];
}
UserPage.prototype = proto;
UserPage.prototype.constructor = UserPage;
return UserPage;
};
return Cyra;
}());
exports.Cyra = Cyra;