2016-01-29 01:27:15 +03:00
|
|
|
/*
|
2017-01-06 21:11:18 +03:00
|
|
|
* Copyright (c) 2016-2017 Martin Donath <martin.donath@squidfunk.com>
|
2016-01-29 01:27:15 +03:00
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
* of this software and associated documentation files (the "Software"), to
|
|
|
|
* deal in the Software without restriction, including without limitation the
|
|
|
|
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
|
|
|
|
* sell copies of the Software, and to permit persons to whom the Software is
|
|
|
|
* furnished to do so, subject to the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be included in
|
|
|
|
* all copies or substantial portions of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
|
|
|
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
|
|
|
|
* IN THE SOFTWARE.
|
|
|
|
*/
|
|
|
|
|
2016-09-30 14:29:45 +03:00
|
|
|
import FastClick from "fastclick"
|
2016-10-06 13:14:33 +03:00
|
|
|
import Material from "./components/Material"
|
2016-09-30 14:29:45 +03:00
|
|
|
|
2016-01-29 01:27:15 +03:00
|
|
|
/* ----------------------------------------------------------------------------
|
2016-08-07 19:01:56 +03:00
|
|
|
* Application
|
2016-01-29 01:27:15 +03:00
|
|
|
* ------------------------------------------------------------------------- */
|
|
|
|
|
2017-02-25 16:49:49 +03:00
|
|
|
/**
|
2017-02-25 19:13:50 +03:00
|
|
|
* Initialize Material for MkDocs
|
2017-02-25 16:49:49 +03:00
|
|
|
*
|
2017-02-25 19:13:50 +03:00
|
|
|
* @param {Object} config - Configuration
|
2017-02-25 16:49:49 +03:00
|
|
|
*/
|
2017-02-25 19:13:50 +03:00
|
|
|
function initialize(config) { // eslint-disable-line func-style
|
2016-10-06 13:14:33 +03:00
|
|
|
|
2017-02-11 18:00:45 +03:00
|
|
|
/* Initialize Modernizr and FastClick */
|
|
|
|
new Material.Event.Listener(document, "DOMContentLoaded", () => {
|
2017-02-25 16:49:49 +03:00
|
|
|
if (!(document.body instanceof HTMLElement))
|
|
|
|
throw new ReferenceError
|
|
|
|
|
2017-02-11 18:00:45 +03:00
|
|
|
/* Attach FastClick to mitigate 300ms delay on touch devices */
|
|
|
|
FastClick.attach(document.body)
|
2016-10-23 12:35:41 +03:00
|
|
|
|
2017-02-11 18:00:45 +03:00
|
|
|
/* Test for iOS */
|
|
|
|
Modernizr.addTest("ios", () => {
|
|
|
|
return !!navigator.userAgent.match(/(iPad|iPhone|iPod)/g)
|
|
|
|
})
|
2016-11-02 21:21:14 +03:00
|
|
|
|
2017-02-11 18:00:45 +03:00
|
|
|
/* Wrap all data tables for better overflow scrolling */
|
|
|
|
const tables = document.querySelectorAll("table:not([class])")
|
|
|
|
Array.prototype.forEach.call(tables, table => {
|
2017-02-25 13:29:15 +03:00
|
|
|
const wrap = (
|
|
|
|
<div class="md-typeset__scrollwrap">
|
|
|
|
<div class="md-typeset__table"></div>
|
|
|
|
</div>
|
|
|
|
)
|
2017-02-11 18:00:45 +03:00
|
|
|
if (table.nextSibling) {
|
|
|
|
table.parentNode.insertBefore(wrap, table.nextSibling)
|
|
|
|
} else {
|
|
|
|
table.parentNode.appendChild(wrap)
|
2016-12-29 16:19:26 +03:00
|
|
|
}
|
2017-02-25 13:29:15 +03:00
|
|
|
wrap.children[0].appendChild(table)
|
2017-02-11 18:00:45 +03:00
|
|
|
})
|
2016-12-29 16:19:26 +03:00
|
|
|
|
2017-02-11 18:00:45 +03:00
|
|
|
/* Force 1px scroll offset to trigger overflow scrolling */
|
|
|
|
if (Modernizr.ios) {
|
|
|
|
const scrollable = document.querySelectorAll("[data-md-scrollfix]")
|
|
|
|
Array.prototype.forEach.call(scrollable, item => {
|
|
|
|
item.addEventListener("touchstart", () => {
|
|
|
|
const top = item.scrollTop
|
2016-12-29 16:19:26 +03:00
|
|
|
|
2017-02-11 18:00:45 +03:00
|
|
|
/* We're at the top of the container */
|
|
|
|
if (top === 0) {
|
|
|
|
item.scrollTop = 1
|
2016-12-29 16:19:26 +03:00
|
|
|
|
|
|
|
/* We're at the bottom of the container */
|
2017-02-11 18:00:45 +03:00
|
|
|
} else if (top + item.offsetHeight === item.scrollHeight) {
|
|
|
|
item.scrollTop = top - 1
|
2016-12-15 17:55:40 +03:00
|
|
|
}
|
2016-12-29 16:19:26 +03:00
|
|
|
})
|
2017-02-11 18:00:45 +03:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}).listen()
|
2017-01-28 00:41:55 +03:00
|
|
|
|
2017-02-12 01:02:12 +03:00
|
|
|
/* Component: header shadow toggle */
|
|
|
|
new Material.Event.MatchMedia("(min-width: 1220px)",
|
2017-01-28 00:41:55 +03:00
|
|
|
new Material.Event.Listener(window, [
|
|
|
|
"scroll", "resize", "orientationchange"
|
2017-03-03 23:57:15 +03:00
|
|
|
], new Material.Header.Shadow(
|
|
|
|
"[data-md-component=container]",
|
|
|
|
"[data-md-component=header]")))
|
2017-01-28 00:41:55 +03:00
|
|
|
|
2017-02-12 01:02:12 +03:00
|
|
|
/* Component: tabs visibility toggle */
|
2017-02-26 17:30:48 +03:00
|
|
|
if (document.querySelector("[data-md-component=tabs]"))
|
|
|
|
new Material.Event.Listener(window, [
|
|
|
|
"scroll", "resize", "orientationchange"
|
|
|
|
], new Material.Tabs.Toggle("[data-md-component=tabs]")).listen()
|
2016-12-15 17:55:40 +03:00
|
|
|
|
2017-02-11 18:00:45 +03:00
|
|
|
/* Component: sidebar with navigation */
|
|
|
|
new Material.Event.MatchMedia("(min-width: 1220px)",
|
|
|
|
new Material.Event.Listener(window, [
|
|
|
|
"scroll", "resize", "orientationchange"
|
2017-03-03 23:57:15 +03:00
|
|
|
], new Material.Sidebar.Position(
|
|
|
|
"[data-md-component=navigation]",
|
|
|
|
"[data-md-component=header]")))
|
2016-12-15 17:55:40 +03:00
|
|
|
|
2017-02-12 01:02:12 +03:00
|
|
|
/* Component: sidebar with table of contents - register two separate
|
|
|
|
listeners, as the offset at the top might change */
|
2017-03-11 16:07:07 +03:00
|
|
|
new Material.Event.MatchMedia("(min-width: 960px)",
|
2017-02-11 18:00:45 +03:00
|
|
|
new Material.Event.Listener(window, [
|
|
|
|
"scroll", "resize", "orientationchange"
|
2017-03-03 23:57:15 +03:00
|
|
|
], new Material.Sidebar.Position(
|
|
|
|
"[data-md-component=toc]",
|
|
|
|
"[data-md-component=header]")))
|
2016-12-28 13:49:57 +03:00
|
|
|
|
2017-02-11 18:00:45 +03:00
|
|
|
/* Component: link blurring for table of contents */
|
|
|
|
new Material.Event.MatchMedia("(min-width: 960px)",
|
|
|
|
new Material.Event.Listener(window, "scroll",
|
2017-02-12 01:02:12 +03:00
|
|
|
new Material.Nav.Blur("[data-md-component=toc] [href]")))
|
2016-12-15 17:55:40 +03:00
|
|
|
|
2017-02-11 18:00:45 +03:00
|
|
|
/* Component: collapsible elements for navigation */
|
|
|
|
const collapsibles =
|
|
|
|
document.querySelectorAll("[data-md-component=collapsible]")
|
|
|
|
Array.prototype.forEach.call(collapsibles, collapse => {
|
|
|
|
new Material.Event.MatchMedia("(min-width: 1220px)",
|
|
|
|
new Material.Event.Listener(collapse.previousElementSibling, "click",
|
|
|
|
new Material.Nav.Collapse(collapse)))
|
|
|
|
})
|
|
|
|
|
|
|
|
/* Component: active pane monitor for iOS scrolling fixes */
|
|
|
|
new Material.Event.MatchMedia("(max-width: 1219px)",
|
|
|
|
new Material.Event.Listener(
|
|
|
|
"[data-md-component=navigation] [data-md-toggle]", "change",
|
|
|
|
new Material.Nav.Scrolling("[data-md-component=navigation] nav")))
|
|
|
|
|
|
|
|
/* Component: search body lock for mobile */
|
|
|
|
new Material.Event.MatchMedia("(max-width: 959px)",
|
|
|
|
new Material.Event.Listener("[data-md-toggle=search]", "change",
|
|
|
|
new Material.Search.Lock("[data-md-toggle=search]")))
|
|
|
|
|
|
|
|
/* Component: search results */
|
2017-02-25 19:13:50 +03:00
|
|
|
new Material.Event.Listener("[data-md-component=query]", [
|
2017-03-19 21:49:19 +03:00
|
|
|
"focus", "keyup", "change"
|
2017-02-11 18:00:45 +03:00
|
|
|
], new Material.Search.Result("[data-md-component=result]", () => {
|
|
|
|
return fetch(`${config.url.base}/mkdocs/search_index.json`, {
|
|
|
|
credentials: "same-origin"
|
|
|
|
}).then(response => response.json())
|
|
|
|
.then(data => {
|
|
|
|
return data.docs.map(doc => {
|
|
|
|
doc.location = config.url.base + doc.location
|
|
|
|
return doc
|
2016-12-15 17:55:40 +03:00
|
|
|
})
|
2017-02-11 18:00:45 +03:00
|
|
|
})
|
|
|
|
})).listen()
|
|
|
|
|
|
|
|
/* Listener: prevent touches on overlay if navigation is active */
|
|
|
|
new Material.Event.MatchMedia("(max-width: 1219px)",
|
|
|
|
new Material.Event.Listener("[data-md-component=overlay]", "touchstart",
|
|
|
|
ev => ev.preventDefault()))
|
|
|
|
|
|
|
|
/* Listener: close drawer when anchor links are clicked */
|
|
|
|
new Material.Event.MatchMedia("(max-width: 959px)",
|
|
|
|
new Material.Event.Listener("[data-md-component=navigation] [href^='#']",
|
|
|
|
"click", () => {
|
|
|
|
const toggle = document.querySelector("[data-md-toggle=drawer]")
|
2017-02-25 19:13:50 +03:00
|
|
|
if (!(toggle instanceof HTMLInputElement))
|
|
|
|
throw new ReferenceError
|
2016-12-15 17:55:40 +03:00
|
|
|
if (toggle.checked) {
|
|
|
|
toggle.checked = false
|
2016-12-29 12:50:19 +03:00
|
|
|
toggle.dispatchEvent(new CustomEvent("change"))
|
2016-12-15 17:55:40 +03:00
|
|
|
}
|
|
|
|
}))
|
|
|
|
|
2017-02-11 18:00:45 +03:00
|
|
|
/* Listener: focus input after opening search */
|
|
|
|
new Material.Event.Listener("[data-md-toggle=search]", "change", ev => {
|
|
|
|
setTimeout(toggle => {
|
2017-02-25 19:13:50 +03:00
|
|
|
if (!(toggle instanceof HTMLInputElement))
|
|
|
|
throw new ReferenceError
|
|
|
|
if (toggle.checked) {
|
|
|
|
const query = document.querySelector("[data-md-component=query]")
|
|
|
|
if (!(query instanceof HTMLInputElement))
|
|
|
|
throw new ReferenceError
|
2017-02-11 18:00:45 +03:00
|
|
|
query.focus()
|
2017-02-25 19:13:50 +03:00
|
|
|
}
|
2017-02-11 18:00:45 +03:00
|
|
|
}, 400, ev.target)
|
|
|
|
}).listen()
|
|
|
|
|
|
|
|
/* Listener: open search on focus */
|
|
|
|
new Material.Event.MatchMedia("(min-width: 960px)",
|
2017-02-25 19:13:50 +03:00
|
|
|
new Material.Event.Listener("[data-md-component=query]", "focus", () => {
|
2017-02-11 18:00:45 +03:00
|
|
|
const toggle = document.querySelector("[data-md-toggle=search]")
|
2017-02-25 19:13:50 +03:00
|
|
|
if (!(toggle instanceof HTMLInputElement))
|
|
|
|
throw new ReferenceError
|
2017-02-11 18:00:45 +03:00
|
|
|
if (!toggle.checked) {
|
|
|
|
toggle.checked = true
|
|
|
|
toggle.dispatchEvent(new CustomEvent("change"))
|
2016-12-28 19:32:56 +03:00
|
|
|
}
|
2017-02-11 18:00:45 +03:00
|
|
|
}))
|
|
|
|
|
|
|
|
/* Listener: close search when clicking outside */
|
|
|
|
new Material.Event.MatchMedia("(min-width: 960px)",
|
|
|
|
new Material.Event.Listener(document.body, "click", () => {
|
|
|
|
const toggle = document.querySelector("[data-md-toggle=search]")
|
2017-02-25 19:13:50 +03:00
|
|
|
if (!(toggle instanceof HTMLInputElement))
|
|
|
|
throw new ReferenceError
|
2017-02-11 18:00:45 +03:00
|
|
|
if (toggle.checked) {
|
|
|
|
toggle.checked = false
|
|
|
|
toggle.dispatchEvent(new CustomEvent("change"))
|
2016-12-15 17:55:40 +03:00
|
|
|
}
|
2017-02-11 18:00:45 +03:00
|
|
|
}))
|
|
|
|
|
|
|
|
/* Listener: disable search when ESC key is pressed */
|
|
|
|
new Material.Event.Listener(window, "keyup", ev => {
|
|
|
|
const code = ev.keyCode || ev.which
|
|
|
|
if (code === 27) {
|
|
|
|
const toggle = document.querySelector("[data-md-toggle=search]")
|
2017-02-25 19:13:50 +03:00
|
|
|
if (!(toggle instanceof HTMLInputElement))
|
|
|
|
throw new ReferenceError
|
2017-02-11 18:00:45 +03:00
|
|
|
if (toggle.checked) {
|
|
|
|
toggle.checked = false
|
|
|
|
toggle.dispatchEvent(new CustomEvent("change"))
|
2017-02-25 19:13:50 +03:00
|
|
|
const query = document.querySelector("[data-md-component=query]")
|
|
|
|
if (!(query instanceof HTMLInputElement))
|
|
|
|
throw new ReferenceError
|
|
|
|
query.focus()
|
2017-02-11 18:00:45 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}).listen()
|
|
|
|
|
|
|
|
/* Listener: fix unclickable toggle due to blur handler */
|
|
|
|
new Material.Event.MatchMedia("(min-width: 960px)",
|
|
|
|
new Material.Event.Listener("[data-md-toggle=search]", "click",
|
|
|
|
ev => ev.stopPropagation()))
|
|
|
|
|
|
|
|
/* Listener: prevent search from closing when clicking */
|
|
|
|
new Material.Event.MatchMedia("(min-width: 960px)",
|
|
|
|
new Material.Event.Listener("[data-md-component=search]", "click",
|
|
|
|
ev => ev.stopPropagation()))
|
|
|
|
|
|
|
|
/* Retrieve facts for the given repository type */
|
|
|
|
;(() => {
|
|
|
|
const el = document.querySelector("[data-md-source]")
|
2017-02-25 16:49:49 +03:00
|
|
|
if (!el)
|
|
|
|
return Promise.resolve([])
|
|
|
|
else if (!(el instanceof HTMLAnchorElement))
|
|
|
|
throw new ReferenceError
|
2017-02-11 18:00:45 +03:00
|
|
|
switch (el.dataset.mdSource) {
|
|
|
|
case "github": return new Material.Source.Adapter.GitHub(el).fetch()
|
|
|
|
default: return Promise.resolve([])
|
|
|
|
}
|
|
|
|
|
2017-02-25 19:13:50 +03:00
|
|
|
/* Render repository information */
|
2017-02-11 18:00:45 +03:00
|
|
|
})().then(facts => {
|
|
|
|
const sources = document.querySelectorAll("[data-md-source]")
|
|
|
|
Array.prototype.forEach.call(sources, source => {
|
|
|
|
new Material.Source.Repository(source)
|
|
|
|
.initialize(facts)
|
2016-12-15 17:55:40 +03:00
|
|
|
})
|
2017-02-11 18:00:45 +03:00
|
|
|
})
|
2016-10-06 13:14:33 +03:00
|
|
|
}
|
2017-02-25 19:13:50 +03:00
|
|
|
|
|
|
|
/* ----------------------------------------------------------------------------
|
|
|
|
* Exports
|
|
|
|
* ------------------------------------------------------------------------- */
|
|
|
|
|
|
|
|
export {
|
|
|
|
initialize
|
|
|
|
}
|