Skip to content

Commit 4b18851

Browse files
committed
Externalise js bundles
1 parent a4b684d commit 4b18851

File tree

4 files changed

+354
-11
lines changed

4 files changed

+354
-11
lines changed

docs/_includes/footer.html

+9-8
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,13 @@
44
href="http://creativecommons.org/licenses/by/4.0/">Creative Commons Attribution 4.0 International License</a>.
55
<p><em>Last updated: {{ site.time | date: "%c" }}</em>
66
</span>
7-
<script src="js/ga.js"></script>
8-
<script src="//cdnjs.cloudflare.com/ajax/libs/anchor-js/3.2.2/anchor.min.js"></script>
9-
<script>
10-
anchors.options = {
11-
placement: 'left'
12-
};
13-
anchors.add('h2, h3');
14-
</script>
157
</footer>
8+
9+
<script src="js/ga.js"></script>
10+
<script src="js/anchor.js"></script>
11+
<script>
12+
anchors.options = {
13+
placement: 'left'
14+
};
15+
anchors.add('h2, h3');
16+
</script>

docs/js/anchor.js

+338
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,338 @@
1+
/**
2+
* AnchorJS - v3.2.2 - 2016-10-05
3+
* https://github.com/bryanbraun/anchorjs
4+
* Copyright (c) 2016 Bryan Braun; Licensed MIT
5+
*/
6+
7+
/* eslint-env amd, node */
8+
9+
// https://github.com/umdjs/umd/blob/master/templates/returnExports.js
10+
(function (root, factory) {
11+
'use strict';
12+
13+
if (typeof define === 'function' && define.amd) {
14+
// AMD. Register as an anonymous module.
15+
define([], factory);
16+
} else if (typeof module === 'object' && module.exports) {
17+
// Node. Does not work with strict CommonJS, but
18+
// only CommonJS-like environments that support module.exports,
19+
// like Node.
20+
module.exports = factory();
21+
} else {
22+
// Browser globals (root is window)
23+
root.AnchorJS = factory();
24+
root.anchors = new root.AnchorJS();
25+
}
26+
}(this, function () {
27+
'use strict';
28+
29+
function AnchorJS(options) {
30+
this.options = options || {};
31+
this.elements = [];
32+
33+
/**
34+
* Assigns options to the internal options object, and provides defaults.
35+
* @param {Object} opts - Options object
36+
*/
37+
function _applyRemainingDefaultOptions(opts) {
38+
opts.icon = opts.hasOwnProperty('icon') ? opts.icon : '\ue9cb'; // Accepts characters (and also URLs?), like '#', '¶', '❡', or '§'.
39+
opts.visible = opts.hasOwnProperty('visible') ? opts.visible : 'hover'; // Also accepts 'always' & 'touch'
40+
opts.placement = opts.hasOwnProperty('placement') ? opts.placement : 'right'; // Also accepts 'left'
41+
opts.class = opts.hasOwnProperty('class') ? opts.class : ''; // Accepts any class name.
42+
// Using Math.floor here will ensure the value is Number-cast and an integer.
43+
opts.truncate = opts.hasOwnProperty('truncate') ? Math.floor(opts.truncate) : 64; // Accepts any value that can be typecast to a number.
44+
}
45+
46+
_applyRemainingDefaultOptions(this.options);
47+
48+
/**
49+
* Checks to see if this device supports touch. Uses criteria pulled from Modernizr:
50+
* https://github.com/Modernizr/Modernizr/blob/da22eb27631fc4957f67607fe6042e85c0a84656/feature-detects/touchevents.js#L40
51+
* @return {Boolean} - true if the current device supports touch.
52+
*/
53+
this.isTouchDevice = function() {
54+
return !!(('ontouchstart' in window) || window.DocumentTouch && document instanceof DocumentTouch);
55+
};
56+
57+
/**
58+
* Add anchor links to page elements.
59+
* @param {String|Array|Nodelist} selector - A CSS selector for targeting the elements you wish to add anchor links
60+
* to. Also accepts an array or nodeList containing the relavant elements.
61+
* @return {this} - The AnchorJS object
62+
*/
63+
this.add = function(selector) {
64+
var elements,
65+
elsWithIds,
66+
idList,
67+
elementID,
68+
i,
69+
index,
70+
count,
71+
tidyText,
72+
newTidyText,
73+
readableID,
74+
anchor,
75+
visibleOptionToUse,
76+
indexesToDrop = [];
77+
78+
// We reapply options here because somebody may have overwritten the default options object when setting options.
79+
// For example, this overwrites all options but visible:
80+
//
81+
// anchors.options = { visible: 'always'; }
82+
_applyRemainingDefaultOptions(this.options);
83+
84+
visibleOptionToUse = this.options.visible;
85+
if (visibleOptionToUse === 'touch') {
86+
visibleOptionToUse = this.isTouchDevice() ? 'always' : 'hover';
87+
}
88+
89+
// Provide a sensible default selector, if none is given.
90+
if (!selector) {
91+
selector = 'h1, h2, h3, h4, h5, h6';
92+
}
93+
94+
elements = _getElements(selector);
95+
96+
if (elements.length === 0) {
97+
return false;
98+
}
99+
100+
_addBaselineStyles();
101+
102+
// We produce a list of existing IDs so we don't generate a duplicate.
103+
elsWithIds = document.querySelectorAll('[id]');
104+
idList = [].map.call(elsWithIds, function assign(el) {
105+
return el.id;
106+
});
107+
108+
for (i = 0; i < elements.length; i++) {
109+
if (this.hasAnchorJSLink(elements[i])) {
110+
indexesToDrop.push(i);
111+
continue;
112+
}
113+
114+
if (elements[i].hasAttribute('id')) {
115+
elementID = elements[i].getAttribute('id');
116+
} else {
117+
tidyText = this.urlify(elements[i].textContent);
118+
119+
// Compare our generated ID to existing IDs (and increment it if needed)
120+
// before we add it to the page.
121+
newTidyText = tidyText;
122+
count = 0;
123+
do {
124+
if (index !== undefined) {
125+
newTidyText = tidyText + '-' + count;
126+
}
127+
128+
index = idList.indexOf(newTidyText);
129+
count += 1;
130+
} while (index !== -1);
131+
index = undefined;
132+
idList.push(newTidyText);
133+
134+
elements[i].setAttribute('id', newTidyText);
135+
elementID = newTidyText;
136+
}
137+
138+
readableID = elementID.replace(/-/g, ' ');
139+
140+
// The following code builds the following DOM structure in a more effiecient (albeit opaque) way.
141+
// '<a class="anchorjs-link ' + this.options.class + '" href="#' + elementID + '" aria-label="Anchor link for: ' + readableID + '" data-anchorjs-icon="' + this.options.icon + '"></a>';
142+
anchor = document.createElement('a');
143+
anchor.className = 'anchorjs-link ' + this.options.class;
144+
anchor.href = '#' + elementID;
145+
anchor.setAttribute('aria-label', 'Anchor link for: ' + readableID);
146+
anchor.setAttribute('data-anchorjs-icon', this.options.icon);
147+
148+
if (visibleOptionToUse === 'always') {
149+
anchor.style.opacity = '1';
150+
}
151+
152+
if (this.options.icon === '\ue9cb') {
153+
anchor.style.font = '1em/1 anchorjs-icons';
154+
155+
// We set lineHeight = 1 here because the `anchorjs-icons` font family could otherwise affect the
156+
// height of the heading. This isn't the case for icons with `placement: left`, so we restore
157+
// line-height: inherit in that case, ensuring they remain positioned correctly. For more info,
158+
// see https://github.com/bryanbraun/anchorjs/issues/39.
159+
if (this.options.placement === 'left') {
160+
anchor.style.lineHeight = 'inherit';
161+
}
162+
}
163+
164+
if (this.options.placement === 'left') {
165+
anchor.style.position = 'absolute';
166+
anchor.style.marginLeft = '-1em';
167+
anchor.style.paddingRight = '0.5em';
168+
elements[i].insertBefore(anchor, elements[i].firstChild);
169+
} else { // if the option provided is `right` (or anything else).
170+
anchor.style.paddingLeft = '0.375em';
171+
elements[i].appendChild(anchor);
172+
}
173+
}
174+
175+
for (i = 0; i < indexesToDrop.length; i++) {
176+
elements.splice(indexesToDrop[i] - i, 1);
177+
}
178+
this.elements = this.elements.concat(elements);
179+
180+
return this;
181+
};
182+
183+
/**
184+
* Removes all anchorjs-links from elements targed by the selector.
185+
* @param {String|Array|Nodelist} selector - A CSS selector string targeting elements with anchor links,
186+
* OR a nodeList / array containing the DOM elements.
187+
* @return {this} - The AnchorJS object
188+
*/
189+
this.remove = function(selector) {
190+
var index,
191+
domAnchor,
192+
elements = _getElements(selector);
193+
194+
for (var i = 0; i < elements.length; i++) {
195+
domAnchor = elements[i].querySelector('.anchorjs-link');
196+
if (domAnchor) {
197+
// Drop the element from our main list, if it's in there.
198+
index = this.elements.indexOf(elements[i]);
199+
if (index !== -1) {
200+
this.elements.splice(index, 1);
201+
}
202+
// Remove the anchor from the DOM.
203+
elements[i].removeChild(domAnchor);
204+
}
205+
}
206+
return this;
207+
};
208+
209+
/**
210+
* Removes all anchorjs links. Mostly used for tests.
211+
*/
212+
this.removeAll = function() {
213+
this.remove(this.elements);
214+
};
215+
216+
/**
217+
* Urlify - Refine text so it makes a good ID.
218+
*
219+
* To do this, we remove apostrophes, replace nonsafe characters with hyphens,
220+
* remove extra hyphens, truncate, trim hyphens, and make lowercase.
221+
*
222+
* @param {String} text - Any text. Usually pulled from the webpage element we are linking to.
223+
* @return {String} - hyphen-delimited text for use in IDs and URLs.
224+
*/
225+
this.urlify = function(text) {
226+
// Regex for finding the nonsafe URL characters (many need escaping): & +$,:;=?@"#{}|^~[`%!']./()*\
227+
var nonsafeChars = /[& +$,:;=?@"#{}|^~[`%!'\]\.\/\(\)\*\\]/g,
228+
urlText;
229+
230+
// The reason we include this _applyRemainingDefaultOptions is so urlify can be called independently,
231+
// even after setting options. This can be useful for tests or other applications.
232+
if (!this.options.truncate) {
233+
_applyRemainingDefaultOptions(this.options);
234+
}
235+
236+
// Note: we trim hyphens after truncating because truncating can cause dangling hyphens.
237+
// Example string: // " ⚡⚡ Don't forget: URL fragments should be i18n-friendly, hyphenated, short, and clean."
238+
urlText = text.trim() // "⚡⚡ Don't forget: URL fragments should be i18n-friendly, hyphenated, short, and clean."
239+
.replace(/\'/gi, '') // "⚡⚡ Dont forget: URL fragments should be i18n-friendly, hyphenated, short, and clean."
240+
.replace(nonsafeChars, '-') // "⚡⚡-Dont-forget--URL-fragments-should-be-i18n-friendly--hyphenated--short--and-clean-"
241+
.replace(/-{2,}/g, '-') // "⚡⚡-Dont-forget-URL-fragments-should-be-i18n-friendly-hyphenated-short-and-clean-"
242+
.substring(0, this.options.truncate) // "⚡⚡-Dont-forget-URL-fragments-should-be-i18n-friendly-hyphenated-"
243+
.replace(/^-+|-+$/gm, '') // "⚡⚡-Dont-forget-URL-fragments-should-be-i18n-friendly-hyphenated"
244+
.toLowerCase(); // "⚡⚡-dont-forget-url-fragments-should-be-i18n-friendly-hyphenated"
245+
246+
return urlText;
247+
};
248+
249+
/**
250+
* Determines if this element already has an AnchorJS link on it.
251+
* Uses this technique: http://stackoverflow.com/a/5898748/1154642
252+
* @param {HTMLElemnt} el - a DOM node
253+
* @return {Boolean} true/false
254+
*/
255+
this.hasAnchorJSLink = function(el) {
256+
var hasLeftAnchor = el.firstChild && ((' ' + el.firstChild.className + ' ').indexOf(' anchorjs-link ') > -1),
257+
hasRightAnchor = el.lastChild && ((' ' + el.lastChild.className + ' ').indexOf(' anchorjs-link ') > -1);
258+
259+
return hasLeftAnchor || hasRightAnchor || false;
260+
};
261+
262+
/**
263+
* Turns a selector, nodeList, or array of elements into an array of elements (so we can use array methods).
264+
* It also throws errors on any other inputs. Used to handle inputs to .add and .remove.
265+
* @param {String|Array|Nodelist} input - A CSS selector string targeting elements with anchor links,
266+
* OR a nodeList / array containing the DOM elements.
267+
* @return {Array} - An array containing the elements we want.
268+
*/
269+
function _getElements(input) {
270+
var elements;
271+
if (typeof input === 'string' || input instanceof String) {
272+
// See https://davidwalsh.name/nodelist-array for the technique transforming nodeList -> Array.
273+
elements = [].slice.call(document.querySelectorAll(input));
274+
// I checked the 'input instanceof NodeList' test in IE9 and modern browsers and it worked for me.
275+
} else if (Array.isArray(input) || input instanceof NodeList) {
276+
elements = [].slice.call(input);
277+
} else {
278+
throw new Error('The selector provided to AnchorJS was invalid.');
279+
}
280+
return elements;
281+
}
282+
283+
/**
284+
* _addBaselineStyles
285+
* Adds baseline styles to the page, used by all AnchorJS links irregardless of configuration.
286+
*/
287+
function _addBaselineStyles() {
288+
// We don't want to add global baseline styles if they've been added before.
289+
if (document.head.querySelector('style.anchorjs') !== null) {
290+
return;
291+
}
292+
293+
var style = document.createElement('style'),
294+
linkRule =
295+
' .anchorjs-link {' +
296+
' opacity: 0;' +
297+
' text-decoration: none;' +
298+
' -webkit-font-smoothing: antialiased;' +
299+
' -moz-osx-font-smoothing: grayscale;' +
300+
' }',
301+
hoverRule =
302+
' *:hover > .anchorjs-link,' +
303+
' .anchorjs-link:focus {' +
304+
' opacity: 1;' +
305+
' }',
306+
anchorjsLinkFontFace =
307+
' @font-face {' +
308+
' font-family: "anchorjs-icons";' + // Icon from icomoon; 10px wide & 10px tall; 2 empty below & 4 above
309+
' src: url(data:n/a;base64,AAEAAAALAIAAAwAwT1MvMg8yG2cAAAE4AAAAYGNtYXDp3gC3AAABpAAAAExnYXNwAAAAEAAAA9wAAAAIZ2x5ZlQCcfwA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AQAEAAMAAMAAQQJAAUAAgAiAAMAAQQJAAYAEAAMYW5jaG9yanM0MDBAAGEAbgBjAGgAbwByAGoAcwA0ADAAMABAAAAAAwAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAABAAH//wAP) format("truetype");' +
310+
' }',
311+
pseudoElContent =
312+
' [data-anchorjs-icon]::after {' +
313+
' content: attr(data-anchorjs-icon);' +
314+
' }',
315+
firstStyleEl;
316+
317+
style.className = 'anchorjs';
318+
style.appendChild(document.createTextNode('')); // Necessary for Webkit.
319+
320+
// We place it in the head with the other style tags, if possible, so as to
321+
// not look out of place. We insert before the others so these styles can be
322+
// overridden if necessary.
323+
firstStyleEl = document.head.querySelector('[rel="stylesheet"], style');
324+
if (firstStyleEl === undefined) {
325+
document.head.appendChild(style);
326+
} else {
327+
document.head.insertBefore(style, firstStyleEl);
328+
}
329+
330+
style.sheet.insertRule(linkRule, style.sheet.cssRules.length);
331+
style.sheet.insertRule(hoverRule, style.sheet.cssRules.length);
332+
style.sheet.insertRule(pseudoElContent, style.sheet.cssRules.length);
333+
style.sheet.insertRule(anchorjsLinkFontFace, style.sheet.cssRules.length);
334+
}
335+
}
336+
337+
return AnchorJS;
338+
}));

docs/js/avatars.js

+6-2
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,12 @@
1616
}
1717

1818
for (var i = 0; i < images.length; i += 1) {
19-
images[i].onload = onloadHandler;
20-
images[i].onerror = onloadHandler;
19+
if (images[i].complete) {
20+
counter -= 1;
21+
} else {
22+
images[i].onload = onloadHandler;
23+
images[i].onerror = onloadHandler;
24+
}
2125
}
2226
});
2327

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -305,7 +305,7 @@
305305
"coveralls": "nyc report --reporter=text-lcov | coveralls",
306306
"prebuildDocs": "rm -rf docs/_dist && node scripts/docs-update-toc.js",
307307
"buildDocs": "bundle exec jekyll build --source ./docs --destination ./docs/_site --config ./docs/_config.yml --safe --drafts",
308-
"postbuildDocs": "cp docs/_headers docs/_site/_headers && buildProduction docs/_site/index.html docs/_site/_headers --outroot docs/_dist --canonicalroot https://mochajs.org/ --optimizeimages --inlinehtmlimage 10000 --asyncscripts && cp docs/_headers docs/_dist/_headers && node scripts/netlify-headers.js >> docs/_dist/_headers",
308+
"postbuildDocs": "buildProduction docs/_site/index.html --outroot docs/_dist --canonicalroot https://mochajs.org/ --optimizeimages --inlinehtmlimage 6800 --inlinehtmlscript 0 --asyncscripts && cp docs/_headers docs/_dist/_headers && node scripts/netlify-headers.js >> docs/_dist/_headers",
309309
"prewatchDocs": "node scripts/docs-update-toc.js",
310310
"watchDocs": "bundle exec jekyll serve --source ./docs --destination ./docs/_site --config ./docs/_config.yml --safe --drafts --watch"
311311
},

0 commit comments

Comments
 (0)