$.fn.randomise = function (a) { (a ? this.find(a) : this).parent().each(function () { $(this).children(a).sort(function () { return Math.random() - 0.5 }).detach().insertAfter(($("#videos .video.new").length) ? $("#videos .video:last(new)") : $("#search")) }); return this }; function sort_data(data, direction) { $("#videos .video").sort(function (a, b) { if (direction == "ascending") { return $(a).data(data) > $(b).data(data) ? 1 : -1; } else { return $(a).data(data) < $(b).data(data) ? 1 : -1; } }) .insertAfter($("#search")); } function sort_random() { $("#videos").randomise(".video:not(.new)"); } function sort_text(text, direction) { $("#videos .video").sort(function (a, b) { if (direction == "ascending") { return $(a).find(text).text() > $(b).find(text).text() ? 1 : -1; } else { return $(a).find(text).text() < $(b).find(text).text() ? 1 : -1; } }) .insertAfter($("#search")); } function video_filter() { if ($("#filter .tag").hasClass("on")) { $("#filter .clear").removeClass("hidden"); $("#videos .video").addClass("hidden").each(function () { var flow = []; var tags = $(this).data("tags").replace(/, /g, ",").split(","); $("#filter .tags").each(function () { var mode = -1; $(this).find(".tag.on").each(function () { mode = 0; if ($.inArray($(this).text(), tags) != -1) { mode = 1; return false; } }); flow.push(mode); }); if ($.inArray(0, flow) == -1) { $(this).removeClass("hidden"); } }); $("#message .default").addClass("hidden"); $("#message .matched").removeClass("hidden"); } else { $("#filter .clear").addClass("hidden"); $("#message .default").removeClass("hidden"); $("#message .matched").addClass("hidden"); $("#videos .video").removeClass("hidden"); } var found = $("#videos .video:not(.hidden)").length; $("#message .found").text(found); if (found == 1) { $("#message .plural").addClass("hidden"); } else { $("#message .plural").removeClass("hidden"); } video_limit(); } function video_limit() { if ($("#filter .tag").hasClass("on") || $("#searchbox").val().trim() != "") { $("#videos").removeClass("hidden-default"); $("#limit").addClass("hidden"); } else { if (!$("#expand").hasClass("on")) { $("#videos").addClass("hidden-default"); } $("#limit").removeClass("hidden"); } $("#empty .clear").addClass("hidden"); if ($("#videos .video:not(.hidden):not(.hidden-search)").length == 0) { $("#empty").removeClass("hidden"); if (!$("#filter .clear").hasClass("hidden")) { $("#empty .clear").removeClass("hidden"); } } else { $("#empty").addClass("hidden"); } } function video_load() { if (window.location.hash) { var hash = window.location.hash.replace("#", "").replace(/_/g, " ").split("+"); for (var tag in hash) { $("#filter .tag").filter(function () { return $(this).text() == hash[tag]; }) .addClass("on").attr("aria-checked", true); } } $.when(sort_random()).done(function () { video_filter(); }); } $(document).ready(function () { const options = { ignoreLocation: true, includeMatches: true, includeScore: true, threshold: 0.2 }; $("#searchbox").on("keyup", function () { if ($(this).val().trim() != "") { var input = $(this).val().trim().split(" "); $("#videos .video").each(function () { var match = true; var meta = []; meta = meta.concat($(this).data("tags").split(" ")); meta = meta.concat($(this).find("h2").text().split(" ")); meta = meta.concat($(this).find(".tag").map(function () { return $(this).text(); }).get()); $.each(input, function (index, value) { var fuse = new Fuse(meta, options); var search = fuse.search(value); if (search.length == 0) { match = false; return false; } }); if (match) { $(this).removeClass("hidden-search"); } else { $(this).addClass("hidden-search"); } }); } else { $("#videos .video").removeClass("hidden-search"); } video_limit(); }); $(this).on("click", "#empty .clear", function () { $("#filter .clear").click(); return false; }); $(this).on("click", "#filter .dropdown .name", function () { $(this).parent().toggleClass("on").find(".options").stop().slideToggle(200); }); $(this).on("click", "#filter .dropdown .option:not(.on)", function () { $(this).addClass("on").siblings().removeClass("on"); $(this).parent().siblings(".name").click().text($(this).text()); var what = $(this).text(); if (what == "Newest") { sort_data("date"); } else if (what == "Oldest") { sort_data("date", "ascending"); } else if (what == "A-Z") { sort_text("h2", "ascending"); } else if (what == "Z-A") { sort_text("h2"); } else if (what == "Shortest") { sort_data("length", "ascending"); } else if (what == "Longest") { sort_data("length"); } else { sort_random(); } }); $(this).on("click", "#filter .tag", function () { if ($(this).hasClass("on")) { $(this).removeClass("on").attr("aria-checked", false); } else { $(this).addClass("on").attr("aria-checked", true); } var hash = "#"; $("#filter .tag.on").each(function (index) { if (index) { hash += "+"; } hash += $(this).text().replace(/ /g, "_"); }); if (hash == "#") { hash = "."; } history.pushState(null, null, hash); video_filter(); }); $(this).on("click", "#expand", function () { $(this).toggleClass("on"); if ($(this).hasClass("on")) { $(this).attr("aria-expanded", "true").attr("aria-label", "Show less").text("Show less"); $("#videos").removeClass("hidden-default"); } else { $(this).attr("aria-expanded", "false").attr("aria-label", "Show more").text("Show more"); $("#videos").addClass("hidden-default"); } }); $(this).on("click", "#featured a", function () { $("#filter .tag").removeClass("on"); var hash = $(this).attr("href").replace("#", "").replace(/_/g, " ").split("+"); for (var tag in hash) { $("#filter .tag").filter(function () { return $(this).text() == hash[tag]; }) .addClass("on").attr("aria-checked", true); } video_filter(); }); $(this).on("click", "#filter .clear", function () { $("#filter .tag").removeClass("on"); history.pushState(null, null, "."); video_filter(); }); $(this).on("click", "#filter .toggle", function () { $("#filter .box").stop().slideToggle(200); }); $(this).on("click", "#search .clear", function () { $("#searchbox").val("").focus().keyup(); }); $(this).on("click", "#search .glass", function () { $("#searchbox").focus().keyup(); }); $(this).on("click", "#videos", function () { var box = $("#filter .box"); if ($(window).outerWidth() < 1084 && !$(box).is(":animated") && $(box).is(":visible")) { var box_top = $(box).offset().top; var box_bottom = $(box).offset().top + $(box).outerHeight(); var window_top = $(window).scrollTop(); var window_bottom = $(window).scrollTop() + $(window).innerHeight(); if ((window_bottom > box_top) && (window_top < box_bottom)) { $("#filter .box").stop().slideUp(200); return false; } } }); $(this).on("keyup", "[tabindex]", function (event) { if (event.which == 13 || event.which == 32) { $(this).click(); } }); $(this).on("keyup", function (event) { if (event.which == 27 && $(window).outerWidth() < 1084 && !$("#filter .box").is(":animated") && $("#filter .box").is(":visible")) { $("#filter .box").stop().slideUp(200); } }); video_load(); });