kril-theme/js/gamelist.js

75 lines
2.1 KiB
JavaScript

function shuffle(o) {
for(var j, x, i = o.length; i; j = Math.floor(Math.random() * i), x = o[--i], o[i] = o[j], o[j] = x);
return o;
};
jQuery.fn.randomize = function(childElem) {
return this.each(function() {
var that = jQuery(this);
var elems = that.children(childElem);
shuffle(elems);
that.empty();
elems.appendTo(that);
});
}
jQuery(document).ready(function($){
$("body").on("change", ".order-buttons input", function(){
jQuery(".order-buttons label").removeClass("active");
jQuery(".order-buttons input").attr("checked", "");
jQuery(this).attr("checked", "checked");
jQuery(this).parent().toggleClass("active");
let value = jQuery(this).val().trim();
switch (value) {
case 'name':
jQuery(".gamelist").each(function(index, list) {
list = jQuery(list);
let games = Array.from(list.children());
games.sort(function(a, b) {
let nameA = jQuery(a).find(".nGameName").text().trim();
let nameB = jQuery(b).find(".nGameName").text().trim();
if (nameA > nameB) {
return 1;
}
return -1;
});
games.forEach(function(game, index) {
game.querySelector(".nGameNum").innerHTML = (index + 1);
});
list.empty();
list.append(games);
});
break;
case 'random':
jQuery(".gamelist").randomize(".nKrilGame");
break;
case 'personal':
jQuery(".gamelist").each(function(index, list) {
list = jQuery(list);
let games = list.children();
games.sort(function(a, b) {
let nameA = jQuery(a).data("order");
let nameB = jQuery(b).data("order");
return (nameA > nameB);
});
list.empty();
games.appendTo(list);
});
break;
}
});
});
function hideNom(nomination) {
let block = jQuery(".gamelist_"+nomination);
let link = jQuery("#hideLink_"+nomination);
if (block.is(":visible")) {
link.text("(раскрыть)");
} else {
link.text("(свернуть)");
}
block.slideToggle();
}