File size: 7,063 Bytes
7f8e190 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 | /* Problem-set interactions.
*
* Every filter, sort and page is expressed in the URL, so the page works with
* scripting disabled: the popovers are <details>, the search box is a real
* form field, and each control is a link. This file only adds the affordances
* the captured page has on top of that — instant search submission, popover
* dismissal, the topic-strip expander, the day countdown and company paging. */
(function () {
"use strict";
/* ---- header: mobile navigation and search ------------------------- */
var navToggle = document.querySelector("[data-nav-toggle]");
var nav = document.querySelector("[data-nav]");
if (navToggle && nav) {
navToggle.addEventListener("click", function () {
var open = nav.classList.toggle("is-open");
navToggle.setAttribute("aria-expanded", open ? "true" : "false");
});
}
var navMenus = Array.prototype.slice.call(document.querySelectorAll("[data-navmenu]"));
navMenus.forEach(function (menu) {
menu.addEventListener("mouseenter", function () {
navMenus.forEach(function (other) {
if (other !== menu) other.open = false;
});
menu.open = true;
});
menu.addEventListener("mouseleave", function () {
menu.open = false;
});
});
var searchToggle = document.querySelector("[data-site-search-toggle]");
var siteSearch = document.querySelector(".ap-search");
if (searchToggle && siteSearch) {
searchToggle.addEventListener("click", function () {
siteSearch.classList.toggle("is-open");
var field = siteSearch.querySelector("input");
if (field && siteSearch.classList.contains("is-open")) {
field.focus();
}
});
}
var siteSearchField = document.querySelector("[data-site-search]");
if (siteSearchField) {
siteSearchField.addEventListener("keydown", function (event) {
if (event.key !== "Enter") {
return;
}
event.preventDefault();
var term = siteSearchField.value.trim();
window.location.href = term
? "/problemset/?search=" + encodeURIComponent(term)
: "/problemset/";
});
}
/* ---- collapsible library rail ------------------------------------ */
var railToggle = document.querySelector("[data-rail-toggle]");
if (railToggle) {
var railShell = railToggle.closest(".lc-shell, .ps");
var storageKey = "leetcode-library-rail-collapsed";
var setRail = function (collapsed) {
if (!railShell) return;
railShell.classList.toggle("is-rail-collapsed", collapsed);
railToggle.setAttribute("aria-expanded", collapsed ? "false" : "true");
railToggle.setAttribute("aria-label", collapsed ? "Expand library sidebar" : "Collapse library sidebar");
var glyph = railToggle.querySelector("span");
if (glyph) glyph.textContent = collapsed ? "›" : "‹";
};
setRail(window.sessionStorage.getItem(storageKey) === "1");
railToggle.addEventListener("click", function () {
var collapsed = !railShell.classList.contains("is-rail-collapsed");
setRail(collapsed);
window.sessionStorage.setItem(storageKey, collapsed ? "1" : "0");
});
}
/* ---- filter popovers ---------------------------------------------- */
var pops = Array.prototype.slice.call(document.querySelectorAll("[data-pop]"));
pops.forEach(function (pop) {
pop.addEventListener("toggle", function () {
if (!pop.open) {
return;
}
pops.forEach(function (other) {
if (other !== pop) {
other.open = false;
}
});
});
});
document.addEventListener("click", function (event) {
pops.forEach(function (pop) {
if (pop.open && !pop.contains(event.target)) {
pop.open = false;
}
});
});
document.addEventListener("keydown", function (event) {
if (event.key === "Escape") {
pops.forEach(function (pop) {
pop.open = false;
});
}
});
/* ---- debounced search --------------------------------------------- */
var controls = document.querySelector("[data-controls]");
if (controls) {
var field = controls.querySelector('input[name="search"]');
if (field) {
var timer = null;
field.addEventListener("input", function () {
window.clearTimeout(timer);
timer = window.setTimeout(function () {
controls.submit();
}, 400);
});
}
}
/* ---- topic strip --------------------------------------------------- */
var strip = document.querySelector("[data-topic-strip]");
var expand = document.querySelector("[data-topic-expand]");
if (strip && expand) {
expand.addEventListener("click", function () {
var expanded = strip.classList.toggle("is-expanded");
expand.setAttribute("aria-expanded", expanded ? "true" : "false");
expand.childNodes[0].nodeValue = expanded ? "Collapse" : "Expand";
});
}
/* ---- daily countdown ----------------------------------------------- */
var countdown = document.querySelector("[data-countdown]");
if (countdown) {
var deadline = new Date(countdown.getAttribute("data-deadline"));
var tick = function () {
var remaining = Math.max(0, Math.floor((deadline - new Date()) / 1000));
var hours = Math.floor(remaining / 3600);
var minutes = Math.floor((remaining % 3600) / 60);
var seconds = remaining % 60;
var pad = function (value) {
return value < 10 ? "0" + value : String(value);
};
countdown.textContent =
pad(hours) + ":" + pad(minutes) + ":" + pad(seconds) + " left";
};
tick();
window.setInterval(tick, 1000);
}
/* ---- trending companies: paging and filtering ---------------------- */
var list = document.querySelector("[data-company-list]");
if (list) {
var items = Array.prototype.slice.call(list.children);
var pages =
items.reduce(function (highest, item) {
return Math.max(highest, Number(item.getAttribute("data-page")));
}, 0) + 1;
var page = 0;
var term = "";
var paint = function () {
items.forEach(function (item) {
var text = item.textContent.toLowerCase();
var matches = !term || text.indexOf(term) !== -1;
var onPage = term
? true
: Number(item.getAttribute("data-page")) === page;
item.hidden = !(matches && onPage);
});
};
var prev = document.querySelector("[data-company-prev]");
var next = document.querySelector("[data-company-next]");
if (prev) {
prev.addEventListener("click", function () {
page = (page - 1 + pages) % pages;
paint();
});
}
if (next) {
next.addEventListener("click", function () {
page = (page + 1) % pages;
paint();
});
}
var companySearch = document.querySelector("[data-company-search]");
if (companySearch) {
companySearch.addEventListener("input", function () {
term = companySearch.value.trim().toLowerCase();
paint();
});
}
paint();
}
})();
|