Skip to content

Commit e18d011

Browse files
author
Ludwig Schubert
committed
Move controller registration to components; out of article tag as we now can have e.g. citations outside of article in title
1 parent 70d8507 commit e18d011

File tree

5 files changed

+28
-29
lines changed

5 files changed

+28
-29
lines changed

examples/article.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@
4242
</d-front-matter>
4343
<d-title>
4444
<figure style="grid-column: page; margin: 1rem 0;"><img src="momentum.png" style="width:100%; border: 1px solid rgba(0, 0, 0, 0.2);"/></figure>
45-
<p>We often think of Momentum as a means of dampening oscillations and speeding up the iterations, leading to faster convergence. But it has other interesting behavior. It allows a larger range of step-sizes to be used, and creates its own oscillations. What is going on?</p>
45+
<p>We often think of Momentum<d-cite key="mercier2011humans"></d-cite> as a means of dampening oscillations and speeding up the iterations, leading to faster convergence. But it has other interesting behavior. It allows a larger range of step-sizes to be used, and creates its own oscillations. What is going on?</p>
4646
</d-title>
4747
<d-article>
4848
<a class="marker" href="#section-1" id="section-1"><span>1</span></a>

src/components.js

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { Controller } from './controller';
22

33
/* Transforms */
44
import { makeStyleTag } from './styles/styles';
5-
import { Polyfills, polyfills } from './helpers/polyfills';
5+
import { Polyfills } from './helpers/polyfills';
66

77
/* Components */
88
import { Abstract } from './components/d-abstract';
@@ -48,7 +48,7 @@ const distillMain = function() {
4848
makeStyleTag(document);
4949
console.info('Runlevel 1: Static Distill styles have been added.');
5050
console.info('Runlevel 1->2.');
51-
window.distillRunlevel = 2;
51+
window.distillRunlevel += 1;
5252

5353
/* 3. Register components */
5454
/* Article will register controller which takes control from there */
@@ -73,18 +73,29 @@ const distillMain = function() {
7373

7474
console.info('Runlevel 2: Distill Template finished registering custom elements.');
7575
console.info('Runlevel 2->3.');
76-
window.distillRunlevel = 3;
77-
console.info('Distill Template initialisation complete.');
78-
Controller.listeners.DOMContentLoaded();
76+
window.distillRunlevel += 1;
7977

78+
/* 4. Register Controller listener functions */
79+
for (const [functionName, callback] of Object.entries(Controller.listeners)) {
80+
if (typeof callback === 'function') {
81+
document.addEventListener(functionName, callback);
82+
} else {
83+
console.error('Runlevel 3: Controller listeners need to be functions!');
84+
}
85+
}
86+
console.info('Runlevel 3: We can now listen to controller events.');
87+
console.info('Runlevel 3->4.');
88+
window.distillRunlevel += 1;
89+
90+
console.info('Runlevel 4: Distill Template initialisation complete.');
8091
};
8192

8293
window.distillRunlevel = 0;
8394
/* 0. Check browser feature support; synchronously polyfill if needed */
8495
if (Polyfills.browserSupportsAllFeatures()) {
8596
console.info('Runlevel 0: No need for polyfills.');
8697
console.info('Runlevel 0->1.');
87-
window.distillRunlevel = 1;
98+
window.distillRunlevel += 1;
8899
distillMain();
89100
} else {
90101
console.info('Runlevel 0: Distill Template is loading polyfills.');

src/components/d-article.js

Lines changed: 1 addition & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// import { Template } from '../mixins/template';
2-
import { Controller } from '../controller';
2+
// import { Controller } from '../controller';
33

44
const isOnlyWhitespace = /^\s*$/;
55

@@ -29,20 +29,4 @@ export class Article extends HTMLElement {
2929
}).observe(this, {childList: true});
3030
}
3131

32-
connectedCallback() {
33-
document.onreadystatechange = function () {
34-
console.log('onreadystatechange:');
35-
console.log(document.readyState);
36-
};
37-
console.info('Article tag connected, we can now listen to controller events.');
38-
console.info('Runlevel 3->4.');
39-
for (const [functionName, callback] of Object.entries(Controller.listeners)) {
40-
if (typeof callback === 'function') {
41-
document.addEventListener(functionName, callback);
42-
} else {
43-
console.error('Controller listeners need to be functions!');
44-
}
45-
}
46-
}
47-
4832
}

src/components/d-hover-box.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ export class HoverBox extends T(HTMLElement) {
5252
}
5353

5454
listen(element) {
55-
console.log(element)
55+
// console.log(element)
5656
this.bindDivEvents(this);
5757
this.bindTriggerEvents(element);
5858
// this.style.display = "block";

src/controller.js

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@ import optionalComponents from './transforms/optional-components';
66

77
const frontMatter = new FrontMatter();
88

9+
function domContentLoaded() {
10+
return ['interactive', 'complete'].indexOf(document.readyState) !== -1;
11+
}
12+
913
export const Controller = {
1014

1115
frontMatter: frontMatter,
@@ -120,7 +124,7 @@ export const Controller = {
120124
}
121125

122126
const prerendered = document.body.hasAttribute('distill-prerendered');
123-
if (!prerendered) {
127+
if (!prerendered && domContentLoaded()) {
124128
optionalComponents(document, frontMatter);
125129

126130
const appendix = document.querySelector('distill-appendix');
@@ -141,18 +145,18 @@ export const Controller = {
141145
},
142146

143147
DOMContentLoaded() {
144-
if (Controller.loaded || ['interactive', 'complete'].indexOf(document.readyState) === -1) {
148+
if (Controller.loaded || !domContentLoaded()) {
145149
return;
146150
} else {
147151
Controller.loaded = true;
148-
console.log('Controller running DOMContentLoaded');
152+
console.log('Runlevel 4: Controller running DOMContentLoaded');
149153
}
150154

151155
const frontMatterTag = document.querySelector('d-front-matter');
152156
const data = parseFrontmatter(frontMatterTag);
153157
Controller.listeners.onFrontMatterChanged({detail: data});
154158

155-
// console.debug('Resolving "citations" dependency due to initial DOM load.');
159+
// Resolving "citations" dependency due to initial DOM load
156160
frontMatter.citations = collect_citations();
157161
frontMatter.citationsCollected = true;
158162
for (const waitingCallback of Controller.waitingOn.citations.slice()) {

0 commit comments

Comments
 (0)