This repository has been archived on 2020-06-24. You can view files and clone it, but cannot push or open issues or pull requests.
gitea_sortt/sortt.js

71 lines
1.7 KiB
JavaScript

/*
Sortt
@License MIT
@Author 6543
@Repository https://gitea.com/6543/gitea_sortt
@Version 1.2.1
*/
//Declare Functions
//create global function with main routine
window.sortt=function(normsort,revsort,isdefault){
//sortTable [normsort] (revsort) (isdefault)
//normsort is needed
if (!(normsort)) return false;
//default values of optinal parameters
if (!(revsort)) revsort = "";
// parse URL
/* script check if url has already a sort=
NO: if sort=“” indikates default (with * prefix) set url_sort=
YES: set url_sort variable
*/
let url = new URL(window.location);
let url_sort = url.searchParams.get("sort");
if ((url_sort === null) && isdefault) url_sort = normsort;
// generate new URL
/* script check if url_sort and sort attribute is same
NO: generate URL with sort param
YES: check if reverse attribute exist -> YES: generate URL with reverse sort param
*/
if (url_sort != normsort) {
url.searchParams.set("sort",normsort);
} else if (revsort != "") {
url.searchParams.set("sort",revsort);
}
//open url
window.location.replace(url.href);
};
//USE Functions
// test if jQuery is available
if(!window.jQuery) {
console.log("sortt.js: ERROR no jQuery found!");
} else {
//use JQuery to bind event on each <th> with a "data-sortt" attribute
$('th').each(function() {
//if data attribute sortt is set
if ($(this)[0].dataset.sortt) {
//add onclick event
$(this).on('click', function() {
var data = $(this)[0].dataset.sortt;
data = data.split(",");
sortt(data[0],data[1],data[2]);
});
//add arrow to colume
// - place holder -
}
});
}