-
Notifications
You must be signed in to change notification settings - Fork 4
/
scripts.js
191 lines (172 loc) · 5.45 KB
/
scripts.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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
const config = {
themes: {
tutorial: {
class: 'tutorial-theme',
location: '/themes/tutorial/',
styles: 'styles.css',
},
styleguide: {
class: 'styleguide-theme',
location: '/themes/styleguide/',
styles: 'styles.css',
},
},
blocks: {
'.marquee': {
location: '/blocks/marquee/',
styles: 'styles.css',
scripts: 'scripts.js',
},
'a[href^="https://gist.github.com"]': {
location: '/blocks/embed/',
scripts: 'gist.js',
}
},
};
const getMetadata = (name) => {
const meta = document.head.querySelector(`meta[name="${name}"]`);
return meta && meta.content;
};
const addStyle = (location, loadEvent) => {
const element = document.createElement('link');
element.setAttribute('rel', 'stylesheet');
element.setAttribute('href', location);
if (loadEvent) {
element.addEventListener('load', loadEvent);
}
document.querySelector('head').appendChild(element);
};
const loadTheme = (config) => {
const theme = getMetadata('theme');
const isLoaded = () => {
document.body.classList.add('is-Loaded');
};
if (theme) {
const themeConf = config.themes[theme] || {};
if (themeConf.class) {
document.body.classList.add(themeConf.class);
}
if (themeConf.styles) {
addStyle(`${themeConf.location}${themeConf.styles}`, isLoaded);
} else {
isLoaded();
}
} else {
isLoaded();
}
};
const loadBlocks = (config, suppliedEl) => {
const parentEl = suppliedEl || document;
const initJs = async (element, block) => {
// If the block scripts haven't been loaded, load them.
if (block.scripts) {
if (!block.module) {
// eslint-disable-next-line no-param-reassign
block.module = await import(`${block.location}${block.scripts}`);
}
// If this block type has scripts and they're already imported
if (block.module) {
block.module.default(element, { addStyle });
}
}
element.classList.add('is-Loaded');
return true;
};
/**
* Unlazy each type of block
* @param {HTMLElement} element
*/
const loadElement = async (element) => {
const { blockSelect } = element.dataset;
const block = config.blocks[blockSelect];
if (!block.loaded && block.styles) {
addStyle(`${block.location}${block.styles}`);
}
block.loaded = initJs(element, block);
};
/**
* Iterate through all entries to determine if they are intersecting.
* @param {IntersectionObserverEntry} entries
* @param {IntersectionObserver} observer
*/
const onIntersection = (entries, observer) => {
entries.forEach((entry) => {
if (entry.isIntersecting) {
observer.unobserve(entry.target);
loadElement(entry.target);
}
});
};
/**
* Clean up variant classes
* Ex: marquee--small--contained- -> marquee small contained
* @param {HTMLElement} parent
*/
const cleanVariations = (parent) => {
const variantBlocks = parent.querySelectorAll('[class$="-"]');
variantBlocks.forEach((variant) => {
let { className } = variant;
className = className.slice(0, -1);
variant.classList.remove(className);
const classNames = className.split('--');
variant.classList.add(...classNames);
});
};
/**
* Load blocks
* @param {HTMLElement} element
*/
const init = (element) => {
const isDoc = element instanceof HTMLDocument;
const parent = isDoc ? document.querySelector('body') : element;
const lazyLoad = isDoc && config.lazy;
cleanVariations(parent);
let observer;
if (lazyLoad) {
const options = { rootMargin: config.margin || '1000px 0px' };
observer = new IntersectionObserver(onIntersection, options);
}
Object.keys(config.blocks).forEach((selector) => {
const elements = parent.querySelectorAll(selector);
elements.forEach((el) => {
el.setAttribute('data-block-select', selector);
if (lazyLoad) {
observer.observe(el);
} else {
loadElement(el);
}
});
});
};
const fetchFragment = async (path) => {
const resp = await fetch(`${path}.plain.html`);
if (resp.ok) {
return resp.text();
}
return null;
};
const loadFragment = async (fragmentEl) => {
const path = fragmentEl.querySelector('div > div').textContent;
const html = await fetchFragment(path);
if (html) {
fragmentEl.insertAdjacentHTML('beforeend', html);
fragmentEl.querySelector('div').remove();
fragmentEl.classList.add('is-Visible');
init(fragmentEl);
}
};
/**
* Add fragment to the list of blocks
*/
// eslint-disable-next-line no-param-reassign
config.blocks['.fragment'] = {
loaded: true,
scripts: {},
module: {
default: loadFragment,
},
};
init(parentEl);
};
loadTheme(config);
loadBlocks(config);