1 | |
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 | (function (window) {
|
9 |
|
10 | var zidian = {
|
11 | config: {
|
12 | host: location.origin,
|
13 | word: {
|
14 | rows: 30,
|
15 | source: "/dist/word/00.json"
|
16 | },
|
17 | ci: {
|
18 | rows: 600,
|
19 | source: "/dist/ci/00.json"
|
20 | },
|
21 | idiom: {
|
22 | rows: 150,
|
23 | source: "/dist/idiom/00.json"
|
24 | }
|
25 | },
|
26 | cache: {
|
27 | word: {},
|
28 | ci: {},
|
29 | idiom: {},
|
30 | promise: {}
|
31 | },
|
32 |
|
33 | taskKeys: function (type) {
|
34 | return zidian.cache.promise[type] = new Promise(function (resolve) {
|
35 | var keys = zidian.cache[type]["00"] || [];
|
36 | if (keys.length) {
|
37 | resolve(keys);
|
38 | } else {
|
39 | fetch(zidian.config.host + zidian.config[type].source).then(x => x.json()).then(function (res) {
|
40 | zidian.cache[type]["00"] = keys = res;
|
41 | resolve(keys)
|
42 | })
|
43 | }
|
44 | })
|
45 | },
|
46 |
|
47 | taskItems: function (type, key) {
|
48 | return new Promise(function (resolve) {
|
49 | (zidian.cache.promise[type] || zidian.taskKeys(type)).then(function (keys) {
|
50 | var ki = keys.indexOf(key);
|
51 | if (ki >= 0) {
|
52 | var pi = Math.ceil((ki + 1) / zidian.config[type].rows) - 1;
|
53 | var ii = ki - pi * zidian.config[type].rows;
|
54 | var pd = zidian.cache[type][pi] || [];
|
55 | if (pd.length) {
|
56 | resolve(pd[ii]);
|
57 | } else {
|
58 | fetch(zidian.config.host + zidian.config[type].source.replace("00", pi)).then(x => x.json()).then(function (res) {
|
59 | zidian.cache[type][pi] = pd = res;
|
60 | resolve(pd[ii]);
|
61 | })
|
62 | }
|
63 | } else {
|
64 | resolve();
|
65 | }
|
66 | })
|
67 | })
|
68 | },
|
69 |
|
70 | likeCi: function (key) {
|
71 | return new Promise(function (resolve) {
|
72 | var type = 'ci';
|
73 | (zidian.cache.promise[type] || zidian.taskKeys(type)).then(function (keys) {
|
74 | var res = [];
|
75 | keys.forEach(x => {
|
76 | x.indexOf(key) >= 0 && res.push(x);
|
77 | })
|
78 | resolve(res);
|
79 | })
|
80 | })
|
81 | },
|
82 |
|
83 | likeIdiom: function (key) {
|
84 | return new Promise(function (resolve) {
|
85 | var type = 'idiom';
|
86 | (zidian.cache.promise[type] || zidian.taskKeys(type)).then(function (keys) {
|
87 | var res = [];
|
88 | keys.forEach(x => {
|
89 | x.indexOf(key) >= 0 && res.push(x);
|
90 | })
|
91 | resolve(res);
|
92 | })
|
93 | })
|
94 | },
|
95 |
|
96 | equalWord: function (key) {
|
97 | return new Promise(function (resolve) {
|
98 | zidian.taskItems('word', key).then(resolve)
|
99 | })
|
100 | },
|
101 |
|
102 | equalCi: function (key) {
|
103 | return new Promise(function (resolve) {
|
104 | zidian.taskItems('ci', key).then(resolve)
|
105 | })
|
106 | },
|
107 |
|
108 | equalIdiom: function (key) {
|
109 | return new Promise(function (resolve) {
|
110 | zidian.taskItems('idiom', key).then(resolve)
|
111 | })
|
112 | }
|
113 | }
|
114 |
|
115 | window.zidian = zidian;
|
116 |
|
117 | })(window); |
\ | No newline at end of file |