Commit d6b9a7ed authored by Michael Murtaugh's avatar Michael Murtaugh
Browse files

added links.js

parent 252e4f83
(function () {
var ELT_ID = "aalinks",
STYLES = {
"#aalinks" : {
position: "fixed",
fontSize: "12px",
fontFamily: "monospace",
top: "10px",
right: "10px",
background: "#BBF",
padding: "10px"
},
"#aalinks ul.aalinks": {
margin: "0",
listStyle: "square",
paddingLeft: "0"
},
"#aalinks div.aalinkscontents": {
marginTop: "0.5em"
}
};
// https://developer.mozilla.org/en-US/docs/Web/HTML/Link_types
//https://davidwalsh.name/add-rules-stylesheets
function inject_styles(styles) {
var style = document.createElement("style");
// style.setAttribute("media", "screen")
// style.setAttribute("media", "only screen and (max-width : 1024px)")
style.appendChild(document.createTextNode("")); // WebKit hack :(
document.head.appendChild(style);
function addCSSRule(selector, rules, index) {
if("insertRule" in style.sheet) {
style.sheet.insertRule(selector + "{" + rules + "}", index);
}
else if("addRule" in style.sheet) {
style.sheet.addRule(selector, rules, index);
}
}
function obj2rules (obj) {
if (typeof(obj) == "string") { return obj; }
var ret = "";
for (var key in obj) { ret += key + ": " + obj[key]+"; " }
return ret;
}
for (var selector in styles) { addCSSRule(selector, obj2rules(styles[selector])); }
return style.sheet;
}
// make an element, with options
function _make(elt, opts) {
var ret = document.createElement(elt),
name;
if (opts.parent) {
opts.parent.appendChild(ret);
}
if (opts.attr) {
for (name in opts.attr) {
ret.setAttribute(name, opts.attr[name]);
}
}
if (opts.style) {
for (name in opts.style) {
ret.style[name] = opts.style[name];
}
}
if (opts.class) {
ret.setAttribute("class", opts.class);
}
if (opts.html) {
ret.innerHTML = opts.html;
}
return ret;
}
function linkspanel (elt_id) {
var elt = document.getElementById(elt_id) || _make("div", {
parent: document.body,
attr: {"id": elt_id}
});
elt.innerHTML = "";
var p1 = _make("p", {
parent: elt,
html: "Links",
class: "aalinksp"
}),
contents = _make("div", {
parent: elt,
class: "aalinkscontents"
}),
linksByRel = {};
function append_link(list, href, label) {
var li = document.createElement("li"),
a;
if (window.location.href == href) {
a = document.createElement("b");
a.classList.add("self");
} else {
a = document.createElement("a");
a.href = href;
}
a.innerHTML = label;
li.appendChild(a);
list.appendChild(li);
}
function get_links_by_rel(rel) {
if (linksByRel[rel]) return linkByRel;
var ret = _make("ul", {
parent: contents,
class: "aalinks aalinks-"+rel
})
linksByRel[rel] = ret;
return ret;
}
/* Rollover */
contents.style.display = "none";
elt.addEventListener("touchstart", function () {
contents.style.display = "block";
})
elt.addEventListener("mouseenter", function () {
contents.style.display = "block";
});
elt.addEventListener("mouseleave", function () {
contents.style.display = "none";
});
/* Process <link> elements */
var links = document.querySelectorAll("link[rel][href]");
for (var i=0, l=links.length; i<l; i++) {
var link = links[i];
append_link(get_links_by_rel(link.rel), link.href, link.title || link.href);
}
}
document.addEventListener("DOMContentLoaded", function () {
inject_styles(STYLES);
linkspanel(ELT_ID);
})
})();
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment