forked from kidgrow-microservices-platform

侯瑞军
2020-04-08 6325ff05bb15908b3dd975e958737c5dddf0e50d
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
/**
 * 路由组件
 * q.js-1.2 <https://github.com/itorr/q.js>
 */
var Q = function (W, D, HTML, hash, view, arg, _arg, i, index, Regex, key, Q) {
    HTML = D.documentElement;
    Regex = [];
    key = '!';
    onhashchange = function () {
        Q.hash = hash = location.hash.substring(key.length + 1);
 
        arg = hash.split(/\?/g).shift();
 
        arg = arg.split('/');
 
        i = Regex.length;
        while (i--)
            if (_arg = hash.match(Regex[i])) {
                arg = _arg;
                arg[0] = Regex[i];
                break;
            }
 
 
        if (!Q[arg[0]]) // default
            arg[0] = index;
 
        if (Q.pop)
            Q.pop.apply(W, arg);
 
        Q.lash = view = arg.shift();
 
        HTML.setAttribute('view', view);
 
        Q[view].apply(W, arg);
    };
 
    if (!'onhashchange' in W) {
        Q.path = location.hash;
        setInterval(function () {
            if (Q.path != location.hash) {
                onhashchange();
                Q.path = location.hash;
            }
        }, 100);
    }
 
    Q = {
        init: function (o) {
 
            if (o.key !== undefined)
                key = o.key;
 
            index = o.index || 'V';
 
            if (o.pop && typeof o.pop == 'function')
                Q.pop = o.pop;
 
            onhashchange();
 
            return this
        },
        reg: function (r, u) {
            if (!r)
                return;
 
            if (u == undefined)
                u = function () {
                };
 
            if (r instanceof RegExp) { //正则注册
                Q[r] = u;
                Regex.push(r);
            } else if (r instanceof Array) { //数组注册
                for (var i in r) {
                    this.reg.apply(this, [].concat(r[i]).concat(u));
                }
            } else if (typeof r == 'string') { //关键字注册
                if (typeof u == 'function')
                    Q[r] = u;
                else if (typeof u == 'string' && Q[u])
                    Q[r] = Q[u];
            }
 
            return this
        },
        V: function () {
            console.log('q.js <https://github.com/itorr/q.js> 2014/12/28');
            return this
        },
        go: function (u) {
            location.hash = '#' + key + u;
            return this
        },
        refresh: function () {
            onhashchange();
        }
    };
    return Q;
}(this, document);