UNPKG

4.27 kBJavaScriptView Raw
1import VueAdapt from "./lib/adapt"
2import {bind,unbind,trigger} from "./lib/event"
3import Logger from "./lib/logger"
4import {greeting,extend} from "./lib/utils"
5import {runInterceptor,registerInterceptor} from "./lib/interceptor"
6
7let views = [],
8 config={},
9 VueRouter = VueAdapt.VueRouter;
10
11function registerView(view) {
12 views.push(view);
13 return this;
14}
15
16let Light = require("./lib/core");
17extend(Light, {
18 route:registerView,
19 registerView,
20 filter:registerInterceptor,
21 runInterceptor,
22 registerInterceptor,
23 VueRouter:VueRouter,
24 Logger,
25 config(confi){
26 this.extend(config,confi);
27 return this;
28 },
29 /**
30 *
31 * @param options
32 * @param options.el 挂载的父节点
33 * @param options.routeMode 路由模式
34 * @param options.routeMode 路由模式
35 * @param options.viewport 设置运行页面的宽度(仅支持JSN)
36 * @param cb
37 */
38 start(options,cb){
39 const that = this;
40 that._startNow(...arguments)
41 },
42 _startNow(options,cb) {
43 const that = this;
44 switch(arguments.length){
45 case 0:
46 cb = function () {
47 };
48 options = {};
49 break;
50 case 1:
51 if(typeof options==="function"){
52 cb = options;
53 options = {};
54 }else{
55 cb = function () {
56 };
57 }
58 break;
59 case 2:
60 break;
61 }
62 this.runInterceptor("start",[],function () {
63 let viewCopy = [];
64 viewCopy=viewCopy.concat(views);
65 VueAdapt.initVueRouter({
66 mode:options.routeMode||'hash'
67 },viewCopy,(getRouter)=>{
68 that.router = getRouter();
69 that.getRouter = getRouter;
70 require("./platform").start(that.router,options);
71 trigger("appStarted");
72 if(cb && typeof cb==="function") cb.call(null,that.router);
73 });
74 })
75 },
76 navigate(view, params, options){
77 //navigate之前执行navigate拦截器,传递的值是当前视图,目标视图
78 const that = this;
79 let vCheck = views.filter(v=>v.path===`/${view}`)
80 if(vCheck.length===1) vCheck=vCheck[0];
81 else vCheck={}
82 this.runInterceptor("navigate",[vCheck, params, options],function () {
83 that._navigate.call(that,view, params, options)
84 })
85 },
86 _navigate(view, params, options) {
87 require("./platform").navigate(view, params, options);
88 },
89 /**
90 * 路由回退
91 *
92 * @param number 要返回的页面层数,默认值为1
93 */
94 navigateBack: function (number) {
95 //bug121649:number小于等于0时,不回退;与LightSDK.native.back()接口统一,增加提示。
96 if(number == undefined) {
97 number = 1;
98 }
99 if(!(/^[0-9]+$/.test(number))) {
100 if(process.env.NODE_ENV === "dev") console.warn('[Light.navigateBack]number必须为字符串或整数类型的数字!');
101 return;
102 }
103 if(number <= 0){
104 if(process.env.NODE_ENV === "dev") console.warn('[Light.navigateBack]number的值必须大于或等于1!');
105 return;
106 }
107 require("./platform").navigateBack(number);
108 },
109 inspect(){
110 console.table(this);
111 },
112 bind,unbind,trigger,on:bind,off:unbind,extend,
113 ajax(options){
114 return require("./platform").ajax(options);
115 },
116 log(info){
117 require("./platform").log(info);
118 if(process.env.NODE_ENV==="dev"){
119 //修复Uncaught TypeError: Illegal invocation(…)的问题
120 console.log(info);
121 }
122 },
123 install(Vue){
124 this.Model = Vue;
125 this.Vue = Vue;
126 Vue.use(VueRouter);
127 Vue.component("sub-view",Vue.component("router-view"))
128 Vue.prototype.$light = this;
129 require("./platform").init()
130 }
131});
132
133export default Light;
134
135bind("appStarted",function () {
136 //153077 增加关闭greeting的开关
137 if(__ENV__.config&&__ENV__.config.greeting===false){
138 return
139 }
140 greeting();
141});
\No newline at end of file