/* * dom-lite: the small browser surface used by the legacy public scripts. * * This is intentionally not a jQuery implementation. It keeps the few * collection and request helpers needed by scripts that have not yet been * converted, so the full jQuery payload can be removed from public pages. */ (function (window, document) { "use strict"; var slice = Array.prototype.slice; var hasOwn = Object.prototype.hasOwnProperty; function unique(nodes) { var seen = []; return nodes.filter(function (node) { if (seen.indexOf(node) !== -1) return false; seen.push(node); return true; }); } function selectorForNative(selector) { return String(selector) .replace(/\[@/g, "[") .replace(/:submit\b/g, '[type="submit"]') .replace(/\[([^\]=]+)\$=([^\]"']+)\]/g, "[$1$='$2']"); } function parseHTML(html) { var template = document.createElement("template"); if (template.content) { template.innerHTML = html; return slice.call(template.content.childNodes); } var wrapper = document.createElement("div"); wrapper.innerHTML = html; return slice.call(wrapper.childNodes); } function nodesFrom(value, context) { if (value == null) return []; if (value instanceof Collection) return value.toArray(); if (value === window || value === document || value.nodeType) return [value]; if (typeof value === "function") return []; if (typeof value === "string") { var text = value.trim(); if (text.charAt(0) === "<") return parseHTML(text); var root = context instanceof Collection ? context[0] : context; root = root || document; try { return slice.call(root.querySelectorAll(selectorForNative(text))); } catch (error) { return []; } } if (typeof value.length === "number") return slice.call(value); return []; } function ready(callback) { if (document.readyState === "loading") { document.addEventListener("DOMContentLoaded", callback, false); } else { window.setTimeout(callback, 0); } } function Collection(nodes) { nodes = unique(nodes || []); this.length = nodes.length; for (var i = 0; i < nodes.length; i++) this[i] = nodes[i]; } Collection.prototype.toArray = function () { return slice.call(this); }; Collection.prototype.each = function (callback) { for (var i = 0; i < this.length; i++) callback.call(this[i], i, this[i]); return this; }; Collection.prototype.get = function (index) { if (index == null) return this.toArray(); return this[index < 0 ? this.length + index : index]; }; Collection.prototype.first = function () { return new Collection(this.length ? [this[0]] : []); }; Collection.prototype.ready = function (callback) { ready(callback); return this; }; Collection.prototype.on = function (events, selector, callback) { if (typeof selector === "function") { callback = selector; selector = null; } var names = String(events).split(/\s+/); return this.each(function () { var target = this; names.forEach(function (name) { if (!name) return; target.addEventListener(name, function (event) { if (!selector) return callback.call(target, event); var match = event.target && event.target.closest ? event.target.closest(selector) : null; if (match && target.contains(match)) callback.call(match, event); }, false); }); }); }; Collection.prototype.off = function () { return this; }; Collection.prototype.bind = function (events, selector, callback) { return this.on(events, selector, callback); }; Collection.prototype.unbind = function () { return this.off(); }; Collection.prototype.click = function (callback) { return callback ? this.on("click", callback) : this.trigger("click"); }; Collection.prototype.scroll = function (callback) { return callback ? this.on("scroll", callback) : this.trigger("scroll"); }; Collection.prototype.resize = function (callback) { return callback ? this.on("resize", callback) : this.trigger("resize"); }; Collection.prototype.trigger = function (name) { return this.each(function () { var event = document.createEvent("Event"); event.initEvent(name, true, true); this.dispatchEvent(event); }); }; Collection.prototype.find = function (selector) { var found = []; this.each(function () { try { found = found.concat(slice.call(this.querySelectorAll(selectorForNative(selector)))); } catch (error) {} }); return new Collection(found); }; Collection.prototype.children = function (selector) { var found = []; this.each(function () { found = found.concat(slice.call(this.children)); }); var result = new Collection(found); return selector ? result.filter(selector) : result; }; Collection.prototype.parent = function () { return new Collection(this.toArray().map(function (node) { return node.parentNode; })); }; Collection.prototype.next = function () { return new Collection(this.toArray().map(function (node) { return node.nextElementSibling; })); }; Collection.prototype.prev = function () { return new Collection(this.toArray().map(function (node) { return node.previousElementSibling; })); }; Collection.prototype.siblings = function (selector) { var found = []; this.each(function () { if (!this.parentNode) return; slice.call(this.parentNode.children).forEach(function (node) { if (node !== this) found.push(node); }, this); }); var result = new Collection(found); return selector ? result.filter(selector) : result; }; Collection.prototype.nextUntil = function (selector) { var found = []; this.each(function () { var node = this.nextElementSibling; while (node && !node.matches(selectorForNative(selector))) { found.push(node); node = node.nextElementSibling; } }); return new Collection(found); }; Collection.prototype.filter = function (selector) { if (typeof selector === "function") return new Collection(this.toArray().filter(function (node, i) { return selector.call(node, i, node); })); if (selector === ":visible") return new Collection(this.toArray().filter(function (node) { return node.offsetParent !== null && getComputedStyle(node).display !== "none"; })); if (selector === ":hidden") return new Collection(this.toArray().filter(function (node) { return node.offsetParent === null || getComputedStyle(node).display === "none"; })); return new Collection(this.toArray().filter(function (node) { return node.matches(selectorForNative(selector)); })); }; Collection.prototype.is = function (selector) { if (!this.length) return false; if (selector === ":hidden") return this[0].offsetParent === null || getComputedStyle(this[0]).display === "none"; if (selector === ":visible") return !this.is(":hidden"); return this[0].matches(selectorForNative(selector)); }; Collection.prototype.css = function (name, value) { if (typeof name === "string" && value === undefined) { if (!this.length) return undefined; return getComputedStyle(this[0]).getPropertyValue(name) || this[0].style[name]; } var values = typeof name === "object" ? name : (function () { var o = {}; o[name] = value; return o; }()); return this.each(function () { var node = this; Object.keys(values).forEach(function (key) { var val = values[key]; if (typeof val === "number" && !/^(opacity|zIndex|fontWeight|lineHeight|zoom)$/.test(key)) val += "px"; if (node.style.setProperty && key.indexOf("-") !== -1) node.style.setProperty(key, val); else node.style[key] = val; }); }); }; Collection.prototype.width = function (value) { if (value !== undefined) return this.css("width", value); if (!this.length) return 0; if (this[0] === window) return window.innerWidth; if (this[0] === document) return document.documentElement.scrollWidth; return this[0].getBoundingClientRect().width || this[0].offsetWidth; }; Collection.prototype.height = function (value) { if (value !== undefined) return this.css("height", value); if (!this.length) return 0; if (this[0] === window) return window.innerHeight; if (this[0] === document) return document.documentElement.scrollHeight; return this[0].getBoundingClientRect().height || this[0].offsetHeight; }; Collection.prototype.position = function () { if (!this.length) return { top: 0, left: 0 }; return { top: this[0].offsetTop || 0, left: this[0].offsetLeft || 0 }; }; Collection.prototype.scrollTop = function (value) { if (!this.length) return value === undefined ? 0 : this; if (value === undefined) { return this[0] === window || this[0] === document ? window.pageYOffset || document.documentElement.scrollTop || document.body.scrollTop || 0 : this[0].scrollTop; } return this.each(function () { if (this === window || this === document) window.scrollTo(0, value); else this.scrollTop = value; }); }; function setClass(node, name, add) { if (node.classList) { name.split(/\s+/).filter(Boolean).forEach(function (part) { node.classList[add ? "add" : "remove"](part); }); return; } var classes = (node.className || "").split(/\s+/).filter(Boolean); name.split(/\s+/).filter(Boolean).forEach(function (part) { var index = classes.indexOf(part); if (add && index === -1) classes.push(part); if (!add && index !== -1) classes.splice(index, 1); }); node.className = classes.join(" "); } Collection.prototype.addClass = function (name) { return this.each(function () { setClass(this, name, true); }); }; Collection.prototype.removeClass = function (name) { return this.each(function () { setClass(this, name, false); }); }; Collection.prototype.hasClass = function (name) { return this.length ? (this[0].classList ? this[0].classList.contains(name) : (" " + this[0].className + " ").indexOf(" " + name + " ") !== -1) : false; }; Collection.prototype.toggleClass = function (name) { return this.each(function () { setClass(this, name, !new Collection([this]).hasClass(name)); }); }; Collection.prototype.html = function (value) { if (value === undefined) return this.length ? this[0].innerHTML : undefined; return this.each(function () { this.innerHTML = value == null ? "" : value; }); }; Collection.prototype.text = function (value) { if (value === undefined) return this.length ? this[0].textContent : undefined; return this.each(function () { this.textContent = value == null ? "" : value; }); }; Collection.prototype.val = function (value) { if (value === undefined) return this.length ? this[0].value : undefined; return this.each(function () { this.value = value; }); }; Collection.prototype.attr = function (name, value) { if (typeof name === "string" && value === undefined) return this.length ? this[0].getAttribute(name) : undefined; var values = typeof name === "object" ? name : (function () { var o = {}; o[name] = value; return o; }()); return this.each(function () { var node = this; Object.keys(values).forEach(function (key) { if (values[key] == null) node.removeAttribute(key); else node.setAttribute(key, values[key]); }); }); }; Collection.prototype.removeAttr = function (name) { return this.each(function () { this.removeAttribute(name); }); }; function insertContent(target, content, mode) { if (typeof content === "string") { if (mode === "append") target.insertAdjacentHTML("beforeend", content); else if (mode === "prepend") target.insertAdjacentHTML("afterbegin", content); else if (mode === "before") target.insertAdjacentHTML("beforebegin", content); else target.insertAdjacentHTML("afterend", content); return; } var nodes = nodesFrom(content); nodes.forEach(function (node) { var copy = node.cloneNode ? node.cloneNode(true) : node; if (mode === "append") target.appendChild(copy); else if (mode === "prepend") target.insertBefore(copy, target.firstChild); else if (mode === "before") target.parentNode.insertBefore(copy, target); else target.parentNode.insertBefore(copy, target.nextSibling); }); } Collection.prototype.append = function (content) { return this.each(function () { insertContent(this, content, "append"); }); }; Collection.prototype.prepend = function (content) { return this.each(function () { insertContent(this, content, "prepend"); }); }; Collection.prototype.before = function (content) { return this.each(function () { insertContent(this, content, "before"); }); }; Collection.prototype.after = function (content) { return this.each(function () { insertContent(this, content, "after"); }); }; Collection.prototype.insertBefore = function (target) { return new Collection(nodesFrom(target)).before(this); }; Collection.prototype.replaceWith = function (content) { return this.each(function () { insertContent(this, content, "before"); this.parentNode.removeChild(this); }); }; Collection.prototype.empty = function () { return this.html(""); }; Collection.prototype.remove = function () { return this.each(function () { if (this.parentNode) this.parentNode.removeChild(this); }); }; Collection.prototype.show = function () { return this.each(function () { var stored = this.getAttribute("data-dom-lite-display"); this.style.display = stored || ""; if (!stored && getComputedStyle(this).display === "none") { this.style.display = /^(span|a|img|strong|em|b|i)$/.test(this.tagName.toLowerCase()) ? "inline" : "block"; } }); }; Collection.prototype.hide = function () { return this.each(function () { if (this.style.display !== "none") this.setAttribute("data-dom-lite-display", this.style.display || ""); this.style.display = "none"; }); }; Collection.prototype.toggle = function () { return this.each(function () { new Collection([this]).is(":hidden") ? new Collection([this]).show() : new Collection([this]).hide(); }); }; Collection.prototype.fadeIn = Collection.prototype.slideDown = Collection.prototype.show; Collection.prototype.fadeOut = Collection.prototype.slideUp = Collection.prototype.hide; Collection.prototype.slideToggle = Collection.prototype.toggle; Collection.prototype.fadeTo = function (duration, opacity) { return this.css("opacity", opacity); }; Collection.prototype.animate = function (styles, duration, callback) { this.each(function () { var node = new Collection([this]); Object.keys(styles || {}).forEach(function (key) { var value = styles[key]; if (value === "show") node.show(); else if (value === "hide") node.hide(); else if (value === "toggle") node.toggle(); else node.css(key, value); }); if (typeof callback === "function") callback.call(this); }); return this; }; Collection.prototype.focus = function () { return this.each(function () { if (this.focus) this.focus(); }); }; function serialize(data) { if (!data) return ""; if (typeof data === "string") return data; return Object.keys(data).map(function (key) { return encodeURIComponent(key) + "=" + encodeURIComponent(data[key] == null ? "" : data[key]); }).join("&"); } function ajax(options) { options = options || {}; var method = String(options.type || options.method || "GET").toUpperCase(); var url = options.url || ""; var body = null; var query = serialize(options.data); if (method === "GET" && query) url += (url.indexOf("?") === -1 ? "?" : "&") + query; if (method !== "GET" && query) body = query; var request = new XMLHttpRequest(); request.open(method, url, true); request.setRequestHeader("X-Requested-With", "XMLHttpRequest"); if (body) request.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8"); request.onreadystatechange = function () { if (request.readyState !== 4) return; var ok = request.status >= 200 && request.status < 300 || request.status === 304; var data = request.responseText; if (options.dataType === "json") { try { data = JSON.parse(data); } catch (error) {} } if (ok && options.success) options.success(data, "success", request); if (!ok && options.error) options.error(request, "error"); if (options.complete) options.complete(request, ok ? "success" : "error"); }; request.send(body); return request; } function $(value, context) { if (typeof value === "function") { ready(value); return new Collection([document]); } return new Collection(nodesFrom(value, context)); } $.fn = Collection.prototype; $.ajax = ajax; $.get = function (url, data, success, dataType) { if (typeof data === "function") { dataType = success; success = data; data = null; } return ajax({ url: url, data: data, success: success, dataType: dataType, type: "GET" }); }; $.post = function (url, data, success, dataType) { if (typeof data === "function") { dataType = success; success = data; data = null; } return ajax({ url: url, data: data, success: success, dataType: dataType, type: "POST" }); }; $.extend = function (target) { target = target || {}; for (var i = 1; i < arguments.length; i++) { var source = arguments[i] || {}; Object.keys(source).forEach(function (key) { target[key] = source[key]; }); } return target; }; $.each = function (value, callback) { if (Array.isArray(value) || typeof value.length === "number") slice.call(value).forEach(function (item, i) { callback.call(item, i, item); }); else Object.keys(value || {}).forEach(function (key) { callback.call(value[key], key, value[key]); }); }; $.trim = function (value) { return String(value == null ? "" : value).trim(); }; $.param = serialize; $.parseJSON = JSON.parse; $.contains = function (parent, node) { return parent !== node && parent.contains(node); }; window.$ = window.jQuery = $; }(window, document));