UNPKG

tsp-component

Version:

提供多端和react版本的UI组件

103 lines (102 loc) 3.38 kB
import Container from './container'; import ajax from '../ajax'; import WebApi from '../ajax/webapi'; import { hexToString } from '../util/tools'; import Framework from './index'; var preLoadingIndex = 0; function preLoading(i, files, outputDir) { if (preLoadingIndex < files.length) { ajax({ url: outputDir + "/" + files[i] + ".js", type: 'get', dataType: 'text', data: {}, success: function () { preLoadingIndex++; return preLoading(preLoadingIndex, files, outputDir); } }); } return; } export function init(options) { var childRoutes = []; var preLoadingFiles = []; var length = options && options.childRoutes ? options.childRoutes.length : 0; var env = process.env.NODE_ENV; for (var i = 0; i < length; i++) { if (env === 'production') { preLoadingFiles.push(options.childRoutes[i].outputFileName); } childRoutes.push({ path: options.childRoutes[i].path, onEnter: options.childRoutes[i].onEnter, getComponent: options.childRoutes[i].getComponent }); } Framework.history.listen(function (listener) { var path = listener.pathname; Framework.global.routerPath = path; Framework.global.urlAction = listener.action; if (options && options.outputDir && process.env.NODE_ENV === 'production') { preLoading(preLoadingIndex, preLoadingFiles, options.outputDir); } window.scrollTo(0, 0); if (listener.action === 'PUSH' || listener.action === 'REPLACE') { Framework.global.routerHistory.push(path); } else { Framework.global.routerHistory.pop(); WebApi.abort(); } if (process.env.NODE_ENV === 'production' && Framework.wx) { Framework.wx.wxConfig(); wx.ready(function () { if (Framework.wx.ready) { Framework.wx.ready(); } Framework.wx.getNetworkType(); }); } }); if (options) { return { component: options.component || Container, onEnter: options.onEnter || forwardURL, childRoutes: childRoutes }; } return null; } export function forwardURL(nextState, replace) { var path = nextState.location.pathname; Framework.global.routerHistory.push(path); if (window['forward'] !== '') { replace(hexToString(window['forward'])); window['forward'] = ''; return; } } export function goBack() { var length = Framework.global.routerHistory.length; if (length <= 1) { Framework.history.push('/'); } else { Framework.history.goBack(); } } export function authentication(nextState, replace) { if (!Framework.global.isLogin && Framework.global.urlAction === 'POP') { var length_1 = Framework.global.routerHistory.length; if (length_1 <= 1) { replace('/'); } else { replace(Framework.global.routerHistory[length_1 - 2]); } } else if (!Framework.global.isLogin) { replace('/login'); } }