forked from vnotex/viki
-
Notifications
You must be signed in to change notification settings - Fork 0
/
viki.js
136 lines (108 loc) · 3.6 KB
/
viki.js
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
import logger from "./logger.js";
import VikiInfo from "./vikiinfo.js";
import { Config, ConfigWorker } from "./configworker.js";
import PageWorker from "./pageworker.js";
import { NaviItem, NaviWorker } from "./naviworker.js";
import FetchTargetWorker from "./fetchtargetworker.js";
import ContentWorker from "./contentworker.js";
import FooterWorker from "./footerworker.js";
class Viki {
constructor() {
this.workers = [];
this.curWorkerIdx = -1;
this.config = new Config();
this.naviItems = [];
this.info = new VikiInfo();
}
init() {
window.viki_silent_hash = false;
let registerWorker = (p_worker) => {
p_worker.register(this);
this.workers.push(p_worker);
};
let configWorker = new ConfigWorker();
registerWorker(configWorker);
let pageWorker = new PageWorker();
registerWorker(pageWorker);
let naviWorker = new NaviWorker();
registerWorker(naviWorker);
let fetchTargetWorker = new FetchTargetWorker();
registerWorker(fetchTargetWorker);
let contentWorker = new ContentWorker();
registerWorker(contentWorker);
let footerWorker = new FooterWorker();
registerWorker(footerWorker);
$(document).ready(() => {
if (!this.initTargetFromHash()) {
return;
}
$(window).bind('hashchange', function() {
if (window.viki_silent_hash) {
window.viki_silent_hash = false;
return;
}
window.location.reload(false);
});
logger.log("target", this.info.target,
"anchor", this.info.anchor);
this.curWorkerIdx = -1;
this.scheduleNext();
});
}
scheduleNext() {
if (this.curWorkerIdx >= this.workers.length - 1) {
logger.log("all workers finished");
this.curWorkerIdx = -1;
} else {
++this.curWorkerIdx;
logger.log("schedule worker", this.curWorkerIdx);
this.workers[this.curWorkerIdx].run();
}
}
initTargetFromHash() {
let isValidHash = function(hash) {
var a = document.createElement('a');
a.href = hash;
return window.location.hostname === a.hostname;
};
let target = "index.md";
let hash = window.location.hash || "";
// Default hash completion.
if (hash === '') {
// Empty.
this.info.setTarget(target);
return true;
}
let newHash = '';
if (hash === "#" ||
hash === "#!") {
newHash = "#!" + target;
} else if (hash.startsWith("#!") && hash.endsWith('/')) {
newHash = hash + target;
}
if (newHash) {
window.location.hash = newHash;
window.location.reload(false);
return false;
}
if (hash.startsWith("#!")) {
target = hash.substring(2);
} else if (hash.startsWith("#")) {
target = hash.substring(1);
}
// Validate if it could be located.
if (!isValidHash(target)) {
target = "index.md";
}
target = decodeURIComponent(target);
// Anchor.
let idx = target.indexOf('#');
if (idx != -1) {
this.info.setTarget(target.substring(0, idx), target.substring(idx + 1));
} else {
this.info.setTarget(target);
}
return true;
}
}
export default Viki;