71 lines
2.0 KiB
JavaScript
71 lines
2.0 KiB
JavaScript
/* Minimal DOM helper compatible with the registration form's jQuery usage */
|
|
(function(global) {
|
|
function selectElements(sel) {
|
|
if (!sel) return [];
|
|
if (typeof sel === "function") {
|
|
if (document.readyState === "loading") {
|
|
document.addEventListener("DOMContentLoaded", sel);
|
|
} else {
|
|
sel();
|
|
}
|
|
return [];
|
|
}
|
|
if (typeof sel === "string") {
|
|
return Array.prototype.slice.call(document.querySelectorAll(sel));
|
|
}
|
|
if (sel === document || sel === window || sel.nodeType) {
|
|
return [sel];
|
|
}
|
|
return [];
|
|
}
|
|
|
|
function MiniQuery(sel) {
|
|
if (!(this instanceof MiniQuery)) return new MiniQuery(sel);
|
|
this.els = selectElements(sel);
|
|
}
|
|
|
|
MiniQuery.prototype.ready = function(fn) {
|
|
if (document.readyState === "loading") {
|
|
document.addEventListener("DOMContentLoaded", fn);
|
|
} else {
|
|
fn();
|
|
}
|
|
return this;
|
|
};
|
|
MiniQuery.prototype.show = function() {
|
|
this.els.forEach(function(el) { el.style.display = ""; });
|
|
return this;
|
|
};
|
|
MiniQuery.prototype.hide = function() {
|
|
this.els.forEach(function(el) { el.style.display = "none"; });
|
|
return this;
|
|
};
|
|
MiniQuery.prototype.html = function(val) {
|
|
if (val === undefined) return this.els[0] ? this.els[0].innerHTML : "";
|
|
this.els.forEach(function(el) { el.innerHTML = val; });
|
|
return this;
|
|
};
|
|
MiniQuery.prototype.append = function(htmlStr) {
|
|
this.els.forEach(function(el) { el.insertAdjacentHTML("beforeend", htmlStr); });
|
|
return this;
|
|
};
|
|
MiniQuery.prototype.bind = function(events, fn) {
|
|
var evList = events.split(/\s+/);
|
|
this.els.forEach(function(el) {
|
|
evList.forEach(function(ev) { el.addEventListener(ev, fn); });
|
|
});
|
|
return this;
|
|
};
|
|
MiniQuery.prototype.keydown = function(fn) {
|
|
return this.bind("keydown", fn);
|
|
};
|
|
|
|
MiniQuery.noConflict = function() {
|
|
return MiniQuery;
|
|
};
|
|
MiniQuery.fn = MiniQuery.prototype;
|
|
|
|
global.$ = MiniQuery;
|
|
global.jQuery = MiniQuery;
|
|
})(window);
|