1 | import VueAdapt from "./lib/adapt"
|
2 | import {bind,unbind,trigger} from "./lib/event"
|
3 | import Logger from "./lib/logger"
|
4 | import {greeting,extend} from "./lib/utils"
|
5 | import {runInterceptor,registerInterceptor} from "./lib/interceptor"
|
6 |
|
7 | let views = [],
|
8 | config={},
|
9 | VueRouter = VueAdapt.VueRouter;
|
10 |
|
11 | function registerView(view) {
|
12 | views.push(view);
|
13 | return this;
|
14 | }
|
15 |
|
16 | let Light = require("./lib/core");
|
17 | extend(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 |
|
32 |
|
33 |
|
34 |
|
35 |
|
36 |
|
37 |
|
38 | start(options,cb){
|
39 | const that = this;
|
40 | require("./platform").deviceReady(()=>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,(router)=>{
|
68 | that.router = router;
|
69 | require("./platform").start(router,options);
|
70 |
|
71 | trigger("start");
|
72 | if(cb && typeof cb==="function") cb.call(null,router);
|
73 | });
|
74 | })
|
75 | },
|
76 | navigate(view, params, options){
|
77 |
|
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 |
|
93 |
|
94 | navigateBack: function (number) {
|
95 |
|
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 |
|
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 |
|
133 | export default Light;
|
134 |
|
135 | bind("appStarted",function () {
|
136 |
|
137 | if(__ENV__.config&&__ENV__.config.greeting===false){
|
138 | return
|
139 | }
|
140 | greeting();
|
141 | }); |
\ | No newline at end of file |