-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig.js
66 lines (54 loc) · 1.66 KB
/
config.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
if (!window.NexT) window.NexT = {};
(function() {
const className = 'next-config';
const staticConfig = {};
let variableConfig = {};
const parse = text => JSON.parse(text || '{}');
const update = name => {
const targetEle = document.querySelector(`.${className}[data-name="${name}"]`);
if (!targetEle) return;
const parsedConfig = parse(targetEle.text);
if (name === 'main') {
Object.assign(staticConfig, parsedConfig);
} else {
variableConfig[name] = parsedConfig;
}
};
update('main');
window.CONFIG = new Proxy({}, {
get(overrideConfig, name) {
let existing;
if (name in staticConfig) {
existing = staticConfig[name];
} else {
if (!(name in variableConfig)) update(name);
existing = variableConfig[name];
}
// For unset override and mixable existing
if (!(name in overrideConfig) && typeof existing === 'object') {
// Get ready to mix.
overrideConfig[name] = {};
}
if (name in overrideConfig) {
const override = overrideConfig[name];
// When mixable
if (typeof override === 'object' && typeof existing === 'object') {
// Mix, proxy changes to the override.
return new Proxy({ ...existing, ...override }, {
set(target, prop, value) {
target[prop] = value;
override[prop] = value;
return true;
}
});
}
return override;
}
// Only when not mixable and override hasn't been set.
return existing;
}
});
document.addEventListener('pjax:success', () => {
variableConfig = {};
});
})();